From a46178b04cffd85fac09b037f115dd1fbac1ada0 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 22 Jun 2019 22:06:59 +1200 Subject: [PATCH] Experimental build for client interaction/weapon damage - Needs testing and thread safety, pushed due to time constraints. #368 --- .../disguise/utilities/DisguiseUtilities.java | 41 +++++++++++ .../PacketListenerClientInteract.java | 68 +++++++++---------- .../packetlisteners/PacketListenerSounds.java | 6 +- 3 files changed, 78 insertions(+), 37 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index c5ba5df2..c09a9a1b 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -1826,6 +1826,47 @@ public class DisguiseUtilities { } } + public static Disguise getDisguise(Player observer, int entityId) { + // If the entity ID is the same as self disguises id, then it needs to be set to the observers id + if (entityId == DisguiseAPI.getSelfDisguiseId()) { + entityId = observer.getEntityId(); + } + + // TODO Needs to be thread safe, not thread safe atm due to testing + + if (getFutureDisguises().containsKey(entityId)) { + HashSet hashSet = getFutureDisguises().get(entityId); + + for (TargetedDisguise dis : hashSet) { + if (!dis.canSee(observer) || !dis.isDisguiseInUse()) { + continue; + } + + return dis; + } + } + + for (HashSet disguises : getDisguises().values()) { + for (TargetedDisguise dis : disguises) { + if (dis.getEntity() == null || !dis.isDisguiseInUse()) { + continue; + } + + if (dis.getEntity().getEntityId() != entityId) { + continue; + } + + if (!dis.canSee(observer)) { + continue; + } + + return dis; + } + } + + return null; + } + public static Entity getEntity(World world, int entityId) { for (Entity e : world.getEntities()) { if (e.getEntityId() != entityId) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerClientInteract.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerClientInteract.java index d7cff659..08b1ccac 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerClientInteract.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerClientInteract.java @@ -41,47 +41,54 @@ public class PacketListenerClientInteract extends PacketAdapter { PacketContainer packet = event.getPacket(); - Entity entity = DisguiseUtilities.getEntity(observer.getWorld(), packet.getIntegers().read(0)); + final Disguise disguise = DisguiseUtilities.getDisguise(event.getPlayer(), packet.getIntegers().read(0)); - if (entity == null) { + if (disguise == null) { return; } - if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow || - entity == observer) { - event.setCancelled(true); - } else if (packet.getIntegers().read(0) == DisguiseAPI.getSelfDisguiseId()) { + if (disguise.getEntity() == observer) { // If it's a self-interact event.setCancelled(true); - Disguise disguise = DisguiseAPI.getDisguise(observer, observer); + // The type of interact, we don't care the difference with "Interact_At" however as it's not + // useful + // for self disguises + EnumWrappers.EntityUseAction interactType = packet.getEntityUseActions().read(0); + final EquipmentSlot handUsed; - if (disguise != null) { - // The type of interact, we don't care the difference with "Interact_At" however as it's not useful - // for self disguises - EnumWrappers.EntityUseAction interactType = packet.getEntityUseActions().read(0); - EquipmentSlot handUsed = EquipmentSlot.HAND; + // Attack has a null hand, which throws an error if you attempt to fetch + // If the hand used wasn't their main hand + if (interactType != EnumWrappers.EntityUseAction.ATTACK && + packet.getHands().read(0) == EnumWrappers.Hand.OFF_HAND) { + handUsed = EquipmentSlot.OFF_HAND; + } else { + handUsed = EquipmentSlot.HAND; + } - // Attack has a null hand, which throws an error if you attempt to fetch - if (interactType != EnumWrappers.EntityUseAction.ATTACK) { - // If the hand used wasn't their main hand - if (packet.getHands().read(0) == EnumWrappers.Hand.OFF_HAND) { - handUsed = EquipmentSlot.OFF_HAND; - } + new BukkitRunnable() { + @Override + public void run() { + // Fire self interact event + DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed, + interactType == EnumWrappers.EntityUseAction.ATTACK); + + Bukkit.getPluginManager().callEvent(selfEvent); } + }.runTask(LibsDisguises.getInstance()); + } else { + Entity entity = disguise.getEntity(); - DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed, - interactType == EnumWrappers.EntityUseAction.ATTACK); - - new BukkitRunnable() { - @Override - public void run() { - Bukkit.getPluginManager().callEvent(selfEvent); - } - }.runTask(LibsDisguises.getInstance()); + if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow) { + event.setCancelled(true); } } + if (disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF) { + return; + } + + // If this is something the player can dye the disguise with for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(), observer.getInventory().getItemInOffHand()}) { if (item == null) { @@ -94,13 +101,6 @@ public class PacketListenerClientInteract extends PacketAdapter { continue; } - Disguise disguise = DisguiseAPI.getDisguise(observer, entity); - - if (disguise == null || - (disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF)) { - continue; - } - if (disguise.getType() == DisguiseType.SHEEP) { SheepWatcher watcher = (SheepWatcher) disguise.getWatcher(); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerSounds.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerSounds.java index 8beb12a4..df7595e3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerSounds.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerSounds.java @@ -249,13 +249,13 @@ public class PacketListenerSounds extends PacketAdapter { } // It made a damage animation - Entity entity = DisguiseUtilities.getEntity(observer.getWorld(), event.getPacket().getIntegers().read(0)); + Disguise disguise = DisguiseUtilities.getDisguise(observer, event.getPacket().getIntegers().read(0)); - if (entity == null) { + if (disguise == null) { return; } - Disguise disguise = DisguiseAPI.getDisguise(observer, entity); + Entity entity = disguise.getEntity(); if (disguise != null && !disguise.getType().isPlayer() && (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) {