Skip to content

Commit de56a77

Browse files
Catch exceptions in getHardware
1 parent 54faeae commit de56a77

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,59 @@
11
package pw.kaboom.extras.commands;
22

3+
import it.unimi.dsi.fastutil.Pair;
4+
import it.unimi.dsi.fastutil.objects.ObjectObjectImmutablePair;
35
import net.kyori.adventure.text.Component;
46
import net.kyori.adventure.text.format.NamedTextColor;
57
import org.bukkit.command.Command;
68
import org.bukkit.command.CommandExecutor;
79
import org.bukkit.command.CommandSender;
10+
import org.jetbrains.annotations.Nullable;
811
import oshi.SystemInfo;
912
import oshi.hardware.GraphicsCard;
10-
import oshi.hardware.HardwareAbstractionLayer;
13+
import pw.kaboom.extras.util.Utility;
1114

1215
import javax.annotation.Nonnull;
1316
import java.lang.management.ManagementFactory;
1417
import java.net.InetAddress;
1518

1619
public final class CommandServerInfo implements CommandExecutor {
17-
private static final String[] GPU_DEVICES;
18-
private static final String PROCESSOR_NAME;
20+
private static final @Nullable String[] GPU_DEVICES;
21+
private static final @Nullable String PROCESSOR_NAME;
1922

2023
static {
2124
// No need to store this in a static variable as it would
2225
// just waste memory & won't be accessed outside construction
2326
// anyway.
2427

2528
final SystemInfo systemInfo = new SystemInfo();
26-
final HardwareAbstractionLayer hardware = systemInfo.getHardware();
27-
28-
GPU_DEVICES = hardware.getGraphicsCards()
29-
.stream()
30-
.map(GraphicsCard::getName)
31-
.toArray(String[]::new);
32-
PROCESSOR_NAME = hardware.getProcessor()
33-
.getProcessorIdentifier()
34-
.getName();
29+
30+
// Unfortunately, we need to do something like this
31+
// because calls to getHardware may fail if the
32+
// server is running on an unrecognized platform,
33+
// and we're unable to use guard clauses due to
34+
// returns not being supported in static blocks.
35+
36+
final @Nullable Pair<String[], String> hardwareInfo = Utility.composeCallable(
37+
systemInfo::getHardware,
38+
hardware ->
39+
new ObjectObjectImmutablePair<>(
40+
hardware.getGraphicsCards()
41+
.stream()
42+
.map(GraphicsCard::getName)
43+
.toArray(String[]::new),
44+
hardware.getProcessor()
45+
.getProcessorIdentifier()
46+
.getName()
47+
)
48+
);
49+
50+
if (hardwareInfo == null) {
51+
GPU_DEVICES = null;
52+
PROCESSOR_NAME = null;
53+
} else {
54+
GPU_DEVICES = hardwareInfo.first();
55+
PROCESSOR_NAME = hardwareInfo.second();
56+
}
3557
}
3658

3759
private void sendInfoMessage(final CommandSender target, final String description,

src/main/java/pw/kaboom/extras/util/Utility.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import javax.annotation.Nonnull;
99
import javax.annotation.Nullable;
10+
import java.util.concurrent.Callable;
11+
import java.util.function.Function;
1012

1113
public final class Utility {
1214
public static @Nullable Player getPlayerExactIgnoreCase(final String username) {
@@ -36,4 +38,15 @@ public static String translateLegacyColors(@Nonnull String text) {
3638
}
3739
return new String(b);
3840
}
41+
42+
public static <T, R> @Nullable R composeCallable(
43+
Callable<T> callable,
44+
Function<T, R> composer
45+
) {
46+
try {
47+
return composer.apply(callable.call());
48+
} catch (Exception e) {
49+
return null;
50+
}
51+
}
3952
}

0 commit comments

Comments
 (0)