Workaround for TPS

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

View File

@ -21,29 +21,34 @@ 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 };
}
}
} }