From af393c564f20f40f14d276e8e209bd625a6c24d8 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Wed, 29 May 2024 10:21:50 +0200 Subject: [PATCH] Fixup ComponentUtil#trimStrings detection (#3888) --- .../com/viaversion/viaversion/util/ComponentUtil.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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 08fb014ee..5517a921e 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java @@ -23,6 +23,7 @@ import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.StringTag; import com.viaversion.nbt.tag.Tag; import com.viaversion.viaversion.api.Via; +import java.nio.charset.StandardCharsets; import java.util.logging.Level; import net.lenni0451.mcstructs.text.ATextComponent; import net.lenni0451.mcstructs.text.Style; @@ -38,6 +39,8 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ public final class ComponentUtil { + private static final int MAX_UNSIGNED_SHORT = 65535; + public static JsonObject emptyJsonComponent() { return plainToJson(""); } @@ -83,10 +86,10 @@ public final class ComponentUtil { return null; } return TagUtil.handleDeep(input, (key, tag) -> { - if (tag instanceof StringTag) { - final String value = ((StringTag) tag).getValue(); - if (value.length() > Short.MAX_VALUE) { - ((StringTag) tag).setValue("{}"); + if (tag instanceof StringTag stringTag) { + final byte[] value = stringTag.getValue().getBytes(StandardCharsets.UTF_8); + if (value.length > MAX_UNSIGNED_SHORT) { + stringTag.setValue("{}"); } } return tag;