Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PlayerInteractEvent.java
1 package org.bukkit.event.player;
2 
3 import org.bukkit.block.Block;
7 import org.bukkit.Material;
8 import org.bukkit.entity.Player;
10 import org.bukkit.event.block.Action;
11 
12 /**
13  * Called when a player interacts with an object or air.
14  */
15 public class PlayerInteractEvent extends PlayerEvent implements Cancellable {
16  private static final HandlerList handlers = new HandlerList();
17  protected ItemStack item;
18  protected Action action;
19  protected Block blockClicked;
20  protected BlockFace blockFace;
21  private Result useClickedBlock;
22  private Result useItemInHand;
23 
24  public PlayerInteractEvent(final Player who, final Action action, final ItemStack item, final Block clickedBlock, final BlockFace clickedFace) {
25  super(who);
26  this.action = action;
27  this.item = item;
28  this.blockClicked = clickedBlock;
29  this.blockFace = clickedFace;
30 
31  useItemInHand = Result.DEFAULT;
32  useClickedBlock = clickedBlock == null ? Result.DENY : Result.ALLOW;
33  }
34 
35  /**
36  * Returns the action type
37  *
38  * @return Action returns the type of interaction
39  */
40  public Action getAction() {
41  return action;
42  }
43 
44  /**
45  * Gets the cancellation state of this event. Set to true if you want to
46  * prevent buckets from placing water and so forth
47  *
48  * @return boolean cancellation state
49  */
50  public boolean isCancelled() {
51  return useInteractedBlock() == Result.DENY;
52  }
53 
54  /**
55  * Sets the cancellation state of this event. A canceled event will not be
56  * executed in the server, but will still pass to other plugins
57  * <p>
58  * Canceling this event will prevent use of food (player won't lose the
59  * food item), prevent bows/snowballs/eggs from firing, etc. (player won't
60  * lose the ammo)
61  *
62  * @param cancel true if you wish to cancel this event
63  */
64  public void setCancelled(boolean cancel) {
66  setUseItemInHand(cancel ? Result.DENY : useItemInHand() == Result.DENY ? Result.DEFAULT : useItemInHand());
67  }
68 
69  /**
70  * Returns the item in hand represented by this event
71  *
72  * @return ItemStack the item used
73  */
74  public ItemStack getItem() {
75  return this.item;
76  }
77 
78  /**
79  * Convenience method. Returns the material of the item represented by
80  * this event
81  *
82  * @return Material the material of the item used
83  */
84  public Material getMaterial() {
85  if (!hasItem()) {
86  return Material.AIR;
87  }
88 
89  return item.getType();
90  }
91 
92  /**
93  * Check if this event involved a block
94  *
95  * @return boolean true if it did
96  */
97  public boolean hasBlock() {
98  return this.blockClicked != null;
99  }
100 
101  /**
102  * Check if this event involved an item
103  *
104  * @return boolean true if it did
105  */
106  public boolean hasItem() {
107  return this.item != null;
108  }
109 
110  /**
111  * Convenience method to inform the user whether this was a block
112  * placement event.
113  *
114  * @return boolean true if the item in hand was a block
115  */
116  public boolean isBlockInHand() {
117  if (!hasItem()) {
118  return false;
119  }
120 
121  return item.getType().isBlock();
122  }
123 
124  /**
125  * Returns the clicked block
126  *
127  * @return Block returns the block clicked with this item.
128  */
130  return blockClicked;
131  }
132 
133  /**
134  * Returns the face of the block that was clicked
135  *
136  * @return BlockFace returns the face of the block that was clicked
137  */
139  return blockFace;
140  }
141 
142  /**
143  * This controls the action to take with the block (if any) that was
144  * clicked on. This event gets processed for all blocks, but most don't
145  * have a default action
146  *
147  * @return the action to take with the interacted block
148  */
150  return useClickedBlock;
151  }
152 
153  /**
154  * @param useInteractedBlock the action to take with the interacted block
155  */
157  this.useClickedBlock = useInteractedBlock;
158  }
159 
160  /**
161  * This controls the action to take with the item the player is holding.
162  * This includes both blocks and items (such as flint and steel or
163  * records). When this is set to default, it will be allowed if no action
164  * is taken on the interacted block.
165  *
166  * @return the action to take with the item in hand
167  */
169  return useItemInHand;
170  }
171 
172  /**
173  * @param useItemInHand the action to take with the item in hand
174  */
175  public void setUseItemInHand(Result useItemInHand) {
176  this.useItemInHand = useItemInHand;
177  }
178 
179  @Override
180  public HandlerList getHandlers() {
181  return handlers;
182  }
183 
184  public static HandlerList getHandlerList() {
185  return handlers;
186  }
187 }
void setUseInteractedBlock(Result useInteractedBlock)