From cd65925d6b7a887f6c414635c3ac3e6b36aa228b Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Mon, 1 Apr 2024 15:52:57 +0200 Subject: [PATCH] Add conversion methods to SerializerVersion and replace missing MCStructs usages (#3769) --- .../rewriter/ChatItemRewriter.java | 3 +- .../data/ComponentRewriter1_13.java | 26 +++---- .../Protocol1_19_1To1_19.java | 6 +- .../viaversion/util/ComponentUtil.java | 14 ++-- .../viaversion/util/SerializerVersion.java | 67 +++++++++++++++++-- .../viaversion/viaversion/util/TagUtil.java | 16 ----- 6 files changed, 84 insertions(+), 48 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/rewriter/ChatItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/rewriter/ChatItemRewriter.java index 9d0c67a2e..facac457c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/rewriter/ChatItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/rewriter/ChatItemRewriter.java @@ -23,7 +23,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.SerializerVersion; -import com.viaversion.viaversion.util.TagUtil; public final class ChatItemRewriter { @@ -45,7 +44,7 @@ public final class ChatItemRewriter { if (type.equals("show_item")) { final CompoundTag compound = ComponentUtil.deserializeLegacyShowItem(value, SerializerVersion.V1_8); - hoverEvent.addProperty("value", TagUtil.toSNBT(compound, SerializerVersion.V1_12)); + hoverEvent.addProperty("value", SerializerVersion.V1_12.toSNBT(compound)); } } else if (obj.has("extra")) { toClient(obj.get("extra")); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/ComponentRewriter1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/ComponentRewriter1_13.java index 062937225..7515df25c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/ComponentRewriter1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/ComponentRewriter1_13.java @@ -32,7 +32,6 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_ import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.SerializerVersion; -import com.viaversion.viaversion.util.TagUtil; import java.util.logging.Level; public class ComponentRewriter1_13 extends ComponentRewriter { @@ -60,12 +59,13 @@ public class ComponentRewriter1_13 extends Comp return; } - CompoundTag itemTag = tag.getCompoundTag("tag"); - NumberTag damageTag = tag.getNumberTag("Damage"); + final CompoundTag itemTag = tag.getCompoundTag("tag"); + final NumberTag damageTag = tag.getNumberTag("Damage"); // Call item converter - short damage = damageTag != null ? damageTag.asShort() : 0; - Item item = new DataItem(); + final short damage = damageTag != null ? damageTag.asShort() : 0; + + final Item item = new DataItem(); item.setData(damage); item.setTag(itemTag); protocol.getItemRewriter().handleItemToClient(item); @@ -78,16 +78,16 @@ public class ComponentRewriter1_13 extends Comp tag.put("tag", itemTag); } - JsonArray array = new JsonArray(); - JsonObject object = new JsonObject(); - array.add(object); - String serializedNBT; + final JsonArray newValue = new JsonArray(); + final JsonObject showItem = new JsonObject(); + newValue.add(showItem); try { - serializedNBT = TagUtil.toSNBT(tag, SerializerVersion.V1_13); - object.addProperty("text", serializedNBT); - hoverEvent.add("value", array); + showItem.addProperty("text", SerializerVersion.V1_13.toSNBT(tag)); + hoverEvent.add("value", newValue); } catch (Exception e) { - Via.getPlatform().getLogger().log(Level.WARNING, "Error writing 1.13 NBT in show_item: " + value, e); + if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + Via.getPlatform().getLogger().log(Level.WARNING, "Error writing 1.13 NBT in show_item: " + value, e); + } } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_1to1_19/Protocol1_19_1To1_19.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_1to1_19/Protocol1_19_1To1_19.java index 23a89eb04..0d61cb445 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_1to1_19/Protocol1_19_1To1_19.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_1to1_19/Protocol1_19_1To1_19.java @@ -49,11 +49,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; +import com.viaversion.viaversion.util.SerializerVersion; import net.lenni0451.mcstructs.core.TextFormatting; import net.lenni0451.mcstructs.text.ATextComponent; import net.lenni0451.mcstructs.text.Style; import net.lenni0451.mcstructs.text.components.TranslationComponent; -import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer; import org.checkerframework.checker.nullness.qual.Nullable; public final class Protocol1_19_1To1_19 extends AbstractProtocol { @@ -392,11 +392,11 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol"); @@ -126,7 +125,7 @@ public final class ComponentUtil { if (itemData) { component.setParentStyle(new Style().setItalic(false)); } - return TextComponentSerializer.V1_12.serialize(component); + return SerializerVersion.V1_12.toString(component); } public static String jsonToLegacy(final String value) { @@ -134,11 +133,10 @@ public final class ComponentUtil { } public static String jsonToLegacy(final JsonElement value) { - return TextComponentSerializer.V1_12.deserialize(value).asLegacyFormatString(); + return SerializerVersion.V1_12.toComponent(value).asLegacyFormatString(); } public static CompoundTag deserializeLegacyShowItem(final JsonElement element, final SerializerVersion version) { - final ATextComponent component = version.jsonSerializer.deserialize(element); - return TagUtil.fromSNBT(component.asUnformattedString(), version); + return (CompoundTag) version.toTag(version.toComponent(element).asUnformattedString()); } } diff --git a/common/src/main/java/com/viaversion/viaversion/util/SerializerVersion.java b/common/src/main/java/com/viaversion/viaversion/util/SerializerVersion.java index 73e6b0f97..ae76835a5 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/SerializerVersion.java +++ b/common/src/main/java/com/viaversion/viaversion/util/SerializerVersion.java @@ -17,14 +17,18 @@ */ package com.viaversion.viaversion.util; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.Tag; import com.google.gson.JsonElement; import net.lenni0451.mcstructs.snbt.SNbtSerializer; +import net.lenni0451.mcstructs.snbt.exceptions.SNbtDeserializeException; +import net.lenni0451.mcstructs.snbt.exceptions.SNbtSerializeException; import net.lenni0451.mcstructs.text.ATextComponent; import net.lenni0451.mcstructs.text.serializer.TextComponentCodec; import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer; public enum SerializerVersion { + V1_6(TextComponentSerializer.V1_6, null), + V1_7(TextComponentSerializer.V1_7, SNbtSerializer.V1_7), V1_8(TextComponentSerializer.V1_8, SNbtSerializer.V1_8), V1_9(TextComponentSerializer.V1_9, SNbtSerializer.V1_8), V1_12(TextComponentSerializer.V1_12, SNbtSerializer.V1_12), @@ -38,19 +42,70 @@ public enum SerializerVersion { V1_20_3(TextComponentCodec.V1_20_3, SNbtSerializer.V1_14); final TextComponentSerializer jsonSerializer; - final SNbtSerializer snbtSerializer; + final SNbtSerializer snbtSerializer; + final TextComponentCodec codec; - SerializerVersion(final TextComponentSerializer jsonSerializer, final SNbtSerializer snbtSerializer) { + SerializerVersion(final TextComponentSerializer jsonSerializer, final SNbtSerializer snbtSerializer) { this.jsonSerializer = jsonSerializer; this.snbtSerializer = snbtSerializer; + this.codec = null; + } + + SerializerVersion(final TextComponentCodec codec, final SNbtSerializer snbtSerializer) { + this.codec = codec; + this.jsonSerializer = codec.asSerializer(); + this.snbtSerializer = snbtSerializer; } - SerializerVersion(final TextComponentCodec codec, final SNbtSerializer snbtSerializer) { - this.jsonSerializer = codec.asSerializer(); - this.snbtSerializer = snbtSerializer; + public String toString(final ATextComponent component) { + return jsonSerializer.serialize(component); } public JsonElement toJson(final ATextComponent component) { return jsonSerializer.serializeJson(component); } + + public Tag toTag(final ATextComponent component) { + if (codec == null) { + throw new IllegalStateException("Cannot convert component to NBT with this version"); + } + return codec.serializeNbt(component); + } + + public ATextComponent toComponent(final JsonElement json) { + return jsonSerializer.deserialize(json); + } + + public ATextComponent toComponent(final String json) { + return jsonSerializer.deserializeReader(json); + } + + public ATextComponent toComponent(final Tag tag) { + if (codec == null) { + throw new IllegalStateException("Cannot convert NBT to component with this version"); + } + return codec.deserializeNbtTree(tag); + } + + public Tag toTag(final String snbt) { + if (snbtSerializer == null) { + throw new IllegalStateException("Cannot convert SNBT to NBT with this version"); + } + try { + return snbtSerializer.deserialize(snbt); + } catch (SNbtDeserializeException e) { + throw new RuntimeException(e); + } + } + + public String toSNBT(final Tag tag) { + if (snbtSerializer == null) { + throw new IllegalStateException("Cannot convert SNBT to NBT with this version"); + } + try { + return snbtSerializer.serialize(tag); + } catch (SNbtSerializeException e) { + throw new RuntimeException(e); + } + } } \ No newline at end of file diff --git a/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java index 5a06ce966..2bfd4ed54 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java @@ -48,22 +48,6 @@ public final class TagUtil { listTag.getValue().replaceAll(t -> (T) handleDeep(null, t, consumer)); } - public static String toSNBT(final Tag tag, final SerializerVersion version) { - try { - return version.snbtSerializer.serialize(tag); - } catch (final SNbtSerializeException e) { - throw new RuntimeException(e); - } - } - - public static CompoundTag fromSNBT(final String snbt, final SerializerVersion version) { - try { - return version.snbtSerializer.deserialize(snbt); - } catch (final SNbtDeserializeException e) { - throw new RuntimeException(e); - } - } - @FunctionalInterface public interface TagUpdater {