From 587a5e487fd0305b1c8d92a13e760e1f149f8642 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 18 Mar 2023 10:02:23 +0100 Subject: [PATCH] Improve 1.13->1.12 translatable handling --- build.gradle.kts | 2 +- .../Protocol1_12_2To1_13.java | 68 ++++++++++++++++--- .../packets/BlockItemPackets1_13.java | 2 +- .../packets/EntityPackets1_13.java | 43 ++++-------- .../packets/PlayerPacket1_13.java | 17 +++-- 5 files changed, 83 insertions(+), 49 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 6c85e6e5..34df0265 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { allprojects { group = "com.viaversion" - version = "4.6.1" + version = "4.6.2-SNAPSHOT" description = "Allow older clients to join newer server versions." } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java index 018b1e0e..68cb4166 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java @@ -35,19 +35,43 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types; import com.viaversion.viaversion.data.entity.EntityTrackerBase; +import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; +import com.viaversion.viaversion.libs.gson.JsonParser; +import com.viaversion.viaversion.libs.kyori.adventure.text.Component; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; +import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import org.checkerframework.checker.nullness.qual.Nullable; public class Protocol1_12_2To1_13 extends BackwardsProtocol { public static final BackwardsMappings MAPPINGS = new BackwardsMappings(); private final EntityPackets1_13 entityRewriter = new EntityPackets1_13(this); private final BlockItemPackets1_13 blockItemPackets = new BlockItemPackets1_13(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter(this) { + @Override + protected void handleTranslate(JsonObject root, String translate) { + String newTranslate = mappedTranslationKey(translate); + if (newTranslate != null || (newTranslate = getMappingData().getTranslateMappings().get(translate)) != null) { + root.addProperty("translate", newTranslate); + } + } + }; + private final TranslatableRewriter translatableToLegacyRewriter = new TranslatableRewriter(this) { + @Override + protected void handleTranslate(JsonObject root, String translate) { + String newTranslate = mappedTranslationKey(translate); + if (newTranslate != null || (newTranslate = getMappingData().getTranslateMappings().get(translate)) != null) { + root.addProperty("translate", Protocol1_13To1_12_2.MAPPINGS.getMojangTranslation().getOrDefault(newTranslate, newTranslate)); + } + } + }; public Protocol1_12_2To1_13() { super(ClientboundPackets1_13.class, ClientboundPackets1_12_1.class, ServerboundPackets1_13.class, ServerboundPackets1_12_1.class); @@ -61,15 +85,6 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter(this) { - @Override - protected void handleTranslate(JsonObject root, String translate) { - String newTranslate = mappedTranslationKey(translate); - if (newTranslate != null || (newTranslate = getMappingData().getTranslateMappings().get(translate)) != null) { - root.addProperty("translate", newTranslate); - } - } - }; translatableRewriter.registerPing(); translatableRewriter.registerBossBar(ClientboundPackets1_13.BOSSBAR); translatableRewriter.registerComponentPacket(ClientboundPackets1_13.CHAT_MESSAGE); @@ -126,4 +141,39 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol getTranslatableRewriter() { + return translatableRewriter; + } + + public String jsonToLegacy(String value) { + if (value.isEmpty()) { + return ""; + } + + try { + return jsonToLegacy(JsonParser.parseString(value)); + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + } + + public String jsonToLegacy(@Nullable JsonElement value) { + if (value == null || value.isJsonNull()) { + return ""; + } + + translatableToLegacyRewriter.processText(value); + + try { + Component component = ChatRewriter.HOVER_GSON_SERIALIZER.deserializeFromTree(value); + return LegacyComponentSerializer.legacySection().serialize(component); + } catch (Exception e) { + ViaBackwards.getPlatform().getLogger().warning("Error converting json text to legacy: " + value); + e.printStackTrace(); + } + return ""; + } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java index d95eb4e6..b2580f0e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java @@ -550,7 +550,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit StringTag name = display.get("Name"); if (name != null) { display.put(extraNbtTag + "|Name", new StringTag(name.getValue())); - name.setValue(ChatRewriter.jsonToLegacyText(name.getValue())); + name.setValue(protocol.jsonToLegacy(name.getValue())); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java index fc2cae92..d575f9e5 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java @@ -37,9 +37,9 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; import com.viaversion.viaversion.api.type.types.version.Types1_12; import com.viaversion.viaversion.api.type.types.version.Types1_13; +import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import java.util.Optional; @@ -277,37 +277,22 @@ public class EntityPackets1_13 extends LegacyEntityRewriter { int typeId = meta.metaType().typeId(); - - // Rewrite optional chat to string - if (typeId == 5) { - // Json -> Legacy is done below - meta.setTypeAndValue(MetaType1_12.String, meta.getValue() != null ? meta.getValue().toString() : ""); - } - - // Rewrite items - else if (typeId == 6) { + if (typeId == 4) { + JsonElement element = meta.value(); + protocol.getTranslatableRewriter().processText(element); + meta.setMetaType(MetaType1_12.Chat); + } else if (typeId == 5) { + // Rewrite optional chat to string + JsonElement element = meta.value(); + meta.setTypeAndValue(MetaType1_12.String, protocol.jsonToLegacy(element)); + } else if (typeId == 6) { Item item = (Item) meta.getValue(); meta.setTypeAndValue(MetaType1_12.Slot, protocol.getItemRewriter().handleItemToClient(item)); - } - - // Discontinue particles - else if (typeId == 15) { + } else if (typeId == 15) { + // Discontinue particles event.cancel(); - } - - // Rewrite to 1.12 ids - else if (typeId > 5) { - meta.setMetaType(MetaType1_12.byId( - typeId - 1 - )); - } - }); - - // Rewrite Custom Name from Chat to String - filter().filterFamily(Entity1_13Types.EntityType.ENTITY).index(2).handler((event, meta) -> { - String value = meta.getValue().toString(); - if (!value.isEmpty()) { - meta.setValue(ChatRewriter.jsonToLegacyText(value)); + } else { + meta.setMetaType(MetaType1_12.byId(typeId > 5 ? typeId - 1 : typeId)); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java index c9bebd98..e124d1f6 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java @@ -35,7 +35,6 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; @@ -202,13 +201,13 @@ public class PlayerPacket1_13 extends RewriterBase { handler(wrapper -> { byte mode = wrapper.get(Type.BYTE, 0); if (mode == 0 || mode == 2) { - String value = wrapper.read(Type.COMPONENT).toString(); - value = ChatRewriter.jsonToLegacyText(value); - if (value.length() > 32) { - value = value.substring(0, 32); + JsonElement value = wrapper.read(Type.COMPONENT); + String legacyValue = protocol.jsonToLegacy(value); + if (legacyValue.length() > 32) { + legacyValue = legacyValue.substring(0, 32); } - wrapper.write(Type.STRING, value); + wrapper.write(Type.STRING, legacyValue); int type = wrapper.read(Type.VAR_INT); wrapper.write(Type.STRING, type == 1 ? "hearts" : "integer"); } @@ -225,7 +224,7 @@ public class PlayerPacket1_13 extends RewriterBase { byte action = wrapper.get(Type.BYTE, 0); if (action == 0 || action == 2) { JsonElement displayName = wrapper.read(Type.COMPONENT); - String legacyTextDisplayName = displayName == null || displayName.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(displayName.toString()); + String legacyTextDisplayName = protocol.jsonToLegacy(displayName); legacyTextDisplayName = ChatUtil.removeUnusedColor(legacyTextDisplayName, 'f'); if (legacyTextDisplayName.length() > 32) { legacyTextDisplayName = legacyTextDisplayName.substring(0, 32); @@ -244,7 +243,7 @@ public class PlayerPacket1_13 extends RewriterBase { JsonElement prefixComponent = wrapper.read(Type.COMPONENT); JsonElement suffixComponent = wrapper.read(Type.COMPONENT); - String prefix = prefixComponent == null || prefixComponent.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(prefixComponent.toString()); + String prefix = protocol.jsonToLegacy(prefixComponent); if (ViaBackwards.getConfig().addTeamColorTo1_13Prefix()) { prefix += "§" + (colour > -1 && colour <= 15 ? Integer.toHexString(colour) : "r"); } @@ -253,7 +252,7 @@ public class PlayerPacket1_13 extends RewriterBase { if (prefix.length() > 16) prefix = prefix.substring(0, 16); if (prefix.endsWith("§")) prefix = prefix.substring(0, prefix.length() - 1); - String suffix = suffixComponent == null || suffixComponent.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(suffixComponent.toString()); + String suffix = protocol.jsonToLegacy(suffixComponent); suffix = ChatUtil.removeUnusedColor(suffix, '\0'); // Don't remove white coloring if (suffix.length() > 16) suffix = suffix.substring(0, 16); if (suffix.endsWith("§")) suffix = suffix.substring(0, suffix.length() - 1);