From 1ed22327f6022a0823eda53025c12c713db3100a Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Sun, 21 Apr 2024 16:00:30 +0200 Subject: [PATCH] Replace string->component conversion in 1.8->1.9 (#3794) --- .../api/configuration/ViaVersionConfig.java | 7 ---- .../protocol1_9to1_8/DeathListener.java | 2 +- .../configuration/AbstractViaConfig.java | 7 ---- .../viaversion/legacy/bossbar/CommonBoss.java | 5 ++- .../protocols/base/BaseProtocol1_7.java | 6 ++- .../packets/InventoryPackets.java | 1 - .../protocol1_9to1_8/ItemRewriter.java | 2 +- .../protocol1_9to1_8/Protocol1_9To1_8.java | 40 +++---------------- .../protocol1_9to1_8/metadata/MetaIndex.java | 2 +- .../metadata/MetadataRewriter1_9To1_8.java | 7 +++- .../packets/EntityPackets.java | 2 +- .../packets/InventoryPackets.java | 2 +- .../packets/PlayerPackets.java | 15 +++---- .../packets/WorldPackets.java | 21 ++++++---- .../viaversion/util/ComponentUtil.java | 8 ++++ .../resources/assets/viaversion/config.yml | 2 - 16 files changed, 52 insertions(+), 77 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java index dff9d8721..e3f04aba4 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java +++ b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java @@ -233,13 +233,6 @@ public interface ViaVersionConfig extends Config { */ boolean isChunkBorderFix(); - /** - * Force json transform - * - * @return true if enabled - */ - boolean isForceJsonTransform(); - /** * Should we fix nbt array's in json chat messages for 1.12 clients * diff --git a/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_9to1_8/DeathListener.java b/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_9to1_8/DeathListener.java index 9939707b8..079096dbd 100644 --- a/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_9to1_8/DeathListener.java +++ b/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_9to1_8/DeathListener.java @@ -63,7 +63,7 @@ public class DeathListener extends ViaBukkitListener { wrapper.write(Type.VAR_INT, 2); // Event - Entity dead wrapper.write(Type.VAR_INT, p.getEntityId()); // Player ID wrapper.write(Type.INT, p.getEntityId()); // Entity ID - Protocol1_9To1_8.FIX_JSON.write(wrapper, msg); // Message + Protocol1_9To1_8.STRING_TO_JSON.write(wrapper, msg); // Message wrapper.scheduleSend(Protocol1_9To1_8.class); } catch (Exception e) { diff --git a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java index 40d3f3871..c0d029a38 100644 --- a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java +++ b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java @@ -63,7 +63,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf private int pistonReplacementId; private boolean chunkBorderFix; private boolean autoTeam; - private boolean forceJsonTransform; private boolean nbtArrayFix; private BlockedProtocolVersions blockedProtocolVersions; private String blockedDisconnectMessage; @@ -129,7 +128,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf pistonReplacementId = getInt("replacement-piston-id", 0); chunkBorderFix = getBoolean("chunk-border-fix", false); autoTeam = getBoolean("auto-team", true); - forceJsonTransform = getBoolean("force-json-transform", false); nbtArrayFix = getBoolean("chat-nbt-fix", true); blockedProtocolVersions = loadBlockedProtocolVersions(); blockedDisconnectMessage = getString("block-disconnect-msg", "You are using an unsupported Minecraft version!"); @@ -371,11 +369,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf return preventCollision && autoTeam; } - @Override - public boolean isForceJsonTransform() { - return forceJsonTransform; - } - @Override public boolean is1_12NBTArrayFix() { return nbtArrayFix; diff --git a/common/src/main/java/com/viaversion/viaversion/legacy/bossbar/CommonBoss.java b/common/src/main/java/com/viaversion/viaversion/legacy/bossbar/CommonBoss.java index 9690af79e..bf4574ca8 100644 --- a/common/src/main/java/com/viaversion/viaversion/legacy/bossbar/CommonBoss.java +++ b/common/src/main/java/com/viaversion/viaversion/legacy/bossbar/CommonBoss.java @@ -29,6 +29,7 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9; import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; +import com.viaversion.viaversion.util.ComponentUtil; import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; @@ -245,7 +246,7 @@ public class CommonBoss implements BossBar { wrapper.write(Type.VAR_INT, action.getId()); switch (action) { case ADD: - Protocol1_9To1_8.FIX_JSON.write(wrapper, title); + wrapper.write(Type.COMPONENT, ComponentUtil.plainToJson(title)); wrapper.write(Type.FLOAT, health); wrapper.write(Type.VAR_INT, color.getId()); wrapper.write(Type.VAR_INT, style.getId()); @@ -257,7 +258,7 @@ public class CommonBoss implements BossBar { wrapper.write(Type.FLOAT, health); break; case UPDATE_TITLE: - Protocol1_9To1_8.FIX_JSON.write(wrapper, title); + wrapper.write(Type.COMPONENT, ComponentUtil.plainToJson(title)); break; case UPDATE_STYLE: wrapper.write(Type.VAR_INT, color.getId()); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol1_7.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol1_7.java index f3dae8b1f..1c9fb0742 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol1_7.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/BaseProtocol1_7.java @@ -37,8 +37,8 @@ import com.viaversion.viaversion.protocol.ServerProtocolVersionSingleton; import com.viaversion.viaversion.protocols.base.packet.BaseClientboundPacket; import com.viaversion.viaversion.protocols.base.packet.BasePacketTypesProvider; import com.viaversion.viaversion.protocols.base.packet.BaseServerboundPacket; -import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import com.viaversion.viaversion.util.ChatColorUtil; +import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.GsonUtil; import io.netty.channel.ChannelFuture; import java.util.List; @@ -163,8 +163,10 @@ public class BaseProtocol1_7 extends AbstractProtocol { - public static final ValueTransformer FIX_JSON = new ValueTransformer(Type.COMPONENT) { + + public static final ValueTransformer STRING_TO_JSON = new ValueTransformer(Type.COMPONENT) { @Override public JsonElement transform(PacketWrapper wrapper, String line) { - return fixJson(line); + return ComponentUtil.convertJsonOrEmpty(line, SerializerVersion.V1_8, SerializerVersion.V1_9); } }; private final MetadataRewriter1_9To1_8 metadataRewriter = new MetadataRewriter1_9To1_8(this); @@ -64,35 +65,6 @@ public class Protocol1_9To1_8 extends AbstractProtocol { @@ -127,8 +129,9 @@ public class MetadataRewriter1_9To1_8 extends EntityRewriter { @@ -61,15 +62,15 @@ public class PlayerPackets { protocol.registerClientbound(ClientboundPackets1_8.TAB_LIST, new PacketHandlers() { @Override public void register() { - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 0 - Header - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 1 - Footer + map(Type.STRING, Protocol1_9To1_8.STRING_TO_JSON); // 0 - Header + map(Type.STRING, Protocol1_9To1_8.STRING_TO_JSON); // 1 - Footer } }); protocol.registerClientbound(ClientboundPackets1_8.DISCONNECT, new PacketHandlers() { @Override public void register() { - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 0 - Reason + map(Type.STRING, Protocol1_9To1_8.STRING_TO_JSON); // 0 - Reason } }); @@ -81,7 +82,7 @@ public class PlayerPackets { handler(wrapper -> { int action = wrapper.get(Type.VAR_INT, 0); if (action == 0 || action == 1) { - Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING)); + Protocol1_9To1_8.STRING_TO_JSON.write(wrapper, wrapper.read(Type.STRING)); } }); // Everything else is handled. @@ -248,13 +249,13 @@ public class PlayerPackets { wrapper.passthrough(Type.VAR_INT); // ping String displayName = wrapper.read(Type.OPTIONAL_STRING); wrapper.write(Type.OPTIONAL_COMPONENT, displayName != null ? - Protocol1_9To1_8.FIX_JSON.transform(wrapper, displayName) : null); + Protocol1_9To1_8.STRING_TO_JSON.transform(wrapper, displayName) : null); } else if ((action == 1) || (action == 2)) { // update gamemode || update latency wrapper.passthrough(Type.VAR_INT); } else if (action == 3) { // update display name String displayName = wrapper.read(Type.OPTIONAL_STRING); wrapper.write(Type.OPTIONAL_COMPONENT, displayName != null ? - Protocol1_9To1_8.FIX_JSON.transform(wrapper, displayName) : null); + Protocol1_9To1_8.STRING_TO_JSON.transform(wrapper, displayName) : null); } } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java index 6eedc83f7..39ac43821 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -45,6 +45,7 @@ import com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.Effect; import com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.SoundEffect; import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks; import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; +import com.viaversion.viaversion.util.ComponentUtil; import java.util.ArrayList; import java.util.Optional; @@ -54,10 +55,12 @@ public class WorldPackets { @Override public void register() { map(Type.POSITION1_8); // 0 - Sign Position - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 1 - Sign Line (json) - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 2 - Sign Line (json) - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 3 - Sign Line (json) - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 4 - Sign Line (json) + handler(wrapper -> { + for (int i = 0; i < 4; i++) { + final String line = wrapper.read(Type.STRING); // Should be Type.COMPONENT but would break in some cases + Protocol1_9To1_8.STRING_TO_JSON.write(wrapper, line); + } + }); } }); @@ -245,10 +248,12 @@ public class WorldPackets { @Override public void register() { map(Type.POSITION1_8); // 0 - Sign Position - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 1 - Sign Line (json) - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 2 - Sign Line (json) - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 3 - Sign Line (json) - map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 4 - Sign Line (json) + handler(wrapper -> { + for (int i = 0; i < 4; i++) { + final String line = wrapper.read(Type.STRING); + wrapper.write(Type.COMPONENT, ComponentUtil.plainToJson(line)); + } + }); } }); 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 370d83d7f..f64ce2d3d 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/ComponentUtil.java @@ -100,6 +100,14 @@ public final class ComponentUtil { return json != null ? convert(from, to, from.toComponent(json)) : null; } + public static @Nullable JsonElement convertJsonOrEmpty(@Nullable final String json, final SerializerVersion from, final SerializerVersion to) { + final ATextComponent component = from.toComponent(json); + if (component == null) { + return emptyJsonComponent(); + } + return to.toJson(component); + } + private static JsonElement convert(final SerializerVersion from, final SerializerVersion to, final ATextComponent component) { if (from.ordinal() >= SerializerVersion.V1_16.ordinal() && to.ordinal() < SerializerVersion.V1_16.ordinal()) { // Convert hover event to legacy format diff --git a/common/src/main/resources/assets/viaversion/config.yml b/common/src/main/resources/assets/viaversion/config.yml index 7211ec81e..05cd26e8f 100644 --- a/common/src/main/resources/assets/viaversion/config.yml +++ b/common/src/main/resources/assets/viaversion/config.yml @@ -219,8 +219,6 @@ replace-pistons: false replacement-piston-id: 0 # Fix 1.9+ clients not rendering the far away chunks and improve chunk rendering when moving fast (Increases network usage and decreases client fps slightly) chunk-border-fix: false -# Force the string -> json transform -force-json-transform: false # Minimize the cooldown animation in 1.8 servers minimize-cooldown: true # Allows 1.9+ left-handedness (main hand) on 1.8 servers