Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,6 +76,8 @@ protected void handleTranslate(JsonObject root, String translate) {
}
};

private final ConversionCache<String, String> teamLegacyTextCache = new ConversionCache<>(2048);

public Protocol1_13To1_12_2() {
super(ClientboundPackets1_13.class, ClientboundPackets1_12_1.class, ServerboundPackets1_13.class, ServerboundPackets1_12_1.class);
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -70,6 +76,8 @@ public final class Protocol1_20_3To1_20_2 extends BackwardsProtocol<ClientboundP
private final JsonNBTComponentRewriter<ClientboundPacket1_20_3> translatableRewriter = new JsonNBTComponentRewriter<>(this, ComponentRewriterBase.ReadType.NBT);
private final TagRewriter<ClientboundPacket1_20_3> tagRewriter = new TagRewriter<>(this);
private final BlockRewriter<ClientboundPacket1_20_3> blockRewriter = new BlockPacketRewriter1_20_3(this);
private final ConversionCache<Tag, JsonElement> teamComponentCache = new ConversionCache<>(2048);
private final ConversionCache<Tag, JsonElement> 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);
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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
}
});

Expand Down Expand Up @@ -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<Tag, JsonElement> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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 <K> key type, must not be modified after being put into the cache
* @param <V> value type, must not be modified after being put into the cache
*/
public final class ConversionCache<K, V> {

private final Map<K, V> 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();
}
}
8 changes: 8 additions & 0 deletions common/src/main/resources/assets/viabackwards/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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