From 509055307ae20db0d6471f5953b0e8ff0b746a79 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 18 Mar 2023 13:55:16 +0100 Subject: [PATCH] Handle 1.12 keybind components Fixes #474 --- .../api/rewriters/EntityRewriterBase.java | 9 -------- .../packets/ChatPackets1_12.java | 21 +++++++++++++++++-- .../packets/EntityPackets1_12.java | 8 +++++++ .../packets/EntityPackets1_14.java | 3 ++- .../packets/EntityPackets1_13_1.java | 20 ++---------------- .../packets/EntityPackets1_15.java | 3 ++- .../packets/EntityPackets1_16_2.java | 2 +- .../packets/EntityPackets1_17.java | 3 ++- .../packets/EntityPackets1_18.java | 3 ++- .../packets/EntityPackets1_19.java | 3 ++- .../packets/EntityPackets1_19_3.java | 3 ++- .../packets/EntityPackets1_19_4.java | 3 ++- 12 files changed, 44 insertions(+), 37 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java index bc006929..152d004b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java @@ -175,15 +175,6 @@ public abstract class EntityRewriterBase { - private final ComponentRewriter componentRewriter = new ComponentRewriter() { + public static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() { + @Override + public void processText(JsonElement element) { + super.processText(element); + if (element == null || !element.isJsonObject()) { + return; + } + + JsonObject object = element.getAsJsonObject(); + JsonElement keybind = object.remove("keybind"); + if (keybind == null) { + return; + } + + //TODO Add nicer text for the key, also use this component rewriter in more packets + object.addProperty("text", keybind.getAsString()); + } + @Override protected void handleTranslate(JsonObject object, String translate) { String text = AdvancementTranslations.get(translate); @@ -47,7 +64,7 @@ public class ChatPackets1_12 extends RewriterBase { protected void registerPackets() { protocol.registerClientbound(ClientboundPackets1_12.CHAT_MESSAGE, wrapper -> { JsonElement element = wrapper.passthrough(Type.COMPONENT); - componentRewriter.processText(element); + COMPONENT_REWRITER.processText(element); }); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java index c78a19dc..0a6352c7 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java @@ -28,10 +28,12 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_12; +import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_12; +import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; @@ -205,6 +207,12 @@ public class EntityPackets1_12 extends LegacyEntityRewriter storage.add(new Metadata(12, MetaType1_12.Byte, (byte) 0x00))); mapEntityTypeWithData(Entity1_12Types.EntityType.ILLUSION_ILLAGER, Entity1_12Types.EntityType.EVOCATION_ILLAGER).plainName(); + filter().handler((event, meta) -> { + if (meta.metaType() == MetaType1_9.Chat) { + ChatPackets1_12.COMPONENT_REWRITER.processText((JsonElement) meta.getValue()); + } + }); + // Handle Illager filter().filterFamily(Entity1_12Types.EntityType.EVOCATION_ILLAGER).cancel(12); filter().filterFamily(Entity1_12Types.EntityType.EVOCATION_ILLAGER).index(13).toIndex(12); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java index 88d7b6fa..71a1457b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java @@ -348,7 +348,8 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { - if (meta.metaType() == Types1_13.META_TYPES.itemType) { - protocol.getItemRewriter().handleItemToClient((Item) meta.getValue()); - } else if (meta.metaType() == Types1_13.META_TYPES.blockStateType) { - // Convert to new block id - int data = (int) meta.getValue(); - meta.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (meta.metaType() == Types1_13.META_TYPES.particleType) { - rewriteParticle((Particle) meta.getValue()); - } else if (meta.metaType() == Types1_13.META_TYPES.optionalComponentType) { - JsonElement element = meta.value(); - protocol.getTranslatableRewriter().processText(element); - } - }); + registerMetaTypeHandler(Types1_13.META_TYPES.itemType, Types1_13.META_TYPES.blockStateType, Types1_13.META_TYPES.particleType, + Types1_13.META_TYPES.optionalComponentType, Types1_13.META_TYPES.componentType); // Remove shooter UUID filter().filterFamily(Entity1_13Types.EntityType.ABSTRACT_ARROW).cancel(7); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java index 613b507f..a4035fba 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java @@ -192,7 +192,8 @@ public class EntityPackets1_15 extends EntityRewriter { final int data = (int) meta.getValue(); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/EntityPackets1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/EntityPackets1_19_3.java index 98b6c903..d26989f3 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/EntityPackets1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/EntityPackets1_19_3.java @@ -230,7 +230,8 @@ public final class EntityPackets1_19_3 extends EntityRewriter { // Sitting pose added diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/packets/EntityPackets1_19_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/packets/EntityPackets1_19_4.java index 3b3d00e4..cc163da0 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/packets/EntityPackets1_19_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/packets/EntityPackets1_19_4.java @@ -143,7 +143,8 @@ public final class EntityPackets1_19_4 extends EntityRewriter { final int blockState = meta.value();