|
1 | 1 | package pw.kaboom.extras.commands;
|
2 | 2 |
|
| 3 | +import it.unimi.dsi.fastutil.Pair; |
| 4 | +import it.unimi.dsi.fastutil.objects.ObjectObjectImmutablePair; |
3 | 5 | import net.kyori.adventure.text.Component;
|
4 | 6 | import net.kyori.adventure.text.format.NamedTextColor;
|
5 | 7 | import org.bukkit.command.Command;
|
6 | 8 | import org.bukkit.command.CommandExecutor;
|
7 | 9 | import org.bukkit.command.CommandSender;
|
| 10 | +import org.jetbrains.annotations.Nullable; |
8 | 11 | import oshi.SystemInfo;
|
9 | 12 | import oshi.hardware.GraphicsCard;
|
10 |
| -import oshi.hardware.HardwareAbstractionLayer; |
| 13 | +import pw.kaboom.extras.util.Utility; |
11 | 14 |
|
12 | 15 | import javax.annotation.Nonnull;
|
13 | 16 | import java.lang.management.ManagementFactory;
|
14 | 17 | import java.net.InetAddress;
|
15 | 18 |
|
16 | 19 | 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; |
19 | 22 |
|
20 | 23 | static {
|
21 | 24 | // No need to store this in a static variable as it would
|
22 | 25 | // just waste memory & won't be accessed outside construction
|
23 | 26 | // anyway.
|
24 | 27 |
|
25 | 28 | 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 | + } |
35 | 57 | }
|
36 | 58 |
|
37 | 59 | private void sendInfoMessage(final CommandSender target, final String description,
|
|
0 commit comments