From 34fa4f85dbcf768cf09c8f23f3eb053249dc2bb6 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 5 May 2022 11:20:03 +0200 Subject: [PATCH] More or less handle new chat type formats --- .../viabackwards/ViaBackwardsConfig.java | 9 +++ .../viabackwards/api/ViaBackwardsConfig.java | 4 ++ .../Protocol1_18_2To1_19.java | 67 +++++++++++-------- .../resources/assets/viabackwards/config.yml | 10 ++- .../data/translation-mappings.json | 2 +- 5 files changed, 62 insertions(+), 30 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java b/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java index e97e7d86..79117e79 100644 --- a/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java +++ b/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java @@ -18,10 +18,12 @@ package com.viaversion.viabackwards; import com.viaversion.viaversion.util.Config; +import org.checkerframework.checker.nullness.qual.Nullable; import java.io.File; import java.net.URL; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -33,6 +35,7 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback private boolean alwaysShowOriginalMobName; private boolean fix1_13FormattedInventoryTitles; private boolean handlePingsAsInvAcknowledgements; + private Map chatTypeFormats; public ViaBackwardsConfig(File configFile) { super(configFile); @@ -51,6 +54,7 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback fix1_13FormattedInventoryTitles = getBoolean("fix-formatted-inventory-titles", true); alwaysShowOriginalMobName = getBoolean("always-show-original-mob-name", true); handlePingsAsInvAcknowledgements = getBoolean("handle-pings-as-inv-acknowledgements", false); + chatTypeFormats = get("chat-types", Map.class, new HashMap()); } @Override @@ -83,6 +87,11 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback return handlePingsAsInvAcknowledgements || Boolean.getBoolean("com.viaversion.handlePingsAsInvAcknowledgements"); } + @Override + public @Nullable String chatTypeFormat(final String translationKey) { + return chatTypeFormats.get(translationKey); + } + @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 57150d3f..11ea3c9f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java @@ -17,6 +17,8 @@ */ package com.viaversion.viabackwards.api; +import org.checkerframework.checker.nullness.qual.Nullable; + public interface ViaBackwardsConfig { /** @@ -50,4 +52,6 @@ public interface ViaBackwardsConfig { boolean alwaysShowOriginalMobName(); boolean handlePingsAsInvAcknowledgements(); + + @Nullable String chatTypeFormat(String translationKey); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java index 35790624..8d67a8e1 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java @@ -27,6 +27,7 @@ import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.packets.EntityP import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19Types; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.rewriter.EntityRewriter; @@ -35,6 +36,7 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.kyori.adventure.text.Component; +import com.viaversion.viaversion.libs.kyori.adventure.text.TextReplacementConfig; import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; @@ -53,6 +55,7 @@ import java.util.UUID; public final class Protocol1_18_2To1_19 extends BackwardsProtocol { public static final BackwardsMappings MAPPINGS = new BackwardsMappings(); + private static final String[] CHAT_KEYS = {"chat.type.text", null, null, "chat.type.announcement", "commands.message.display.incoming", "chat.type.team.text", "chat.type.emote", null}; private static final UUID ZERO_UUID = new UUID(0, 0); private static final byte[] EMPTY_BYTES = new byte[0]; private final EntityPackets1_19 entityRewriter = new EntityPackets1_19(this); @@ -177,35 +180,21 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol { - int type = wrapper.read(Type.VAR_INT); - if (type > 2) { - type = 0; // Chat - } - wrapper.write(Type.BYTE, (byte) type); - }); + map(Type.VAR_INT, Type.BYTE); // Chat type map(Type.UUID); // Sender handler(wrapper -> { final JsonElement senderName = wrapper.read(Type.COMPONENT); - wrapper.read(Type.COMPONENT); // Team name - wrapper.read(Type.LONG); // Timestamp - wrapper.read(Type.LONG); // Salt - wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); // Signature - + final JsonElement teamName = wrapper.read(Type.COMPONENT); final JsonElement element = wrapper.get(Type.COMPONENT, 0); - translatableRewriter.processText(element); - - Component component = GsonComponentSerializer.gson().deserializeFromTree(element); - component = Component.text().append(Component.text(" ")).append(component).build(); - wrapper.set(Type.COMPONENT, 0, GsonComponentSerializer.gson().serializeToTree(component)); + handleChatType(wrapper, senderName, teamName, element); }); + read(Type.LONG); // Timestamp + read(Type.LONG); // Salt + read(Type.BYTE_ARRAY_PRIMITIVE); // Signature } }); @@ -213,15 +202,9 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol { - int type = wrapper.read(Type.VAR_INT); - if (type > 2) { - type = 0; // Chat - } - wrapper.write(Type.BYTE, (byte) type); - }); + map(Type.VAR_INT, Type.BYTE); // Chat type create(Type.UUID, ZERO_UUID); // Sender - handler(wrapper -> translatableRewriter.processText(wrapper.get(Type.COMPONENT, 0))); + handler(wrapper -> handleChatType(wrapper, null, null, wrapper.get(Type.COMPONENT, 0))); } }); @@ -304,4 +287,32 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol 2) { + wrapper.set(Type.BYTE, 0, (byte) 0); // Chat type + } + + final String key = CHAT_KEYS[type]; + if (key != null) { + Component component = Component.text(ViaBackwards.getConfig().chatTypeFormat(key)); + if (key.equals("chat.type.team.text")) { + //TODO team message broken...? + component = component.replaceText(replace(teamName)); + } + if (senderName != null) { + component = component.replaceText(replace(senderName)); + } + component = component.replaceText(replace(text)); + wrapper.set(Type.COMPONENT, 0, GsonComponentSerializer.gson().serializeToTree(component)); + } + } } diff --git a/common/src/main/resources/assets/viabackwards/config.yml b/common/src/main/resources/assets/viabackwards/config.yml index b9ce0f60..60b68c48 100644 --- a/common/src/main/resources/assets/viabackwards/config.yml +++ b/common/src/main/resources/assets/viabackwards/config.yml @@ -19,4 +19,12 @@ fix-formatted-inventory-titles: true # # Sends inventory acknowledgement packets to act as a replacement for ping packets for sub 1.17 clients. # This only takes effect for ids in the short range. Useful for anticheat compatibility. -handle-pings-as-inv-acknowledgements: false \ No newline at end of file +handle-pings-as-inv-acknowledgements: false +# +# Chat type formats used for sub 1.19 clients. +chat-types: + "chat.type.text": "<%s> %s" + "chat.type.announcement": "[%s] %s" + "commands.message.display.incoming": "%s whispers to you: %s" + "chat.type.team.text": "%s <%s> %s" + "chat.type.emote": "* %s %s" \ No newline at end of file diff --git a/common/src/main/resources/assets/viabackwards/data/translation-mappings.json b/common/src/main/resources/assets/viabackwards/data/translation-mappings.json index b49dcf47..63868468 100644 --- a/common/src/main/resources/assets/viabackwards/data/translation-mappings.json +++ b/common/src/main/resources/assets/viabackwards/data/translation-mappings.json @@ -71,7 +71,7 @@ "item.minecraft.mangrove_boat": "Mangrove Boat", "item.minecraft.recovery_compass": "Recovery Compass", "item.minecraft.music_disc_5": "Music Disc", - "item.minecraft.music_disc_5.desc": "Samuel �berg - 5", + "item.minecraft.music_disc_5.desc": "Samuel Åberg - 5", "item.minecraft.mangrove_chest_boat": "Mangrove Boat with Chest", "item.minecraft.disc_fragment_5": "Disc Fragment", "item.minecraft.disc_fragment_5.desc": "Music Disc - 5",