diff --git a/src/main/java/com/io/yutian/aulib/serialize/SerializeHelper.java b/src/main/java/com/io/yutian/aulib/serialize/SerializeHelper.java index 0ea423c..331b969 100644 --- a/src/main/java/com/io/yutian/aulib/serialize/SerializeHelper.java +++ b/src/main/java/com/io/yutian/aulib/serialize/SerializeHelper.java @@ -1,7 +1,10 @@ package com.io.yutian.aulib.serialize; import com.io.yutian.aulib.exception.SerializeException; +import com.io.yutian.aulib.serialize.serializers.ItemStackSerializer; +import com.io.yutian.aulib.serialize.serializers.UUIDSerializer; import com.io.yutian.aulib.util.ReflectionUtil; +import org.bukkit.inventory.ItemStack; import org.json.JSONArray; import org.json.JSONObject; import sun.misc.Unsafe; @@ -16,7 +19,7 @@ public class SerializeHelper { private static Map, Serializer> serializers = new HashMap<>(); - public static void registerSerializer(Class clazz, Serializer serializer) { + public static void registerSerializer(Class clazz, Serializer serializer) { serializers.put(clazz, serializer); } @@ -177,17 +180,8 @@ public class SerializeHelper { WRAPPER_TO_PRIMITIVE.put(Float.class, float.class); WRAPPER_TO_PRIMITIVE.put(Double.class, double.class); - registerSerializer(UUID.class, new Serializer<>() { - @Override - public Object serialize(UUID value) { - return value.toString(); - } - - @Override - public UUID deserialize(Object value) { - return UUID.fromString((String) value); - } - }); + registerSerializer(UUID.class, new UUIDSerializer()); + registerSerializer(ItemStack.class, new ItemStackSerializer()); } } diff --git a/src/main/java/com/io/yutian/aulib/serialize/Serializer.java b/src/main/java/com/io/yutian/aulib/serialize/Serializer.java index c134414..eafef8d 100644 --- a/src/main/java/com/io/yutian/aulib/serialize/Serializer.java +++ b/src/main/java/com/io/yutian/aulib/serialize/Serializer.java @@ -1,9 +1,9 @@ package com.io.yutian.aulib.serialize; -public interface Serializer { +public interface Serializer { - V serialize(K value); + Object serialize(T value); - K deserialize(V value); + T deserialize(Object value); } diff --git a/src/main/java/com/io/yutian/aulib/serialize/serializers/ItemStackSerializer.java b/src/main/java/com/io/yutian/aulib/serialize/serializers/ItemStackSerializer.java new file mode 100644 index 0000000..a589f6c --- /dev/null +++ b/src/main/java/com/io/yutian/aulib/serialize/serializers/ItemStackSerializer.java @@ -0,0 +1,37 @@ +package com.io.yutian.aulib.serialize.serializers; + +import com.io.yutian.aulib.serialize.Serializer; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.nbt.NBTTagCompound; +import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; +import org.json.JSONObject; + +public class ItemStackSerializer implements Serializer { + + @Override + public Object serialize(ItemStack value) { + JSONObject jsonObject = new JSONObject(); + net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(value); + jsonObject.put("Type", nmsItemStack.c().k().g().a()); + jsonObject.put("Count", nmsItemStack.J()); + NBTTagCompound nbtTagCompound = nmsItemStack.u(); + jsonObject.put("Tag", nbtTagCompound.toString()); + return null; + } + + @Override + public ItemStack deserialize(Object value) { + JSONObject jsonObject = (JSONObject) value; + String arg = jsonObject.toString(); + NBTTagCompound nbtTagCompound; + try { + nbtTagCompound = net.minecraft.nbt.MojangsonParser.a(arg); + } catch (CommandSyntaxException e) { + e.printStackTrace(); + return null; + } + return CraftItemStack.asBukkitCopy(net.minecraft.world.item.ItemStack.a(nbtTagCompound)); + } + +} diff --git a/src/main/java/com/io/yutian/aulib/serialize/serializers/UUIDSerializer.java b/src/main/java/com/io/yutian/aulib/serialize/serializers/UUIDSerializer.java new file mode 100644 index 0000000..fcf5b40 --- /dev/null +++ b/src/main/java/com/io/yutian/aulib/serialize/serializers/UUIDSerializer.java @@ -0,0 +1,17 @@ +package com.io.yutian.aulib.serialize.serializers; + +import com.io.yutian.aulib.serialize.Serializer; + +import java.util.UUID; + +public class UUIDSerializer implements Serializer { + @Override + public Object serialize(UUID value) { + return value.toString(); + } + + @Override + public UUID deserialize(Object value) { + return UUID.fromString((String) value); + } +}