From 4df20dbd4c03ad079ff8a2852270163cf12dc643 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 19 Apr 2019 16:52:56 +1200 Subject: [PATCH] Fixed #354 --- .../disguise/disguisetypes/FlagWatcher.java | 19 ++++++------ .../utilities/json/SerializerFlagWatcher.java | 30 ++++++++++++++++--- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index d259ed8c..a8e5dcb7 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -171,16 +171,15 @@ public class FlagWatcher { if (newHealth > 0 && hasDied) { hasDied = false; - Bukkit.getScheduler() - .scheduleSyncDelayedTask(LibsDisguises.getInstance(), () -> { - try { - DisguiseUtilities.sendSelfDisguise((Player) getDisguise().getEntity(), - getDisguise()); - } - catch (Exception ex) { - ex.printStackTrace(); - } - }, 2); + Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), () -> { + try { + DisguiseUtilities + .sendSelfDisguise((Player) getDisguise().getEntity(), getDisguise()); + } + catch (Exception ex) { + ex.printStackTrace(); + } + }, 2); } else if (newHealth <= 0 && !hasDied) { hasDied = true; } 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 caac0683..924c567e 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerFlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerFlagWatcher.java @@ -1,10 +1,18 @@ package me.libraryaddict.disguise.utilities.json; +import com.comphenix.protocol.wrappers.WrappedBlockData; +import com.comphenix.protocol.wrappers.WrappedChatComponent; +import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.google.gson.*; +import com.google.gson.reflect.TypeToken; +import com.mojang.authlib.properties.PropertyMap; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.MetaIndex; +import me.libraryaddict.disguise.disguisetypes.watchers.ArrowWatcher; +import org.bukkit.inventory.ItemStack; +import sun.reflect.generics.tree.BaseType; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -15,14 +23,28 @@ import java.util.Map; /** * Created by libraryaddict on 1/06/2017. */ -public class SerializerFlagWatcher implements JsonDeserializer, JsonSerializer, InstanceCreator { +public class SerializerFlagWatcher implements JsonDeserializer, JsonSerializer, + InstanceCreator { + private Gson gson; + + public SerializerFlagWatcher() { + GsonBuilder gsonBuilder = new GsonBuilder(); + 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(PropertyMap.class, new PropertyMap.Serializer()); + gsonBuilder.registerTypeAdapter(ItemStack.class, new SerializerItemStack()); + + gson = gsonBuilder.create(); + } @Override public FlagWatcher deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { - FlagWatcher watcher = context.deserialize(json, - Class.forName(((JsonObject) json).get("flagType").getAsString())); + FlagWatcher watcher = (FlagWatcher) gson + .fromJson(json, Class.forName(((JsonObject) json).get("flagType").getAsString())); DisguiseType entity = DisguiseType.valueOf(((JsonObject) json).get("entityType").getAsString()); @@ -79,7 +101,7 @@ public class SerializerFlagWatcher implements JsonDeserializer, Jso @Override public JsonElement serialize(FlagWatcher src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject obj = (JsonObject) context.serialize(src); + JsonObject obj = (JsonObject) gson.toJsonTree(src); obj.addProperty("flagType", src.getClass().getName());