Skip to content

Commit bb53099

Browse files
WouterGritterZECHEESELORD
authored andcommitted
Delay instantiating ConnectedPlayer to after Velocity#canRegisterConnection returns true
(cherry picked from commit 36fb250)
1 parent d11511c commit bb53099

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,16 +692,16 @@ public HttpClient createHttpClient() {
692692
/**
693693
* Checks if the {@code connection} can be registered with the proxy.
694694
*
695-
* @param connection the connection to check
695+
* @param gameProfile the {@link GameProfile} of the incoming connection
696696
* @return {@code true} if we can register the connection, {@code false} if not
697697
*/
698-
public boolean canRegisterConnection(ConnectedPlayer connection) {
698+
public boolean canRegisterConnection(GameProfile gameProfile) {
699699
if (configuration.isOnlineMode() && configuration.isOnlineModeKickExistingPlayers()) {
700700
return true;
701701
}
702-
String lowerName = connection.getUsername().toLowerCase(Locale.US);
702+
String lowerName = gameProfile.getName().toLowerCase(Locale.US);
703703
return !(connectionsByName.containsKey(lowerName)
704-
|| connectionsByUuid.containsKey(connection.getUniqueId()));
704+
|| connectionsByUuid.containsKey(gameProfile.getId()));
705705
}
706706

707707
/**

proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,21 @@
4040
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
4141
import com.velocitypowered.proxy.crypto.IdentifiedKeyImpl;
4242
import com.velocitypowered.proxy.protocol.StateRegistry;
43+
import com.velocitypowered.proxy.protocol.packet.DisconnectPacket;
4344
import com.velocitypowered.proxy.protocol.packet.LoginAcknowledgedPacket;
4445
import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccessPacket;
4546
import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket;
4647
import com.velocitypowered.proxy.protocol.packet.SetCompressionPacket;
48+
import com.velocitypowered.proxy.util.ClosestLocaleMatcher;
4749
import io.netty.buffer.ByteBuf;
50+
import java.util.Locale;
4851
import java.util.Objects;
4952
import java.util.Optional;
5053
import java.util.UUID;
5154
import java.util.concurrent.CompletableFuture;
5255
import net.kyori.adventure.text.Component;
5356
import net.kyori.adventure.text.format.NamedTextColor;
57+
import net.kyori.adventure.translation.GlobalTranslator;
5458
import org.apache.logging.log4j.LogManager;
5559
import org.apache.logging.log4j.Logger;
5660
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -97,16 +101,25 @@ public void activated() {
97101
}
98102

99103
// Initiate a regular connection and move over to it.
104+
if (!server.canRegisterConnection(profileEvent.getGameProfile())) {
105+
// ConnectedPlayer#disconnect0 uses its own translateMessage(), which uses the players' PlayerSettings
106+
// to translate the message, but at this stage this wouldn't have been set yet, resulting in
107+
// Locale.getDefault() being used anyway.
108+
mcConnection.closeWith(DisconnectPacket.create(
109+
GlobalTranslator.render(
110+
Component.translatable("velocity.error.already-connected-proxy", NamedTextColor.RED),
111+
ClosestLocaleMatcher.INSTANCE.lookupClosest(Locale.getDefault())
112+
),
113+
mcConnection.getProtocolVersion(),
114+
mcConnection.getState()
115+
));
116+
return CompletableFuture.completedFuture(null);
117+
}
118+
100119
ConnectedPlayer player = new ConnectedPlayer(server, profileEvent.getGameProfile(),
101120
mcConnection, inbound.getVirtualHost().orElse(null), inbound.getRawVirtualHost().orElse(null), onlineMode,
102121
inbound.getHandshakeIntent(), inbound.getIdentifiedKey());
103122
this.connectedPlayer = player;
104-
if (!server.canRegisterConnection(player)) {
105-
player.disconnect0(
106-
Component.translatable("velocity.error.already-connected-proxy", NamedTextColor.RED),
107-
true);
108-
return CompletableFuture.completedFuture(null);
109-
}
110123

111124
if (server.getConfiguration().isLogPlayerConnections()) {
112125
logger.info("{} has connected", player);

0 commit comments

Comments
 (0)