@@ -10,6 +10,10 @@ public class Ping implements BaseMod {
1010
1111 private static final ModSettings SETTINGS = new ModSettings ("top-left" );
1212
13+ private static long lastPingSent = 0 ;
14+ private static long lastPingValue = -1 ;
15+ private static final long PING_INTERVAL_MS = 10000 ;
16+
1317 @ Override
1418 public String getModID () {
1519 return "Ping" ;
@@ -22,23 +26,26 @@ public ModSettings getModSettings() {
2226
2327 @ Override
2428 public CustomText onStartTick (MinecraftClient client ) {
25- PlayerEntity player = client .player ;
26-
27- if (
28- player == null ||
29- player .getUuid () == null ||
30- client .getNetworkHandler () == null ||
31- client .getNetworkHandler ().getPlayerListEntry (player .getUuid ()) ==
32- null
33- ) return null ;
34-
35- return new CustomText (
36- client
37- .getNetworkHandler ()
38- .getPlayerListEntry (player .getUuid ())
39- .getLatency () +
40- " ms" ,
41- getModSettings ()
42- );
29+ if (client .player == null || client .getNetworkHandler () == null )
30+ return null ;
31+
32+ long currentTime = System .currentTimeMillis ();
33+
34+ if (currentTime - lastPingSent >= PING_INTERVAL_MS ) {
35+ lastPingSent = now ;
36+ client .getNetworkHandler ().sendPacket (
37+ new net .minecraft .network .packet .c2s .query .PingRequestC2SPacket (currentTime ));
38+ }
39+
40+ if (lastPingValue == -1 ) {
41+ return new CustomText ("... ms" , getModSettings ());
42+ }
43+
44+ return new CustomText (lastPingValue + " ms" , getModSettings ());
45+ }
46+
47+ // Called when server responds
48+ public static void handlePingResponse (long sentTime ) {
49+ lastPingValue = System .currentTimeMillis () - sentTime ;
4350 }
4451}
0 commit comments