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
This commit is contained in:
Nassim Jahnke 2024-03-06 12:31:31 +01:00
parent f6d48e258f
commit cad78ea68f
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
3 changed files with 77 additions and 2 deletions

View File

@ -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("<error>");
}
}
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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, Tag> 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);
}
}

View File

@ -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