From 9bab6b8c8ec80901fdfc96f1253829d9c5f8de16 Mon Sep 17 00:00:00 2001 From: Daniel Naylor Date: Sat, 24 Nov 2018 21:24:35 +0000 Subject: [PATCH] Update for master changes and comments --- .../backend/VelocityServerConnection.java | 25 +++++++++++++++---- .../connection/client/ConnectedPlayer.java | 6 ++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index 54f7a3c42..bb0bb0a41 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -50,6 +50,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, private BackendConnectionPhase connectionPhase = BackendConnectionPhases.UNKNOWN; private long lastPingId; private long lastPingSent; + private boolean hasCompletedJoin = false; public VelocityServerConnection(VelocityRegisteredServer registeredServer, ConnectedPlayer proxyPlayer, VelocityServer server) { @@ -204,11 +205,14 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, } public void completeJoin() { - if (connectionPhase == BackendConnectionPhases.UNKNOWN) { - // Now we know - connectionPhase = BackendConnectionPhases.VANILLA; - if (connection != null) { - connection.setType(ConnectionTypes.VANILLA); + if (!hasCompletedJoin) { + hasCompletedJoin = true; + if (connectionPhase == BackendConnectionPhases.UNKNOWN) { + // Now we know + connectionPhase = BackendConnectionPhases.VANILLA; + if (connection != null) { + connection.setType(ConnectionTypes.VANILLA); + } } } } @@ -264,4 +268,15 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, public void setConnectionPhase(BackendConnectionPhase connectionPhase) { this.connectionPhase = connectionPhase; } + + /** + * Gets whether the {@link com.velocitypowered.proxy.protocol.packet.JoinGame} + * packet has been sent by this server. + * + * @return Whether the join has been completed. + */ + public boolean hasCompletedJoin() { + return hasCompletedJoin; + } + } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index ba055d9b9..35f4bc7f7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -46,10 +46,10 @@ import java.net.InetSocketAddress; import java.util.Collections; import java.util.List; import java.util.Optional; -import java.util.Random; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; +import java.util.concurrent.ThreadLocalRandom; import net.kyori.text.Component; import net.kyori.text.TextComponent; import net.kyori.text.TranslatableComponent; @@ -67,7 +67,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { c -> "", TranslatableComponent::key); static final PermissionProvider DEFAULT_PERMISSIONS = s -> PermissionFunction.ALWAYS_UNDEFINED; - private static final ThreadLocal threadLocalRandom = ThreadLocal.withInitial(Random::new); + private static final ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); private static final Logger logger = LogManager.getLogger(ConnectedPlayer.class); private final MinecraftConnection connection; @@ -479,7 +479,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { public void sendKeepAlive() { if (connection.getState() == StateRegistry.PLAY) { KeepAlive keepAlive = new KeepAlive(); - keepAlive.setRandomId(threadLocalRandom.get().nextLong()); + keepAlive.setRandomId(threadLocalRandom.nextLong()); connection.write(keepAlive); } }