From 41b16d11d61899ff72996fd9c9550bbf2c7c22cb Mon Sep 17 00:00:00 2001 From: mathiascode Date: Tue, 15 Oct 2019 03:03:27 +0300 Subject: [PATCH] Server info command --- src/main/java/pw/kaboom/extras/Main.java | 1 + .../extras/commands/CommandServerInfo.java | 113 ++++++++++++++++++ src/main/resources/plugin.yml | 4 + 3 files changed, 118 insertions(+) create mode 100644 src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java diff --git a/src/main/java/pw/kaboom/extras/Main.java b/src/main/java/pw/kaboom/extras/Main.java index 62407f5..926ea95 100644 --- a/src/main/java/pw/kaboom/extras/Main.java +++ b/src/main/java/pw/kaboom/extras/Main.java @@ -661,6 +661,7 @@ public class Main extends JavaPlugin { this.getCommand("jumpscare").setExecutor(new CommandJumpscare()); this.getCommand("prefix").setExecutor(new CommandPrefix()); this.getCommand("pumpkin").setExecutor(new CommandPumpkin()); + this.getCommand("serverinfo").setExecutor(new CommandServerInfo()); this.getCommand("skin").setExecutor(new CommandSkin()); this.getCommand("spawn").setExecutor(new CommandSpawn()); this.getCommand("spidey").setExecutor(new CommandSpidey()); diff --git a/src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java b/src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java new file mode 100644 index 0000000..22353f3 --- /dev/null +++ b/src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java @@ -0,0 +1,113 @@ +package pw.kaboom.extras; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryPoolMXBean; + +import java.net.InetAddress; + +import org.bukkit.ChatColor; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +class CommandServerInfo implements CommandExecutor { + private void sendInfoMessage(CommandSender target, String description, String value) { + target.sendMessage( + ChatColor.GRAY + description + ": " + + ChatColor.WHITE + value + ); + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + try { + sendInfoMessage(sender, "Hostname", + InetAddress.getLocalHost().getHostName() + ); + sendInfoMessage(sender, "IP address", + InetAddress.getLocalHost().getHostAddress() + ); + } catch (Exception exception) { + } + + sendInfoMessage(sender, "OS name", + ManagementFactory.getOperatingSystemMXBean().getName() + ); + sendInfoMessage(sender, "OS architecture", + ManagementFactory.getOperatingSystemMXBean().getArch() + ); + sendInfoMessage(sender, "OS version", + ManagementFactory.getOperatingSystemMXBean().getVersion() + ); + sendInfoMessage(sender, "Java VM", + ManagementFactory.getRuntimeMXBean().getVmName() + ); + sendInfoMessage(sender, "Java version", + ManagementFactory.getRuntimeMXBean().getSpecVersion() + + " " + + ManagementFactory.getRuntimeMXBean().getVmVersion() + ); + + try { + String[] shCommand = { + "/bin/sh", + "-c", + "cat /proc/cpuinfo | grep 'model name' | cut -f 2 -d ':' | awk '{$1=$1}1' | head -1" + }; + + Process process = Runtime.getRuntime().exec(shCommand); + InputStreamReader isr = new InputStreamReader(process.getInputStream()); + BufferedReader br = new BufferedReader(isr); + String line; + + while ((line = br.readLine()) != null) { + sendInfoMessage(sender, "CPU model", + line + ); + } + + br.close(); + } catch (Exception exception) { + } + + sendInfoMessage(sender, "CPU cores", + String.valueOf(Runtime.getRuntime().availableProcessors()) + ); + sendInfoMessage(sender, "CPU load", + String.valueOf(ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()) + ); + + long heapUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); + long nonHeapUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed(); + long memoryMax = ( + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax() + + ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getMax() + ); + long memoryUsage = (heapUsage + nonHeapUsage); + + sendInfoMessage(sender, "Available memory", + (memoryMax / 1024 / 1024) + " MB" + ); + sendInfoMessage(sender, "Heap memory usage", + (heapUsage / 1024 / 1024) + " MB" + ); + sendInfoMessage(sender, "Non-heap memory usage", + (nonHeapUsage / 1024 / 1024) + " MB" + ); + sendInfoMessage(sender, "Total memory usage", + (memoryUsage / 1024 / 1024) + " MB" + ); + + long minutes = (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) / 60; + long seconds = (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) % 60; + + sendInfoMessage(sender, "Server uptime", + minutes + " minute(s) " + + seconds + " second(s)" + ); + return true; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 841ee96..dace348 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -30,6 +30,10 @@ commands: pumpkin: description: Places a pumpkin on a player's head permission: extras.pumpkin + serverinfo: + aliases: specs + description: Shows detailed server information + permission: extras.serverinfo skin: description: Changes your skin permission: extras.skin