From 65f606ee83f690866ae46b871ff697ec18c89fa7 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Wed, 29 Mar 2023 19:21:54 +0200 Subject: [PATCH] Refactor `/songoda diag` command and change output formatting a bit This should make reading the version information displayed easier. No more confusing the plugin version vs. the core version. Additionally fixed `Mb` to `MiB`. --- .../core/core/SongodaCoreDiagCommand.java | 101 ++++++++++-------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/core/SongodaCoreDiagCommand.java b/Core/src/main/java/com/songoda/core/core/SongodaCoreDiagCommand.java index 04c2a94b..cf30d502 100644 --- a/Core/src/main/java/com/songoda/core/core/SongodaCoreDiagCommand.java +++ b/Core/src/main/java/com/songoda/core/core/SongodaCoreDiagCommand.java @@ -9,66 +9,26 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.text.DecimalFormat; import java.util.List; public class SongodaCoreDiagCommand extends AbstractCommand { - private final DecimalFormat format = new DecimalFormat("##.##"); + private final DecimalFormat decimalFormat = new DecimalFormat("##.##"); - private Object serverInstance; - private Field tpsField; + private Object nmsServerInstance; + private Field recentTpsOnNmsServer; public SongodaCoreDiagCommand() { super(CommandType.CONSOLE_OK, "diag"); try { - serverInstance = ClassMapping.MINECRAFT_SERVER.getClazz().getMethod("getServer").invoke(null); - tpsField = serverInstance.getClass().getField("recentTps"); - } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException ex) { + this.nmsServerInstance = ClassMapping.MINECRAFT_SERVER.getClazz().getMethod("getServer").invoke(null); + this.recentTpsOnNmsServer = this.nmsServerInstance.getClass().getField("recentTps"); + } catch (ReflectiveOperationException | SecurityException | IllegalArgumentException ex) { ex.printStackTrace(); } } - @Override - protected ReturnType runCommand(CommandSender sender, String... args) { - sender.sendMessage(""); - sender.sendMessage("Songoda Diagnostics Information"); - sender.sendMessage(""); - sender.sendMessage("Plugins:"); - - for (PluginInfo plugin : SongodaCore.getPlugins()) { - sender.sendMessage(plugin.getJavaPlugin().getName() - + " (" + plugin.getJavaPlugin().getDescription().getVersion() + " Core " + plugin.getCoreLibraryVersion() + ")"); - } - - sender.sendMessage(""); - sender.sendMessage("Server Version: " + Bukkit.getVersion()); - sender.sendMessage("NMS: " + ServerProject.getServerVersion() + " " + ServerVersion.getServerVersionString()); - sender.sendMessage("Operating System: " + System.getProperty("os.name")); - sender.sendMessage("Allocated Memory: " + format.format(Runtime.getRuntime().maxMemory() / (1024 * 1024)) + "Mb"); - sender.sendMessage("Online Players: " + Bukkit.getOnlinePlayers().size()); - - if (tpsField != null) { - try { - double[] tps = ((double[]) tpsField.get(serverInstance)); - - sender.sendMessage("TPS from last 1m, 5m, 15m: " + format.format(tps[0]) + ", " - + format.format(tps[1]) + ", " + format.format(tps[2])); - } catch (IllegalAccessException ex) { - ex.printStackTrace(); - } - } - - return ReturnType.SUCCESS; - } - - @Override - protected List onTab(CommandSender sender, String... args) { - return null; - } - @Override public String getPermissionNode() { return "songoda.admin"; @@ -83,4 +43,53 @@ public class SongodaCoreDiagCommand extends AbstractCommand { public String getDescription() { return "Display diagnostics information."; } + + @Override + protected ReturnType runCommand(CommandSender sender, String... args) { + sender.sendMessage(""); + sender.sendMessage("Songoda Diagnostics Information"); + sender.sendMessage(""); + sender.sendMessage("Plugins:"); + + for (PluginInfo plugin : SongodaCore.getPlugins()) { + sender.sendMessage(String.format("%s v%s (Core v%s)", + plugin.getJavaPlugin().getName(), + plugin.getJavaPlugin().getDescription().getVersion(), + plugin.getCoreLibraryVersion())); + } + + sender.sendMessage(""); + sender.sendMessage("Server Version: " + Bukkit.getVersion()); + sender.sendMessage("NMS: " + ServerProject.getServerVersion() + " " + ServerVersion.getServerVersionString()); + sender.sendMessage("Operating System: " + System.getProperty("os.name")); + sender.sendMessage("Allocated Memory: " + getRuntimeMaxMemory()); + sender.sendMessage("Online Players: " + Bukkit.getOnlinePlayers().size()); + sendCurrentTps(sender); + sender.sendMessage(""); + + return ReturnType.SUCCESS; + } + + @Override + protected List onTab(CommandSender sender, String... args) { + return null; + } + + private String getRuntimeMaxMemory() { + return this.decimalFormat.format(Runtime.getRuntime().maxMemory() / (1024 * 1024)) + " MiB"; + } + + private void sendCurrentTps(CommandSender receiver) { + if (this.recentTpsOnNmsServer == null) { + return; + } + + try { + double[] tps = ((double[]) this.recentTpsOnNmsServer.get(this.nmsServerInstance)); + + receiver.sendMessage(String.format("TPS from last 1m, 5m, 15m: %s, %s, %s", this.decimalFormat.format(tps[0]), this.decimalFormat.format(tps[1]), this.decimalFormat.format(tps[2]))); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + } }