From 9f6182f8df9dbf9d162cf05316221770699cfede Mon Sep 17 00:00:00 2001 From: qlow <20094418+qlow@users.noreply.github.com> Date: Thu, 17 Dec 2020 17:58:49 +0100 Subject: [PATCH] Added a simple way to get a player by their xuid (#1642) * Added IGeyserPingPassthrough#getPingInformation(InetSocketAddress) to make logging of the pinging IPs possible * Added GeyserConnector#getPlayerByXboxUuid * Added GeyserConnector#getPlayerByUuid and added some javadocs * Update connector/src/main/java/org/geysermc/connector/GeyserConnector.java Co-authored-by: rtm516 * Update connector/src/main/java/org/geysermc/connector/GeyserConnector.java Co-authored-by: rtm516 * Update GeyserConnector.java * Update SkinManager.java * Update SkinProvider.java * Renamed getPlayerByXboxUuid to getPlayerByXuid Co-authored-by: qlow Co-authored-by: rtm516 --- .../geysermc/connector/GeyserConnector.java | 39 ++++++++++++++++--- .../geysermc/connector/skin/SkinManager.java | 18 ++++----- .../geysermc/connector/skin/SkinProvider.java | 10 ++--- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 1bfa700fb..05040a86f 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -44,6 +44,7 @@ import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.BiomeTranslator; import org.geysermc.connector.network.translators.EntityIdentifierRegistry; import org.geysermc.connector.network.translators.PacketTranslatorRegistry; +import org.geysermc.connector.network.translators.collision.CollisionTranslator; import org.geysermc.connector.network.translators.effect.EffectRegistry; import org.geysermc.connector.network.translators.item.ItemRegistry; import org.geysermc.connector.network.translators.item.ItemTranslator; @@ -54,7 +55,6 @@ import org.geysermc.connector.network.translators.sound.SoundRegistry; import org.geysermc.connector.network.translators.world.WorldManager; import org.geysermc.connector.network.translators.world.block.BlockTranslator; import org.geysermc.connector.network.translators.world.block.entity.BlockEntityTranslator; -import org.geysermc.connector.network.translators.collision.CollisionTranslator; import org.geysermc.connector.network.translators.world.block.entity.SkullBlockEntityTranslator; import org.geysermc.connector.utils.DimensionUtils; import org.geysermc.connector.utils.LanguageUtils; @@ -67,10 +67,7 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.text.DecimalFormat; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -326,6 +323,38 @@ public class GeyserConnector { players.remove(player); } + /** + * Gets a player by their current UUID + * + * @param uuid the uuid + * @return the player or null if there is no player online with this UUID + */ + public GeyserSession getPlayerByUuid(UUID uuid) { + for (GeyserSession session : players) { + if (session.getPlayerEntity().getUuid().equals(uuid)) { + return session; + } + } + + return null; + } + + /** + * Gets a player by their Xbox user identifier + * + * @param xboxUuid the Xbox user identifier + * @return the player or null if there is no player online with this xuid + */ + public GeyserSession getPlayerByXuid(String xuid) { + for (GeyserSession session : players) { + if (session.getAuthData() != null && session.getAuthData().getXboxUUID().equals(xuid)) { + return session; + } + } + + return null; + } + public static GeyserConnector start(PlatformType platformType, GeyserBootstrap bootstrap) { return new GeyserConnector(platformType, bootstrap); } diff --git a/connector/src/main/java/org/geysermc/connector/skin/SkinManager.java b/connector/src/main/java/org/geysermc/connector/skin/SkinManager.java index db8f25929..a08f567a3 100644 --- a/connector/src/main/java/org/geysermc/connector/skin/SkinManager.java +++ b/connector/src/main/java/org/geysermc/connector/skin/SkinManager.java @@ -81,11 +81,10 @@ public class SkinManager { // This attempts to find the xuid of the player so profile images show up for xbox accounts String xuid = ""; - for (GeyserSession player : GeyserConnector.getInstance().getPlayers()) { - if (player.getPlayerEntity().getUuid().equals(uuid)) { - xuid = player.getAuthData().getXboxUUID(); - break; - } + GeyserSession player = GeyserConnector.getInstance().getPlayerByUuid(uuid); + + if (player != null) { + xuid = player.getAuthData().getXboxUUID(); } PlayerListPacket.Entry entry; @@ -268,11 +267,10 @@ public class SkinManager { // return default skin with default cape when texture data is invalid String skinUrl = isAlex ? SkinProvider.EMPTY_SKIN_ALEX.getTextureUrl() : SkinProvider.EMPTY_SKIN.getTextureUrl(); if ("steve".equals(skinUrl) || "alex".equals(skinUrl)) { - for (GeyserSession session : GeyserConnector.getInstance().getPlayers()) { - if (session.getPlayerEntity().getUuid().equals(profile.getId())) { - skinUrl = session.getClientData().getSkinId(); - break; - } + GeyserSession session = GeyserConnector.getInstance().getPlayerByUuid(profile.getId()); + + if (session != null) { + skinUrl = session.getClientData().getSkinId(); } } return new GameProfileData(skinUrl, SkinProvider.EMPTY_CAPE.getTextureUrl(), isAlex); diff --git a/connector/src/main/java/org/geysermc/connector/skin/SkinProvider.java b/connector/src/main/java/org/geysermc/connector/skin/SkinProvider.java index 117198685..948e4b374 100644 --- a/connector/src/main/java/org/geysermc/connector/skin/SkinProvider.java +++ b/connector/src/main/java/org/geysermc/connector/skin/SkinProvider.java @@ -144,12 +144,10 @@ public class SkinProvider { String newSkinUrl = skinUrl; if ("steve".equals(skinUrl) || "alex".equals(skinUrl)) { - // TODO: Don't have a for loop for this? Have a proper map? - for (GeyserSession session : GeyserConnector.getInstance().getPlayers()) { - if (session.getPlayerEntity().getUuid().equals(playerId)) { - newSkinUrl = session.getClientData().getSkinId(); - break; - } + GeyserSession session = GeyserConnector.getInstance().getPlayerByUuid(playerId); + + if (session != null) { + newSkinUrl = session.getClientData().getSkinId(); } }