From b84cda54c3581fd185b9027f9caa97b0b5c17e02 Mon Sep 17 00:00:00 2001 From: Gero Date: Thu, 9 Jun 2022 20:49:27 +0200 Subject: [PATCH] Fully implement chat types (#455) --- .../Protocol1_18_2To1_19.java | 104 ++++++++++++------ .../packets/EntityPackets1_19.java | 3 +- .../storage/DimensionRegistryStorage.java | 8 +- 3 files changed, 73 insertions(+), 42 deletions(-) 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 99187789..f10b6f93 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 @@ -41,6 +41,9 @@ 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.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17; @@ -53,6 +56,7 @@ import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; import java.time.Instant; +import java.util.Map; import java.util.UUID; public final class Protocol1_18_2To1_19 extends BackwardsProtocol { @@ -308,44 +312,72 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol handler : handlers) { + byte oldId; + switch (handler.getKey()) { + case "chat": + oldId = 1; + break; + case "overlay": + oldId = 2; + break; + default: + continue; + } + + CompoundTag decoration = ((CompoundTag) handler.getValue()).get("decoration"); + + JsonElement formattedMessage; + if (decoration != null) { + String translationKey = (String) decoration.get("translation_key").getValue(); + String rawTranslation = ViaBackwards.getConfig().chatTypeFormat(translationKey); + if (rawTranslation == null) { + ViaBackwards.getPlatform().getLogger().warning("Missing chat type translation for key " + translationKey); + continue; + } + Component component = Component.text(rawTranslation); + ListTag parameters = decoration.get("parameters"); + if (parameters != null) for (Tag element : parameters) { + switch ((String) element.getValue()) { + case "sender": + component = component.replaceText(replace(senderName)); + break; + case "content": + component = component.replaceText(replace(message)); + break; + case "team_name": + Preconditions.checkNotNull(teamName, "Team name is null"); + component = component.replaceText(replace(teamName)); + break; + default: + ViaBackwards.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue()); + } + } + formattedMessage = GsonComponentSerializer.gson().serializeToTree(component); + } else { + formattedMessage = message; + } + + if (!handled) { + handled = true; + wrapper.set(Type.BYTE, 0, oldId); + wrapper.set(Type.COMPONENT, 0, formattedMessage); + } else { + PacketWrapper extra = wrapper.create(ClientboundPackets1_18.CHAT_MESSAGE); + extra.write(Type.COMPONENT, formattedMessage); + extra.write(Type.BYTE, oldId); + extra.write(Type.UUID, wrapper.get(Type.UUID, 0)); + } } - final String key = CHAT_KEYS[chatTypeId]; - if (key != null) { - final String chatFormat = ViaBackwards.getConfig().chatTypeFormat(key); - if (chatFormat == null) { - wrapper.cancel(); - ViaBackwards.getPlatform().getLogger().severe("Chat type format " + key + " is not defined under chat-types in the ViaBackwards config."); - return; - } - - Component component = Component.text(chatFormat); - if (key.equals("chat.type.team.text")) { - Preconditions.checkNotNull(teamName, "Team name is null"); - 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)); - } + if (!handled) wrapper.cancel(); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/packets/EntityPackets1_19.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/packets/EntityPackets1_19.java index c6172779..40702364 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/packets/EntityPackets1_19.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/packets/EntityPackets1_19.java @@ -162,8 +162,7 @@ public final class EntityPackets1_19 extends EntityRewriter dimensions = new HashMap<>(); - private final Int2ObjectMap chatTypes = new Int2ObjectOpenHashMap<>(); + private final Int2ObjectMap chatTypes = new Int2ObjectOpenHashMap<>(); public @Nullable CompoundTag dimension(final String dimensionKey) { final CompoundTag compoundTag = dimensions.get(dimensionKey); @@ -40,12 +40,12 @@ public final class DimensionRegistryStorage implements StorableObject { dimensions.put(dimensionKey, dimension); } - public @Nullable String chatTypeKey(final int id) { + public @Nullable CompoundTag chatType(final int id) { return chatTypes.get(id); } - public void addChatType(final int id, final String chatTypeKey) { - chatTypes.put(id, chatTypeKey); + public void addChatType(final int id, final CompoundTag chatType) { + chatTypes.put(id, chatType); } public void clear() {