From b43d8d60a671a7375acae9542cf42e945211aed5 Mon Sep 17 00:00:00 2001 From: durron597 Date: Sat, 8 Jan 2011 05:44:42 -0500 Subject: Implemented BlockInteract, BlockRightClicked and PlayerItem. Removed BlockItem as we're not doing it that way anymore. --- src/main/java/org/bukkit/event/Event.java | 34 +++------ .../org/bukkit/event/block/BlockInteractEvent.java | 69 ++++++++++++++++++ .../java/org/bukkit/event/block/BlockListener.java | 16 ++++ .../org/bukkit/event/block/BlockPlacedEvent.java | 83 +++++++++++++++++---- .../bukkit/event/block/BlockRightClickedEvent.java | 59 +++++++++++++++ .../bukkit/event/player/PlayerBlockItemEvent.java | 85 ---------------------- .../org/bukkit/event/player/PlayerItemEvent.java | 38 +++++++++- .../org/bukkit/event/player/PlayerListener.java | 8 ++ .../org/bukkit/plugin/java/JavaPluginLoader.java | 12 +++ 9 files changed, 282 insertions(+), 122 deletions(-) create mode 100644 src/main/java/org/bukkit/event/block/BlockInteractEvent.java create mode 100644 src/main/java/org/bukkit/event/block/BlockRightClickedEvent.java delete mode 100644 src/main/java/org/bukkit/event/player/PlayerBlockItemEvent.java (limited to 'src') diff --git a/src/main/java/org/bukkit/event/Event.java b/src/main/java/org/bukkit/event/Event.java index f6bf4236..05cd96a0 100644 --- a/src/main/java/org/bukkit/event/Event.java +++ b/src/main/java/org/bukkit/event/Event.java @@ -169,31 +169,9 @@ public abstract class Event { * Called when a player undergoes an animation, such as arm swinging */ PLAYER_ANIMATION (Category.PLAYER), - - /** - * Always called when a player uses an item while pointing at a block - * Sometimes, cancelling this event doesn't do anything. - * - * This is the event that is called on block placement. Cancel this - * to prevent block placement. This will ALWAYS be called, even if - * universe physics prevents the block from being placed. This allows - * you to add functionality to rightclicking with block items even - * if the universe won't allow them to get placed. Use BLOCK_CANBUILD - * to override notch's block placement rules. - * - * Example: This event is also called, for example when redstone is - * placed, when a sign is placed, when minecarts are placed on a track, - * when boats are placed (in both water and air) - */ - PLAYER_BLOCKITEM (Category.PLAYER), /** - * Called when a player uses an item while pointing at the air - * This can also be additionally called while pointing at the ground - * - * Example: all food will also call this event while pointing at the - * ground, bows/snowballs/eggs will all call this while pointing at - * the ground, buckets call this event. + * Called when a player uses an item */ PLAYER_ITEM (Category.PLAYER), @@ -239,11 +217,21 @@ public abstract class Event { */ BLOCK_PHYSICS (Category.BLOCK), + /** + * Called when a player is attempting to place a block + */ + BLOCK_RIGHTCLICKED (Category.BLOCK), + /** * Called when a player is attempting to place a block */ BLOCK_PLACED (Category.BLOCK), + /** + * Called when an entity interacts with a block (lever, door, pressure plate, chest, furnace) + */ + BLOCK_INTERACT (Category.BLOCK), + /** * Called when leaves are decaying naturally */ diff --git a/src/main/java/org/bukkit/event/block/BlockInteractEvent.java b/src/main/java/org/bukkit/event/block/BlockInteractEvent.java new file mode 100644 index 00000000..7587e329 --- /dev/null +++ b/src/main/java/org/bukkit/event/block/BlockInteractEvent.java @@ -0,0 +1,69 @@ +package org.bukkit.event.block; + +import org.bukkit.Block; +import org.bukkit.Entity; +import org.bukkit.Player; +import org.bukkit.event.Cancellable; + +/** + * This event is triggered whenever an entity interacts with the universe + * it's always called, on a left click or a right click, or walking on + * (as in the case of pressure plates). Use cancellable to prevent things + * from happening (doors opening, buttons, pressure plates being walked + * on, etc). Note: even though pressure plates work totally differently + * than the other interact events, it's still thrown in with this event. + * + * @author durron597 + */ +public class BlockInteractEvent extends BlockEvent implements Cancellable { + protected boolean cancel; + protected Entity theEntity; + + /** + * @param type The type of this event + * @param interactedBlock the block that was interacted with + * @param who The entity that interacted with + */ + public BlockInteractEvent(Type type, Block interactedBlock, Entity who) { + super(type, interactedBlock); + theEntity = who; + } + + /** + * Gets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + * + * @return true if this event is cancelled + */ + public boolean isCancelled() { + return cancel; + } + + /** + * Sets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + * + * @param cancel true if you wish to cancel this event + */ + public void setCancelled(boolean cancel) { + this.cancel = cancel; + } + + /** + * Returns the entity that triggered this event + * + * @return Entity the entity that triggered this event + */ + public Entity getEntity() { + return theEntity; + } + + /** + * Convenience method for seeing if this event was triggered by a player + * + * @return boolean whether this event was triggered by a player + */ + public boolean isPlayer() { + return theEntity instanceof Player; + } +} diff --git a/src/main/java/org/bukkit/event/block/BlockListener.java b/src/main/java/org/bukkit/event/block/BlockListener.java index a26c42c2..d7ee313e 100644 --- a/src/main/java/org/bukkit/event/block/BlockListener.java +++ b/src/main/java/org/bukkit/event/block/BlockListener.java @@ -59,6 +59,22 @@ public class BlockListener implements Listener { */ public void onBlockPlaced(BlockPlacedEvent event) { } + + /** + * Called when a block is interacted with + * + * @param event Relevant event details + */ + public void onBlockInteracted(BlockInteractEvent event) { + } + + /** + * Called when a player right clicks a block + * + * @param event Relevant event details + */ + public void onBlockRightClicked(BlockRightClickedEvent event) { + } /** * Called when redstone changes diff --git a/src/main/java/org/bukkit/event/block/BlockPlacedEvent.java b/src/main/java/org/bukkit/event/block/BlockPlacedEvent.java index 6bc18968..586f35bd 100644 --- a/src/main/java/org/bukkit/event/block/BlockPlacedEvent.java +++ b/src/main/java/org/bukkit/event/block/BlockPlacedEvent.java @@ -1,6 +1,7 @@ package org.bukkit.event.block; import org.bukkit.Block; +import org.bukkit.ItemStack; import org.bukkit.Player; import org.bukkit.event.Cancellable; @@ -8,23 +9,21 @@ import org.bukkit.event.Cancellable; * Not implemented yet */ public class BlockPlacedEvent extends BlockEvent implements Cancellable { - private boolean cancel; - private Player player; + protected boolean cancel; + protected boolean canBuild; + protected Block placedAgainst; + protected ItemStack itemInHand; + protected Player player; - public BlockPlacedEvent(Type type, Block theBlock) { - super(type, theBlock); + public BlockPlacedEvent(Type type, Block placedBlock, Block placedAgainst, ItemStack itemInHand, Player thePlayer, boolean canBuild) { + super(type, placedBlock); + this.placedAgainst = placedAgainst; + this.itemInHand = itemInHand; + this.player = thePlayer; + this.canBuild = canBuild; cancel = false; } - /** - * Gets the player who placed this block - * - * @return Player who placed the block - */ - public Player getPlayer() { - return player; - } - /** * Gets the cancellation state of this event. A cancelled event will not * be executed in the server, but will still pass to other plugins @@ -45,4 +44,62 @@ public class BlockPlacedEvent extends BlockEvent implements Cancellable { this.cancel = cancel; } + /** + * Gets the player who placed this block + * + * @return Player who placed the block + */ + public Player getPlayer() { + return player; + } + + /** + * Clarity method for getting the placed block. Not really needed + * except for reasons of clarity + * + * @return Block the block that was placed + */ + public Block getBlockPlaced() { + return getBlock(); + } + + + /** + * Get the block that this block was placed against + * + * @return Block the block that the new block was placed against + */ + public Block getBlockAgainst() { + return placedAgainst; + } + + /** + * Returns the item in your hand when you placed the block + * + * @return ItemStack the item in your hand when placing the block + */ + public ItemStack getItemInHand() { + return itemInHand; + } + + /** + * Gets the value whether the player would be allowed to build here. + * Defaults to spawn if the server was going to stop them (such as, the + * player is in Spawn). Note that this is an entirely different check + * than BLOCK_CANBUILD, as this refers to a player, not universe-physics + * rule like cactus on dirt. + * + * @return boolean whether the server would allow a player to build here + */ + public boolean canBuild() { + return this.canBuild; + } + + /** + * Sets the canBuild state of this event. Set to true if you want the + * player to be able to build. + */ + public void setBuild(boolean canBuild) { + this.canBuild = canBuild; + } } diff --git a/src/main/java/org/bukkit/event/block/BlockRightClickedEvent.java b/src/main/java/org/bukkit/event/block/BlockRightClickedEvent.java new file mode 100644 index 00000000..6a11acac --- /dev/null +++ b/src/main/java/org/bukkit/event/block/BlockRightClickedEvent.java @@ -0,0 +1,59 @@ +package org.bukkit.event.block; + +import org.bukkit.Block; +import org.bukkit.BlockFace; +import org.bukkit.ItemStack; +import org.bukkit.Player; + +/** + * Not implemented yet + */ +public class BlockRightClickedEvent extends BlockEvent { + protected Block clickedBlock; + protected BlockFace direction; + protected ItemStack itemInHand; + protected Player player; + + public BlockRightClickedEvent(Type type, Block placedAgainst, BlockFace direction, ItemStack itemInHand, Player thePlayer) { + super(type, placedAgainst); + this.clickedBlock = placedAgainst; + this.direction = direction; + this.itemInHand = itemInHand; + this.player = thePlayer; + } + + /** + * Gets the player who placed this block + * + * @return Player who placed the block + */ + public Player getPlayer() { + return player; + } + + + /** + * Get the block that this block was placed against + * + * @return Block the block that the new block was placed against + */ + public Block getBlockAgainst() { + return clickedBlock; + } + + /** + * @return BlockFace the direction this block was clicked + */ + public BlockFace getDirection() { + return direction; + } + + /** + * Returns the item in your hand when you placed the block + * + * @return ItemStack the item in your hand when placing the block + */ + public ItemStack getItemInHand() { + return itemInHand; + } +} diff --git a/src/main/java/org/bukkit/event/player/PlayerBlockItemEvent.java b/src/main/java/org/bukkit/event/player/PlayerBlockItemEvent.java deleted file mode 100644 index 766296e3..00000000 --- a/src/main/java/org/bukkit/event/player/PlayerBlockItemEvent.java +++ /dev/null @@ -1,85 +0,0 @@ -package org.bukkit.event.player; - -import org.bukkit.Block; -import org.bukkit.BlockFace; -import org.bukkit.ItemStack; -import org.bukkit.Player; -import org.bukkit.event.Cancellable; - -/** - * Represents an event that a block was clicked with an item. - * - * Note: while this is the event that is thrown on block placement, there is no - * BlockPlaced associated with this event. This is because the event is thrown - * before the block is written to the universe, so the returned block would not - * be the new placed block. In hMod, BlockPlaced worked by UNDOING the block - * placement; in Bukkit, we catch the event before it even gets written to the - * universe, so the concept of a placed block is meaningless. - * - * To get the type of block that's being placed, use the method getItem (for - * the item in your hand). - * - * @author durron597 - */ -public class PlayerBlockItemEvent extends PlayerItemEvent implements Cancellable { - protected Block blockClicked; - protected BlockFace direction; - protected boolean canBuild; - - public PlayerBlockItemEvent(Type type, Player who, ItemStack item, Block blockClicked, BlockFace direction, boolean canBuild) { - super(type, who, item); - this.blockClicked = blockClicked; - this.canBuild = canBuild; - } - - /** - * Convenience method to inform the user whether this was a block placement - * event. - * - * @return boolean true if the item in hand was a block - */ - public boolean isBlock() { - if (item == null) return false; - - return item.getType().isBlock(); - } - - /** - * Returns the clicked block - * - * @return Block returns the block clicked with this item. - */ - public Block getBlockClicked() { - return blockClicked; - } - - /** - * Returns the face of the block that was clicked - * - * @return BlockFace returns the face of the block that was clicked - */ - public BlockFace getBlockFace() { - return direction; - } - - /** - * Gets the value whether the player would be allowed to build here. - * Defaults to spawn if the server was going to stop them (such as, the - * player is in Spawn). Note that this is an entirely different check - * than BLOCK_CANBUILD, as this refers to a player, not universe-physics - * rule like cactus on dirt. - * - * @return boolean whether the server would allow a player to build here - */ - public boolean canBuild() { - return this.canBuild; - } - - /** - * Sets the canBuild state of this event. Set to true if you want the - * player to be able to build. - */ - public void setBuild(boolean canBuild) { - this.canBuild = canBuild; - } -} diff --git a/src/main/java/org/bukkit/event/player/PlayerItemEvent.java b/src/main/java/org/bukkit/event/player/PlayerItemEvent.java index 842a0eb0..42bb3f59 100644 --- a/src/main/java/org/bukkit/event/player/PlayerItemEvent.java +++ b/src/main/java/org/bukkit/event/player/PlayerItemEvent.java @@ -1,5 +1,7 @@ package org.bukkit.event.player; +import org.bukkit.Block; +import org.bukkit.BlockFace; import org.bukkit.ItemStack; import org.bukkit.Material; import org.bukkit.Player; @@ -13,11 +15,15 @@ import org.bukkit.event.Cancellable; public class PlayerItemEvent extends PlayerEvent implements Cancellable { protected ItemStack item; protected boolean cancel; + protected Block blockClicked; + protected BlockFace blockFace; - public PlayerItemEvent(Type type, Player who, ItemStack item) { + public PlayerItemEvent(Type type, Player who, ItemStack item, Block blockClicked, BlockFace blockFace) { super(type, who); this.item = item; cancel = false; + this.blockClicked = blockClicked; + this.blockFace = blockFace; } /** @@ -64,4 +70,34 @@ public class PlayerItemEvent extends PlayerEvent implements Cancellable { return item.getType(); } + + /** + * Convenience method to inform the user whether this was a block placement + * event. + * + * @return boolean true if the item in hand was a block + */ + public boolean isBlock() { + if (item == null) return false; + + return item.getType().isBlock(); + } + + /** + * Returns the clicked block + * + * @return Block returns the block clicked with this item. + */ + public Block getBlockClicked() { + return blockClicked; + } + + /** + * Returns the face of the block that was clicked + * + * @return BlockFace returns the face of the block that was clicked + */ + public BlockFace getBlockFace() { + return blockFace; + } } diff --git a/src/main/java/org/bukkit/event/player/PlayerListener.java b/src/main/java/org/bukkit/event/player/PlayerListener.java index 0c6817e2..b5da4b97 100644 --- a/src/main/java/org/bukkit/event/player/PlayerListener.java +++ b/src/main/java/org/bukkit/event/player/PlayerListener.java @@ -57,6 +57,14 @@ public class PlayerListener implements Listener { */ public void onPlayerTeleport(PlayerMoveEvent event) { } + + /** + * Called when a player uses an item + * + * @param event Relevant event details + */ + public void onPlayerItem(PlayerItemEvent event) { + } /** * Called when a player attempts to log in to the server diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index f0d7d13c..fd68f0c4 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -108,6 +108,9 @@ public final class JavaPluginLoader implements PluginLoader { case PLAYER_TELEPORT: trueListener.onPlayerTeleport((PlayerMoveEvent)event); break; + case PLAYER_ITEM: + trueListener.onPlayerItem((PlayerItemEvent)event); + break; case PLAYER_LOGIN: trueListener.onPlayerLogin((PlayerLoginEvent)event); break; @@ -122,6 +125,15 @@ public final class JavaPluginLoader implements PluginLoader { case BLOCK_CANBUILD: trueListener.onBlockCanBuild((BlockCanBuildEvent)event); break; + case BLOCK_RIGHTCLICKED: + trueListener.onBlockRightClicked((BlockRightClickedEvent) event); + break; + case BLOCK_PLACED: + trueListener.onBlockPlaced((BlockPlacedEvent)event); + break; + case BLOCK_INTERACT: + trueListener.onBlockInteracted((BlockInteractEvent)event); + break; case BLOCK_FLOW: trueListener.onBlockFlow((BlockFromToEvent)event); break; -- cgit v1.2.3