From 6fd006280e8c70e3d7be25e68b365021e180250b Mon Sep 17 00:00:00 2001 From: LOLYAY <118024018+LOLYAYDEV@users.noreply.github.com> Date: Fri, 26 Jul 2024 22:42:25 +0200 Subject: [PATCH 1/3] Update TpsCommand.java TPSFIX --- .../webconsole/websocket/command/TpsCommand.java | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/es/mesacarlos/webconsole/websocket/command/TpsCommand.java b/src/es/mesacarlos/webconsole/websocket/command/TpsCommand.java index 5b9247b..84468f5 100644 --- a/src/es/mesacarlos/webconsole/websocket/command/TpsCommand.java +++ b/src/es/mesacarlos/webconsole/websocket/command/TpsCommand.java @@ -9,6 +9,7 @@ package es.mesacarlos.webconsole.websocket.command; //------------------------------ import es.mesacarlos.webconsole.util.Internationalization; +import es.mesacarlos.webconsole.util.TpsTracker; import es.mesacarlos.webconsole.websocket.WSServer; import es.mesacarlos.webconsole.websocket.response.Tps; import org.java_websocket.WebSocket; @@ -35,20 +36,7 @@ public class TpsCommand implements WSCommand { * @return Current server Tps */ public double[] getTps() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { - try { - Class minecraftServerClass = Class.forName("net.minecraft.server." + mcVer + ".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; - } 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 }; - } + return new double[] { Math.round(TpsTracker.getTPS()) }; // rounding elsewe would get something like 19.93620414673046 / 20 tps } } From 2881e93f361ee89d3b668a2c5c210200845819a8 Mon Sep 17 00:00:00 2001 From: LOLYAY <118024018+LOLYAYDEV@users.noreply.github.com> Date: Fri, 26 Jul 2024 22:43:39 +0200 Subject: [PATCH 2/3] Create TpsTracker.java TPSFIX --- .../webconsole/util/TpsTracker.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/es/mesacarlos/webconsole/util/TpsTracker.java diff --git a/src/es/mesacarlos/webconsole/util/TpsTracker.java b/src/es/mesacarlos/webconsole/util/TpsTracker.java new file mode 100644 index 0000000..e460efe --- /dev/null +++ b/src/es/mesacarlos/webconsole/util/TpsTracker.java @@ -0,0 +1,34 @@ +package es.mesacarlos.webconsole.util; + +public class TpsTracker implements Runnable { + public static int TICK_COUNT = 0; + public static long[] TICKS = new long[600]; + public static long LAST_TICK = 0L; + + public static double getTPS() { + return getTPS(100); + } + + public static double getTPS(int ticks) { + if (TICK_COUNT < ticks) { + return 20.0; + } else { + int target = (TICK_COUNT - 1 - ticks) % TICKS.length; + long elapsed = System.currentTimeMillis() - TICKS[target]; + return (double)ticks / ((double)elapsed / 1000.0); + } + } + + public static long getElapsed(int tickID) { + if (TICK_COUNT - tickID >= TICKS.length) { + } + + long time = TICKS[tickID % TICKS.length]; + return System.currentTimeMillis() - time; + } + + public void run() { + TICKS[TICK_COUNT % TICKS.length] = System.currentTimeMillis(); + ++TICK_COUNT; + } +} From 264e6214b399ba335c95fbff7d3f96688004efe4 Mon Sep 17 00:00:00 2001 From: LOLYAY <118024018+LOLYAYDEV@users.noreply.github.com> Date: Fri, 26 Jul 2024 22:44:21 +0200 Subject: [PATCH 3/3] Update WebConsole.java TPSFIX --- src/es/mesacarlos/webconsole/WebConsole.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/es/mesacarlos/webconsole/WebConsole.java b/src/es/mesacarlos/webconsole/WebConsole.java index 1bccafb..36c94b0 100644 --- a/src/es/mesacarlos/webconsole/WebConsole.java +++ b/src/es/mesacarlos/webconsole/WebConsole.java @@ -9,6 +9,7 @@ import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; +import es.mesacarlos.webconsole.util.TpsTracker; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.Filter; import org.bukkit.Bukkit; @@ -28,6 +29,7 @@ public class WebConsole extends JavaPlugin { @Override public void onEnable() { + Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new TpsTracker(), 100L, 1L); //Change language to user-specified language. Internationalization.setCurrentLocale(ConfigManager.getInstance().getLanguage()); @@ -100,4 +102,4 @@ public class WebConsole extends JavaPlugin { public WSServer getWSServer() { return server; } -} \ No newline at end of file +}