diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index dffe441ab..c5e5198f5 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -24,6 +24,7 @@ import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets.WorldPackets; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage.EntityTracker1_16; +import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import us.myles.ViaVersion.util.GsonUtil; import java.nio.charset.StandardCharsets; @@ -273,5 +274,6 @@ public class Protocol1_16To1_15_2 extends Protocol { + InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class); + if (inventoryTracker.getInventory() != -1) { + // Close open inventory before opening a new one. + PacketWrapper closePacket = wrapper.create(0x13); // 1.16 inventory close + closePacket.write(Type.UNSIGNED_BYTE, inventoryTracker.getInventory()); + closePacket.send(Protocol1_16To1_15_2.class, true, true); + } + + int windowId = wrapper.get(Type.VAR_INT, 0); int windowType = wrapper.get(Type.VAR_INT, 1); if (windowType >= 20) { // smithing added with id 20 wrapper.set(Type.VAR_INT, 1, ++windowType); } + + inventoryTracker.setInventory((short) windowId); + + // Workaround for packet order issue + wrapper.send(Protocol1_16To1_15_2.class, true, true); + wrapper.cancel(); }); } }); + + protocol.registerOutgoing(ClientboundPackets1_15.CLOSE_WINDOW, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.UNSIGNED_BYTE); + + handler(wrapper -> { + InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class); + inventoryTracker.setInventory((short) -1); + }); + } + }); + protocol.registerOutgoing(ClientboundPackets1_15.WINDOW_PROPERTY, new PacketRemapper() { @Override public void registerMap() { - map(Type.UNSIGNED_BYTE); // Window id + map(Type.UNSIGNED_BYTE); // Window Id map(Type.SHORT); // Property map(Type.SHORT); // Value @@ -83,6 +114,18 @@ public class InventoryPackets { itemRewriter.registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM); itemRewriter.registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); + protocol.registerIncoming(ServerboundPackets1_16.CLOSE_WINDOW, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.UNSIGNED_BYTE); + + handler(wrapper -> { + InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class); + inventoryTracker.setInventory((short) -1); + }); + } + }); + protocol.registerIncoming(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() { @Override public void registerMap() { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/storage/InventoryTracker1_16.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/storage/InventoryTracker1_16.java new file mode 100644 index 000000000..d19501e9b --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/storage/InventoryTracker1_16.java @@ -0,0 +1,20 @@ +package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage; + +import us.myles.ViaVersion.api.data.StoredObject; +import us.myles.ViaVersion.api.data.UserConnection; + +public class InventoryTracker1_16 extends StoredObject { + private short inventory = -1; + + public InventoryTracker1_16(UserConnection user) { + super(user); + } + + public short getInventory() { + return this.inventory; + } + + public void setInventory(short inventory) { + this.inventory = inventory; + } +}