Skip to content

Commit a942ce7

Browse files
authored
Merge pull request #154 from LOLYAYDEV/master
Fix the 0 tps issue
2 parents 652a81b + 264e621 commit a942ce7

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

src/es/mesacarlos/webconsole/WebConsole.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.net.ssl.SSLContext;
1010
import javax.net.ssl.TrustManagerFactory;
1111

12+
import es.mesacarlos.webconsole.util.TpsTracker;
1213
import org.apache.logging.log4j.LogManager;
1314
import org.apache.logging.log4j.core.Filter;
1415
import org.bukkit.Bukkit;
@@ -28,6 +29,7 @@ public class WebConsole extends JavaPlugin {
2829

2930
@Override
3031
public void onEnable() {
32+
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new TpsTracker(), 100L, 1L);
3133
//Change language to user-specified language.
3234
Internationalization.setCurrentLocale(ConfigManager.getInstance().getLanguage());
3335

@@ -100,4 +102,4 @@ public void run() {
100102
public WSServer getWSServer() {
101103
return server;
102104
}
103-
}
105+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package es.mesacarlos.webconsole.util;
2+
3+
public class TpsTracker implements Runnable {
4+
public static int TICK_COUNT = 0;
5+
public static long[] TICKS = new long[600];
6+
public static long LAST_TICK = 0L;
7+
8+
public static double getTPS() {
9+
return getTPS(100);
10+
}
11+
12+
public static double getTPS(int ticks) {
13+
if (TICK_COUNT < ticks) {
14+
return 20.0;
15+
} else {
16+
int target = (TICK_COUNT - 1 - ticks) % TICKS.length;
17+
long elapsed = System.currentTimeMillis() - TICKS[target];
18+
return (double)ticks / ((double)elapsed / 1000.0);
19+
}
20+
}
21+
22+
public static long getElapsed(int tickID) {
23+
if (TICK_COUNT - tickID >= TICKS.length) {
24+
}
25+
26+
long time = TICKS[tickID % TICKS.length];
27+
return System.currentTimeMillis() - time;
28+
}
29+
30+
public void run() {
31+
TICKS[TICK_COUNT % TICKS.length] = System.currentTimeMillis();
32+
++TICK_COUNT;
33+
}
34+
}

src/es/mesacarlos/webconsole/websocket/command/TpsCommand.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//------------------------------
1010

1111
import es.mesacarlos.webconsole.util.Internationalization;
12+
import es.mesacarlos.webconsole.util.TpsTracker;
1213
import es.mesacarlos.webconsole.websocket.WSServer;
1314
import es.mesacarlos.webconsole.websocket.response.Tps;
1415
import org.java_websocket.WebSocket;
@@ -35,20 +36,7 @@ public void execute(WSServer wsServer, WebSocket conn, String params) {
3536
* @return Current server Tps
3637
*/
3738
public double[] getTps() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
38-
try {
39-
Class<?> minecraftServerClass = Class.forName("net.minecraft.server." + mcVer + ".MinecraftServer");
40-
Method getServerMethod = minecraftServerClass.getDeclaredMethod("getServer");
41-
Object serverInstance = getServerMethod.invoke(null);
42-
Field recentTpsField = serverInstance.getClass().getField("recentTps");
43-
double[] recentTps = (double[]) recentTpsField.get(serverInstance);
44-
for (int i = 0; i < recentTps.length; i++) {
45-
recentTps[i] = Math.round(recentTps[i]);
46-
}
47-
return recentTps;
48-
} catch (Exception e) {
49-
//If an uncaught exception is thrown, maybe it is because this method of getting TPS does not work in the MV version currently running..
50-
return new double[] { 0 };
51-
}
39+
return new double[] { Math.round(TpsTracker.getTPS()) }; // rounding elsewe would get something like 19.93620414673046 / 20 tps
5240
}
5341

5442
}

0 commit comments

Comments
 (0)