From 13848def72ff6d358e12a972880d3a4c8751d1c8 Mon Sep 17 00:00:00 2001 From: Keir Nellyer Date: Wed, 2 Apr 2014 10:25:42 +0100 Subject: [PATCH] Return a Users UUID as a UUID object whilst keeping support for returning as a String --- api/src/main/java/net/md_5/bungee/Util.java | 12 ++++++++++++ .../api/connection/PendingConnection.java | 10 ++++++++++ .../bungee/api/connection/ProxiedPlayer.java | 10 ++++++++++ .../java/net/md_5/bungee/UserConnection.java | 9 ++++++++- .../md_5/bungee/connection/InitialHandler.java | 17 ++++++++++++----- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/Util.java b/api/src/main/java/net/md_5/bungee/Util.java index 143a29e9..5b6c3315 100644 --- a/api/src/main/java/net/md_5/bungee/Util.java +++ b/api/src/main/java/net/md_5/bungee/Util.java @@ -2,6 +2,7 @@ package net.md_5.bungee; import com.google.common.base.Joiner; import java.net.InetSocketAddress; +import java.util.UUID; /** * Series of utility classes to perform various operations. @@ -63,4 +64,15 @@ public class Util { return Joiner.on( separators ).join( objects ); } + + /** + * Converts a String to a UUID + * + * @param uuid The string to be converted + * @return The result + */ + public static UUID getUUID(String uuid) + { + return UUID.fromString( uuid.substring( 0, 8 ) + "-" + uuid.substring( 8, 12 ) + "-" + uuid.substring( 12, 16 ) + "-" + uuid.substring( 16, 20 ) + "-" + uuid.substring( 20, 32 ) ); + } } diff --git a/api/src/main/java/net/md_5/bungee/api/connection/PendingConnection.java b/api/src/main/java/net/md_5/bungee/api/connection/PendingConnection.java index 3c84687f..203de9cf 100644 --- a/api/src/main/java/net/md_5/bungee/api/connection/PendingConnection.java +++ b/api/src/main/java/net/md_5/bungee/api/connection/PendingConnection.java @@ -1,6 +1,7 @@ package net.md_5.bungee.api.connection; import java.net.InetSocketAddress; +import java.util.UUID; import net.md_5.bungee.api.config.ListenerInfo; /** @@ -41,9 +42,18 @@ public interface PendingConnection extends Connection * Get this connection's UUID, if set. * * @return the UUID + * @deprecated In favour of {@link #getUniqueId()} */ + @Deprecated String getUUID(); + /** + * Get this connection's UUID, if set. + * + * @return the UUID + */ + UUID getUniqueId(); + /** * Get this connection's online mode. * diff --git a/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java b/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java index 46faa9fc..1a7a4b26 100644 --- a/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java +++ b/api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java @@ -4,6 +4,7 @@ import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.tab.TabListHandler; +import java.util.UUID; /** * Represents a player who's connection is being connected to somewhere else, @@ -117,6 +118,15 @@ public interface ProxiedPlayer extends Connection, CommandSender * Get this connection's UUID, if set. * * @return the UUID + * @deprecated In favour of {@link #getUniqueId()} */ + @Deprecated String getUUID(); + + /** + * Get this connection's UUID, if set. + * + * @return the UUID + */ + UUID getUniqueId(); } diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 92db22ad..e0c2d0b1 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -14,6 +14,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Objects; +import java.util.UUID; import java.util.logging.Level; import lombok.Getter; import lombok.NonNull; @@ -401,7 +402,7 @@ public final class UserConnection implements ProxiedPlayer @Override public Collection getPermissions() { - return Collections.unmodifiableCollection(permissions); + return Collections.unmodifiableCollection( permissions ); } @Override @@ -421,4 +422,10 @@ public final class UserConnection implements ProxiedPlayer { return getPendingConnection().getUUID(); } + + @Override + public UUID getUniqueId() + { + return getPendingConnection().getUniqueId(); + } } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index b66cf806..306e8086 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -8,6 +8,7 @@ import java.net.URLEncoder; import java.security.MessageDigest; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import java.util.logging.Level; import javax.crypto.SecretKey; import lombok.Getter; @@ -81,7 +82,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection @Getter private InetSocketAddress virtualHost; @Getter - private String UUID; + private UUID uniqueId; private enum State { @@ -320,7 +321,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection LoginResult obj = BungeeCord.getInstance().gson.fromJson( result, LoginResult.class ); if ( obj != null ) { - UUID = obj.getId(); + uniqueId = Util.getUUID( obj.getId() ); finish(); return; } @@ -366,11 +367,11 @@ public class InitialHandler extends PacketHandler implements PendingConnection { if ( ch.getHandle().isActive() ) { - if ( UUID == null ) + if ( uniqueId == null ) { - UUID = java.util.UUID.nameUUIDFromBytes( ( "OfflinePlayer:" + getName() ).getBytes( Charsets.UTF_8 ) ).toString(); + uniqueId = java.util.UUID.nameUUIDFromBytes( ( "OfflinePlayer:" + getName() ).getBytes( Charsets.UTF_8 ) ); } - unsafe.sendPacket( new LoginSuccess( UUID, getName() ) ); + unsafe.sendPacket( new LoginSuccess( uniqueId.toString(), getName() ) ); ch.setProtocol( Protocol.GAME ); UserConnection userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this ); @@ -466,6 +467,12 @@ public class InitialHandler extends PacketHandler implements PendingConnection this.onlineMode = onlineMode; } + @Override + public String getUUID() + { + return uniqueId.toString().replaceAll( "-", "" ); + } + @Override public String toString() {