From cad78ea68fc6fa94f4fc1608fc996a4b199994ff Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 6 Mar 2024 12:31:31 +0100 Subject: [PATCH] Trim string tags in component conversion Somewhat dirty but inconsequential, as the given protocol does not use this method in item conversion and it should only ever be triggered in hover events Closes #3650 --- .../viaversion/util/ComponentUtil.java | 19 +++++- .../viaversion/viaversion/util/TagUtil.java | 58 +++++++++++++++++++ gradle.properties | 2 +- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viaversion/util/TagUtil.java 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 d530b120a..47ceac110 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java @@ -69,13 +69,30 @@ public final class ComponentUtil { try { final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element); - return TextComponentCodec.V1_20_3.serializeNbt(component); + return trimStrings(TextComponentCodec.V1_20_3.serializeNbt(component)); } catch (final Exception e) { Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + element, e); return new StringTag(""); } } + private static Tag trimStrings(final Tag input) { + // Dirty fix for https://github.com/ViaVersion/ViaVersion/issues/3650 + // Usually tripped by hover event data being too long, e.g. book or shulker box contents + if (input == null) { + 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("{}"); + } + } + return tag; + }); + } + public static @Nullable JsonElement convertJson(@Nullable final JsonElement element, final SerializerVersion from, final SerializerVersion to) { return element != null ? convert(from, to, from.jsonSerializer.deserialize(element)) : null; } diff --git a/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java new file mode 100644 index 000000000..7b43ca2f7 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java @@ -0,0 +1,58 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.util; + +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.ListTag; +import com.github.steveice10.opennbt.tag.builtin.Tag; +import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class TagUtil { + + public static Tag handleDeep(final Tag tag, final TagUpdater consumer) { + return handleDeep(null, tag, consumer); + } + + private static Tag handleDeep(@Nullable final String key, final Tag tag, final TagUpdater consumer) { + if (tag instanceof CompoundTag) { + final CompoundTag compoundTag = (CompoundTag) tag; + for (final Map.Entry entry : compoundTag.entrySet()) { + final Tag updatedTag = handleDeep(entry.getKey(), entry.getValue(), consumer); + entry.setValue(updatedTag); + } + } else if (tag instanceof ListTag) { + final ListTag listTag = (ListTag) tag; + listTag.getValue().replaceAll(t -> handleDeep(null, t, consumer)); + } + return consumer.update(key, tag); + } + + @FunctionalInterface + public interface TagUpdater { + + /** + * Updates the given tag. + * + * @param key key of the tag if inside a CompoundTag + * @param tag the tag to update + * @return the updated tag, can be the original one + */ + Tag update(@Nullable String key, Tag tag); + } +} diff --git a/gradle.properties b/gradle.properties index fc02dcc2e..76ee51291 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Project properties - we put these here so they can be modified without causing a recompile of the build scripts -projectVersion=4.9.3 +projectVersion=4.9.4-SNAPSHOT # Smile emoji mcVersions=1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10, 1.9.4, 1.9.3, 1.9.2, 1.9.1, 1.9, 1.8.9