From 7ab9dd2aa82d96076b85244c1cc418dc4381bc23 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 27 Sep 2020 15:50:19 +0200 Subject: [PATCH] Add TPSWatcher --- .../src/de/steamwar/core/Core.java | 6 +- .../src/de/steamwar/core/TPSWatcher.java | 73 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 27d799c..9294b30 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -31,6 +31,7 @@ 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(); @@ -61,6 +62,7 @@ public class Core extends JavaPlugin{ ChunkListener.init(); if(version >= 12) ErrorLogger.init(); + tpsWatcher = new TPSWatcher(); } @Override @@ -74,7 +76,9 @@ public class Core extends JavaPlugin{ public static int getVersion(){ 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 new file mode 100644 index 0000000..71456c5 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java @@ -0,0 +1,73 @@ +package de.steamwar.core; + +import org.bukkit.Bukkit; + +import java.util.LinkedList; + +public class TPSWatcher { + + private static class TPSMeter { + + private LinkedList meter = new LinkedList<>(); + private int count; + + public TPSMeter(int count) { + this.count = count; + } + + public void add(double tps) { + meter.addLast(tps); + if (meter.size() > count) meter.removeFirst(); + } + + public double get() { + double total = 0; + for (double d : meter) total += d; + return total / meter.size(); + } + + } + + private double now = 20.0; + private TPSMeter one_minute = new TPSMeter(1200); + private TPSMeter five_minutes = new TPSMeter(6000); + private TPSMeter ten_minutes = new TPSMeter(12000); + + private long lastTime = System.currentTimeMillis(); + + private static final double tickTimeDefault = 1000 / 20.0; + private static final double tickDefault = 20.0; + + TPSWatcher() { + Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> { + long tickTime = System.currentTimeMillis() - lastTime; + lastTime = System.currentTimeMillis(); + + if (tickTime != 0) { + now = (tickTimeDefault / tickTime) * tickDefault; + } else { + now = 0; + } + one_minute.add(now); + five_minutes.add(now); + ten_minutes.add(now); + }, 1, 1); + } + + public double getTPS() { + return now; + } + + public double get1TPS() { + return one_minute.get(); + } + + public double get5TPS() { + return five_minutes.get(); + } + + public double get10TPS() { + return ten_minutes.get(); + } + +} -- 2.39.5