From ae3042074ea2ad0f62c7280aff5c978923b32c94 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Tue, 11 Jun 2024 15:02:04 +0200 Subject: [PATCH] Add trade list rewriter functions to ItemRewriter (#3926) --- .../rewriter/ItemPacketRewriter1_11.java | 32 +---------------- .../rewriter/ItemPacketRewriter1_12.java | 31 +--------------- .../rewriter/ItemPacketRewriter1_13.java | 28 ++------------- .../rewriter/ItemPacketRewriter1_9.java | 2 +- .../rewriter/PlayerPacketRewriter1_9.java | 26 ++------------ .../viaversion/rewriter/ItemRewriter.java | 35 +++++++++++++++++++ 6 files changed, 42 insertions(+), 112 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/rewriter/ItemPacketRewriter1_11.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/rewriter/ItemPacketRewriter1_11.java index c305288f6..f7dcd7075 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/rewriter/ItemPacketRewriter1_11.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/rewriter/ItemPacketRewriter1_11.java @@ -21,7 +21,6 @@ import com.viaversion.nbt.tag.ByteTag; import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.protocols.v1_10to1_11.Protocol1_10To1_11; import com.viaversion.viaversion.protocols.v1_10to1_11.data.EntityMappings1_11; @@ -40,36 +39,7 @@ public class ItemPacketRewriter1_11 extends ItemRewriter Trading - protocol.registerClientbound(ClientboundPackets1_9_3.CUSTOM_PAYLOAD, new PacketHandlers() { - @Override - public void register() { - map(Types.STRING); // 0 - Channel - - handlerSoftFail(wrapper -> { - if (wrapper.get(Types.STRING, 0).equals("MC|TrList")) { - wrapper.passthrough(Types.INT); // Passthrough Window ID - - int size = wrapper.passthrough(Types.UNSIGNED_BYTE); - for (int i = 0; i < size; i++) { - handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Input Item - handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Output Item - - boolean secondItem = wrapper.passthrough(Types.BOOLEAN); // Has second item - if (secondItem) { - handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Second Item - } - - wrapper.passthrough(Types.BOOLEAN); // Trade disabled - wrapper.passthrough(Types.INT); // Number of tools uses - wrapper.passthrough(Types.INT); // Maximum number of trade uses - } - } - }); - } - }); - + registerCustomPayloadTradeList(ClientboundPackets1_9_3.CUSTOM_PAYLOAD); registerContainerClick(ServerboundPackets1_9_3.CONTAINER_CLICK); registerSetCreativeModeSlot(ServerboundPackets1_9_3.SET_CREATIVE_MODE_SLOT); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/rewriter/ItemPacketRewriter1_12.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/rewriter/ItemPacketRewriter1_12.java index 375e73266..1d5808464 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/rewriter/ItemPacketRewriter1_12.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/rewriter/ItemPacketRewriter1_12.java @@ -40,36 +40,7 @@ public class ItemPacketRewriter1_12 extends ItemRewriter Trading - protocol.registerClientbound(ClientboundPackets1_9_3.CUSTOM_PAYLOAD, new PacketHandlers() { - @Override - public void register() { - map(Types.STRING); // 0 - Channel - - handlerSoftFail(wrapper -> { - if (wrapper.get(Types.STRING, 0).equals("MC|TrList")) { - wrapper.passthrough(Types.INT); // Passthrough Window ID - - int size = wrapper.passthrough(Types.UNSIGNED_BYTE); - for (int i = 0; i < size; i++) { - handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Input Item - handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Output Item - - boolean secondItem = wrapper.passthrough(Types.BOOLEAN); // Has second item - if (secondItem) { - handleItemToClient(wrapper.user(), wrapper.passthrough(Types.ITEM1_8)); // Second Item - } - - wrapper.passthrough(Types.BOOLEAN); // Trade disabled - wrapper.passthrough(Types.INT); // Number of tools uses - wrapper.passthrough(Types.INT); // Maximum number of trade uses - } - } - }); - } - }); - + registerCustomPayloadTradeList(ClientboundPackets1_9_3.CUSTOM_PAYLOAD); protocol.registerServerbound(ServerboundPackets1_12.CONTAINER_CLICK, new PacketHandlers() { @Override diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java index e6e45f061..1cafb02a3 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java @@ -51,7 +51,7 @@ import java.util.Optional; public class ItemPacketRewriter1_13 extends ItemRewriter { public ItemPacketRewriter1_13(Protocol1_12_2To1_13 protocol) { - super(protocol, null, null); + super(protocol, Types.ITEM1_8, null, Types.ITEM1_13, null); } @Override @@ -136,31 +136,7 @@ public class ItemPacketRewriter1_13 extends ItemRewriter { public ItemPacketRewriter1_9(final Protocol1_8To1_9 protocol) { - super(protocol, null, null); + super(protocol, Types.ITEM1_8, null); } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/PlayerPacketRewriter1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/PlayerPacketRewriter1_9.java index c58488bda..f7d7b8464 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/PlayerPacketRewriter1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/PlayerPacketRewriter1_9.java @@ -276,33 +276,11 @@ public class PlayerPacketRewriter1_9 { public void register() { map(Types.STRING); // 0 - Channel Name handlerSoftFail(wrapper -> { - String name = wrapper.get(Types.STRING, 0); + final String name = wrapper.get(Types.STRING, 0); if (name.equals("MC|BOpen")) { wrapper.write(Types.VAR_INT, 0); } else if (name.equals("MC|TrList")) { - wrapper.passthrough(Types.INT); // ID - - Short size = wrapper.passthrough(Types.UNSIGNED_BYTE); - - for (int i = 0; i < size; ++i) { - Item item1 = wrapper.passthrough(Types.ITEM1_8); - protocol.getItemRewriter().handleItemToClient(wrapper.user(), item1); - - Item item2 = wrapper.passthrough(Types.ITEM1_8); - protocol.getItemRewriter().handleItemToClient(wrapper.user(), item2); - - boolean present = wrapper.passthrough(Types.BOOLEAN); - - if (present) { - Item item3 = wrapper.passthrough(Types.ITEM1_8); - protocol.getItemRewriter().handleItemToClient(wrapper.user(), item3); - } - - wrapper.passthrough(Types.BOOLEAN); - - wrapper.passthrough(Types.INT); - wrapper.passthrough(Types.INT); - } + protocol.getItemRewriter().handleTradeList(wrapper); } }); } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java index 80003cdf2..9e1d7b137 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java @@ -234,6 +234,41 @@ public class ItemRewriter { + final String channel = wrapper.get(Types.STRING, 0); + if (channel.equals("MC|TrList")) { + handleTradeList(wrapper); + } + }); + } + }); + } + + public void handleTradeList(final PacketWrapper wrapper) { + wrapper.passthrough(Types.INT); // Window ID + + final int size = wrapper.passthrough(Types.UNSIGNED_BYTE); + for (int i = 0; i < size; i++) { + handleClientboundItem(wrapper); // Input Item + handleClientboundItem(wrapper); // Output Item + + if (wrapper.passthrough(Types.BOOLEAN)) { + handleClientboundItem(wrapper); // Second Item + } + + wrapper.passthrough(Types.BOOLEAN); // Trade disabled + wrapper.passthrough(Types.INT); // Number of tools uses + wrapper.passthrough(Types.INT); // Maximum number of trade uses + } + } + + // 1.14.4+ public void registerMerchantOffers(C packetType) { protocol.registerClientbound(packetType, wrapper -> {