mirror of
https://github.com/songoda/SongodaCore.git
synced 2025-01-22 07:21:21 +01:00
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`.
This commit is contained in:
parent
07374fdbfd
commit
65f606ee83
@ -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<String> 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<String> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user