From b5e41f3144205f03aa3f76e21987be3b3450ce16 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 5 Dec 2020 08:10:53 +1300 Subject: [PATCH] Fix particle serialization --- .../disguise/utilities/DisguiseUtilities.java | 2 ++ .../utilities/json/SerializerFlagWatcher.java | 11 ++++++++++- .../utilities/json/SerializerParticle.java | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/libraryaddict/disguise/utilities/json/SerializerParticle.java diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index cf53ff8b..9b2893da 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -1238,11 +1238,13 @@ public class DisguiseUtilities { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.disableHtmlEscaping(); + gsonBuilder.setPrettyPrinting(); gsonBuilder.registerTypeAdapter(MetaIndex.class, new SerializerMetaIndex()); gsonBuilder.registerTypeAdapter(WrappedGameProfile.class, new SerializerGameProfile()); gsonBuilder.registerTypeAdapter(WrappedBlockData.class, new SerializerWrappedBlockData()); gsonBuilder.registerTypeAdapter(WrappedChatComponent.class, new SerializerChatComponent()); + gsonBuilder.registerTypeAdapter(WrappedParticle.class, new SerializerParticle()); gsonBuilder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()); gsonBuilder.registerTypeHierarchyAdapter(ItemStack.class, new SerializerItemStack()); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerFlagWatcher.java b/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerFlagWatcher.java index 1acc8b3c..12c30c8c 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerFlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerFlagWatcher.java @@ -1,9 +1,13 @@ package me.libraryaddict.disguise.utilities.json; +import com.comphenix.protocol.wrappers.WrappedParticle; import com.google.gson.*; import com.google.gson.internal.LinkedTreeMap; import me.libraryaddict.disguise.disguisetypes.*; import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import me.libraryaddict.disguise.utilities.params.ParamInfoManager; +import me.libraryaddict.disguise.utilities.params.types.custom.ParamInfoParticle; +import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import org.bukkit.inventory.ItemStack; import java.lang.reflect.Field; @@ -46,7 +50,7 @@ public class SerializerFlagWatcher } private void correct(FlagWatcher watcher, Class flagWatcher, String name) - throws NoSuchFieldException, IllegalAccessException { + throws NoSuchFieldException, IllegalAccessException, DisguiseParseException { Field field = FlagWatcher.class.getDeclaredField(name); field.setAccessible(true); HashMap map = (HashMap) field.get(watcher); @@ -69,6 +73,11 @@ public class SerializerFlagWatcher } else if (def instanceof Byte) { entry.setValue(((Double) entry.getValue()).byteValue()); } + } else if (entry.getValue() instanceof String) { + if (index.getDefault() instanceof WrappedParticle) { + entry.setValue(((ParamInfoParticle) ParamInfoManager.getParamInfo(WrappedParticle.class)) + .fromString((String) entry.getValue())); + } } else if (entry.getValue() instanceof LinkedTreeMap) { // If it's deserialized incorrectly as a map // If the default value is not VillagerData if (index.getDefault() instanceof VillagerData) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerParticle.java b/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerParticle.java new file mode 100644 index 00000000..96bffa9f --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerParticle.java @@ -0,0 +1,19 @@ +package me.libraryaddict.disguise.utilities.json; + +import com.comphenix.protocol.wrappers.WrappedParticle; +import com.google.gson.*; +import me.libraryaddict.disguise.utilities.params.ParamInfoManager; +import org.bukkit.Bukkit; +import org.bukkit.block.data.BlockData; + +import java.lang.reflect.Type; + +/** + * Created by libraryaddict on 5/12/2020. + */ +public class SerializerParticle implements JsonSerializer { + @Override + public JsonElement serialize(WrappedParticle src, Type typeOfSrc, JsonSerializationContext context) { + return context.serialize(ParamInfoManager.getParamInfo(WrappedParticle.class).toString(src)); + } +}