SteamWar/SpigotCore
Archiviert
13
0

Fix and simplify YoyoNows solution

Dieser Commit ist enthalten in:
Lixfel 2020-11-07 11:32:38 +01:00
Ursprung 1088e3e037
Commit 6bdb25a3c1
2 geänderte Dateien mit 28 neuen und 62 gelöschten Zeilen

Datei anzeigen

@ -64,8 +64,6 @@ public class Core extends JavaPlugin{
ErrorLogger.init(); ErrorLogger.init();
getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new BungeeReceiver()); getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new BungeeReceiver());
getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:bridge"); getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:bridge");
TPSWatcher.init();
} }
@Override @Override

Datei anzeigen

@ -23,57 +23,38 @@ import org.bukkit.Bukkit;
public class TPSWatcher { public class TPSWatcher {
private static final double tickDefault = 20.0; private static final double TICK_DEFAULT = 20.0;
private static TPSWatcher tps_OneSecond; private static final TPSWatcher tps_OneSecond = new TPSWatcher(1000);
private static TPSWatcher tps_TenSecond; private static final TPSWatcher tps_TenSecond = new TPSWatcher(10000);
public static void init() {
tps_OneSecond = new TPSWatcher(1000);
tps_TenSecond = new TPSWatcher(10000);
}
private long lastTime = System.currentTimeMillis(); private long lastTime = System.currentTimeMillis();
private double tps = 20.0; private double tps = 20.0;
private TPSWatcher(long timeInterval) { private TPSWatcher(long timeInterval) {
Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> { Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> {
long tickTime = System.currentTimeMillis() - lastTime; long currentTime = System.currentTimeMillis();
lastTime = System.currentTimeMillis();
double now = 0; if (currentTime > lastTime)
if (tickTime != 0) { tps = (timeInterval / (double) (currentTime - lastTime)) * TICK_DEFAULT;
now = (timeInterval / (double) tickTime) * tickDefault;
} lastTime = currentTime;
if (now < 0) {
now = 0;
}
tps = now;
}, timeInterval / 50, timeInterval / 50); }, timeInterval / 50, timeInterval / 50);
} }
public enum TPSType { public static double getTPS() {
// With own implementation
ONE_SECOND,
TEN_SECONDS,
// With getting the values from Spigot itself
ONE_MINUTE,
FIVE_MINUTES,
TEN_MINUTES
}
public double getTPS() {
return getTPS(TPSType.ONE_SECOND); return getTPS(TPSType.ONE_SECOND);
} }
public double getTPSUnlimited() { public static double getTPSUnlimited() {
return getTPSUnlimited(TPSType.ONE_SECOND); return getTPSUnlimited(TPSType.ONE_SECOND);
} }
public double getTPS(TPSType tpsType) { public static double getTPS(TPSType tpsType) {
return Math.min(getTPSUnlimited(tpsType), 20.0);
}
public static double getTPSUnlimited(TPSType tpsType) {
switch (tpsType) { switch (tpsType) {
case TEN_SECONDS: case TEN_SECONDS:
return round(tps_TenSecond.tps); return round(tps_TenSecond.tps);
@ -83,29 +64,13 @@ public class TPSWatcher {
return round(getSpigotTPS()[1]); return round(getSpigotTPS()[1]);
case TEN_MINUTES: case TEN_MINUTES:
return round(getSpigotTPS()[2]); return round(getSpigotTPS()[2]);
case ONE_SECOND:
default: default:
return round(tps_OneSecond.tps); return round(tps_OneSecond.tps);
} }
} }
public double getTPSUnlimited(TPSType tpsType) { private static double[] getSpigotTPS() {
switch (tpsType) {
case TEN_SECONDS:
return roundUnlimited(tps_TenSecond.tps);
case ONE_MINUTE:
return roundUnlimited(getSpigotTPS()[0]);
case FIVE_MINUTES:
return roundUnlimited(getSpigotTPS()[1]);
case TEN_MINUTES:
return roundUnlimited(getSpigotTPS()[2]);
default:
return roundUnlimited(tps_OneSecond.tps);
}
}
private double[] getSpigotTPS() {
switch (Core.getVersion()) { switch (Core.getVersion()) {
case 8: case 8:
return SpigotTPS_8.getTps(); return SpigotTPS_8.getTps();
@ -113,22 +78,25 @@ public class TPSWatcher {
return SpigotTPS_9.getTps(); return SpigotTPS_9.getTps();
case 10: case 10:
return SpigotTPS_10.getTps(); return SpigotTPS_10.getTps();
case 12:
return SpigotTPS_12.getTps();
case 14: case 14:
return SpigotTPS_14.getTps(); return SpigotTPS_14.getTps();
case 15: case 15:
return SpigotTPS_15.getTps();
default: default:
return SpigotTPS_12.getTps(); return SpigotTPS_15.getTps();
} }
} }
private double round(double d) { private static double round(double d) {
return Math.max(Math.round(d * 10) / 10.0, 20.0);
}
private double roundUnlimited(double d) {
return Math.round(d * 10) / 10.0; return Math.round(d * 10) / 10.0;
} }
public enum TPSType {
ONE_SECOND,
TEN_SECONDS,
ONE_MINUTE,
FIVE_MINUTES,
TEN_MINUTES
}
} }