From 4e64b04464d6f573f919c096457a0f67bf690edd Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 5 Aug 2018 01:30:40 -0400 Subject: [PATCH] Avoid duplicate logins. --- .../com/velocitypowered/proxy/VelocityServer.java | 12 +++++++++--- .../proxy/connection/client/LoginSessionHandler.java | 8 ++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 174ff6012..a18cd2db8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -118,9 +118,15 @@ public class VelocityServer implements ProxyServer { return httpClient; } - public void registerConnection(ConnectedPlayer connection) { - connectionsByName.put(connection.getUsername(), connection); - connectionsByUuid.put(connection.getUniqueId(), connection); + public boolean registerConnection(ConnectedPlayer connection) { + if (connectionsByName.putIfAbsent(connection.getUsername(), connection) != null) { + return false; + } + if (connectionsByUuid.putIfAbsent(connection.getUniqueId(), connection) != null) { + connectionsByName.remove(connection.getUsername(), connection); + return false; + } + return true; } public void unregisterConnection(ConnectedPlayer connection) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java index 1826baa48..d97bdb11f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java @@ -136,11 +136,15 @@ public class LoginSessionHandler implements MinecraftSessionHandler { success.setUuid(profile.idAsUuid()); inbound.write(success); - logger.info("{} has connected", player); inbound.setAssociation(player); inbound.setState(StateRegistry.PLAY); + + if (!VelocityServer.getServer().registerConnection(player)) { + inbound.closeWith(Disconnect.create(TextComponent.of("You are already on this proxy!", TextColor.RED))); + } + + logger.info("{} has connected", player); inbound.setSessionHandler(new InitialConnectSessionHandler(player)); - VelocityServer.getServer().registerConnection(player); player.createConnectionRequest(toTry.get()).fireAndForget(); } }