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