From f9af6947f7b2c90863e33c47dd022f0051bbf995 Mon Sep 17 00:00:00 2001 From: _tomcraft <936063+tomcraft@users.noreply.github.com> Date: Sun, 20 Jun 2021 22:50:37 +0200 Subject: [PATCH] Improve arm-swing cancel by resetting cursor item (#2528) --- .../Protocol1_16To1_15_2.java | 2 -- .../packets/EntityPackets.java | 16 +-------- .../packets/InventoryPackets.java | 36 ++++++++----------- .../storage/InventoryTracker1_16.java | 32 ----------------- 4 files changed, 15 insertions(+), 71 deletions(-) delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/storage/InventoryTracker1_16.java diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index 5c45d7969..f71ee4c34 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -39,7 +39,6 @@ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.Metadat import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.WorldPackets; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.rewriter.SoundRewriter; @@ -276,7 +275,6 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol { - 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/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java index 91b887207..9a9e18fb3 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java @@ -25,14 +25,16 @@ import com.github.steveice10.opennbt.tag.builtin.NumberTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.UUIDIntArrayType; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import com.viaversion.viaversion.rewriter.ItemRewriter; import java.util.UUID; @@ -45,6 +47,15 @@ public class InventoryPackets extends ItemRewriter { @Override public void registerPackets() { + // clear cursor item to prevent client to try dropping it during navigation between multiple inventories causing arm swing + PacketHandler cursorRemapper = wrapper -> { + PacketWrapper clearPacket = wrapper.create(ClientboundPackets1_16.SET_SLOT); + clearPacket.write(Type.UNSIGNED_BYTE, (short)-1); + clearPacket.write(Type.SHORT, (short)-1); + clearPacket.write(Type.FLAT_VAR_INT_ITEM, null); + clearPacket.send(Protocol1_16To1_15_2.class); + }; + protocol.registerClientbound(ClientboundPackets1_15.OPEN_WINDOW, new PacketRemapper() { @Override public void registerMap() { @@ -53,26 +64,19 @@ public class InventoryPackets extends ItemRewriter { map(Type.COMPONENT); // Window Title handler(wrapper -> { - InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class); - 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); }); + handler(cursorRemapper); } }); protocol.registerClientbound(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); - }); + handler(cursorRemapper); } }); @@ -119,18 +123,6 @@ public class InventoryPackets extends ItemRewriter { registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM); registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); - protocol.registerServerbound(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.registerServerbound(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() { @Override public void registerMap() { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/storage/InventoryTracker1_16.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/storage/InventoryTracker1_16.java deleted file mode 100644 index 9320582c6..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/storage/InventoryTracker1_16.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage; - -import com.viaversion.viaversion.api.connection.StorableObject; - -public class InventoryTracker1_16 implements StorableObject { - private short inventory = -1; - - public short getInventory() { - return this.inventory; - } - - public void setInventory(short inventory) { - this.inventory = inventory; - } -}