From 5e256c9f0e81cd14134a1d08b06431b11e35b04e Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Wed, 14 Nov 2018 17:07:43 +1300 Subject: [PATCH] Add DisguiseInteractEvent which is fired on self-disguise interaction --- .../events/DisguiseInteractEvent.java | 65 +++++++++++ .../PacketListenerClientInteract.java | 102 +++++++++++------- 2 files changed, 130 insertions(+), 37 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/events/DisguiseInteractEvent.java diff --git a/src/main/java/me/libraryaddict/disguise/events/DisguiseInteractEvent.java b/src/main/java/me/libraryaddict/disguise/events/DisguiseInteractEvent.java new file mode 100644 index 00000000..92e1e16b --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/events/DisguiseInteractEvent.java @@ -0,0 +1,65 @@ +package me.libraryaddict.disguise.events; + +import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.bukkit.inventory.EquipmentSlot; + +/** + * Created by libraryaddict on 13/11/2018. + *

+ * Invoked when a player interacts with their own self disguise + */ +public class DisguiseInteractEvent extends PlayerEvent { + private static final HandlerList handlers = new HandlerList(); + + public static HandlerList getHandlerList() { + return handlers; + } + + private TargetedDisguise disguise; + private EquipmentSlot hand; + private boolean leftClick; + + public DisguiseInteractEvent(TargetedDisguise disguise, EquipmentSlot hand, boolean leftClick) { + super((Player) disguise.getEntity()); + + this.disguise = disguise; + this.hand = hand; + this.leftClick = leftClick; + } + + /** + * @return Disguise interacted with + */ + public TargetedDisguise getDisguise() { + return disguise; + } + + /** + * @return Returns the hand used, HAND or OFF_HAND + */ + public EquipmentSlot getHand() { + return hand; + } + + /** + * @return If the player left clicked (Attacked) + */ + public boolean isLeftClick() { + return leftClick; + } + + /** + * @return If the player right clicked (Interacted) + */ + public boolean isRightClick() { + return !isLeftClick(); + } + + @Override + public HandlerList getHandlers() { + return handlers; + } +} diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java b/src/main/java/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java index 1e6c4fce..c3032658 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java @@ -3,17 +3,23 @@ package me.libraryaddict.disguise.utilities.packetlisteners; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.reflect.StructureModifier; +import com.comphenix.protocol.wrappers.EnumWrappers; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.DisguiseType; +import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.watchers.SheepWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher; +import me.libraryaddict.disguise.events.DisguiseInteractEvent; +import org.bukkit.Bukkit; import org.bukkit.entity.*; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; public class PacketListenerClientInteract extends PacketAdapter { @@ -26,53 +32,75 @@ public class PacketListenerClientInteract extends PacketAdapter { if (event.isCancelled()) return; - try { - Player observer = event.getPlayer(); + Player observer = event.getPlayer(); - if (observer.getName().contains("UNKNOWN[")) // If the player is temporary - return; + if (observer.getName().contains("UNKNOWN[")) // If the player is temporary + return; - StructureModifier entityModifer = event.getPacket().getEntityModifier(observer.getWorld()); + PacketContainer packet = event.getPacket(); - Entity entity = entityModifer.read(0); + StructureModifier entityModifer = packet.getEntityModifier(observer.getWorld()); - if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow || - entity == observer) { - event.setCancelled(true); - } + Entity entity = entityModifer.read(0); - for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(), - observer.getInventory().getItemInOffHand()}) { - if (item == null) { - continue; + if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow || + entity == observer) { + event.setCancelled(true); + } else if (packet.getIntegers().read(0) == DisguiseAPI.getSelfDisguiseId()) { + // If it's a self-interact + event.setCancelled(true); + + Disguise disguise = DisguiseAPI.getDisguise(observer, observer); + + 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 (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; + } } - AnimalColor color = AnimalColor.getColorByMaterial(item.getType()); + DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed, + interactType == EnumWrappers.EntityUseAction.ATTACK); - if (color == null) { - 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(); - - watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor()); - } else { - WolfWatcher watcher = (WolfWatcher) disguise.getWatcher(); - - watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor()); - } + Bukkit.getPluginManager().callEvent(selfEvent); } } - catch (Exception e) { - e.printStackTrace(); + + for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(), + observer.getInventory().getItemInOffHand()}) { + if (item == null) { + continue; + } + + AnimalColor color = AnimalColor.getColorByMaterial(item.getType()); + + if (color == null) { + 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(); + + watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor()); + } else { + WolfWatcher watcher = (WolfWatcher) disguise.getWatcher(); + + watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor()); + } } } }