/* * Copyright (C) 2024 Minecon724 * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file * in the project root for the full license text. */ package eu.m724.tweaks.ping; import org.bukkit.entity.Player; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class PlayerPingStorage { // in nanos private static final Map pings = new ConcurrentHashMap<>(); private static final Map pingsCount = new ConcurrentHashMap<>(); // nanoseconds per tick private static volatile long nspt = 0L; public static long getPingNanos(Player player) { return pings.getOrDefault(player, 0L); } public static double getPingMillis(Player player) { return getPingNanos(player) / 1000000.0; // a mil ns in ms } public static long getNanosPerTick() { return nspt; } public static double getMillisPerTick() { return nspt / 1000000.0; // a mil ns in ms } static void submitPing(Player player, long currentPing) { int count = pingsCount.getOrDefault(player, 0); long average = pings.getOrDefault(player, 0L); if (count < 5) { average = ((average * count) + currentPing) / (count + 1); pingsCount.put(player, count + 1); } else { average += (currentPing - average) / 5; } pings.put(player, average); } static void submitNspt(long currentNspt) { nspt = currentNspt; } }