Skip to content

Fix the 0 tps issue #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/es/mesacarlos/webconsole/WebConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand All @@ -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());

Expand Down Expand Up @@ -100,4 +102,4 @@ public void run() {
public WSServer getWSServer() {
return server;
}
}
}
34 changes: 34 additions & 0 deletions src/es/mesacarlos/webconsole/util/TpsTracker.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 2 additions & 14 deletions src/es/mesacarlos/webconsole/websocket/command/TpsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//------------------------------

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;
Expand All @@ -35,20 +36,7 @@ public void execute(WSServer wsServer, WebSocket conn, String params) {
* @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
}

}
Loading