From d0af42f291a9d72a123de43ff8968aca2c6900a5 Mon Sep 17 00:00:00 2001 From: YuTian <2953516620@qq.com> Date: Sat, 20 Jul 2024 03:28:53 +0800 Subject: [PATCH] v1.7.3 --- .../com/io/yutian/aulib/nbt/NBTCompound.java | 28 ++++++++- .../serializers/ItemStackSerializer.java | 61 ++++++++++++++++++- src/main/resources/plugin.yml | 2 +- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/io/yutian/aulib/nbt/NBTCompound.java b/src/main/java/com/io/yutian/aulib/nbt/NBTCompound.java index 48d3ffe..219f9bd 100644 --- a/src/main/java/com/io/yutian/aulib/nbt/NBTCompound.java +++ b/src/main/java/com/io/yutian/aulib/nbt/NBTCompound.java @@ -1,5 +1,7 @@ package com.io.yutian.aulib.nbt; +import com.google.common.collect.Maps; + import java.util.*; public class NBTCompound implements INBT { @@ -225,7 +227,31 @@ public class NBTCompound implements INBT { if (o == null || getClass() != o.getClass()) return false; NBTCompound that = (NBTCompound) o; - return Objects.equals(nbtMap, that.nbtMap); + Map nbtMap1 = this.nbtMap; + Map nbtMap2 = that.nbtMap; + if (nbtMap1.size() != nbtMap2.size()) { + return false; + } + for (Map.Entry entry : nbtMap1.entrySet()) { + String key = entry.getKey(); + INBT value = entry.getValue(); + if (!nbtMap2.containsKey(key)) { + return false; + } + INBT nbt2 = nbtMap2.get(key); + Object object1 = value.getValue(); + Object object2 = nbt2.getValue(); + if (value == null && nbt2 != null) { + return false; + } else if (object1 instanceof Number && object2 instanceof Number) { + if (Double.compare(((Number) object1).doubleValue(), ((Number) object2).doubleValue()) != 0) { + return false; + } + } else if (!object1.equals(object2)) { + return false; + } + } + return true; } @Override 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 index da16f24..74fc217 100644 --- a/src/main/java/com/io/yutian/aulib/serialize/serializers/ItemStackSerializer.java +++ b/src/main/java/com/io/yutian/aulib/serialize/serializers/ItemStackSerializer.java @@ -2,9 +2,10 @@ 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 net.minecraft.nbt.*; import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; +import org.json.JSONArray; import org.json.JSONObject; public class ItemStackSerializer implements Serializer { @@ -16,10 +17,66 @@ public class ItemStackSerializer implements Serializer { jsonObject.put("id", nmsItemStack.c().k().g().a()); jsonObject.put("Count", nmsItemStack.J()); NBTTagCompound nbtTagCompound = nmsItemStack.u(); - jsonObject.put("tag", new JSONObject(nbtTagCompound.toString())); + JSONObject tagJsonObject = new JSONObject(); + for (String key : nbtTagCompound.d()) { + NBTBase nbtBase = nbtTagCompound.c(key); + tagJsonObject.put(key, serializeNBT(nbtBase)); + } + jsonObject.put("tag", tagJsonObject); return jsonObject; } + private Object serializeNBT(NBTBase nbtBase) { + byte type = nbtBase.a(); + switch (type) { + case 1: + NBTTagByte nbtTagByte = (NBTTagByte) nbtBase; + return nbtTagByte.h(); + case 2: + NBTTagShort nbtTagShort = (NBTTagShort) nbtBase; + return nbtTagShort.g(); + case 3: + NBTTagInt nbtTagInt = (NBTTagInt) nbtBase; + return nbtTagInt.f(); + case 4: + NBTTagLong nbtTagLong = (NBTTagLong) nbtBase; + return nbtTagLong.e(); + case 5: + NBTTagFloat nbtTagFloat = (NBTTagFloat) nbtBase; + return nbtTagFloat.j(); + case 6: + NBTTagDouble nbtTagDouble = (NBTTagDouble) nbtBase; + return nbtTagDouble.i(); + case 7: + NBTTagByteArray tagByteArray = (NBTTagByteArray) nbtBase; + return tagByteArray.d(); + case 8: + NBTTagString nbtTagString = (NBTTagString) nbtBase; + return nbtTagString.e_(); + case 9: + NBTTagList nbtTagList = (NBTTagList) nbtBase; + JSONArray jsonArray = new JSONArray(); + for (net.minecraft.nbt.NBTBase base : nbtTagList) { + jsonArray.put(serializeNBT(base)); + } + return jsonArray; + case 10: + NBTTagCompound nbtTagCompound = (NBTTagCompound) nbtBase; + JSONObject jsonObject = new JSONObject(); + for (String key : nbtTagCompound.d()) { + jsonObject.put(key, serializeNBT(nbtTagCompound.c(key))); + } + return jsonObject; + case 11: + NBTTagIntArray nbtTagIntArray = (NBTTagIntArray) nbtBase; + return nbtTagIntArray.f(); + case 12: + NBTTagLongArray nbtTagLongArray = (NBTTagLongArray) nbtBase; + return nbtTagLongArray.f(); + } + return null; + } + @Override public ItemStack deserialize(Object value) { JSONObject jsonObject = (JSONObject) value; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 4ffc2f3..109b955 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: AuLib main: com.io.yutian.aulib.AuLib -version: 1.7 +version: 1.7.3 api-version: 1.18 author: SuperYuTian \ No newline at end of file