public class PlayerInteractEvent extends PlayerEvent implements Cancellable
无论玩家使用的是左手还是右手, 都有可能分别触发本事件. 你可以使用 getHand()
方法来确定玩家使用的是哪只手.
如果是原版行为触发了本事件, 则本事件将会被取消(例如与空气进行交互).
For the purpose of avoiding doubt, this means
that the event will only be in the cancelled state if it is fired as a result
of some prediction made by the server where no subsequent code will run,
rather than when the subsequent interaction activity (e.g. placing a block in
an illegal position (BlockCanBuildEvent
) will fail.
原文:Represents an event that is called when a player interacts with an object or
air, potentially fired once for each hand. The hand can be determined using
getHand()
.
This event will fire as cancelled if the vanilla behavior is to do nothing
(e.g interacting with air). For the purpose of avoiding doubt, this means
that the event will only be in the cancelled state if it is fired as a result
of some prediction made by the server where no subsequent code will run,
rather than when the subsequent interaction activity (e.g. placing a block in
an illegal position (BlockCanBuildEvent
) will fail.
Event.Result
限定符和类型 | 字段和说明 |
---|---|
protected Action |
action |
protected Block |
blockClicked |
protected BlockFace |
blockFace |
protected ItemStack |
item |
player
构造器和说明 |
---|
PlayerInteractEvent(Player who,
Action action,
ItemStack item,
Block clickedBlock,
BlockFace clickedFace) |
PlayerInteractEvent(Player who,
Action action,
ItemStack item,
Block clickedBlock,
BlockFace clickedFace,
EquipmentSlot hand) |
限定符和类型 | 方法和说明 |
---|---|
Action |
getAction()
获取本次交互的动作类型.
|
BlockFace |
getBlockFace()
返回被点击的方块的朝向.
|
Block |
getClickedBlock()
返回被点击的方块.
|
EquipmentSlot |
getHand()
获取用来进行本次交互的手, 但当玩家的交互动作类型为
Action.PHYSICAL 时可能为空. |
static HandlerList |
getHandlerList() |
HandlerList |
getHandlers() |
ItemStack |
getItem()
获取玩家手中的物品, 手中未拿物品时会返回null.
|
Material |
getMaterial()
返回玩家所用物品的材质 (相当于getItem()后再getMateria(),但更简便一些).
|
boolean |
hasBlock()
检查交互事件是否涉及方块(与方块交互、放置方块等).
|
boolean |
hasItem()
检查玩家交互时是否手拿物品或与物品交互/交互是否涉及到物品.
|
boolean |
isBlockInHand()
判断本次交互事件是否为方块放置事件 (由玩家放置方块引起的交互事件).
|
boolean |
isCancelled()
已过时。
This event has two possible cancellation states, one for
useInteractedBlock() and one for useItemInHand() . It is
possible a call might have the former false, but the latter true, eg in
the case of using a firework whilst gliding. Callers should check the
relevant methods individually. |
void |
setCancelled(boolean cancel)
取消这个事件.
|
void |
setUseInteractedBlock(Event.Result useInteractedBlock) |
void |
setUseItemInHand(Event.Result useItemInHand) |
Event.Result |
useInteractedBlock()
获取(控制见对应set方法)对被交互的方块采取的动作(如果有).
|
Event.Result |
useItemInHand()
获取(控制见下一个方法)对玩家手持的物品采取的动作/行动.
|
getPlayer
getEventName, isAsynchronous
protected ItemStack item
protected Action action
protected Block blockClicked
protected BlockFace blockFace
public PlayerInteractEvent(Player who, Action action, ItemStack item, Block clickedBlock, BlockFace clickedFace)
public Action getAction()
原文:Returns the action type
@Deprecated public boolean isCancelled()
useInteractedBlock()
and one for useItemInHand()
. It is
possible a call might have the former false, but the latter true, eg in
the case of using a firework whilst gliding. Callers should check the
relevant methods individually.原文:Gets the cancellation state of this event. Set to true if you want to prevent buckets from placing water and so forth
isCancelled
在接口中 Cancellable
public void setCancelled(boolean cancel)
取消本事件将阻止玩家食用食物(玩家不会失去食物), 阻止弓箭/雪球/蛋的发射等(玩家不会失去弹药).
原文:Sets the cancellation state of this event. A canceled event will not be executed in the server, but will still pass to other plugins
Canceling this event will prevent use of food (player won't lose the food item), prevent bows/snowballs/eggs from firing, etc. (player won't lose the ammo)
setCancelled
在接口中 Cancellable
cancel
- 设置为 true 时将会阻止本事件触发public ItemStack getItem()
原文:Returns the item in hand represented by this event
public Material getMaterial()
原文:Convenience method. Returns the material of the item represented by this event
public boolean hasBlock()
原文:Check if this event involved a block
public boolean hasItem()
猜测(译注):只要不是与空气交互一般都会返回一个物品?等待验证.
原文:Check if this event involved an item
public boolean isBlockInHand()
原文:Convenience method to inform the user whether this was a block placement event.
public Block getClickedBlock()
原文:Returns the clicked block
public BlockFace getBlockFace()
Returns the face of the block that was clicked
public Event.Result useInteractedBlock()
原文:This controls the action to take with the block (if any) that was clicked on. This event gets processed for all blocks, but most don't have a default action
public void setUseInteractedBlock(Event.Result useInteractedBlock)
useInteractedBlock
- 对被交互的方块采取的动作public Event.Result useItemInHand()
原文:This controls the action to take with the item the player is holding. This includes both blocks and items (such as flint and steel or records). When this is set to default, it will be allowed if no action is taken on the interacted block.
public void setUseItemInHand(Event.Result useItemInHand)
useItemInHand
- 对玩家手持的物品采取的动作(the action to take with the item in hand)public EquipmentSlot getHand()
Action.PHYSICAL
时可能为空.
原文:The hand used to perform this interaction. May be null in the case of
Action.PHYSICAL
.
public HandlerList getHandlers()
getHandlers
在类中 Event
public static HandlerList getHandlerList()