From 7ab0c41be6fcf0c3cea0231a20a46973975bedf8 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 8 Jun 2022 10:07:47 +0200 Subject: [PATCH] Fix villager trade offers Fixes #2919 --- .../viaversion/viaversion/BungeePlugin.java | 2 +- .../packets/InventoryPackets.java | 30 ++++++++++++++++++- .../viaversion/rewriter/ItemRewriter.java | 26 ++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/bungee/src/main/java/com/viaversion/viaversion/BungeePlugin.java b/bungee/src/main/java/com/viaversion/viaversion/BungeePlugin.java index 11e7dcce1..badb52042 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/BungeePlugin.java +++ b/bungee/src/main/java/com/viaversion/viaversion/BungeePlugin.java @@ -56,7 +56,7 @@ public class BungeePlugin extends Plugin implements ViaPlatform, @Override public void onLoad() { try { - ProtocolConstants.class.getField("MINECRAFT_1_18"); + ProtocolConstants.class.getField("MINECRAFT_1_19"); } catch (NoSuchFieldException e) { getLogger().warning(" / \\"); getLogger().warning(" / \\"); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/packets/InventoryPackets.java index f8aea24f5..c8abce709 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/packets/InventoryPackets.java @@ -38,7 +38,6 @@ public final class InventoryPackets extends ItemRewriter { public void registerPackets() { registerSetCooldown(ClientboundPackets1_18.COOLDOWN); registerWindowItems1_17_1(ClientboundPackets1_18.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, Type.FLAT_VAR_INT_ITEM); - registerTradeList(ClientboundPackets1_18.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); registerSetSlot1_17_1(ClientboundPackets1_18.SET_SLOT, Type.FLAT_VAR_INT_ITEM); registerAdvancements(ClientboundPackets1_18.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM); registerEntityEquipmentArray(ClientboundPackets1_18.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM); @@ -64,6 +63,35 @@ public final class InventoryPackets extends ItemRewriter { registerWindowPropertyEnchantmentHandler(ClientboundPackets1_18.WINDOW_PROPERTY); + protocol.registerClientbound(ClientboundPackets1_18.TRADE_LIST, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // Container id + handler(wrapper -> { + final int size = wrapper.read(Type.UNSIGNED_BYTE); + wrapper.write(Type.VAR_INT, size); + for (int i = 0; i < size; i++) { + handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // First item + handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + + if (wrapper.read(Type.BOOLEAN)) { + handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); + } else { + wrapper.write(Type.FLAT_VAR_INT_ITEM, null); + } + + wrapper.passthrough(Type.BOOLEAN); // Out of stock + wrapper.passthrough(Type.INT); // Uses + wrapper.passthrough(Type.INT); // Max uses + wrapper.passthrough(Type.INT); // Xp + wrapper.passthrough(Type.INT); // Special price diff + wrapper.passthrough(Type.FLOAT); // Price multiplier + wrapper.passthrough(Type.INT); //Demand + } + }); + } + }); + protocol.registerServerbound(ServerboundPackets1_19.PLAYER_DIGGING, new PacketRemapper() { @Override public void registerMap() { 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 54faaf15e..90bce2191 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java @@ -240,6 +240,32 @@ public abstract class ItemRewriter extends RewriterBase i }); } + public void registerTradeList1_19(ClientboundPacketType packetType, Type type) { + protocol.registerClientbound(packetType, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + wrapper.passthrough(Type.VAR_INT); // Container id + int size = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < size; i++) { + handleItemToClient(wrapper.passthrough(type)); // Input + handleItemToClient(wrapper.passthrough(type)); // Output + handleItemToClient(wrapper.passthrough(type)); // Second Item + + wrapper.passthrough(Type.BOOLEAN); // Trade disabled + wrapper.passthrough(Type.INT); // Number of tools uses + wrapper.passthrough(Type.INT); // Maximum number of trade uses + + wrapper.passthrough(Type.INT); // XP + wrapper.passthrough(Type.INT); // Special price + wrapper.passthrough(Type.FLOAT); // Price multiplier + wrapper.passthrough(Type.INT); // Demand + } + }); + } + }); + } + public void registerAdvancements(ClientboundPacketType packetType, Type type) { protocol.registerClientbound(packetType, new PacketRemapper() { @Override