diff --git a/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java b/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java index 5789ea016..9884a7680 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java @@ -17,11 +17,17 @@ */ package com.viaversion.viaversion.util; +import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.viaversion.viaversion.api.Via; +import java.util.logging.Level; +import net.lenni0451.mcstructs.snbt.SNbtSerializer; import net.lenni0451.mcstructs.text.ATextComponent; import net.lenni0451.mcstructs.text.Style; +import net.lenni0451.mcstructs.text.events.hover.AHoverEvent; +import net.lenni0451.mcstructs.text.events.hover.impl.TextHoverEvent; import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer; import net.lenni0451.mcstructs.text.serializer.TextComponentCodec; import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer; @@ -48,20 +54,46 @@ public final class ComponentUtil { public static @Nullable JsonElement tagToJson(@Nullable final Tag tag) { final ATextComponent component = TextComponentCodec.V1_20_3.deserializeNbtTree(NBTConverter.viaToMcStructs(tag)); - return component != null ? TextComponentSerializer.V1_19_4.serializeJson(component) : null; + return component != null ? SerializerVersion.V1_19_4.toJson(component) : null; } public static @Nullable Tag jsonToTag(@Nullable final JsonElement element) { - final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element); - return component != null ? NBTConverter.mcStructsToVia(TextComponentCodec.V1_20_3.serializeNbt(component)) : null; + if (element == null) { + return null; + } + + try { + final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element); + return NBTConverter.mcStructsToVia(TextComponentCodec.V1_20_3.serializeNbt(component)); + } catch (final Exception e) { + Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + element, e); + return new StringTag(""); + } + } + + public static @Nullable JsonElement convertJson(@Nullable final JsonElement element, final SerializerVersion from, final SerializerVersion to) { + final ATextComponent component = from.jsonSerializer.deserialize(element); + if (element == null) { + return null; + } + + if (from.ordinal() >= SerializerVersion.V1_16.ordinal() && to.ordinal() < SerializerVersion.V1_16.ordinal()) { + // Convert hover event to legacy format + final Style style = component.getStyle(); + final AHoverEvent hoverEvent = style.getHoverEvent(); + if (hoverEvent != null && !(hoverEvent instanceof TextHoverEvent)) { + style.setHoverEvent(hoverEvent.toLegacy(to.jsonSerializer, to.snbtSerializer)); + } + } + return to.toJson(component); } public static JsonElement legacyToJson(final String message) { - return TextComponentSerializer.V1_12.serializeJson(LegacyStringDeserializer.parse(message, true)); + return SerializerVersion.V1_12.toJson(LegacyStringDeserializer.parse(message, true)); } - public static String legacyToJsonString(final String legacyText) { - return legacyToJsonString(legacyText, false); + public static String legacyToJsonString(final String message) { + return legacyToJsonString(message, false); } public static String legacyToJsonString(final String message, final boolean itemData) { @@ -81,36 +113,35 @@ public final class ComponentUtil { } public enum SerializerVersion { - V1_8(TextComponentSerializer.V1_8), - V1_9(TextComponentSerializer.V1_9), - V1_12(TextComponentSerializer.V1_12), - V1_14(TextComponentSerializer.V1_14), - V1_15(TextComponentSerializer.V1_15), - V1_16(TextComponentSerializer.V1_16), - V1_17(TextComponentSerializer.V1_17), - V1_18(TextComponentSerializer.V1_18), - V1_19_4(TextComponentSerializer.V1_19_4), - V1_20_3(TextComponentCodec.V1_20_3); + V1_8(TextComponentSerializer.V1_8, SNbtSerializer.V1_8), + V1_9(TextComponentSerializer.V1_9, SNbtSerializer.V1_8), + V1_12(TextComponentSerializer.V1_12, SNbtSerializer.V1_12), + V1_14(TextComponentSerializer.V1_14, SNbtSerializer.V1_14), + V1_15(TextComponentSerializer.V1_15, SNbtSerializer.V1_14), + V1_16(TextComponentSerializer.V1_16, SNbtSerializer.V1_14), + V1_17(TextComponentSerializer.V1_17, SNbtSerializer.V1_14), + V1_18(TextComponentSerializer.V1_18, SNbtSerializer.V1_14), + V1_19_4(TextComponentSerializer.V1_19_4, SNbtSerializer.V1_14), + V1_20_3(TextComponentCodec.V1_20_3, SNbtSerializer.V1_14); - private final TextComponentSerializer serializer; + private final TextComponentSerializer jsonSerializer; + private final SNbtSerializer snbtSerializer; private final TextComponentCodec codec; - SerializerVersion(final TextComponentSerializer serializer) { - this.serializer = serializer; + SerializerVersion(final TextComponentSerializer jsonSerializer, final SNbtSerializer snbtSerializer) { + this.jsonSerializer = jsonSerializer; + this.snbtSerializer = snbtSerializer; this.codec = null; } - SerializerVersion(final TextComponentCodec codec) { - this.serializer = codec.asSerializer(); + SerializerVersion(final TextComponentCodec codec, final SNbtSerializer snbtSerializer) { this.codec = codec; + this.jsonSerializer = codec.asSerializer(); + this.snbtSerializer = snbtSerializer; } - public TextComponentSerializer serializer() { - return serializer; - } - - public @Nullable TextComponentCodec codec() { - return codec; + public JsonElement toJson(final ATextComponent component) { + return jsonSerializer.serializeJson(component); } } }