Add /status command

This commit is contained in:
Melledy
2024-05-19 10:46:27 -07:00
parent 4a96becdf0
commit 1c77932348
3 changed files with 44 additions and 2 deletions

View File

@@ -226,7 +226,7 @@ public class LunarCore {
return ""; return "";
} }
private static String getGitHash() { public static String getGitHash() {
// Use a string builder in case one of the build config fields are missing // Use a string builder in case one of the build config fields are missing
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
@@ -246,7 +246,7 @@ public class LunarCore {
} }
if (builder.isEmpty()) { if (builder.isEmpty()) {
return ""; return "UNKNOWN";
} else { } else {
return builder.toString(); return builder.toString();
} }
@@ -274,6 +274,14 @@ public class LunarCore {
timeOffset = 0; timeOffset = 0;
} }
} }
/**
* Returns the memory usage of the server, in megabytes.
*/
public static long getMemoryUsage() {
Runtime runtime = Runtime.getRuntime();
return (runtime.totalMemory() - runtime.freeMemory()) / 1_048_576L;
}
// Server console // Server console

View File

@@ -0,0 +1,28 @@
package emu.lunarcore.command.commands;
import emu.lunarcore.LunarCore;
import emu.lunarcore.command.Command;
import emu.lunarcore.command.CommandArgs;
import emu.lunarcore.command.CommandHandler;
@Command(label = "status", aliases = {"st", "stats"}, permission = "admin.status", desc = "/status. Displays the status of the server.")
public class StatusCommand implements CommandHandler {
@Override
public void execute(CommandArgs args) {
// Run garbage collector
if (!args.hasFlag("-nogc")) {
System.gc();
}
// Show status
args.sendMessage("Showing server status");
args.sendMessage("Git hash: " + LunarCore.getGitHash());
args.sendMessage("Memory usage: " + LunarCore.getMemoryUsage() + " MB");
if (LunarCore.getGameServer() != null) {
args.sendMessage("Player count: " + LunarCore.getGameServer().getPlayerCount());
}
}
}

View File

@@ -70,6 +70,12 @@ public class GameServer extends KcpServer {
public GameServerConfig getServerConfig() { public GameServerConfig getServerConfig() {
return this.serverConfig; return this.serverConfig;
} }
public int getPlayerCount() {
synchronized (this.players) {
return this.players.size();
}
}
public void registerPlayer(Player player) { public void registerPlayer(Player player) {
synchronized (this.players) { synchronized (this.players) {