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.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.util.Vector;
/**
@ -14,10 +15,14 @@ public class PlayerInteractAtEntityEvent extends PlayerInteractEntityEvent {
private final 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;
}
public Vector getClickedPosition() {
return position.clone();
}

View file

@ -4,6 +4,7 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.EquipmentSlot;
/**
* 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();
protected Entity clickedEntity;
boolean cancelled = false;
private EquipmentSlot hand;
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);
this.clickedEntity = clickedEntity;
this.hand = hand;
}
public boolean isCancelled() {
@ -35,6 +42,15 @@ public class PlayerInteractEntityEvent extends PlayerEvent implements Cancellabl
return this.clickedEntity;
}
/**
* The hand used to perform this interaction.
*
* @return the hand used to interact
*/
public EquipmentSlot getHand() {
return hand;
}
@Override
public HandlerList getHandlers() {
return handlers;

View file

@ -8,6 +8,7 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.block.Action;
import org.bukkit.inventory.EquipmentSlot;
/**
* Called when a player interacts with an object or air.
@ -23,13 +24,19 @@ public class PlayerInteractEvent extends PlayerEvent implements Cancellable {
protected BlockFace blockFace;
private Result useClickedBlock;
private Result useItemInHand;
private EquipmentSlot hand;
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);
this.action = action;
this.item = item;
this.blockClicked = clickedBlock;
this.blockFace = clickedFace;
this.hand = hand;
useItemInHand = Result.DEFAULT;
useClickedBlock = clickedBlock == null ? Result.DENY : Result.ALLOW;
@ -179,6 +186,16 @@ public class PlayerInteractEvent extends PlayerEvent implements Cancellable {
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
public HandlerList getHandlers() {
return handlers;