From 6bdb25a3c1ed5ded66482c0f6c4ae814f981a182 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 7 Nov 2020 11:32:38 +0100 Subject: [PATCH] Fix and simplify YoyoNows solution --- .../src/de/steamwar/core/Core.java | 2 - .../src/de/steamwar/core/TPSWatcher.java | 88 ++++++------------- 2 files changed, 28 insertions(+), 62 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index a7e1cab..d9c59df 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -64,8 +64,6 @@ public class Core extends JavaPlugin{ ErrorLogger.init(); getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new BungeeReceiver()); getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:bridge"); - TPSWatcher.init(); - } @Override diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java index 9b36d29..0e37709 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java @@ -23,57 +23,38 @@ import org.bukkit.Bukkit; 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 TPSWatcher tps_TenSecond; - - public static void init() { - tps_OneSecond = new TPSWatcher(1000); - tps_TenSecond = new TPSWatcher(10000); - } + private static final TPSWatcher tps_OneSecond = new TPSWatcher(1000); + private static final TPSWatcher tps_TenSecond = new TPSWatcher(10000); private long lastTime = System.currentTimeMillis(); - private double tps = 20.0; private TPSWatcher(long timeInterval) { Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> { - long tickTime = System.currentTimeMillis() - lastTime; - lastTime = System.currentTimeMillis(); + long currentTime = System.currentTimeMillis(); - double now = 0; - if (tickTime != 0) { - now = (timeInterval / (double) tickTime) * tickDefault; - } - if (now < 0) { - now = 0; - } - tps = now; + if (currentTime > lastTime) + tps = (timeInterval / (double) (currentTime - lastTime)) * TICK_DEFAULT; + + lastTime = currentTime; }, timeInterval / 50, timeInterval / 50); } - public enum TPSType { - - // With own implementation - ONE_SECOND, - TEN_SECONDS, - // With getting the values from Spigot itself - ONE_MINUTE, - FIVE_MINUTES, - TEN_MINUTES - - } - - public double getTPS() { + public static double getTPS() { return getTPS(TPSType.ONE_SECOND); } - public double getTPSUnlimited() { + public static double getTPSUnlimited() { 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) { case TEN_SECONDS: return round(tps_TenSecond.tps); @@ -83,29 +64,13 @@ public class TPSWatcher { return round(getSpigotTPS()[1]); case TEN_MINUTES: return round(getSpigotTPS()[2]); - + case ONE_SECOND: default: return round(tps_OneSecond.tps); } } - public double getTPSUnlimited(TPSType tpsType) { - 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() { + private static double[] getSpigotTPS() { switch (Core.getVersion()) { case 8: return SpigotTPS_8.getTps(); @@ -113,22 +78,25 @@ public class TPSWatcher { return SpigotTPS_9.getTps(); case 10: return SpigotTPS_10.getTps(); + case 12: + return SpigotTPS_12.getTps(); case 14: return SpigotTPS_14.getTps(); case 15: - return SpigotTPS_15.getTps(); - default: - return SpigotTPS_12.getTps(); + return SpigotTPS_15.getTps(); } } - private double round(double d) { - return Math.max(Math.round(d * 10) / 10.0, 20.0); - } - - private double roundUnlimited(double d) { + private static double round(double d) { return Math.round(d * 10) / 10.0; } + public enum TPSType { + ONE_SECOND, + TEN_SECONDS, + ONE_MINUTE, + FIVE_MINUTES, + TEN_MINUTES + } }