tweaks724/src/main/java/eu/m724/tweaks/ping/PlayerPingStorage.java
Minecon724 b23f773198
All checks were successful
/ deploy (push) Successful in 5m3s
New ping checker
2024-11-30 20:13:22 +01:00

55 lines
1.5 KiB
Java

/*
* 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<Player, Long> pings = new ConcurrentHashMap<>();
private static final Map<Player, Integer> 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;
}
}