From 7935f6556493c6ba0404285b1d1b9e53b307f224 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Tue, 25 May 2021 10:42:34 +1200 Subject: [PATCH] Handle setRightClicking for fake disguise items --- .../utilities/packets/LibsPackets.java | 5 ++- .../PacketHandlerEquipment.java | 44 +++++++++---------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java index e6676596..2d0e5123 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java @@ -29,11 +29,11 @@ import java.util.Map; */ @Getter @RequiredArgsConstructor +@Setter public class LibsPackets { private final ArrayList packets = new ArrayList<>(); private final HashMap> delayedPacketsMap = new HashMap<>(); private final Disguise disguise; - @Setter private boolean unhandled; public Disguise getDisguise() { @@ -79,7 +79,8 @@ public class LibsPackets { try { for (PacketContainer packet : entry.getValue()) { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + // To have right click handled properly, equip packets sent are normal + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, packet.getType() == PacketType.Play.Server.ENTITY_EQUIPMENT); } } catch (InvocationTargetException e) { e.printStackTrace(); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java index 53b5634a..422799e7 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java @@ -38,8 +38,7 @@ public class PacketHandlerEquipment implements IPacketHandler { } @Override - public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, - Entity entity) { + public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, Entity entity) { if (NmsVersion.v1_16.isSupported()) { handleNew(disguise, sentPacket, packets, observer, entity); } else { @@ -47,12 +46,10 @@ public class PacketHandlerEquipment implements IPacketHandler { } } - public void handleNew(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, - Entity entity) { + public void handleNew(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, Entity entity) { // Else if the disguise is updating equipment - List> slots = (List>) packets.getPackets().get(0).getModifier() - .read(1); + List> slots = (List>) packets.getPackets().get(0).getModifier().read(1); List> newSlots = new ArrayList<>(); boolean constructed = false; @@ -78,26 +75,24 @@ public class PacketHandlerEquipment implements IPacketHandler { equipPacket.getModifier().write(1, newSlots); } - newSlots.add(Pair.of(pair.getFirst(), - ReflectionManager.getNmsItem(itemStack.getType() == Material.AIR ? null : itemStack))); + newSlots.add(Pair.of(pair.getFirst(), ReflectionManager.getNmsItem(itemStack.getType() == Material.AIR ? null : itemStack))); } else { newSlots.add(pair); + itemStack = ReflectionManager.getBukkitItem(pair.getSecond()); } - if (disguise.getWatcher().isRightClicking() && - (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { - itemStack = ReflectionManager.getBukkitItem(pair.getSecond()); - + if (disguise.getWatcher().isRightClicking() && (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { if (itemStack != null && itemStack.getType() != Material.AIR) { // Convert the datawatcher List list = new ArrayList<>(); if (DisguiseConfig.isMetaPacketsEnabled()) { - WrappedWatchableObject watch = ReflectionManager.createWatchable(MetaIndex.LIVING_HAND, - WrappedDataWatcher.getEntityWatcher(entity).getByte(MetaIndex.LIVING_HAND.getIndex())); + WrappedWatchableObject watch = ReflectionManager + .createWatchable(MetaIndex.LIVING_HAND, WrappedDataWatcher.getEntityWatcher(entity).getByte(MetaIndex.LIVING_HAND.getIndex())); - if (watch != null) + if (watch != null) { list.add(watch); + } list = disguise.getWatcher().convert(observer, list); } else { @@ -140,8 +135,7 @@ public class PacketHandlerEquipment implements IPacketHandler { } } - public void handleOld(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, - Entity entity) { + public void handleOld(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, Entity entity) { // Else if the disguise is updating equipment EquipmentSlot slot = ReflectionManager.createEquipmentSlot(packets.getPackets().get(0).getModifier().read(1)); @@ -155,24 +149,26 @@ public class PacketHandlerEquipment implements IPacketHandler { packets.addPacket(equipPacket); - equipPacket.getModifier() - .write(2, ReflectionManager.getNmsItem(itemStack.getType() == Material.AIR ? null : itemStack)); + equipPacket.getModifier().write(2, ReflectionManager.getNmsItem(itemStack.getType() == Material.AIR ? null : itemStack)); } if (disguise.getWatcher().isRightClicking() && (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { - ItemStack heldItem = packets.getPackets().get(0).getItemModifier().read(0); + if (itemStack == null) { + itemStack = packets.getPackets().get(0).getItemModifier().read(0); + } - if (heldItem != null && heldItem.getType() != Material.AIR) { + if (itemStack != null && itemStack.getType() != Material.AIR) { // Convert the datawatcher List list = new ArrayList<>(); MetaIndex toUse = NmsVersion.v1_13.isSupported() ? MetaIndex.LIVING_HAND : MetaIndex.ENTITY_META; if (DisguiseConfig.isMetaPacketsEnabled()) { - WrappedWatchableObject watch = ReflectionManager.createWatchable(toUse, - WrappedDataWatcher.getEntityWatcher(entity).getByte(toUse.getIndex())); + WrappedWatchableObject watch = + ReflectionManager.createWatchable(toUse, WrappedDataWatcher.getEntityWatcher(entity).getByte(toUse.getIndex())); - if (watch != null) + if (watch != null) { list.add(watch); + } list = disguise.getWatcher().convert(observer, list); } else {