1.15.2 Get server TPS (not deprecated)

Discussion in 'Spigot Plugin Development' started by redbau, Feb 7, 2020.

  1. What are folks using now that MinecraftServer.getServer().recentTps no longer works?

    So what are you using—Essentials? ClearLagg? Any help would be greatly appreciated!
     
  2. md_5

    Administrator Developer

    [[citation needed]]
     
  3. DevSnx

    DevSnx Previously Snx

    you can use this class

    Code (Text):
    public class ServerTps implements Runnable {

        public static int TICK_COUNT = 0;
        public static long[] TICKS = new long[600];
        public static long LAST_TICK = 0L;

        public static double getTPS() {
            return getTPS(100);
        }

        public static double getTPS(int ticks) {
            if (TICK_COUNT < ticks) {
                return 20.0D;
            }
            int target = (TICK_COUNT - 1 - ticks) % TICKS.length;
            long elapsed = System.currentTimeMillis() - TICKS[target];

            return ticks / (elapsed / 1000.0D);
        }

        public static long getElapsed(int tickID) {
            if (TICK_COUNT - tickID >= TICKS.length) {
            }

            long time = TICKS[(tickID % TICKS.length)];
            return System.currentTimeMillis() - time;
        }

        public void run() {
            TICKS[(TICK_COUNT % TICKS.length)] = System.currentTimeMillis();

            TICK_COUNT += 1;
        }
    }
     
  4. As noted above, you can invoke the getTPS method, which returns an array of longs for the TPS. Something like this would work, implementing a Supplier:
    Code (Java):
    private Supplier tpsSupplier;
    Method method = getServer().getClass().getMethod("getTPS");
    tpsSupplier = () -> {
        try {
            return method.invoke(getServer());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
     
  5. I don't know why you are saying 'Not Deprecated'
    I think that you are only referencing a bad experience with an old plugin.

    At any time whilst using a Spigot server and have the permission to use the command, you can simply type ...
    /TPS

    Perhaps you could expand upon how you came to this conclusion?