Added TPS
This commit is contained in:
@ -23,7 +23,8 @@ import es.mesacarlos.webconsole.websocket.response.LoggedIn;
|
||||
import es.mesacarlos.webconsole.websocket.response.UnknownCommand;
|
||||
|
||||
public class WSServer extends WebSocketServer {
|
||||
private HashMap<String, WSCommand> commands = WSCommandFactory.getCommandsHashMap();
|
||||
|
||||
private final HashMap<String, WSCommand> commands = WSCommandFactory.getCommandsHashMap();
|
||||
|
||||
public WSServer(InetSocketAddress address) {
|
||||
super(address);
|
||||
|
@ -0,0 +1,47 @@
|
||||
package es.mesacarlos.webconsole.websocket.command;
|
||||
|
||||
//------------------------------
|
||||
//
|
||||
// This class was developed by Rafael K.
|
||||
// On 1/8/2022 at 10:22 PM
|
||||
// In the project WebConsole
|
||||
//
|
||||
//------------------------------
|
||||
|
||||
import es.mesacarlos.webconsole.util.Internationalization;
|
||||
import es.mesacarlos.webconsole.websocket.WSServer;
|
||||
import es.mesacarlos.webconsole.websocket.response.Tps;
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class TpsCommand implements WSCommand {
|
||||
|
||||
@Override
|
||||
public void execute(WSServer wsServer, WebSocket conn, String params) {
|
||||
try {
|
||||
double tps = getTps()[0];
|
||||
wsServer.sendToClient(conn, new Tps(Internationalization.getPhrase("tps-message", tps), tps));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Current server Tps
|
||||
*/
|
||||
public double[] getTps() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
|
||||
Class<?> minecraftServerClass = Class.forName("net.minecraft.server.MinecraftServer");
|
||||
Method getServerMethod = minecraftServerClass.getDeclaredMethod("getServer");
|
||||
Object serverInstance = getServerMethod.invoke(null);
|
||||
Field recentTpsField = serverInstance.getClass().getField("recentTps");
|
||||
double[] recentTps = (double[]) recentTpsField.get(serverInstance);
|
||||
for (int i = 0; i < recentTps.length; i++) {
|
||||
recentTps[i] = Math.round(recentTps[i]);
|
||||
}
|
||||
return recentTps;
|
||||
}
|
||||
|
||||
}
|
@ -11,6 +11,7 @@ public class WSCommandFactory {
|
||||
commands.put("PLAYERS", new PlayersCommand());
|
||||
commands.put("CPUUSAGE", new CpuUsageCommand());
|
||||
commands.put("RAMUSAGE", new RamUsageCommand());
|
||||
commands.put("TPS", new TpsCommand());
|
||||
commands.put("READLOGFILE", new ReadLogFileCommand());
|
||||
return commands;
|
||||
}
|
||||
|
@ -2,9 +2,10 @@ package es.mesacarlos.webconsole.websocket.response;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class ConsoleOutput implements JSONOutput{
|
||||
private String message;
|
||||
private String time;
|
||||
public class ConsoleOutput implements JSONOutput {
|
||||
|
||||
private final String message;
|
||||
private final String time;
|
||||
|
||||
public ConsoleOutput(String message, String time) {
|
||||
this.message = message;
|
||||
|
@ -2,9 +2,10 @@ package es.mesacarlos.webconsole.websocket.response;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class CpuUsage implements JSONOutput{
|
||||
private String message;
|
||||
private double usage;
|
||||
public class CpuUsage implements JSONOutput {
|
||||
|
||||
private final String message;
|
||||
private final double usage;
|
||||
|
||||
public CpuUsage(String message, double usage) {
|
||||
this.message = message;
|
||||
|
@ -4,8 +4,9 @@ import com.google.gson.JsonObject;
|
||||
|
||||
import es.mesacarlos.webconsole.config.UserType;
|
||||
|
||||
public class LoggedIn implements JSONOutput{
|
||||
private String message;
|
||||
public class LoggedIn implements JSONOutput {
|
||||
|
||||
private final String message;
|
||||
private String respondsTo;
|
||||
private String username;
|
||||
private UserType as;
|
||||
|
@ -2,8 +2,9 @@ package es.mesacarlos.webconsole.websocket.response;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class LoginRequired implements JSONOutput{
|
||||
private String message;
|
||||
public class LoginRequired implements JSONOutput {
|
||||
|
||||
private final String message;
|
||||
|
||||
public LoginRequired(String message) {
|
||||
this.message = message;
|
||||
|
@ -5,11 +5,12 @@ import java.util.List;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class Players implements JSONOutput{
|
||||
private String message;
|
||||
private int connectedPlayers;
|
||||
private int maxPlayers;
|
||||
private List<String> connectedPlayersList;
|
||||
public class Players implements JSONOutput {
|
||||
|
||||
private final String message;
|
||||
private final int connectedPlayers;
|
||||
private final int maxPlayers;
|
||||
private final List<String> connectedPlayersList;
|
||||
|
||||
public Players(String message, int connectedPlayers, int maxPlayers, List<String> connectedPlayersList) {
|
||||
this.message = message;
|
||||
|
@ -3,10 +3,11 @@ package es.mesacarlos.webconsole.websocket.response;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class RamUsage implements JSONOutput {
|
||||
private String message;
|
||||
private long free;
|
||||
private long used;
|
||||
private long max;
|
||||
|
||||
private final String message;
|
||||
private final long free;
|
||||
private final long used;
|
||||
private final long max;
|
||||
|
||||
public RamUsage(String message, long free, long used, long max) {
|
||||
this.message = message;
|
||||
|
51
src/es/mesacarlos/webconsole/websocket/response/Tps.java
Normal file
51
src/es/mesacarlos/webconsole/websocket/response/Tps.java
Normal file
@ -0,0 +1,51 @@
|
||||
package es.mesacarlos.webconsole.websocket.response;
|
||||
|
||||
//------------------------------
|
||||
//
|
||||
// This class was developed by Rafael K.
|
||||
// On 1/8/2022 at 10:23 PM
|
||||
// In the project WebConsole
|
||||
//
|
||||
//------------------------------
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class Tps implements JSONOutput {
|
||||
|
||||
private final String message;
|
||||
private final double tps;
|
||||
|
||||
public Tps(String message, double tps) {
|
||||
this.message = message;
|
||||
this.tps = tps;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStatusCode() {
|
||||
return 1003;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets current server TPS
|
||||
* @return Global Server TPS
|
||||
*/
|
||||
public double getTps() {
|
||||
return tps;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJSON() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("status", getStatusCode());
|
||||
object.addProperty("statusDescription", "TPS Usage");
|
||||
object.addProperty("tps", getTps());
|
||||
object.addProperty("message", getMessage());
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
}
|
@ -2,9 +2,10 @@ package es.mesacarlos.webconsole.websocket.response;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class UnknownCommand implements JSONOutput{
|
||||
private String message;
|
||||
private String respondsTo;
|
||||
public class UnknownCommand implements JSONOutput {
|
||||
|
||||
private final String message;
|
||||
private final String respondsTo;
|
||||
|
||||
public UnknownCommand(String message, String respondsTo) {
|
||||
this.message = message;
|
||||
|
Reference in New Issue
Block a user