From d23e518c1b342016f42dad46afe7863b329b9df0 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 5 Jun 2014 08:40:15 +1200 Subject: [PATCH] Instead of using Object's for gameprofiles. Use WrappedGameProfile --- .../disguisetypes/PlayerDisguise.java | 12 ++-- .../disguise/utilities/DisguiseUtilities.java | 29 ++++---- .../disguise/utilities/LibsProfileLookup.java | 4 +- .../utilities/LibsProfileLookupCaller.java | 8 ++- .../disguise/utilities/PacketsManager.java | 5 +- .../disguise/utilities/ReflectionManager.java | 68 ++++++------------- 6 files changed, 52 insertions(+), 74 deletions(-) diff --git a/src/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java b/src/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java index f2f1d1c8..a756d3f1 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java +++ b/src/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java @@ -2,6 +2,8 @@ package me.libraryaddict.disguise.disguisetypes; import org.bukkit.entity.Player; +import com.comphenix.protocol.wrappers.WrappedGameProfile; + import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.LibsProfileLookup; import me.libraryaddict.disguise.utilities.ReflectionManager; @@ -9,7 +11,7 @@ import me.libraryaddict.disguise.utilities.ReflectionManager.LibVersion; public class PlayerDisguise extends TargetedDisguise { private LibsProfileLookup currentLookup; - private Object gameProfile; + private WrappedGameProfile gameProfile; private String playerName; private String skinToUse; @@ -53,7 +55,7 @@ public class PlayerDisguise extends TargetedDisguise { } @Deprecated - public Object getGameProfile() { + public WrappedGameProfile getGameProfile() { if (getSkin() != null) { if (gameProfile != null) { return gameProfile; @@ -79,7 +81,7 @@ public class PlayerDisguise extends TargetedDisguise { } @Deprecated - public void setGameProfile(Object gameProfile) { + public void setGameProfile(WrappedGameProfile gameProfile) { this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(null, getName(), gameProfile); } @@ -97,7 +99,7 @@ public class PlayerDisguise extends TargetedDisguise { currentLookup = new LibsProfileLookup() { @Override - public void onLookup(Object gameProfile) { + public void onLookup(WrappedGameProfile gameProfile) { if (currentLookup == this && gameProfile != null) { setGameProfile(gameProfile); if (DisguiseUtilities.isDisguiseInUse(PlayerDisguise.this)) { @@ -109,7 +111,7 @@ public class PlayerDisguise extends TargetedDisguise { } } }; - Object obj = DisguiseUtilities.getProfileFromMojang(this.skinToUse, currentLookup); + WrappedGameProfile obj = DisguiseUtilities.getProfileFromMojang(this.skinToUse, currentLookup); if (obj != null) { setGameProfile(obj); } diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 8ee8cd4a..83c26058 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -38,6 +38,7 @@ import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.wrappers.WrappedDataWatcher; +import com.comphenix.protocol.wrappers.WrappedGameProfile; public class DisguiseUtilities { /** @@ -58,7 +59,7 @@ public class DisguiseUtilities { /** * A hashmap storing the uuid and skin of a playername */ - private static HashMap gameProfiles = new HashMap(); + private static HashMap gameProfiles = new HashMap(); private static LibsDisguises libsDisguises; private static HashMap> runnables = new HashMap>(); /** @@ -122,7 +123,7 @@ public class DisguiseUtilities { runnable.runTaskLater(libsDisguises, 20); } - public static void addGameProfile(String string, Object gameProfile) { + public static void addGameProfile(String string, WrappedGameProfile gameProfile) { getGameProfiles().put(string, gameProfile); addedByPlugins.add(string); } @@ -263,7 +264,7 @@ public class DisguiseUtilities { } return null; } - + public static TargetedDisguise getDisguise(Player observer, Entity entity) { UUID entityId = entity.getUniqueId(); if (futureDisguises.containsKey(entity.getEntityId())) { @@ -297,11 +298,11 @@ public class DisguiseUtilities { return futureDisguises; } - public static Object getGameProfile(String playerName) { + public static WrappedGameProfile getGameProfile(String playerName) { return gameProfiles.get(playerName); } - public static HashMap getGameProfiles() { + public static HashMap getGameProfiles() { return gameProfiles; } @@ -345,11 +346,11 @@ public class DisguiseUtilities { return players; } - public static Object getProfileFromMojang(final PlayerDisguise disguise) { + public static WrappedGameProfile getProfileFromMojang(final PlayerDisguise disguise) { return getProfileFromMojang(disguise.getName(), new LibsProfileLookup() { @Override - public void onLookup(Object gameProfile) { + public void onLookup(WrappedGameProfile gameProfile) { getAddedByPlugins().remove(disguise.getName()); if (DisguiseAPI.isDisguiseInUse(disguise)) { DisguiseUtilities.refreshTrackers(disguise); @@ -365,11 +366,11 @@ public class DisguiseUtilities { * Thread safe to use. This returns a GameProfile. And if its GameProfile doesn't have a skin blob. Then it does a lookup * using schedulers. The runnable is run once the GameProfile has been successfully dealt with */ - public static Object getProfileFromMojang(String playerName, LibsProfileLookup runnableIfCantReturn) { + public static WrappedGameProfile getProfileFromMojang(String playerName, LibsProfileLookup runnableIfCantReturn) { return getProfileFromMojang(playerName, (Object) runnableIfCantReturn); } - private static Object getProfileFromMojang(final String playerName, final Object runnable) { + private static WrappedGameProfile getProfileFromMojang(final String playerName, final Object runnable) { if (gameProfiles.containsKey(playerName)) { if (gameProfiles.get(playerName) != null) { return gameProfiles.get(playerName); @@ -378,7 +379,7 @@ public class DisguiseUtilities { getAddedByPlugins().add(playerName); Player player = Bukkit.getPlayerExact(playerName); if (player != null) { - Object gameProfile = ReflectionManager.getGameProfile(player); + WrappedGameProfile gameProfile = ReflectionManager.getGameProfile(player); if (ReflectionManager.hasSkinBlob(gameProfile)) { gameProfiles.put(playerName, gameProfile); return gameProfile; @@ -389,7 +390,7 @@ public class DisguiseUtilities { Bukkit.getScheduler().scheduleAsyncDelayedTask(libsDisguises, new Runnable() { public void run() { try { - final Object gameProfile = lookupGameProfile(playerName); + final WrappedGameProfile gameProfile = lookupGameProfile(playerName); Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() { public void run() { if (gameProfiles.containsKey(playerName) && gameProfiles.get(playerName) == null) { @@ -430,7 +431,7 @@ public class DisguiseUtilities { * Thread safe to use. This returns a GameProfile. And if its GameProfile doesn't have a skin blob. Then it does a lookup * using schedulers. The runnable is run once the GameProfile has been successfully dealt with */ - public static Object getProfileFromMojang(String playerName, Runnable runnableIfCantReturn) { + public static WrappedGameProfile getProfileFromMojang(String playerName, Runnable runnableIfCantReturn) { return getProfileFromMojang(playerName, (Object) runnableIfCantReturn); } @@ -454,8 +455,8 @@ public class DisguiseUtilities { /** * This is called on a thread as it is thread blocking */ - public static Object lookupGameProfile(String playerName) { - Object gameprofile = ReflectionManager.grabProfileAddUUID(playerName); + public static WrappedGameProfile lookupGameProfile(String playerName) { + WrappedGameProfile gameprofile = ReflectionManager.grabProfileAddUUID(playerName); return ReflectionManager.getSkullBlob(gameprofile); } diff --git a/src/me/libraryaddict/disguise/utilities/LibsProfileLookup.java b/src/me/libraryaddict/disguise/utilities/LibsProfileLookup.java index 003eafcf..24bd17cb 100644 --- a/src/me/libraryaddict/disguise/utilities/LibsProfileLookup.java +++ b/src/me/libraryaddict/disguise/utilities/LibsProfileLookup.java @@ -1,6 +1,8 @@ package me.libraryaddict.disguise.utilities; +import com.comphenix.protocol.wrappers.WrappedGameProfile; + public interface LibsProfileLookup { - public void onLookup(Object gameProfile); + public void onLookup(WrappedGameProfile gameProfile); } diff --git a/src/me/libraryaddict/disguise/utilities/LibsProfileLookupCaller.java b/src/me/libraryaddict/disguise/utilities/LibsProfileLookupCaller.java index 01340060..5efe78c2 100644 --- a/src/me/libraryaddict/disguise/utilities/LibsProfileLookupCaller.java +++ b/src/me/libraryaddict/disguise/utilities/LibsProfileLookupCaller.java @@ -3,13 +3,15 @@ package me.libraryaddict.disguise.utilities; import java.lang.reflect.Field; import java.util.UUID; +import com.comphenix.protocol.wrappers.WrappedGameProfile; + import net.minecraft.util.com.mojang.authlib.GameProfile; import net.minecraft.util.com.mojang.authlib.ProfileLookupCallback; public class LibsProfileLookupCaller implements ProfileLookupCallback { - private GameProfile gameProfile; + private WrappedGameProfile gameProfile; - public GameProfile getGameProfile() { + public WrappedGameProfile getGameProfile() { return gameProfile; } @@ -27,7 +29,7 @@ public class LibsProfileLookupCaller implements ProfileLookupCallback { @Override public void onProfileLookupSucceeded(GameProfile profile) { - gameProfile = profile; + gameProfile = WrappedGameProfile.fromHandle(profile); } } diff --git a/src/me/libraryaddict/disguise/utilities/PacketsManager.java b/src/me/libraryaddict/disguise/utilities/PacketsManager.java index 6e2d7ee2..d6f4bb0c 100644 --- a/src/me/libraryaddict/disguise/utilities/PacketsManager.java +++ b/src/me/libraryaddict/disguise/utilities/PacketsManager.java @@ -49,6 +49,7 @@ import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.wrappers.WrappedAttribute; import com.comphenix.protocol.wrappers.WrappedAttribute.Builder; import com.comphenix.protocol.wrappers.WrappedDataWatcher; +import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedWatchableObject; public class PacketsManager { @@ -196,7 +197,7 @@ public class PacketsManager { stringMods.write(i, ((PlayerDisguise) disguise).getName()); } } else { - Object gameProfile; + WrappedGameProfile gameProfile; String name = ((PlayerDisguise) disguise).getName(); boolean removeName = false; if (!DisguiseUtilities.hasGameProfile(name)) { @@ -206,7 +207,7 @@ public class PacketsManager { if (removeName) { DisguiseUtilities.getAddedByPlugins().remove(name); } - spawnPackets[0].getModifier().write(1, gameProfile); + spawnPackets[0].getGameProfiles().write(0, gameProfile); } StructureModifier intMods = spawnPackets[0].getIntegers(); intMods.write(0, disguisedEntity.getEntityId()); diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index b736ea05..6506f1f6 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -14,6 +14,8 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import com.comphenix.protocol.wrappers.WrappedGameProfile; + public class ReflectionManager { public enum LibVersion { V1_6, V1_7; @@ -90,10 +92,10 @@ public class ReflectionManager { Object playerinteractmanager = getNmsClass("PlayerInteractManager").getConstructor(getNmsClass("World")) .newInstance(world); if (LibVersion.is1_7()) { - Object gameProfile = getGameProfile(null, "LibsDisguises"); + WrappedGameProfile gameProfile = getGameProfile(null, "LibsDisguises"); entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"), - gameProfile.getClass(), playerinteractmanager.getClass()).newInstance(minecraftServer, world, - gameProfile, playerinteractmanager); + gameProfile.getHandleType(), playerinteractmanager.getClass()).newInstance(minecraftServer, world, + gameProfile.getHandle(), playerinteractmanager); } else { entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("World"), String.class, playerinteractmanager.getClass()).newInstance(minecraftServer, world, "LibsDisguises", @@ -205,47 +207,26 @@ public class ReflectionManager { return null; } - public static Object getGameProfile(Player player) { + public static WrappedGameProfile getGameProfile(Player player) { if (LibVersion.is1_7()) { - try { - return getNmsClass("EntityHuman").getMethod("getProfile").invoke(getNmsEntity(player)); - } catch (Exception ex) { - ex.printStackTrace(); - } + return WrappedGameProfile.fromPlayer(player); } return null; } - public static Object getGameProfile(UUID uuid, String playerName) { + public static WrappedGameProfile getGameProfile(UUID uuid, String playerName) { try { - try { - return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile") - .getConstructor(UUID.class, String.class) - .newInstance(uuid != null ? uuid : UUID.randomUUID(), playerName); - } catch (NoSuchMethodException ex) { - return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile") - .getConstructor(String.class, String.class).newInstance(uuid != null ? uuid.toString() : "", playerName); - } + return new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName); } catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Object getGameProfileWithThisSkin(UUID uuid, String playerName, Object profileWithSkin) { + public static WrappedGameProfile getGameProfileWithThisSkin(UUID uuid, String playerName, WrappedGameProfile profileWithSkin) { try { - Object gameProfile; - try { - gameProfile = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile") - .getConstructor(UUID.class, String.class) - .newInstance(uuid != null ? uuid : UUID.randomUUID(), playerName); - } catch (NoSuchMethodException ex) { - gameProfile = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile") - .getConstructor(String.class, String.class).newInstance(uuid != null ? uuid.toString() : "", playerName); - } - Field properties = gameProfile.getClass().getDeclaredField("properties"); - properties.setAccessible(true); - properties.set(gameProfile, properties.get(profileWithSkin)); + WrappedGameProfile gameProfile = new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName); + gameProfile.getProperties().putAll(profileWithSkin.getProperties()); return gameProfile; } catch (Exception ex) { ex.printStackTrace(); @@ -301,14 +282,15 @@ public class ReflectionManager { return null; } - public static Object getSkullBlob(Object gameProfile) { + public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) { try { Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); for (Method method : getNmsClass("MinecraftServer").getMethods()) { if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) { Object session = method.invoke(minecraftServer); - return session.getClass().getMethod("fillProfileProperties", gameProfile.getClass(), boolean.class) - .invoke(session, gameProfile, true); + return WrappedGameProfile.fromHandle(session.getClass() + .getMethod("fillProfileProperties", gameProfile.getHandleType(), boolean.class) + .invoke(session, gameProfile.getHandle(), true)); } } } catch (Exception ex) { @@ -335,7 +317,7 @@ public class ReflectionManager { return null; } - public static Object grabProfileAddUUID(String playername) { + public static WrappedGameProfile grabProfileAddUUID(String playername) { try { Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); for (Method method : getNmsClass("MinecraftServer").getMethods()) { @@ -357,20 +339,8 @@ public class ReflectionManager { return null; } - public static boolean hasSkinBlob(Object gameprofile) { - try { - Field propField = gameprofile.getClass().getDeclaredField("properties"); - propField.setAccessible(true); - Object propMap = propField.get(gameprofile); - propField = propMap.getClass().getDeclaredField("properties"); - propField.setAccessible(true); - propMap = propField.get(propMap); - return !(Boolean) propMap.getClass().getMethod("isEmpty").invoke(propMap); - } catch (NoSuchFieldException ex) { - } catch (Exception ex) { - ex.printStackTrace(); - } - return true; + public static boolean hasSkinBlob(WrappedGameProfile gameProfile) { + return !gameProfile.getProperties().isEmpty(); } public static void setAllowSleep(Player player) {