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.
This commit is contained in:
FlorianMichael 2024-06-02 21:19:39 +02:00
parent 82452dbdfe
commit 6714f8c7d2
2 changed files with 10 additions and 3 deletions

View File

@ -260,7 +260,7 @@ public class ItemPacketRewriter1_14 extends ItemRewriter<ClientboundPackets1_13,
if (item.tag() == null) return item; if (item.tag() == null) return item;
// Display Name now uses JSON // Lore now uses JSON
CompoundTag display = item.tag().getCompoundTag("display"); CompoundTag display = item.tag().getCompoundTag("display");
if (display != null) { if (display != null) {
ListTag<StringTag> lore = display.getListTag("Lore", StringTag.class); ListTag<StringTag> lore = display.getListTag("Lore", StringTag.class);

View File

@ -19,6 +19,7 @@ package com.viaversion.viaversion.util;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.StringTag; import com.viaversion.nbt.tag.StringTag;
import com.viaversion.nbt.tag.Tag; import com.viaversion.nbt.tag.Tag;
@ -165,8 +166,14 @@ public final class ComponentUtil {
return SerializerVersion.V1_12.toString(component); return SerializerVersion.V1_12.toString(component);
} }
public static String jsonToLegacy(final String value) { public static @Nullable String jsonToLegacy(final String value) {
return TextComponentSerializer.V1_12.deserializeReader(value).asLegacyFormatString(); 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) { public static String jsonToLegacy(final JsonElement value) {