Skip to content

Commit f4b1cb9

Browse files
authored
Remove dependency on Paper (servertap-io#27)
* replace paper dependency with bukkit * add Lag runnable to monitor tick rate instead
1 parent 9282a97 commit f4b1cb9

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
<repositories>
2222
<repository>
23-
<id>papermc</id>
24-
<url>https://papermc.io/repo/repository/maven-public/</url>
23+
<id>spigotmc</id>
24+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
2525
</repository>
2626
<repository>
2727
<id>jitpack.io</id>
@@ -37,8 +37,8 @@
3737
<dependencies>
3838
<!-- Minecraft plugin -->
3939
<dependency>
40-
<groupId>com.destroystokyo.paper</groupId>
41-
<artifactId>paper-api</artifactId>
40+
<groupId>org.bukkit</groupId>
41+
<artifactId>bukkit</artifactId>
4242
<version>1.15.2-R0.1-SNAPSHOT</version>
4343
<scope>provided</scope>
4444
</dependency>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.servertap;
2+
3+
public class Lag implements Runnable {
4+
5+
private static final long[] TICKS = new long[600];
6+
private static int TICK_COUNT = 0;
7+
8+
public static String getTPSString() {
9+
try {
10+
double tpsDouble = getTPS();
11+
if (tpsDouble > 19.5) tpsDouble = 20;
12+
String tps = Double.toString(tpsDouble);
13+
return tps.length() > 4 ? tps.substring(0, 4) : tps;
14+
} catch (Exception e) {
15+
return "3.14";
16+
}
17+
}
18+
19+
public static double getTPS() {
20+
return getTPS(100);
21+
}
22+
23+
public static double getTPS(int ticks) {
24+
if (TICK_COUNT < ticks) return 20;
25+
int target = (TICK_COUNT - 1 - ticks) % TICKS.length;
26+
long elapsed = System.currentTimeMillis() - TICKS[target];
27+
return ticks / (elapsed / 1000d);
28+
}
29+
30+
public void run() {
31+
TICKS[(TICK_COUNT % TICKS.length)] = System.currentTimeMillis();
32+
TICK_COUNT += 1;
33+
}
34+
35+
}

src/main/java/io/servertap/PluginEntrypoint.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public void onEnable() {
2929
FileConfiguration bukkitConfig = getConfig();
3030
setupEconomy();
3131

32+
Bukkit.getScheduler().runTaskTimer(this, new Lag(), 100, 1);
33+
3234
// Get the current class loader.
3335
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
3436

src/main/java/io/servertap/api/v1/ServerApi.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.javalin.http.Context;
55
import io.javalin.http.NotFoundResponse;
66
import io.javalin.plugin.openapi.annotations.*;
7+
import io.servertap.Lag;
78
import io.servertap.api.v1.models.*;
89
import org.bukkit.BanList;
910
import org.bukkit.Bukkit;
@@ -52,15 +53,9 @@ public static void serverGet(Context ctx) {
5253
server.setVersion(bukkitServer.getVersion());
5354
server.setBukkitVersion(bukkitServer.getBukkitVersion());
5455
server.setWhitelistedPlayers(getWhitelist());
55-
// Probably a better way to do this
56-
DecimalFormat df = new DecimalFormat("#.##");
57-
// Possibly add 5m and 15m in the future?
58-
if (bukkitServer.getTPS().length > 0) {
59-
server.setTps(df.format(bukkitServer.getTPS()[0]));
60-
} else {
61-
server.setTps("0.0");
62-
}
6356

57+
// Possibly add 5m and 15m in the future?
58+
server.setTps(Lag.getTPSString());
6459

6560
// Get the list of IP bans
6661
Set<ServerBan> bannedIps = new HashSet<>();

0 commit comments

Comments
 (0)