SPIGOT-1608: Add a way to get the hand used in PlayerInteract*Events

By: md_5 <git@md-5.net>
This commit is contained in:
Bukkit/Spigot 2016-03-03 19:20:28 +11:00
parent 3589bbb79e
commit b748e04c05
3 changed files with 40 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package org.bukkit.event.player;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
/** /**
@ -14,10 +15,14 @@ public class PlayerInteractAtEntityEvent extends PlayerInteractEntityEvent {
private final Vector position; private final Vector position;
public PlayerInteractAtEntityEvent(Player who, Entity clickedEntity, Vector position) { public PlayerInteractAtEntityEvent(Player who, Entity clickedEntity, Vector position) {
super(who, clickedEntity); this(who, clickedEntity, position, EquipmentSlot.HAND);
}
public PlayerInteractAtEntityEvent(Player who, Entity clickedEntity, Vector position, EquipmentSlot hand) {
super(who, clickedEntity, hand);
this.position = position; this.position = position;
} }
public Vector getClickedPosition() { public Vector getClickedPosition() {
return position.clone(); return position.clone();
} }

View File

@ -4,6 +4,7 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.inventory.EquipmentSlot;
/** /**
* Represents an event that is called when a player right clicks an entity. * Represents an event that is called when a player right clicks an entity.
@ -12,10 +13,16 @@ public class PlayerInteractEntityEvent extends PlayerEvent implements Cancellabl
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
protected Entity clickedEntity; protected Entity clickedEntity;
boolean cancelled = false; boolean cancelled = false;
private EquipmentSlot hand;
public PlayerInteractEntityEvent(final Player who, final Entity clickedEntity) { public PlayerInteractEntityEvent(final Player who, final Entity clickedEntity) {
this(who, clickedEntity, EquipmentSlot.HAND);
}
public PlayerInteractEntityEvent(final Player who, final Entity clickedEntity, final EquipmentSlot hand) {
super(who); super(who);
this.clickedEntity = clickedEntity; this.clickedEntity = clickedEntity;
this.hand = hand;
} }
public boolean isCancelled() { public boolean isCancelled() {
@ -35,6 +42,15 @@ public class PlayerInteractEntityEvent extends PlayerEvent implements Cancellabl
return this.clickedEntity; return this.clickedEntity;
} }
/**
* The hand used to perform this interaction.
*
* @return the hand used to interact
*/
public EquipmentSlot getHand() {
return hand;
}
@Override @Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;

View File

@ -8,6 +8,7 @@ import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.inventory.EquipmentSlot;
/** /**
* Called when a player interacts with an object or air. * Called when a player interacts with an object or air.
@ -23,13 +24,19 @@ public class PlayerInteractEvent extends PlayerEvent implements Cancellable {
protected BlockFace blockFace; protected BlockFace blockFace;
private Result useClickedBlock; private Result useClickedBlock;
private Result useItemInHand; private Result useItemInHand;
private EquipmentSlot hand;
public PlayerInteractEvent(final Player who, final Action action, final ItemStack item, final Block clickedBlock, final BlockFace clickedFace) { public PlayerInteractEvent(final Player who, final Action action, final ItemStack item, final Block clickedBlock, final BlockFace clickedFace) {
this(who, action, item, clickedBlock, clickedFace, EquipmentSlot.HAND);
}
public PlayerInteractEvent(final Player who, final Action action, final ItemStack item, final Block clickedBlock, final BlockFace clickedFace, final EquipmentSlot hand) {
super(who); super(who);
this.action = action; this.action = action;
this.item = item; this.item = item;
this.blockClicked = clickedBlock; this.blockClicked = clickedBlock;
this.blockFace = clickedFace; this.blockFace = clickedFace;
this.hand = hand;
useItemInHand = Result.DEFAULT; useItemInHand = Result.DEFAULT;
useClickedBlock = clickedBlock == null ? Result.DENY : Result.ALLOW; useClickedBlock = clickedBlock == null ? Result.DENY : Result.ALLOW;
@ -179,6 +186,16 @@ public class PlayerInteractEvent extends PlayerEvent implements Cancellable {
this.useItemInHand = useItemInHand; this.useItemInHand = useItemInHand;
} }
/**
* The hand used to perform this interaction. May be null in the case of
* {@link Action#PHYSICAL}.
*
* @return the hand used to interact. May be null.
*/
public EquipmentSlot getHand() {
return hand;
}
@Override @Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;