From 6714f8c7d256781dfe2fcdc80b0dfb9353287240 Mon Sep 17 00:00:00 2001 From: FlorianMichael Date: Sun, 2 Jun 2024 21:19:39 +0200 Subject: [PATCH] Change ComponentUtil#jsonToLegacy validation to match Vanilla MC 1.14 validates both lore and display name properly when reading it, this doesn't matter for ViaVersion where the server doesn't send a json and we build it (and the client never sends invalid data to us). But it is important for ViaBackwards to handle invalid sent json properly like MC does. --- .../rewriter/ItemPacketRewriter1_14.java | 2 +- .../com/viaversion/viaversion/util/ComponentUtil.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java index 3d25e4922..6512ca1d6 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java @@ -260,7 +260,7 @@ public class ItemPacketRewriter1_14 extends ItemRewriter lore = display.getListTag("Lore", StringTag.class); 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 45778bbe3..ae1c01bf4 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java @@ -19,6 +19,7 @@ package com.viaversion.viaversion.util; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.StringTag; import com.viaversion.nbt.tag.Tag; @@ -165,8 +166,14 @@ public final class ComponentUtil { return SerializerVersion.V1_12.toString(component); } - public static String jsonToLegacy(final String value) { - return TextComponentSerializer.V1_12.deserializeReader(value).asLegacyFormatString(); + public static @Nullable String jsonToLegacy(final String value) { + try { + final ATextComponent component = TextComponentSerializer.V1_12.deserializeReader(value); + if (component == null) return null; + return component.asLegacyFormatString(); + } catch (final JsonParseException e) { + return null; + } } public static String jsonToLegacy(final JsonElement value) {