Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -50,13 +95,23 @@ public boolean onCommand(final @Nonnull CommandSender sender,
+ ManagementFactory.getRuntimeMXBean().getVmVersion()
);

sendInfoMessage(sender, "CPU model", PROCESSOR_NAME);

sendInfoMessage(sender, "CPU cores",
String.valueOf(Runtime.getRuntime().availableProcessors())
);
sendInfoMessage(sender, "CPU load",
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();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/pw/kaboom/extras/util/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

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) {
Expand Down Expand Up @@ -36,4 +38,15 @@ public static String translateLegacyColors(@Nonnull String text) {
}
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