This commit is contained in:
YuTian 2024-07-20 03:28:53 +08:00
parent 920c5ff445
commit d0af42f291
3 changed files with 87 additions and 4 deletions

View File

@ -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<String, INBT> nbtMap1 = this.nbtMap;
Map<String, INBT> nbtMap2 = that.nbtMap;
if (nbtMap1.size() != nbtMap2.size()) {
return false;
}
for (Map.Entry<String, INBT> 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

View File

@ -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<ItemStack> {
@ -16,10 +17,66 @@ public class ItemStackSerializer implements Serializer<ItemStack> {
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;

View File

@ -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