From 408b420af7e04909ecb27cca7225875059eaca07 Mon Sep 17 00:00:00 2001 From: 0xAda Date: Thu, 4 Jan 2024 17:35:03 +0000 Subject: [PATCH] Use System.nanoTime in place of System.currentTimeMillis when comparing timestamps. (#1174) --- proxy/src/main/java/com/velocitypowered/proxy/Velocity.java | 5 +++-- .../proxy/connection/backend/BackendPlaySessionHandler.java | 2 +- .../proxy/connection/client/ClientConfigSessionHandler.java | 3 ++- .../proxy/connection/client/ClientPlaySessionHandler.java | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java b/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java index 779879eeb..cd4720cf5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java @@ -20,6 +20,7 @@ package com.velocitypowered.proxy; import io.netty.util.ResourceLeakDetector; import io.netty.util.ResourceLeakDetector.Level; import java.text.DecimalFormat; +import java.util.concurrent.TimeUnit; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -63,14 +64,14 @@ public class Velocity { return; } - long startTime = System.currentTimeMillis(); + long startTime = System.nanoTime(); VelocityServer server = new VelocityServer(options); server.start(); Runtime.getRuntime().addShutdownHook(new Thread(() -> server.shutdown(false), "Shutdown thread")); - double bootTime = (System.currentTimeMillis() - startTime) / 1000d; + double bootTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) / 1000d; logger.info("Done ({}s)!", new DecimalFormat("#.##").format(bootTime)); server.getConsoleCommandSource().start(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index c34b6fa7c..612b858da 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -137,7 +137,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public boolean handle(KeepAlive packet) { - serverConn.getPendingPings().put(packet.getRandomId(), System.currentTimeMillis()); + serverConn.getPendingPings().put(packet.getRandomId(), System.nanoTime()); return false; // forwards on } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java index d14e3b823..e2d53e989 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java @@ -36,6 +36,7 @@ import com.velocitypowered.proxy.protocol.util.PluginMessageUtil; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.logging.log4j.LogManager; @@ -81,7 +82,7 @@ public class ClientConfigSessionHandler implements MinecraftSessionHandler { if (sentTime != null) { MinecraftConnection smc = serverConnection.getConnection(); if (smc != null) { - player.setPing(System.currentTimeMillis() - sentTime); + player.setPing(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sentTime)); smc.write(packet); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 08da36ab5..b79a6a8e6 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -81,6 +81,7 @@ import java.util.Queue; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.logging.log4j.LogManager; @@ -177,7 +178,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (sentTime != null) { MinecraftConnection smc = serverConnection.getConnection(); if (smc != null) { - player.setPing(System.currentTimeMillis() - sentTime); + player.setPing(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sentTime)); smc.write(packet); } }