|
| 1 | +package it.renvins.serverpulse.fabric.metrics; |
| 2 | + |
| 3 | +import it.renvins.serverpulse.api.metrics.IPingRetriever; |
| 4 | +import it.renvins.serverpulse.fabric.ServerPulseFabric; |
| 5 | +import lombok.RequiredArgsConstructor; |
| 6 | +import net.fabricmc.fabric.api.networking.v1.PlayerLookup; |
| 7 | +import net.minecraft.server.network.ServerPlayerEntity; |
| 8 | + |
| 9 | +@RequiredArgsConstructor |
| 10 | +public class FabricPingRetriever implements IPingRetriever { |
| 11 | + |
| 12 | + private final ServerPulseFabric mod; |
| 13 | + |
| 14 | + @Override |
| 15 | + public int getMinPing() { |
| 16 | + int minPing = Integer.MAX_VALUE; |
| 17 | + if (PlayerLookup.all(mod.getServer()).isEmpty()) { |
| 18 | + return 0; |
| 19 | + } |
| 20 | + for (ServerPlayerEntity player : PlayerLookup.all(mod.getServer())) { |
| 21 | + minPing = Math.toIntExact(Math.min(minPing, player.networkHandler.getLatency())); |
| 22 | + } |
| 23 | + return minPing; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public int getMaxPing() { |
| 28 | + int maxPing = 0; |
| 29 | + if (PlayerLookup.all(mod.getServer()).isEmpty()) { |
| 30 | + return 0; |
| 31 | + } |
| 32 | + for (ServerPlayerEntity player : PlayerLookup.all(mod.getServer())) { |
| 33 | + maxPing = Math.toIntExact(Math.max(maxPing, player.networkHandler.getLatency())); |
| 34 | + } |
| 35 | + return maxPing; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public int getAveragePing() { |
| 40 | + int totalPing = 0; |
| 41 | + int playerCount = PlayerLookup.all(mod.getServer()).size(); |
| 42 | + if (playerCount == 0) { |
| 43 | + return 0; |
| 44 | + } |
| 45 | + for (ServerPlayerEntity player : PlayerLookup.all(mod.getServer())) { |
| 46 | + totalPing += player.networkHandler.getLatency(); |
| 47 | + } |
| 48 | + return totalPing / playerCount; |
| 49 | + } |
| 50 | +} |
0 commit comments