Migrated from ViaNBT to MCStructs NBT

This commit is contained in:
RaphiMC 2023-10-08 11:09:58 +02:00
parent 768a5f851a
commit f4d0fe0d35
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
2 changed files with 50 additions and 10 deletions

View File

@ -19,7 +19,6 @@ package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
import com.viaversion.viaversion.libs.gson.JsonArray;
import com.viaversion.viaversion.libs.gson.JsonElement;
@ -30,11 +29,12 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import net.lenni0451.mcstructs.snbt.SNbtSerializer;
import net.lenni0451.mcstructs.snbt.exceptions.SNbtSerializeException;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.Protocol1_8to1_7_6_10;
import net.raphimc.vialegacy.util.NbtConverter;
import java.io.IOException;
import java.util.logging.Level;
public class ChatItemRewriter {
@ -376,7 +376,7 @@ public class ChatItemRewriter {
final CompoundTag tag;
try {
tag = (CompoundTag) NbtConverter.mcStructToVia(SNbtSerializer.V1_7.deserialize(text));
tag = (CompoundTag) NbtConverter.mcStructsToVia(SNbtSerializer.V1_7.deserialize(text));
} catch (Throwable e) {
ViaLegacy.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text);
throw new RuntimeException(e);
@ -409,12 +409,11 @@ public class ChatItemRewriter {
array.add(object);
final String serializedNBT;
try {
serializedNBT = BinaryTagIO.writeString(tag);
serializedNBT = SNbtSerializer.V1_8.serialize(NbtConverter.viaToMcStructs(tag));
object.addProperty("text", serializedNBT);
hoverEvent.add("value", array);
} catch (IOException e) {
ViaLegacy.getPlatform().getLogger().warning("Error writing NBT in show_item:" + text);
e.printStackTrace();
} catch (SNbtSerializeException e) {
ViaLegacy.getPlatform().getLogger().log(Level.WARNING, "Error writing NBT in show_item:" + text, e);
}
}

View File

@ -24,8 +24,9 @@ import java.util.Map;
public class NbtConverter {
public static Tag mcStructToVia(final INbtTag tag) {
public static Tag mcStructsToVia(final INbtTag tag) {
if (tag == null) return null;
if (tag instanceof net.lenni0451.mcstructs.nbt.tags.ByteTag) {
return new ByteTag(tag.asByteTag().getValue());
} else if (tag instanceof net.lenni0451.mcstructs.nbt.tags.ShortTag) {
@ -45,13 +46,13 @@ public class NbtConverter {
} else if (tag instanceof net.lenni0451.mcstructs.nbt.tags.ListTag) {
final ListTag list = new ListTag();
for (INbtTag e : tag.asListTag()) {
list.add(mcStructToVia(e));
list.add(mcStructsToVia(e));
}
return list;
} else if (tag instanceof net.lenni0451.mcstructs.nbt.tags.CompoundTag) {
final CompoundTag compound = new CompoundTag();
for (Map.Entry<String, INbtTag> e : tag.asCompoundTag().getValue().entrySet()) {
compound.put(e.getKey(), mcStructToVia(e.getValue()));
compound.put(e.getKey(), mcStructsToVia(e.getValue()));
}
return compound;
} else if (tag instanceof net.lenni0451.mcstructs.nbt.tags.IntArrayTag) {
@ -59,7 +60,47 @@ public class NbtConverter {
} else if (tag instanceof net.lenni0451.mcstructs.nbt.tags.LongArrayTag) {
return new LongArrayTag(tag.asLongArrayTag().getValue());
}
throw new IllegalArgumentException("Unsupported tag type: " + tag.getClass().getName());
}
public static INbtTag viaToMcStructs(final Tag tag) {
if (tag == null) return null;
if (tag instanceof ByteTag) {
return new net.lenni0451.mcstructs.nbt.tags.ByteTag(((ByteTag) tag).asByte());
} else if (tag instanceof ShortTag) {
return new net.lenni0451.mcstructs.nbt.tags.ShortTag(((ShortTag) tag).asShort());
} else if (tag instanceof IntTag) {
return new net.lenni0451.mcstructs.nbt.tags.IntTag(((IntTag) tag).asInt());
} else if (tag instanceof LongTag) {
return new net.lenni0451.mcstructs.nbt.tags.LongTag(((LongTag) tag).asLong());
} else if (tag instanceof FloatTag) {
return new net.lenni0451.mcstructs.nbt.tags.FloatTag(((FloatTag) tag).asFloat());
} else if (tag instanceof DoubleTag) {
return new net.lenni0451.mcstructs.nbt.tags.DoubleTag(((DoubleTag) tag).asDouble());
} else if (tag instanceof ByteArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.ByteArrayTag(((ByteArrayTag) tag).getValue());
} else if (tag instanceof StringTag) {
return new net.lenni0451.mcstructs.nbt.tags.StringTag(((StringTag) tag).getValue());
} else if (tag instanceof ListTag) {
final net.lenni0451.mcstructs.nbt.tags.ListTag<INbtTag> list = new net.lenni0451.mcstructs.nbt.tags.ListTag<>();
for (Tag e : ((ListTag) tag)) {
list.add(viaToMcStructs(e));
}
return list;
} else if (tag instanceof CompoundTag) {
final net.lenni0451.mcstructs.nbt.tags.CompoundTag compound = new net.lenni0451.mcstructs.nbt.tags.CompoundTag();
for (Map.Entry<String, Tag> e : ((CompoundTag) tag).entrySet()) {
compound.add(e.getKey(), viaToMcStructs(e.getValue()));
}
return compound;
} else if (tag instanceof IntArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.IntArrayTag(((IntArrayTag) tag).getValue());
} else if (tag instanceof LongArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.LongArrayTag(((LongArrayTag) tag).getValue());
}
throw new IllegalArgumentException("Unsupported tag type: " + tag.getClass().getName());
}
}