Workaround for TPS

This commit is contained in:
Carlos 2022-04-24 12:00:35 +02:00
parent c5799af7dc
commit dc1ad6d418

View File

@ -1,49 +1,54 @@
package es.mesacarlos.webconsole.websocket.command; package es.mesacarlos.webconsole.websocket.command;
//------------------------------ //------------------------------
// //
// This class was developed by Rafael K. // This class was developed by Rafael K.
// On 1/8/2022 at 10:22 PM // On 1/8/2022 at 10:22 PM
// In the project WebConsole // In the project WebConsole
// //
//------------------------------ //------------------------------
import es.mesacarlos.webconsole.util.Internationalization; import es.mesacarlos.webconsole.util.Internationalization;
import es.mesacarlos.webconsole.websocket.WSServer; import es.mesacarlos.webconsole.websocket.WSServer;
import es.mesacarlos.webconsole.websocket.response.Tps; import es.mesacarlos.webconsole.websocket.response.Tps;
import org.java_websocket.WebSocket; import org.java_websocket.WebSocket;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class TpsCommand implements WSCommand { public class TpsCommand implements WSCommand {
private static final String mcVer = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; private static final String mcVer = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
@Override @Override
public void execute(WSServer wsServer, WebSocket conn, String params) { public void execute(WSServer wsServer, WebSocket conn, String params) {
try { try {
double tps = getTps()[0]; double tps = getTps()[0];
wsServer.sendToClient(conn, new Tps(Internationalization.getPhrase("tps-message", tps), tps)); wsServer.sendToClient(conn, new Tps(Internationalization.getPhrase("tps-message", tps), tps));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* @return Current server Tps * @return Current server Tps
*/ */
public double[] getTps() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { public double[] getTps() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
Class<?> minecraftServerClass = Class.forName("net.minecraft.server." + mcVer + ".MinecraftServer"); try {
Method getServerMethod = minecraftServerClass.getDeclaredMethod("getServer"); Class<?> minecraftServerClass = Class.forName("net.minecraft.server." + mcVer + ".MinecraftServer");
Object serverInstance = getServerMethod.invoke(null); Method getServerMethod = minecraftServerClass.getDeclaredMethod("getServer");
Field recentTpsField = serverInstance.getClass().getField("recentTps"); Object serverInstance = getServerMethod.invoke(null);
double[] recentTps = (double[]) recentTpsField.get(serverInstance); Field recentTpsField = serverInstance.getClass().getField("recentTps");
for (int i = 0; i < recentTps.length; i++) { double[] recentTps = (double[]) recentTpsField.get(serverInstance);
recentTps[i] = Math.round(recentTps[i]); for (int i = 0; i < recentTps.length; i++) {
} recentTps[i] = Math.round(recentTps[i]);
return recentTps; }
} return recentTps;
} catch (Exception e) {
} //If an uncaught exception is thrown, maybe it is because this method of getting TPS does not work in the MV version currently running..
return new double[] { 0 };
}
}
}