From 48822fe55cc73c06f82cc39b83498ff9462737b5 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 5 Aug 2018 17:14:39 -0400 Subject: [PATCH] Better case-insensitivity. --- .../com/velocitypowered/proxy/VelocityServer.java | 12 +++++------- .../com/velocitypowered/proxy/util/ServerMap.java | 14 +++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 71659208b..630ad4ef8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -29,10 +29,7 @@ import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; import java.security.KeyPair; -import java.util.Collection; -import java.util.Map; -import java.util.Optional; -import java.util.UUID; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; public class VelocityServer implements ProxyServer { @@ -119,11 +116,12 @@ public class VelocityServer implements ProxyServer { } public boolean registerConnection(ConnectedPlayer connection) { - if (connectionsByName.putIfAbsent(connection.getUsername(), connection) != null) { + String lowerName = connection.getUsername().toLowerCase(Locale.US); + if (connectionsByName.putIfAbsent(lowerName, connection) != null) { return false; } if (connectionsByUuid.putIfAbsent(connection.getUniqueId(), connection) != null) { - connectionsByName.remove(connection.getUsername(), connection); + connectionsByName.remove(lowerName, connection); return false; } return true; @@ -137,7 +135,7 @@ public class VelocityServer implements ProxyServer { @Override public Optional getPlayer(@Nonnull String username) { Preconditions.checkNotNull(username, "username"); - return Optional.ofNullable(connectionsByName.get(username)); + return Optional.ofNullable(connectionsByName.get(username.toLowerCase(Locale.US))); } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/ServerMap.java b/proxy/src/main/java/com/velocitypowered/proxy/util/ServerMap.java index c31b77712..d961d719d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/ServerMap.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/ServerMap.java @@ -4,10 +4,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.server.ServerInfo; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -17,9 +14,10 @@ public class ServerMap { public Optional getServer(String server) { Preconditions.checkNotNull(server, "server"); + String lowerName = server.toLowerCase(Locale.US); lock.readLock().lock(); try { - return Optional.ofNullable(servers.get(server.toLowerCase())); + return Optional.ofNullable(servers.get(lowerName)); } finally { lock.readLock().unlock(); } @@ -36,9 +34,10 @@ public class ServerMap { public void register(ServerInfo server) { Preconditions.checkNotNull(server, "server"); + String lowerName = server.getName().toLowerCase(Locale.US); lock.writeLock().lock(); try { - Preconditions.checkArgument(servers.putIfAbsent(server.getName(), server) == null, "Server with name %s already registered", server.getName()); + Preconditions.checkArgument(servers.putIfAbsent(lowerName, server) == null, "Server with name %s already registered", server.getName()); } finally { lock.writeLock().unlock(); } @@ -46,9 +45,10 @@ public class ServerMap { public void unregister(ServerInfo server) { Preconditions.checkNotNull(server, "server"); + String lowerName = server.getName().toLowerCase(Locale.US); lock.writeLock().lock(); try { - Preconditions.checkArgument(servers.remove(server.getName(), server), "Server with this name is not registered!"); + Preconditions.checkArgument(servers.remove(lowerName, server), "Server with this name is not registered!"); } finally { lock.writeLock().unlock(); }