Skip to content

Commit cad78ea

Browse files
committed
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 ViaVersion#3650
1 parent f6d48e2 commit cad78ea

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,30 @@ public static JsonObject plainToJson(final String message) {
6969

7070
try {
7171
final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element);
72-
return TextComponentCodec.V1_20_3.serializeNbt(component);
72+
return trimStrings(TextComponentCodec.V1_20_3.serializeNbt(component));
7373
} catch (final Exception e) {
7474
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + element, e);
7575
return new StringTag("<error>");
7676
}
7777
}
7878

79+
private static Tag trimStrings(final Tag input) {
80+
// Dirty fix for https://github.com/ViaVersion/ViaVersion/issues/3650
81+
// Usually tripped by hover event data being too long, e.g. book or shulker box contents
82+
if (input == null) {
83+
return null;
84+
}
85+
return TagUtil.handleDeep(input, (key, tag) -> {
86+
if (tag instanceof StringTag) {
87+
final String value = ((StringTag) tag).getValue();
88+
if (value.length() > Short.MAX_VALUE) {
89+
((StringTag) tag).setValue("{}");
90+
}
91+
}
92+
return tag;
93+
});
94+
}
95+
7996
public static @Nullable JsonElement convertJson(@Nullable final JsonElement element, final SerializerVersion from, final SerializerVersion to) {
8097
return element != null ? convert(from, to, from.jsonSerializer.deserialize(element)) : null;
8198
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
3+
* Copyright (C) 2016-2024 ViaVersion and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.viaversion.viaversion.util;
19+
20+
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
21+
import com.github.steveice10.opennbt.tag.builtin.ListTag;
22+
import com.github.steveice10.opennbt.tag.builtin.Tag;
23+
import java.util.Map;
24+
import org.checkerframework.checker.nullness.qual.Nullable;
25+
26+
public final class TagUtil {
27+
28+
public static Tag handleDeep(final Tag tag, final TagUpdater consumer) {
29+
return handleDeep(null, tag, consumer);
30+
}
31+
32+
private static Tag handleDeep(@Nullable final String key, final Tag tag, final TagUpdater consumer) {
33+
if (tag instanceof CompoundTag) {
34+
final CompoundTag compoundTag = (CompoundTag) tag;
35+
for (final Map.Entry<String, Tag> entry : compoundTag.entrySet()) {
36+
final Tag updatedTag = handleDeep(entry.getKey(), entry.getValue(), consumer);
37+
entry.setValue(updatedTag);
38+
}
39+
} else if (tag instanceof ListTag) {
40+
final ListTag listTag = (ListTag) tag;
41+
listTag.getValue().replaceAll(t -> handleDeep(null, t, consumer));
42+
}
43+
return consumer.update(key, tag);
44+
}
45+
46+
@FunctionalInterface
47+
public interface TagUpdater {
48+
49+
/**
50+
* Updates the given tag.
51+
*
52+
* @param key key of the tag if inside a CompoundTag
53+
* @param tag the tag to update
54+
* @return the updated tag, can be the original one
55+
*/
56+
Tag update(@Nullable String key, Tag tag);
57+
}
58+
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Project properties - we put these here so they can be modified without causing a recompile of the build scripts
2-
projectVersion=4.9.3
2+
projectVersion=4.9.4-SNAPSHOT
33

44
# Smile emoji
55
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

0 commit comments

Comments
 (0)