From 7ac7ea485fc4dabf0ff8b58a00d3944fb0c10e1d Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 21 Apr 2024 23:01:58 +0200 Subject: [PATCH] Add UserConnection to component rewriters as well --- .../Protocol1_12To1_11_1.java | 2 +- .../rewriter/TranslateRewriter.java | 13 ++-- .../Protocol1_13To1_12_2.java | 12 ++-- .../data/ComponentRewriter1_13.java | 5 +- .../blockentities/CommandBlockHandler.java | 2 +- .../packets/InventoryPackets.java | 2 +- .../Protocol1_16To1_15_2.java | 2 +- .../data/TranslationMappings.java | 5 +- .../packets/InventoryPackets.java | 2 +- .../packets/WorldPackets.java | 12 ++-- .../rewriter/ComponentRewriter.java | 71 ++++++++++--------- 11 files changed, 67 insertions(+), 61 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java index b7b0ab2f1..88992adae 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java @@ -101,7 +101,7 @@ public class Protocol1_12To1_11_1 extends AbstractProtocol componentRewriter.processText(wrapper.passthrough(Type.COMPONENT))); + registerClientbound(State.LOGIN, 0x00, 0x00, wrapper -> componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT))); registerClientbound(State.STATUS, 0x00, 0x00, new PacketHandlers() { @Override @@ -296,7 +296,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol componentRewriter.processText(wrapper.passthrough(Type.COMPONENT))); // Title + handler(wrapper -> componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT))); // Title } }); @@ -511,8 +511,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol(this).registerSound(ClientboundPackets1_12_1.SOUND); registerClientbound(ClientboundPackets1_12_1.TAB_LIST, wrapper -> { - componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)); - componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)); + componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)); + componentRewriter.processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)); }); registerClientbound(ClientboundPackets1_12_1.ADVANCEMENTS, wrapper -> { @@ -528,8 +528,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol extends Comp } @Override - protected void handleHoverEvent(JsonObject hoverEvent) { - super.handleHoverEvent(hoverEvent); + protected void handleHoverEvent(UserConnection connection, JsonObject hoverEvent) { + super.handleHoverEvent(connection, hoverEvent); final String action = hoverEvent.getAsJsonPrimitive("action").getAsString(); if (!action.equals("show_item")) return; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/CommandBlockHandler.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/CommandBlockHandler.java index 89653af51..252053805 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/CommandBlockHandler.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/CommandBlockHandler.java @@ -41,7 +41,7 @@ public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandl StringTag out = tag.getStringTag("LastOutput"); if (out != null) { JsonElement value = JsonParser.parseString(out.getValue()); - protocol.getComponentRewriter().processText(value); + protocol.getComponentRewriter().processText(user, value); out.setValue(value.toString()); } return -1; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java index c02034760..ee2c51957 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java @@ -72,7 +72,7 @@ public class InventoryPackets extends ItemRewriter { - componentRewriter.processText(wrapper.get(Type.COMPONENT, 0)); + componentRewriter.processText(wrapper.user(), wrapper.get(Type.COMPONENT, 0)); wrapper.write(Type.UUID, ZERO_UUID); // Sender uuid - always send as 'system' }); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/TranslationMappings.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/TranslationMappings.java index 3ffb032cc..5247dc3c5 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/TranslationMappings.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/TranslationMappings.java @@ -20,6 +20,7 @@ package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import com.viaversion.viaversion.rewriter.ComponentRewriter; @@ -67,8 +68,8 @@ public class TranslationMappings extends ComponentRewriter pages = tag.getListTag("pages", StringTag.class); if (pages != null) { for (StringTag pageTag : pages) { - pageTag.setValue(protocol.getComponentRewriter().processText(pageTag.getValue()).toString()); + pageTag.setValue(protocol.getComponentRewriter().processText(connection, pageTag.getValue()).toString()); } } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java index b382975f3..207a1be88 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java @@ -23,6 +23,7 @@ import com.github.steveice10.opennbt.tag.builtin.LongArrayTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.google.gson.JsonElement; +import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; @@ -35,6 +36,7 @@ import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.util.CompactArrayUtil; +import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.UUIDUtil; import java.util.Map; import java.util.UUID; @@ -87,7 +89,7 @@ public class WorldPackets { if (chunk.getBlockEntities() == null) return; for (CompoundTag blockEntity : chunk.getBlockEntities()) { - handleBlockEntity(protocol, blockEntity); + handleBlockEntity(protocol, wrapper.user(), blockEntity); } }); @@ -95,17 +97,17 @@ public class WorldPackets { wrapper.passthrough(Type.POSITION1_14); // Position wrapper.passthrough(Type.UNSIGNED_BYTE); // Action CompoundTag tag = wrapper.passthrough(Type.NAMED_COMPOUND_TAG); - handleBlockEntity(protocol, tag); + handleBlockEntity(protocol, wrapper.user(), tag); }); blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001); } - private static void handleBlockEntity(Protocol1_16To1_15_2 protocol, CompoundTag compoundTag) { + private static void handleBlockEntity(Protocol1_16To1_15_2 protocol, UserConnection connection, CompoundTag compoundTag) { StringTag idTag = compoundTag.getStringTag("id"); if (idTag == null) return; - String id = idTag.getValue(); + String id = Key.namespaced(idTag.getValue()); if (id.equals("minecraft:conduit")) { Tag targetUuidTag = compoundTag.remove("target_uuid"); if (!(targetUuidTag instanceof StringTag)) return; @@ -131,7 +133,7 @@ public class WorldPackets { for (int i = 1; i <= 4; i++) { StringTag line = compoundTag.getStringTag("Text" + i); if (line != null) { - JsonElement text = protocol.getComponentRewriter().processText(line.getValue()); + JsonElement text = protocol.getComponentRewriter().processText(connection, line.getValue()); compoundTag.putString("Text" + i, text.toString()); } } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java index 125dcc3e4..bf81bc054 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java @@ -28,6 +28,7 @@ import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSyntaxException; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -84,7 +85,7 @@ public class ComponentRewriter { if (wrapper.passthrough(Type.VAR_INT) == 2) { wrapper.passthrough(Type.VAR_INT); wrapper.passthrough(Type.INT); - processText(wrapper.passthrough(Type.COMPONENT)); + processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)); } }); } @@ -96,14 +97,14 @@ public class ComponentRewriter { protocol.registerClientbound(packetType, wrapper -> { final int action = wrapper.passthrough(Type.VAR_INT); if (action >= 0 && action <= 2) { - processText(wrapper.passthrough(Type.COMPONENT)); + processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)); } }); } public void registerPing() { // Always json - protocol.registerClientbound(State.LOGIN, ClientboundLoginPackets.LOGIN_DISCONNECT, wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); + protocol.registerClientbound(State.LOGIN, ClientboundLoginPackets.LOGIN_DISCONNECT, wrapper -> processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT))); } public void registerLegacyOpenWindow(final C packetType) { @@ -112,7 +113,7 @@ public class ComponentRewriter { public void register() { map(Type.UNSIGNED_BYTE); // Id map(Type.STRING); // Window Type - handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); + handler(wrapper -> processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT))); } }); } @@ -141,7 +142,7 @@ public class ComponentRewriter { public void register() { map(Type.VAR_INT); map(Type.INT); - handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); + handler(wrapper -> processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT))); } }); } @@ -159,18 +160,18 @@ public class ComponentRewriter { public void passthroughAndProcess(final PacketWrapper wrapper) throws Exception { switch (type) { case JSON: - processText(wrapper.passthrough(Type.COMPONENT)); + processText(wrapper.user(), wrapper.passthrough(Type.COMPONENT)); break; case NBT: - processTag(wrapper.passthrough(Type.TAG)); + processTag(wrapper.user(), wrapper.passthrough(Type.TAG)); break; } } - public JsonElement processText(final String value) { + public JsonElement processText(final UserConnection connection, final String value) { try { final JsonElement root = JsonParser.parseString(value); - processText(root); + processText(connection, root); return root; } catch (final JsonSyntaxException e) { if (Via.getManager().isDebug()) { @@ -182,43 +183,43 @@ public class ComponentRewriter { } } - public void processText(final JsonElement element) { + public void processText(final UserConnection connection, final JsonElement element) { if (element == null || element.isJsonNull()) { return; } if (element.isJsonArray()) { - processJsonArray(element.getAsJsonArray()); + processJsonArray(connection, element.getAsJsonArray()); } else if (element.isJsonObject()) { - processJsonObject(element.getAsJsonObject()); + processJsonObject(connection, element.getAsJsonObject()); } } - protected void processJsonArray(final JsonArray array) { + protected void processJsonArray(final UserConnection connection, final JsonArray array) { for (final JsonElement jsonElement : array) { - processText(jsonElement); + processText(connection, jsonElement); } } - protected void processJsonObject(final JsonObject object) { + protected void processJsonObject(final UserConnection connection, final JsonObject object) { final JsonElement translate = object.get("translate"); if (translate != null && translate.isJsonPrimitive()) { handleTranslate(object, translate.getAsString()); final JsonElement with = object.get("with"); if (with != null && with.isJsonArray()) { - processJsonArray(with.getAsJsonArray()); + processJsonArray(connection, with.getAsJsonArray()); } } final JsonElement extra = object.get("extra"); if (extra != null && extra.isJsonArray()) { - processJsonArray(extra.getAsJsonArray()); + processJsonArray(connection, extra.getAsJsonArray()); } final JsonElement hoverEvent = object.get("hoverEvent"); if (hoverEvent != null && hoverEvent.isJsonObject()) { - handleHoverEvent(hoverEvent.getAsJsonObject()); + handleHoverEvent(connection, hoverEvent.getAsJsonObject()); } } @@ -226,7 +227,7 @@ public class ComponentRewriter { // To override if needed } - protected void handleHoverEvent(final JsonObject hoverEvent) { + protected void handleHoverEvent(final UserConnection connection, final JsonObject hoverEvent) { // To override if needed (don't forget to call super) final JsonPrimitive actionElement = hoverEvent.getAsJsonPrimitive("action"); if (!actionElement.isString()) { @@ -236,11 +237,11 @@ public class ComponentRewriter { final String action = actionElement.getAsString(); if (action.equals("show_text")) { final JsonElement value = hoverEvent.get("value"); - processText(value != null ? value : hoverEvent.get("contents")); + processText(connection, value != null ? value : hoverEvent.get("contents")); } else if (action.equals("show_entity")) { final JsonElement contents = hoverEvent.get("contents"); if (contents != null && contents.isJsonObject()) { - processText(contents.getAsJsonObject().get("name")); + processText(connection, contents.getAsJsonObject().get("name")); } } } @@ -248,51 +249,51 @@ public class ComponentRewriter { // ----------------------------------------------------------------------- // Tag methods - public void processTag(@Nullable final Tag tag) { + public void processTag(final UserConnection connection, @Nullable final Tag tag) { if (tag == null) { return; } if (tag instanceof ListTag) { - processListTag((ListTag) tag); + processListTag(connection, (ListTag) tag); } else if (tag instanceof CompoundTag) { - processCompoundTag((CompoundTag) tag); + processCompoundTag(connection, (CompoundTag) tag); } } - private void processListTag(final ListTag tag) { + private void processListTag(final UserConnection connection, final ListTag tag) { for (final Tag entry : tag) { - processTag(entry); + processTag(connection, entry); } } - protected void processCompoundTag(final CompoundTag tag) { + protected void processCompoundTag(final UserConnection connection, final CompoundTag tag) { final StringTag translate = tag.getStringTag("translate"); if (translate != null) { - handleTranslate(tag, translate); + handleTranslate(connection, tag, translate); final ListTag with = tag.getListTag("with"); if (with != null) { - processListTag(with); + processListTag(connection, with); } } final ListTag extra = tag.getListTag("extra"); if (extra != null) { - processListTag(extra); + processListTag(connection, extra); } final CompoundTag hoverEvent = tag.getCompoundTag("hoverEvent"); if (hoverEvent != null) { - handleHoverEvent(hoverEvent); + handleHoverEvent(connection, hoverEvent); } } - protected void handleTranslate(final CompoundTag parentTag, final StringTag translateTag) { + protected void handleTranslate(final UserConnection connection, final CompoundTag parentTag, final StringTag translateTag) { // To override if needed } - protected void handleHoverEvent(final CompoundTag hoverEventTag) { + protected void handleHoverEvent(final UserConnection connection, final CompoundTag hoverEventTag) { // To override if needed (don't forget to call super) final StringTag actionTag = hoverEventTag.getStringTag("action"); if (actionTag == null) { @@ -302,11 +303,11 @@ public class ComponentRewriter { final String action = actionTag.getValue(); if (action.equals("show_text")) { final Tag value = hoverEventTag.get("value"); - processTag(value != null ? value : hoverEventTag.get("contents")); + processTag(connection, value != null ? value : hoverEventTag.get("contents")); } else if (action.equals("show_entity")) { final CompoundTag contents = hoverEventTag.getCompoundTag("contents"); if (contents != null) { - processTag(contents.get("name")); + processTag(connection, contents.get("name")); } } }