From f130d55170314099cd90444c5eb78247159e762b Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 21 Dec 2019 14:45:10 +1300 Subject: [PATCH] Correctly serialize and deserialize ItemMeta (Previously not working) #414 --- .../utilities/json/SerializerItemStack.java | 30 +++++++++++++++++-- .../reflection/ReflectionManager.java | 15 ++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerItemStack.java b/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerItemStack.java index c6297c9a..f229df6a 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerItemStack.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerItemStack.java @@ -3,7 +3,9 @@ package me.libraryaddict.disguise.utilities.json; import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.google.gson.*; import com.mojang.authlib.GameProfile; +import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import java.lang.reflect.Type; import java.util.HashMap; @@ -17,12 +19,36 @@ public class SerializerItemStack implements JsonSerializer, JsonDeser @Override public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) { - return context.serialize(src.serialize()); + Map partialSerialize = src.serialize(); + + if (partialSerialize.containsKey("meta")) { + partialSerialize.put("meta", src.getItemMeta().serialize()); + } + + return context.serialize(partialSerialize); } @Override public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - return ItemStack.deserialize(context.deserialize(json, HashMap.class)); + HashMap map = context.deserialize(json, HashMap.class); + + if (map.containsKey("meta")) { + Map meta = (Map) map.get("meta"); + + if (meta.containsKey("meta-type")) { + for (Object key : meta.keySet()) { + if (meta.get(key) instanceof Number) { + meta.put(key, ((Number) meta.get(key)).intValue()); + } + } + + ItemMeta itemMeta = ReflectionManager.getDeserializedItemMeta(meta); + + map.put("meta", itemMeta); + } + } + + return ItemStack.deserialize(map); } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java index c7ed7250..9c6b3ff4 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java @@ -18,6 +18,7 @@ import org.bukkit.craftbukkit.libs.org.apache.commons.io.IOUtils; import org.bukkit.entity.*; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.potion.PotionEffect; import org.bukkit.util.Vector; @@ -1158,4 +1159,18 @@ public class ReflectionManager { return null; } + + public static ItemMeta getDeserializedItemMeta(Map meta) { + try { + Method method = getCraftMethod(getCraftClass("inventory.CraftMetaItem$SerializableMeta"), "deserialize", + Map.class); + + return (ItemMeta) method.invoke(null, meta); + } + catch (Exception e) { + e.printStackTrace(); + } + + return null; + } }