Add DisguiseInteractEvent which is fired on self-disguise interaction

This commit is contained in:
libraryaddict 2018-11-14 17:07:43 +13:00
parent 93e2cdb0a4
commit 5e256c9f0e
2 changed files with 130 additions and 37 deletions

View File

@ -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.
* <p>
* 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;
}
}

View File

@ -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,19 +32,45 @@ public class PacketListenerClientInteract extends PacketAdapter {
if (event.isCancelled())
return;
try {
Player observer = event.getPlayer();
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
return;
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
PacketContainer packet = event.getPacket();
StructureModifier<Entity> entityModifer = packet.getEntityModifier(observer.getWorld());
Entity entity = entityModifer.read(0);
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;
}
}
DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed,
interactType == EnumWrappers.EntityUseAction.ATTACK);
Bukkit.getPluginManager().callEvent(selfEvent);
}
}
for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(),
@ -71,8 +103,4 @@ public class PacketListenerClientInteract extends PacketAdapter {
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}