mirror of
https://github.com/kaboomserver/extras.git
synced 2025-08-01 18:23:00 +00:00
Re-add CPU model detection and add GPU model detection via OSHI (#372)
* Re-add CPU model detection and add GPU model detection via OSHI * Catch exceptions in getHardware
This commit is contained in:
parent
fce68a8907
commit
763089ddf6
3 changed files with 76 additions and 0 deletions
8
pom.xml
8
pom.xml
|
@ -18,6 +18,14 @@
|
|||
<version>1.20.4-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.oshi</groupId>
|
||||
<artifactId>oshi-core</artifactId>
|
||||
<version>6.4.5</version>
|
||||
<!-- This is a vanilla dependency, as of 1.20.4 -->
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
|
|
@ -1,16 +1,61 @@
|
|||
package pw.kaboom.extras.commands;
|
||||
|
||||
import it.unimi.dsi.fastutil.Pair;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectObjectImmutablePair;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import oshi.SystemInfo;
|
||||
import oshi.hardware.GraphicsCard;
|
||||
import pw.kaboom.extras.util.Utility;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.net.InetAddress;
|
||||
|
||||
public final class CommandServerInfo implements CommandExecutor {
|
||||
private static final @Nullable String[] GPU_DEVICES;
|
||||
private static final @Nullable String PROCESSOR_NAME;
|
||||
|
||||
static {
|
||||
// No need to store this in a static variable as it would
|
||||
// just waste memory & won't be accessed outside construction
|
||||
// anyway.
|
||||
|
||||
final SystemInfo systemInfo = new SystemInfo();
|
||||
|
||||
// Unfortunately, we need to do something like this
|
||||
// because calls to getHardware may fail if the
|
||||
// server is running on an unrecognized platform,
|
||||
// and we're unable to use guard clauses due to
|
||||
// returns not being supported in static blocks.
|
||||
|
||||
final @Nullable Pair<String[], String> hardwareInfo = Utility.composeCallable(
|
||||
systemInfo::getHardware,
|
||||
hardware ->
|
||||
new ObjectObjectImmutablePair<>(
|
||||
hardware.getGraphicsCards()
|
||||
.stream()
|
||||
.map(GraphicsCard::getName)
|
||||
.toArray(String[]::new),
|
||||
hardware.getProcessor()
|
||||
.getProcessorIdentifier()
|
||||
.getName()
|
||||
)
|
||||
);
|
||||
|
||||
if (hardwareInfo == null) {
|
||||
GPU_DEVICES = null;
|
||||
PROCESSOR_NAME = null;
|
||||
} else {
|
||||
GPU_DEVICES = hardwareInfo.first();
|
||||
PROCESSOR_NAME = hardwareInfo.second();
|
||||
}
|
||||
}
|
||||
|
||||
private void sendInfoMessage(final CommandSender target, final String description,
|
||||
final String value) {
|
||||
target.sendMessage(
|
||||
|
@ -50,6 +95,8 @@ public final class CommandServerInfo implements CommandExecutor {
|
|||
+ ManagementFactory.getRuntimeMXBean().getVmVersion()
|
||||
);
|
||||
|
||||
sendInfoMessage(sender, "CPU model", PROCESSOR_NAME);
|
||||
|
||||
sendInfoMessage(sender, "CPU cores",
|
||||
String.valueOf(Runtime.getRuntime().availableProcessors())
|
||||
);
|
||||
|
@ -57,6 +104,14 @@ public final class CommandServerInfo implements CommandExecutor {
|
|||
String.valueOf(ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage())
|
||||
);
|
||||
|
||||
for (int i = 0; i < GPU_DEVICES.length; i++) {
|
||||
sendInfoMessage(
|
||||
sender,
|
||||
"GPU device (" + i + ")",
|
||||
GPU_DEVICES[i]
|
||||
);
|
||||
}
|
||||
|
||||
final long heapUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
|
||||
final long nonHeapUsage = ManagementFactory.getMemoryMXBean()
|
||||
.getNonHeapMemoryUsage().getUsed();
|
||||
|
|
|
@ -7,6 +7,8 @@ import org.bukkit.entity.Player;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.function.Function;
|
||||
|
||||
public final class Utility {
|
||||
public static @Nullable Player getPlayerExactIgnoreCase(final String username) {
|
||||
|
@ -36,4 +38,15 @@ public final class Utility {
|
|||
}
|
||||
return new String(b);
|
||||
}
|
||||
|
||||
public static <T, R> @Nullable R composeCallable(
|
||||
Callable<T> callable,
|
||||
Function<T, R> composer
|
||||
) {
|
||||
try {
|
||||
return composer.apply(callable.call());
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue