diff --git a/src/fr/maxlego08/koth/zcore/ZPlugin.java b/src/fr/maxlego08/koth/zcore/ZPlugin.java index 0d11942..6046e19 100644 --- a/src/fr/maxlego08/koth/zcore/ZPlugin.java +++ b/src/fr/maxlego08/koth/zcore/ZPlugin.java @@ -7,7 +7,6 @@ import java.util.List; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.event.Listener; -import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.PotionEffect; @@ -18,7 +17,6 @@ import com.google.gson.GsonBuilder; import fr.maxlego08.koth.listener.ListenerAdapter; import fr.maxlego08.koth.zcore.logger.Logger; import fr.maxlego08.koth.zcore.logger.Logger.LogType; -import fr.maxlego08.koth.zcore.utils.gson.ItemStackAdapter; import fr.maxlego08.koth.zcore.utils.gson.LocationAdapter; import fr.maxlego08.koth.zcore.utils.gson.PotionEffectAdapter; import fr.maxlego08.koth.zcore.utils.storage.Persist; @@ -80,7 +78,6 @@ public abstract class ZPlugin extends JavaPlugin { public GsonBuilder getGsonBuilder() { return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().serializeNulls() .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE) - .registerTypeHierarchyAdapter(ItemStack.class, new ItemStackAdapter()) .registerTypeAdapter(PotionEffect.class, new PotionEffectAdapter()) .registerTypeAdapter(Location.class, new LocationAdapter()); } diff --git a/src/fr/maxlego08/koth/zcore/utils/gson/ItemStackAdapter.java b/src/fr/maxlego08/koth/zcore/utils/gson/ItemStackAdapter.java deleted file mode 100644 index 8b341c5..0000000 --- a/src/fr/maxlego08/koth/zcore/utils/gson/ItemStackAdapter.java +++ /dev/null @@ -1,149 +0,0 @@ -package fr.maxlego08.koth.zcore.utils.gson; - -import java.io.IOException; -import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - -import org.bukkit.configuration.serialization.ConfigurationSerializable; -import org.bukkit.configuration.serialization.ConfigurationSerialization; -import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -import com.google.gson.TypeAdapter; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonToken; -import com.google.gson.stream.JsonWriter; - -import fr.maxlego08.koth.zcore.ZPlugin; -import net.minecraft.server.v1_15_R1.NBTTagCompound; - -public class ItemStackAdapter extends TypeAdapter{ - - private static Type seriType = new TypeToken>(){}.getType(); - - @Override - public void write(JsonWriter jsonWriter, ItemStack itemStack) throws IOException { - if(itemStack == null) { - jsonWriter.nullValue(); - return; - } - jsonWriter.value(getRaw(removeSlotNBT(itemStack))); - } - - @Override - public ItemStack read(JsonReader jsonReader) throws IOException { - if(jsonReader.peek() == JsonToken.NULL) { - jsonReader.nextNull(); - return null; - } - return fromRaw(jsonReader.nextString()); - } - - private String getRaw (ItemStack item) { - Map serial = item.serialize(); - - if(serial.get("meta") != null) { - ItemMeta itemMeta = item.getItemMeta(); - - Map originalMeta = itemMeta.serialize(); - Map meta = new HashMap(); - for(Entry entry : originalMeta.entrySet()) - meta.put(entry.getKey(), entry.getValue()); - Object o; - for(Entry entry : meta.entrySet()) { - o = entry.getValue(); - if(o instanceof ConfigurationSerializable) { - ConfigurationSerializable serializable = (ConfigurationSerializable) o; - Map serialized = recursiveSerialization(serializable); - meta.put(entry.getKey(), serialized); - } - } - serial.put("meta", meta); - } - - return ZPlugin.z().getGson().toJson(serial); - } - - @SuppressWarnings("unchecked") - private ItemStack fromRaw (String raw) { - Map keys = ZPlugin.z().getGson().fromJson(raw, seriType); - - if(keys.get("amount") != null) { - Double d = (Double) keys.get("amount"); - Integer i = d.intValue(); - keys.put("amount", i); - } - - ItemStack item; - try { - item = ItemStack.deserialize(keys); - }catch(Exception e) { - return null; - } - - if(item == null) - return null; - - if(keys.containsKey("meta")) { - Map itemmeta = (Map) keys.get("meta"); - itemmeta = recursiveDoubleToInteger(itemmeta); - ItemMeta meta = (ItemMeta) ConfigurationSerialization.deserializeObject(itemmeta, ConfigurationSerialization.getClassByAlias("ItemMeta")); - item.setItemMeta(meta); - } - - return item; - } - - private static ItemStack removeSlotNBT (ItemStack item) { - if (item == null) - return null; - net.minecraft.server.v1_15_R1.ItemStack nmsi = CraftItemStack.asNMSCopy(item); - if (nmsi == null) - return null; - NBTTagCompound nbtt = nmsi.getTag(); - if(nbtt != null) { - nbtt.remove("Slot"); - nmsi.setTag(nbtt); - } - return item; - } - @SuppressWarnings("unchecked") - private static Map recursiveDoubleToInteger(Map originalMap) { - Map map = new HashMap(); - for(Entry entry : originalMap.entrySet()) { - Object o = entry.getValue(); - if(o instanceof Double) { - Double d = (Double) o; - Integer i = d.intValue(); - map.put(entry.getKey(), i); - }else if(o instanceof Map) { - Map subMap = (Map) o; - map.put(entry.getKey(), recursiveDoubleToInteger(subMap)); - }else{ - map.put(entry.getKey(), o); - } - } - return map; - } - - private static Map recursiveSerialization(ConfigurationSerializable o) { - Map originalMap = o.serialize(); - Map map = new HashMap(); - for(Entry entry : originalMap.entrySet()) { - Object o2 = entry.getValue(); - if(o2 instanceof ConfigurationSerializable) { - ConfigurationSerializable serializable = (ConfigurationSerializable) o2; - Map newMap = recursiveSerialization(serializable); - newMap.put("SERIAL-ADAPTER-CLASS-KEY", ConfigurationSerialization.getAlias(serializable.getClass())); - map.put(entry.getKey(), newMap); - } - } - map.put("SERIAL-ADAPTER-CLASS-KEY", ConfigurationSerialization.getAlias(o.getClass())); - return map; - } - -}