001 package org.bukkit.event.block; 002 003 import org.bukkit.block.Block; 004 import org.bukkit.block.BlockState; 005 import org.bukkit.entity.Player; 006 import org.bukkit.event.Cancellable; 007 import org.bukkit.event.HandlerList; 008 import org.bukkit.inventory.ItemStack; 009 010 /** 011 * Called when a block is placed by a player. 012 * <p> 013 * If a Block Place event is cancelled, the block will not be placed. 014 */ 015 public class BlockPlaceEvent extends BlockEvent implements Cancellable { 016 private static final HandlerList handlers = new HandlerList(); 017 protected boolean cancel; 018 protected boolean canBuild; 019 protected Block placedAgainst; 020 protected BlockState replacedBlockState; 021 protected ItemStack itemInHand; 022 protected Player player; 023 024 public BlockPlaceEvent(final Block placedBlock, final BlockState replacedBlockState, final Block placedAgainst, final ItemStack itemInHand, final Player thePlayer, final boolean canBuild) { 025 super(placedBlock); 026 this.placedAgainst = placedAgainst; 027 this.itemInHand = itemInHand; 028 this.player = thePlayer; 029 this.replacedBlockState = replacedBlockState; 030 this.canBuild = canBuild; 031 cancel = false; 032 } 033 034 public boolean isCancelled() { 035 return cancel; 036 } 037 038 public void setCancelled(boolean cancel) { 039 this.cancel = cancel; 040 } 041 042 /** 043 * Gets the player who placed the block involved in this event. 044 * 045 * @return The Player who placed the block involved in this event 046 */ 047 public Player getPlayer() { 048 return player; 049 } 050 051 /** 052 * Clarity method for getting the placed block. Not really needed except 053 * for reasons of clarity. 054 * 055 * @return The Block that was placed 056 */ 057 public Block getBlockPlaced() { 058 return getBlock(); 059 } 060 061 /** 062 * Gets the BlockState for the block which was replaced. Material type air 063 * mostly. 064 * 065 * @return The BlockState for the block which was replaced. 066 */ 067 public BlockState getBlockReplacedState() { 068 return this.replacedBlockState; 069 } 070 071 /** 072 * Gets the block that this block was placed against 073 * 074 * @return Block the block that the new block was placed against 075 */ 076 public Block getBlockAgainst() { 077 return placedAgainst; 078 } 079 080 /** 081 * Gets the item in the player's hand when they placed the block. 082 * 083 * @return The ItemStack for the item in the player's hand when they 084 * placed the block 085 */ 086 public ItemStack getItemInHand() { 087 return itemInHand; 088 } 089 090 /** 091 * Gets the value whether the player would be allowed to build here. 092 * Defaults to spawn if the server was going to stop them (such as, the 093 * player is in Spawn). Note that this is an entirely different check 094 * than BLOCK_CANBUILD, as this refers to a player, not universe-physics 095 * rule like cactus on dirt. 096 * 097 * @return boolean whether the server would allow a player to build here 098 */ 099 public boolean canBuild() { 100 return this.canBuild; 101 } 102 103 /** 104 * Sets the canBuild state of this event. Set to true if you want the 105 * player to be able to build. 106 * 107 * @param canBuild true if you want the player to be able to build 108 */ 109 public void setBuild(boolean canBuild) { 110 this.canBuild = canBuild; 111 } 112 113 @Override 114 public HandlerList getHandlers() { 115 return handlers; 116 } 117 118 public static HandlerList getHandlerList() { 119 return handlers; 120 } 121 }