Add DisguiseInteractEvent which is fired on self-disguise interaction
This commit is contained in:
parent
93e2cdb0a4
commit
5e256c9f0e
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -3,17 +3,23 @@ package me.libraryaddict.disguise.utilities.packetlisteners;
|
|||||||
import com.comphenix.protocol.PacketType;
|
import com.comphenix.protocol.PacketType;
|
||||||
import com.comphenix.protocol.events.ListenerPriority;
|
import com.comphenix.protocol.events.ListenerPriority;
|
||||||
import com.comphenix.protocol.events.PacketAdapter;
|
import com.comphenix.protocol.events.PacketAdapter;
|
||||||
|
import com.comphenix.protocol.events.PacketContainer;
|
||||||
import com.comphenix.protocol.events.PacketEvent;
|
import com.comphenix.protocol.events.PacketEvent;
|
||||||
import com.comphenix.protocol.reflect.StructureModifier;
|
import com.comphenix.protocol.reflect.StructureModifier;
|
||||||
|
import com.comphenix.protocol.wrappers.EnumWrappers;
|
||||||
import me.libraryaddict.disguise.DisguiseAPI;
|
import me.libraryaddict.disguise.DisguiseAPI;
|
||||||
import me.libraryaddict.disguise.DisguiseConfig;
|
import me.libraryaddict.disguise.DisguiseConfig;
|
||||||
import me.libraryaddict.disguise.LibsDisguises;
|
import me.libraryaddict.disguise.LibsDisguises;
|
||||||
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
|
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
|
||||||
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
||||||
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
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.SheepWatcher;
|
||||||
import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher;
|
import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher;
|
||||||
|
import me.libraryaddict.disguise.events.DisguiseInteractEvent;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.*;
|
import org.bukkit.entity.*;
|
||||||
|
import org.bukkit.inventory.EquipmentSlot;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
public class PacketListenerClientInteract extends PacketAdapter {
|
public class PacketListenerClientInteract extends PacketAdapter {
|
||||||
@ -26,53 +32,75 @@ public class PacketListenerClientInteract extends PacketAdapter {
|
|||||||
if (event.isCancelled())
|
if (event.isCancelled())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
try {
|
Player observer = event.getPlayer();
|
||||||
Player observer = event.getPlayer();
|
|
||||||
|
|
||||||
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
|
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
|
||||||
return;
|
return;
|
||||||
|
|
||||||
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
|
PacketContainer packet = event.getPacket();
|
||||||
|
|
||||||
Entity entity = entityModifer.read(0);
|
StructureModifier<Entity> entityModifer = packet.getEntityModifier(observer.getWorld());
|
||||||
|
|
||||||
if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow ||
|
Entity entity = entityModifer.read(0);
|
||||||
entity == observer) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(),
|
if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow ||
|
||||||
observer.getInventory().getItemInOffHand()}) {
|
entity == observer) {
|
||||||
if (item == null) {
|
event.setCancelled(true);
|
||||||
continue;
|
} 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) {
|
Bukkit.getPluginManager().callEvent(selfEvent);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user