diff --git a/SpigotCore_10/src/de/steamwar/core/SpigotTPS_10.java b/SpigotCore_10/src/de/steamwar/core/SpigotTPS_10.java new file mode 100644 index 0000000..792022e --- /dev/null +++ b/SpigotCore_10/src/de/steamwar/core/SpigotTPS_10.java @@ -0,0 +1,13 @@ +package de.steamwar.core; + +import net.minecraft.server.v1_10_R1.MinecraftServer; + +public class SpigotTPS_10 { + + private SpigotTPS_10(){} + + static double[] getTps(){ + return MinecraftServer.getServer().recentTps; + } + +} diff --git a/SpigotCore_12/src/de/steamwar/core/SpigotTPS_12.java b/SpigotCore_12/src/de/steamwar/core/SpigotTPS_12.java new file mode 100644 index 0000000..60bc6d9 --- /dev/null +++ b/SpigotCore_12/src/de/steamwar/core/SpigotTPS_12.java @@ -0,0 +1,13 @@ +package de.steamwar.core; + +import net.minecraft.server.v1_12_R1.MinecraftServer; + +public class SpigotTPS_12 { + + private SpigotTPS_12(){} + + static double[] getTps(){ + return MinecraftServer.getServer().recentTps; + } + +} diff --git a/SpigotCore_14/src/de/steamwar/core/SpigotTPS_14.java b/SpigotCore_14/src/de/steamwar/core/SpigotTPS_14.java new file mode 100644 index 0000000..6bd8491 --- /dev/null +++ b/SpigotCore_14/src/de/steamwar/core/SpigotTPS_14.java @@ -0,0 +1,13 @@ +package de.steamwar.core; + +import net.minecraft.server.v1_14_R1.MinecraftServer; + +public class SpigotTPS_14 { + + private SpigotTPS_14(){} + + static double[] getTps(){ + return MinecraftServer.getServer().recentTps; + } + +} diff --git a/SpigotCore_15/src/de/steamwar/core/SpigotTPS_15.java b/SpigotCore_15/src/de/steamwar/core/SpigotTPS_15.java new file mode 100644 index 0000000..e9919f9 --- /dev/null +++ b/SpigotCore_15/src/de/steamwar/core/SpigotTPS_15.java @@ -0,0 +1,13 @@ +package de.steamwar.core; + +import net.minecraft.server.v1_15_R1.MinecraftServer; + +public class SpigotTPS_15 { + + private SpigotTPS_15(){} + + static double[] getTps(){ + return MinecraftServer.getServer().recentTps; + } + +} diff --git a/SpigotCore_8/src/de/steamwar/core/SpigotTPS_8.java b/SpigotCore_8/src/de/steamwar/core/SpigotTPS_8.java new file mode 100644 index 0000000..8669728 --- /dev/null +++ b/SpigotCore_8/src/de/steamwar/core/SpigotTPS_8.java @@ -0,0 +1,13 @@ +package de.steamwar.core; + +import net.minecraft.server.v1_8_R3.MinecraftServer; + +public class SpigotTPS_8 { + + private SpigotTPS_8(){} + + static double[] getTps(){ + return MinecraftServer.getServer().recentTps; + } + +} diff --git a/SpigotCore_9/src/de/steamwar/core/SpigotTPS_9.java b/SpigotCore_9/src/de/steamwar/core/SpigotTPS_9.java new file mode 100644 index 0000000..fa5d3e6 --- /dev/null +++ b/SpigotCore_9/src/de/steamwar/core/SpigotTPS_9.java @@ -0,0 +1,13 @@ +package de.steamwar.core; + +import net.minecraft.server.v1_9_R2.MinecraftServer; + +public class SpigotTPS_9 { + + private SpigotTPS_9(){} + + static double[] getTps(){ + return MinecraftServer.getServer().recentTps; + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 43e0bf7..5d5ec25 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -32,7 +32,6 @@ import org.bukkit.plugin.java.JavaPlugin; public class Core extends JavaPlugin{ private static Core instance; private static final int version; - private static TPSWatcher tpsWatcher; static{ String packageName = Bukkit.getServer().getClass().getPackage().getName(); @@ -53,7 +52,6 @@ public class Core extends JavaPlugin{ @Override public void onLoad() { setInstance(this); - tpsWatcher = new TPSWatcher(); } @Override @@ -82,10 +80,6 @@ public class Core extends JavaPlugin{ return version; } - public static TPSWatcher getTpsWatcher() { - return tpsWatcher; - } - private static void setInstance(Core instance) { Core.instance = instance; } diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java index 849b838..97ad502 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java @@ -21,45 +21,20 @@ package de.steamwar.core; import org.bukkit.Bukkit; -import java.util.Iterator; -import java.util.LinkedList; - public class TPSWatcher { - private LinkedList meter = new LinkedList<>(); - private int count = 30; + private static final double tickTimeDefault = 1000; + private static final double tickDefault = 20.0; - private double sum = 0; - private int sumCount = 0; - - private void add(double tps) { - sum += tps; - sumCount++; - if (sumCount > 20) { - meter.addLast(sum); - if (meter.size() > count) meter.removeFirst(); - sum = 0; - sumCount = 0; - } - } - - private double average(int count) { - count = Math.min(count, meter.size()); - if (count == 0) return 20.0; - Iterator doubleIterator = meter.iterator(); - double total = 0; - for (int i = 0; i < count; i++) { - total += doubleIterator.next(); - } - return total / count; - } + private static TPSWatcher tps_OneSecond = new TPSWatcher(1000); + private static TPSWatcher tps_TenSecond = new TPSWatcher(10000); private long lastTime = System.currentTimeMillis(); - private static final double tickTimeDefault = 1000 / 20.0; - private static final double tickDefault = 20.0; + private double tps = 20.0; - TPSWatcher() { + private TPSWatcher(long timeInterval) { + timeInterval = timeInterval / 50; Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> { long tickTime = System.currentTimeMillis() - lastTime; lastTime = System.currentTimeMillis(); @@ -68,25 +43,65 @@ public class TPSWatcher { if (tickTime != 0) { now = (tickTimeDefault / tickTime) * tickDefault; } - add(now); - }, 1, 1); + if (now < 0) { + now = 0; + } + tps = now; + }, timeInterval, timeInterval); } - public enum TpsAverage { - LAST_SECOND(1), - LAST_TEN_SECONDS(10), - LAST_THIRTY_SECONDS(30); + public enum TPSType { - private final int count; + // With own implementation + ONE_SECOND, + TEN_SECONDS, + // With getting the values from Spigot itself + ONE_MINUTE, + FIVE_MINUTES, + TEN_MINUTES - TpsAverage(int count) { - this.count = count; + } + + public double getTPS() { + return getTPS(TPSType.ONE_SECOND); + } + + public double getTPS(TPSType tpsType) { + switch (tpsType) { + case TEN_SECONDS: + return round(tps_TenSecond.tps); + case ONE_MINUTE: + return round(getSpigotTPS()[0]); + case FIVE_MINUTES: + return round(getSpigotTPS()[1]); + case TEN_MINUTES: + return round(getSpigotTPS()[2]); + + default: + return round(tps_OneSecond.tps); } - } - public double getTPS(TpsAverage average) { - return average(average.count); + private double[] getSpigotTPS() { + switch (Core.getVersion()) { + case 8: + return SpigotTPS_8.getTps(); + case 9: + return SpigotTPS_9.getTps(); + case 10: + return SpigotTPS_10.getTps(); + case 14: + return SpigotTPS_14.getTps(); + case 15: + return SpigotTPS_15.getTps(); + + default: + return SpigotTPS_12.getTps(); + } + } + + private double round(double d) { + return Math.round(d * 10) / 10.0; } }