From 7e03da66d40f96d41d56b20e90eb0d2d76571fd5 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sat, 22 Aug 2020 13:25:00 -0500 Subject: [PATCH] Rework inventory workaround to cancel animation if in an open inventory On 1.16, when sending a new inventory while another one is open (without explicitly calling `closeInventory` first), a hand swing is sent from the client, which the server handles like any normal arm swing. This in turn results in the `PlayerInteractEvent` and `PlayerAnimationEvent` both being called on Bukkit's end, which are used to detect for left clicks. If an item in the player's hand opens a menu when left clicked and the above is true, this results in that menu being reopened. --- .../packets/EntityPackets.java | 15 +++++++++++++++ .../packets/InventoryPackets.java | 15 --------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java index 3e7861d85..9ecd65a97 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java @@ -16,9 +16,11 @@ import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; +import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2; 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 java.util.UUID; @@ -242,5 +244,18 @@ public class EntityPackets { }); } }); + + protocol.registerIncoming(ServerboundPackets1_16.ANIMATION, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class); + // Don't send an arm swing if the player is switching between inventories. + if (inventoryTracker.getInventory() != -1) { + wrapper.cancel(); + } + }); + } + }); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java index 7f47bd16b..1683a6e5e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java @@ -6,7 +6,6 @@ import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.LongTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; -import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.protocol.Protocol; @@ -16,8 +15,6 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.UUIDIntArrayType; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14; -import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; -import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; @@ -38,24 +35,12 @@ public class InventoryPackets { handler(wrapper -> { 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(ClientboundPackets1_16.CLOSE_WINDOW.ordinal()); - 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(); }); } });