Experimental build for client interaction/weapon damage - Needs testing and thread safety, pushed due to time constraints. #368
This commit is contained in:
		| @@ -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<TargetedDisguise> hashSet = getFutureDisguises().get(entityId); | ||||
|  | ||||
|             for (TargetedDisguise dis : hashSet) { | ||||
|                 if (!dis.canSee(observer) || !dis.isDisguiseInUse()) { | ||||
|                     continue; | ||||
|                 } | ||||
|  | ||||
|                 return dis; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         for (HashSet<TargetedDisguise> 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) { | ||||
|   | ||||
| @@ -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(); | ||||
|  | ||||
|   | ||||
| @@ -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())) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user