From af886f3af9d289c636d3f474ce195b1b6c44fc23 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 1 Jul 2017 10:44:33 +1200 Subject: [PATCH] Fix player disguises setSkin --- .../commands/DisguisePlayerCommand.java | 1 - .../disguisetypes/PlayerDisguise.java | 22 +++++++++------ .../disguise/utilities/DisguiseParser.java | 4 ++- .../disguise/utilities/DisguiseUtilities.java | 4 +++ .../disguise/utilities/ReflectionManager.java | 27 ------------------- .../utilities/json/SerializerGameProfile.java | 10 +++++++ 6 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java index cf6cc0df..83207955 100644 --- a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java @@ -86,7 +86,6 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (disguise.getWatcher() instanceof LivingWatcher) { - disguise.getWatcher().setCustomName(player.getDisplayName()); if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { diff --git a/src/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java b/src/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java index 394d87eb..97075660 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java +++ b/src/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java @@ -8,11 +8,14 @@ import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction; import com.comphenix.protocol.wrappers.PlayerInfoData; import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedGameProfile; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.LibsProfileLookup; import me.libraryaddict.disguise.utilities.ReflectionManager; +import me.libraryaddict.disguise.utilities.json.SerializerGameProfile; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; @@ -99,8 +102,8 @@ public class PlayerDisguise extends TargetedDisguise { if (currentLookup == null && gameProfile != null) { disguise.skinToUse = getSkin(); - disguise.gameProfile = ReflectionManager.getGameProfileWithThisSkin(disguise.uuid, - getGameProfile().getName(), getGameProfile()); + disguise.gameProfile = ReflectionManager + .getGameProfileWithThisSkin(disguise.uuid, getGameProfile().getName(), getGameProfile()); } else { disguise.setSkin(getSkin()); } @@ -127,8 +130,8 @@ public class PlayerDisguise extends TargetedDisguise { if (getSkin() != null) { gameProfile = ReflectionManager.getGameProfile(uuid, getName()); } else { - gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, getName(), - DisguiseUtilities.getProfileFromMojang(this)); + gameProfile = ReflectionManager + .getGameProfileWithThisSkin(uuid, getName(), DisguiseUtilities.getProfileFromMojang(this)); } } @@ -252,11 +255,11 @@ public class PlayerDisguise extends TargetedDisguise { public PlayerDisguise setSkin(String newSkin) { if (newSkin != null && newSkin.length() > 50) { try { - return setSkin(ReflectionManager.parseGameProfile(newSkin)); + return setSkin(DisguiseUtilities.getGson().fromJson(newSkin, WrappedGameProfile.class)); } - catch (Exception ex) { - throw new IllegalArgumentException( - "The skin is too long to be a playername, but cannot be parsed to a GameProfile!"); + catch (Exception ex) {ex.printStackTrace(); + throw new IllegalArgumentException(String.format( + "The skin %s is too long to be a playername, but cannot be parsed to a GameProfile!", newSkin)); } } @@ -293,6 +296,9 @@ public class PlayerDisguise extends TargetedDisguise { this.skinToUse = gameProfile.getName(); this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, getName(), gameProfile); + System.out.println( + new GsonBuilder().registerTypeAdapter(WrappedGameProfile.class, new SerializerGameProfile()).create() + .toJson(gameProfile)); if (DisguiseUtilities.isDisguiseInUse(this)) { if (isDisplayedInTab()) { diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseParser.java b/src/me/libraryaddict/disguise/utilities/DisguiseParser.java index aa168da8..007714f8 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseParser.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseParser.java @@ -6,7 +6,9 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Map.Entry; +import com.google.gson.GsonBuilder; import com.mojang.authlib.GameProfile; +import me.libraryaddict.disguise.utilities.json.SerializerGameProfile; import org.bukkit.Art; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -701,7 +703,7 @@ public class DisguiseParser { } } else if (WrappedGameProfile.class == param && valueString.length() > 20) { try { - value = ReflectionManager.parseGameProfile(valueString); + value = DisguiseUtilities.getGson().fromJson(valueString, WrappedGameProfile.class); } catch (Exception ex) { throw parseToException(WrappedGameProfile.class, valueString, methodName); diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 765b3f9d..256eaa57 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -815,6 +815,10 @@ public class DisguiseUtilities { return selfDisguised; } + public static Gson getGson() { + return gson; + } + public static void init(LibsDisguises disguises) { libsDisguises = disguises; methods = BackwardsSupport.getMethods(); diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index bbe2014d..af1679a2 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -77,33 +77,6 @@ public class ReflectionManager { entityCountField.setAccessible(true); } - public static WrappedGameProfile parseGameProfile(String toParse) { - Map response = new Gson().fromJson(toParse, Map.class); - - String id = (String) response.get("id"); - - if (!id.contains("-")) { - id = Pattern.compile("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)") - .matcher(id).replaceFirst("$1-$2-$3-$4-$5"); - } - - WrappedGameProfile gameProfile = new WrappedGameProfile(UUID.fromString(id), (String) response.get("name")); - - if (response.containsKey("properties")) { - ArrayList> properties = (ArrayList) response.get("properties"); - - for (Map s : properties) { - String gName = s.containsKey("name") ? s.get("name") : null; - String gValue = s.containsKey("value") ? s.get("value") : null; - String gSigned = s.containsKey("signature") ? s.get("signature") : null; - - gameProfile.getProperties().put(gName, new WrappedSignedProperty(gName, gValue, gSigned)); - } - } - - return gameProfile; - } - public static Object createEntityInstance(String entityName) { try { Class entityClass = getNmsClass("Entity" + entityName); diff --git a/src/me/libraryaddict/disguise/utilities/json/SerializerGameProfile.java b/src/me/libraryaddict/disguise/utilities/json/SerializerGameProfile.java index 52204ad9..94f63dc4 100644 --- a/src/me/libraryaddict/disguise/utilities/json/SerializerGameProfile.java +++ b/src/me/libraryaddict/disguise/utilities/json/SerializerGameProfile.java @@ -5,6 +5,8 @@ import com.google.gson.*; import com.mojang.authlib.GameProfile; import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.regex.Pattern; /** * Created by libraryaddict on 1/06/2017. @@ -19,6 +21,14 @@ public class SerializerGameProfile implements JsonSerializer @Override public WrappedGameProfile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject obj = json.getAsJsonObject(); + + if (obj.has("id") && !obj.get("id").getAsString().contains("-")) { + obj.addProperty("id", + Pattern.compile("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)") + .matcher(obj.get("id").getAsString()).replaceFirst("$1-$2-$3-$4-$5")); + } + return WrappedGameProfile.fromHandle(context.deserialize(json, GameProfile.class)); } }