From cab287c1ed5493d4f6ee13e274f45bcf229efd17 Mon Sep 17 00:00:00 2001 From: bobhenl <18662086+bobhenl@users.noreply.github.com> Date: Fri, 30 Oct 2026 16:55:05 +0100 Subject: [PATCH] Cache team and boss bar text conversions for older clients --- .../viabackwards/ViaBackwardsConfig.java | 14 ++++ .../viabackwards/api/ViaBackwardsConfig.java | 16 ++++ .../v1_13to1_12_2/Protocol1_13To1_12_2.java | 22 ++++++ .../rewriter/PlayerPacketRewriter1_13.java | 6 +- .../Protocol1_20_3To1_20_2.java | 79 ++++++++++++++++++- .../viabackwards/utils/ConversionCache.java | 54 +++++++++++++ .../resources/assets/viabackwards/config.yml | 8 ++ 7 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/utils/ConversionCache.java diff --git a/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java b/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java index 9eca8419e..c9fbbf7f4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java +++ b/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java @@ -48,6 +48,8 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback private DialogStyleConfig dialogStyleConfig; private boolean codeOfConductAsDialog; private boolean passOriginalItemNameToResourcePacks; + private boolean cacheTeamTextConversion; + private boolean cacheBossBarTextConversion; public ViaBackwardsConfig(File configFile, Logger logger) { super(configFile, logger); @@ -77,6 +79,8 @@ private void loadFields() { dialogStyleConfig = loadDialogStyleConfig(getSection("dialog-style")); codeOfConductAsDialog = getBoolean("code-of-conduct-as-dialog", true); passOriginalItemNameToResourcePacks = getBoolean("pass-original-item-name-to-resource-packs", true); + cacheTeamTextConversion = getBoolean("cache-team-text-conversion", true); + cacheBossBarTextConversion = getBoolean("cache-bossbar-text-conversion", true); } private DialogStyleConfig loadDialogStyleConfig(final ConfigSection section) { @@ -186,6 +190,16 @@ public boolean passOriginalItemNameToResourcePacks() { return passOriginalItemNameToResourcePacks; } + @Override + public boolean cacheTeamTextConversion() { + return cacheTeamTextConversion; + } + + @Override + public boolean cacheBossBarTextConversion() { + return cacheBossBarTextConversion; + } + @Override public URL getDefaultConfigURL() { return getClass().getClassLoader().getResource("assets/viabackwards/config.yml"); diff --git a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java index b16e54a46..cd2963c64 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java @@ -142,4 +142,20 @@ public interface ViaBackwardsConfig extends Config { * @return true if enabled */ boolean passOriginalItemNameToResourcePacks(); + + /** + * Caches the text conversion of scoreboard team names, prefixes and suffixes for older clients. + * Plugins often send the same team update to every online player, so most conversions can be reused. + * + * @return true if enabled + */ + boolean cacheTeamTextConversion(); + + /** + * Caches the text conversion of boss bar titles for older clients. + * Plugins often resend the same title to every online player, so most conversions can be reused. + * + * @return true if enabled + */ + boolean cacheBossBarTextConversion(); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/Protocol1_13To1_12_2.java index d14c6f37f..258ba9741 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/Protocol1_13To1_12_2.java @@ -31,6 +31,7 @@ import com.viaversion.viabackwards.protocol.v1_13to1_12_2.storage.PlayerPositionStorage1_13; import com.viaversion.viabackwards.protocol.v1_13to1_12_2.storage.ProtocolStorables1_13; import com.viaversion.viabackwards.utils.BackwardsProtocolLogger; +import com.viaversion.viabackwards.utils.ConversionCache; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.ClientWorld; @@ -75,6 +76,8 @@ protected void handleTranslate(JsonObject root, String translate) { } }; + private final ConversionCache teamLegacyTextCache = new ConversionCache<>(2048); + public Protocol1_13To1_12_2() { super(ClientboundPackets1_13.class, ClientboundPackets1_12_1.class, ServerboundPackets1_13.class, ServerboundPackets1_12_1.class); } @@ -171,4 +174,23 @@ public String jsonToLegacy(UserConnection connection, @Nullable JsonElement valu translatableToLegacyRewriter.processText(connection, value); return ComponentUtil.jsonToLegacy(value); } + + /** + * Same as {@link #jsonToLegacy(UserConnection, JsonElement)}, but caches the result if enabled. + * Only hover events depend on the connection, and those are not part of the legacy text. + */ + public String teamJsonToLegacy(UserConnection connection, @Nullable JsonElement value) { + if (value == null || value.isJsonNull() || !ViaBackwards.getConfig().cacheTeamTextConversion()) { + return jsonToLegacy(connection, value); + } + + // Use the serialized text as key, JsonPrimitive#equals treats e.g. 1 and 1.0 as equal + String key = value.toString(); + String legacy = teamLegacyTextCache.get(key); + if (legacy == null) { + legacy = jsonToLegacy(connection, value); + teamLegacyTextCache.put(key, legacy); + } + return legacy; + } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/PlayerPacketRewriter1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/PlayerPacketRewriter1_13.java index c3a784593..6016cfdc7 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/PlayerPacketRewriter1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/PlayerPacketRewriter1_13.java @@ -197,7 +197,7 @@ public void register() { byte action = wrapper.get(Types.BYTE, 0); if (action == 0 || action == 2) { JsonElement displayName = wrapper.read(Types.COMPONENT); - String legacyTextDisplayName = protocol.jsonToLegacy(wrapper.user(), displayName); + String legacyTextDisplayName = protocol.teamJsonToLegacy(wrapper.user(), displayName); wrapper.write(Types.STRING, ChatUtil.fromLegacy(legacyTextDisplayName, 'f', 32)); byte flags = wrapper.read(Types.BYTE); @@ -212,11 +212,11 @@ public void register() { JsonElement prefixComponent = wrapper.read(Types.COMPONENT); JsonElement suffixComponent = wrapper.read(Types.COMPONENT); - String prefix = protocol.jsonToLegacy(wrapper.user(), prefixComponent); + String prefix = protocol.teamJsonToLegacy(wrapper.user(), prefixComponent); if (ViaBackwards.getConfig().addTeamColorTo1_13Prefix()) { prefix += "ยง" + (colour > -1 && colour <= 15 ? Integer.toHexString(colour) : "r"); } - String suffix = protocol.jsonToLegacy(wrapper.user(), suffixComponent); + String suffix = protocol.teamJsonToLegacy(wrapper.user(), suffixComponent); wrapper.write(Types.STRING, ChatUtil.fromLegacyPrefix(prefix, 'f', 16)); wrapper.write(Types.STRING, ChatUtil.fromLegacy(suffix, '\0', 16)); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_3to1_20_2/Protocol1_20_3To1_20_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_3to1_20_2/Protocol1_20_3To1_20_2.java index 297552fc3..e82a64e34 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_3to1_20_2/Protocol1_20_3To1_20_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_3to1_20_2/Protocol1_20_3To1_20_2.java @@ -17,7 +17,10 @@ */ package com.viaversion.viabackwards.protocol.v1_20_3to1_20_2; +import com.viaversion.nbt.tag.CompoundTag; +import com.viaversion.nbt.tag.ListTag; import com.viaversion.nbt.tag.Tag; +import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.data.BackwardsMappingData; import com.viaversion.viabackwards.api.rewriters.text.JsonNBTComponentRewriter; @@ -26,6 +29,7 @@ import com.viaversion.viabackwards.protocol.v1_20_3to1_20_2.rewriter.EntityPacketRewriter1_20_3; import com.viaversion.viabackwards.protocol.v1_20_3to1_20_2.storage.ResourcepackIDStorage; import com.viaversion.viabackwards.protocol.v1_20_3to1_20_2.storage.SpawnPositionStorage; +import com.viaversion.viabackwards.utils.ConversionCache; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.BlockPosition; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_3; @@ -38,6 +42,7 @@ import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.fastutil.Pair; +import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.protocols.v1_19_3to1_19_4.rewriter.CommandRewriter1_19_4; import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.Protocol1_20_2To1_20_3; import com.viaversion.viaversion.protocols.v1_20_2to1_20_3.packet.ClientboundConfigurationPackets1_20_3; @@ -58,6 +63,7 @@ import com.viaversion.viaversion.util.ComponentUtil; import java.util.BitSet; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.Nullable; import static com.viaversion.viaversion.util.ProtocolUtil.packetTypeMap; @@ -70,6 +76,8 @@ public final class Protocol1_20_3To1_20_2 extends BackwardsProtocol translatableRewriter = new JsonNBTComponentRewriter<>(this, ComponentRewriterBase.ReadType.NBT); private final TagRewriter tagRewriter = new TagRewriter<>(this); private final BlockRewriter blockRewriter = new BlockPacketRewriter1_20_3(this); + private final ConversionCache teamComponentCache = new ConversionCache<>(2048); + private final ConversionCache bossBarComponentCache = new ConversionCache<>(2048); public Protocol1_20_3To1_20_2() { super(ClientboundPacket1_20_3.class, ClientboundPacket1_20_2.class, ServerboundPacket1_20_3.class, ServerboundPacket1_20_2.class); @@ -195,7 +203,8 @@ public void handleArgument(final PacketWrapper wrapper, final String argumentTyp final int action = wrapper.passthrough(Types.VAR_INT); if (action == 0 || action == 3) { - convertComponent(wrapper); + final Tag tag = wrapper.read(Types.TRUSTED_TAG); + wrapper.write(Types.COMPONENT, bossBarTagToJson(wrapper.user(), tag)); } }); replaceClientbound(ClientboundPackets1_20_3.PLAYER_CHAT, wrapper -> { @@ -229,13 +238,13 @@ public void handleArgument(final PacketWrapper wrapper, final String argumentTyp wrapper.passthrough(Types.STRING); // Team Name final byte action = wrapper.passthrough(Types.BYTE); // Mode if (action == 0 || action == 2) { - convertComponent(wrapper); // Display Name + convertTeamComponent(wrapper); // Display Name wrapper.passthrough(Types.BYTE); // Flags wrapper.passthrough(Types.STRING); // Name Tag Visibility wrapper.passthrough(Types.STRING); // Collision rule wrapper.passthrough(Types.VAR_INT); // Color - convertComponent(wrapper); // Prefix - convertComponent(wrapper); // Suffix + convertTeamComponent(wrapper); // Prefix + convertTeamComponent(wrapper); // Suffix } }); @@ -361,6 +370,68 @@ private void convertComponent(final PacketWrapper wrapper) { wrapper.write(Types.COMPONENT, ComponentUtil.tagToJson(tag)); } + private void convertTeamComponent(final PacketWrapper wrapper) { + final Tag tag = wrapper.read(Types.TRUSTED_TAG); + wrapper.write(Types.COMPONENT, teamTagToJson(wrapper.user(), tag)); + } + + /** + * Processes and converts a team name, prefix or suffix, caching the result if enabled. + * The given tag may be modified, the returned json may be modified by the caller. + */ + public @Nullable JsonElement teamTagToJson(final UserConnection connection, @Nullable final Tag tag) { + return cachedTagToJson(connection, tag, teamComponentCache, ViaBackwards.getConfig().cacheTeamTextConversion()); + } + + /** + * Processes and converts a boss bar title, caching the result if enabled. + * The given tag may be modified, the returned json may be modified by the caller. + */ + public @Nullable JsonElement bossBarTagToJson(final UserConnection connection, @Nullable final Tag tag) { + return cachedTagToJson(connection, tag, bossBarComponentCache, ViaBackwards.getConfig().cacheBossBarTextConversion()); + } + + private @Nullable JsonElement cachedTagToJson(final UserConnection connection, @Nullable final Tag tag, final ConversionCache cache, final boolean enabled) { + if (tag == null || !enabled) { + translatableRewriter.processTag(connection, tag); + return ComponentUtil.tagToJson(tag); + } + + // Later protocols modify the json in place, so never hand out the cached instance + final JsonElement cached = cache.get(tag); + if (cached != null) { + return cached.deepCopy(); + } + + final Tag key = tag.copy(); + translatableRewriter.processTag(connection, tag); + final JsonElement json = ComponentUtil.tagToJson(tag); + if (json != null && !hasHoverEvent(key)) { // Hover event contents are rewritten per connection + cache.put(key, json.deepCopy()); + } + return json; + } + + private static boolean hasHoverEvent(final Tag tag) { + if (tag instanceof CompoundTag compoundTag) { + if (compoundTag.contains("hoverEvent")) { + return true; + } + for (final Tag value : compoundTag.values()) { + if (hasHoverEvent(value)) { + return true; + } + } + } else if (tag instanceof ListTag listTag) { + for (final Tag value : listTag) { + if (hasHoverEvent(value)) { + return true; + } + } + } + return false; + } + private void convertOptionalComponent(final PacketWrapper wrapper) { final Tag tag = wrapper.read(Types.TRUSTED_OPTIONAL_TAG); translatableRewriter.processTag(wrapper.user(), tag); diff --git a/common/src/main/java/com/viaversion/viabackwards/utils/ConversionCache.java b/common/src/main/java/com/viaversion/viabackwards/utils/ConversionCache.java new file mode 100644 index 000000000..b659aaea8 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/utils/ConversionCache.java @@ -0,0 +1,54 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2026 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.viabackwards.utils; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Small thread-safe cache for connection independent conversions, shared between all connections. + * Once full, it is cleared as a whole, so it never holds more than {@code maxSize} entries. + * + * @param key type, must not be modified after being put into the cache + * @param value type, must not be modified after being put into the cache + */ +public final class ConversionCache { + + private final Map cache = new ConcurrentHashMap<>(); + private final int maxSize; + + public ConversionCache(final int maxSize) { + this.maxSize = maxSize; + } + + public @Nullable V get(final K key) { + return cache.get(key); + } + + public void put(final K key, final V value) { + if (cache.size() >= maxSize) { + cache.clear(); + } + cache.put(key, value); + } + + public int size() { + return cache.size(); + } +} diff --git a/common/src/main/resources/assets/viabackwards/config.yml b/common/src/main/resources/assets/viabackwards/config.yml index 9312805df..4f8193dcd 100644 --- a/common/src/main/resources/assets/viabackwards/config.yml +++ b/common/src/main/resources/assets/viabackwards/config.yml @@ -72,3 +72,11 @@ dialog-style: # Note that this is not supported for clients below 1.21.6 right now due to missing support for # dialogs during the configuration phase. code-of-conduct-as-dialog: true +# +# Caches the text conversion of scoreboard team names, prefixes and suffixes for older clients. +# Plugins often send the same team update to every online player, so this saves a lot of work. Disable if you see wrong team texts. +cache-team-text-conversion: true +# +# Caches the text conversion of boss bar titles for older clients. +# Plugins often resend the same title to every online player, so this saves a lot of work. Disable if you see wrong boss bar titles. +cache-bossbar-text-conversion: true