diff options
author | Erik Broes <erikbroes@grum.nl> | 2011-07-15 23:59:12 +0200 |
---|---|---|
committer | Erik Broes <erikbroes@grum.nl> | 2011-07-17 17:02:37 +0200 |
commit | 36d901c6b17f04df8eeb2edd35695aa124853ac0 (patch) | |
tree | 34d860f42c460e9358d64c4fc9d46728bfa1e683 /src | |
parent | 2a7dd93cb585bf27ff8cf14f198cd626ea6f3e57 (diff) | |
download | bukkit-36d901c6b17f04df8eeb2edd35695aa124853ac0.tar bukkit-36d901c6b17f04df8eeb2edd35695aa124853ac0.tar.gz bukkit-36d901c6b17f04df8eeb2edd35695aa124853ac0.tar.lz bukkit-36d901c6b17f04df8eeb2edd35695aa124853ac0.tar.xz bukkit-36d901c6b17f04df8eeb2edd35695aa124853ac0.zip |
Added Block.getPistonMoveReaction, BlockPistonExtend and BlockPistonRetractEvent
Diffstat (limited to 'src')
9 files changed, 205 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/block/Block.java b/src/main/java/org/bukkit/block/Block.java index 391545b2..05fb9ca8 100644 --- a/src/main/java/org/bukkit/block/Block.java +++ b/src/main/java/org/bukkit/block/Block.java @@ -67,6 +67,15 @@ public interface Block { Block getRelative(BlockFace face); /** + * Gets the block at the given offsets + * + * @param face face + * @param distance distance + * @return Block at the given offset and distance + */ + Block getRelative(BlockFace face, int distance); + + /** * Gets the type of this block * * @return block type @@ -268,4 +277,11 @@ public interface Block { * @return Humidity of this block */ double getHumidity(); + + /** + * Returns the reaction of the block when moved by a piston + * + * @return reaction + */ + PistonMoveReaction getPistonMoveReaction(); } diff --git a/src/main/java/org/bukkit/block/PistonMoveReaction.java b/src/main/java/org/bukkit/block/PistonMoveReaction.java new file mode 100644 index 00000000..83064122 --- /dev/null +++ b/src/main/java/org/bukkit/block/PistonMoveReaction.java @@ -0,0 +1,30 @@ +package org.bukkit.block; + +import java.util.HashMap; +import java.util.Map; + +public enum PistonMoveReaction { + MOVE(0), + BREAK(1), + BLOCK(2); + + private int id; + private static Map<Integer, PistonMoveReaction> byId = new HashMap<Integer, PistonMoveReaction>(); + static { + for (PistonMoveReaction reaction: PistonMoveReaction.values()) { + byId.put(reaction.id, reaction); + } + } + + private PistonMoveReaction(int id) { + this.id = id; + } + + public int getId() { + return this.id; + } + + public static PistonMoveReaction getById(int id) { + return byId.get(id); + } +} diff --git a/src/main/java/org/bukkit/event/Cancellable.java b/src/main/java/org/bukkit/event/Cancellable.java index 91407a9a..3fc13fc8 100644 --- a/src/main/java/org/bukkit/event/Cancellable.java +++ b/src/main/java/org/bukkit/event/Cancellable.java @@ -1,6 +1,19 @@ package org.bukkit.event; public interface Cancellable { + /** + * 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(); + + /** + * 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); } diff --git a/src/main/java/org/bukkit/event/Event.java b/src/main/java/org/bukkit/event/Event.java index 1d725598..80e03418 100644 --- a/src/main/java/org/bukkit/event/Event.java +++ b/src/main/java/org/bukkit/event/Event.java @@ -404,6 +404,18 @@ public abstract class Event implements Serializable { * @see org.bukkit.event.block.BlockFadeEvent */ BLOCK_FADE (Category.BLOCK), + /** + * Called when a piston extends + * + * @see org.bukkit.event.block.PistonExtendEvent + */ + BLOCK_PISTON_EXTEND (Category.BLOCK), + /** + * Called when a piston retracts + * + * @see org.bukkit.event.block.PistonRetractEvent + */ + BLOCK_PISTON_RETRACT (Category.BLOCK), /** * INVENTORY EVENTS diff --git a/src/main/java/org/bukkit/event/block/BlockListener.java b/src/main/java/org/bukkit/event/block/BlockListener.java index f98fe508..1b51b0f2 100644 --- a/src/main/java/org/bukkit/event/block/BlockListener.java +++ b/src/main/java/org/bukkit/event/block/BlockListener.java @@ -183,4 +183,18 @@ public class BlockListener implements Listener { * @param event Relevant event details */ public void onBlockDispense(BlockDispenseEvent event) {} + + /** + * Called when a piston retracts + * + * @param event Relevant event details + */ + public void onBlockPistonRetract(BlockPistonRetractEvent event) {} + + /** + * Called when a piston extends + * + * @param event Relevant event details + */ + public void onBlockPistonExtend(BlockPistonExtendEvent event) {} } diff --git a/src/main/java/org/bukkit/event/block/BlockPistonEvent.java b/src/main/java/org/bukkit/event/block/BlockPistonEvent.java new file mode 100644 index 00000000..3ffaea7e --- /dev/null +++ b/src/main/java/org/bukkit/event/block/BlockPistonEvent.java @@ -0,0 +1,43 @@ +package org.bukkit.event.block; + +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.event.Cancellable; +import org.bukkit.material.PistonBaseMaterial; + +public abstract class BlockPistonEvent extends BlockEvent implements Cancellable { + private boolean cancelled; + + public BlockPistonEvent(Type type, Block block) { + super(type, block); + } + + public boolean isCancelled() { + return this.cancelled; + } + + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + /** + * Returns true if the Piston in the event is sticky. + * + * @return stickiness of the piston + */ + public boolean isSticky() { + return block.getType() == Material.PISTON_STICKY_BASE; + } + + /** + * Return the direction in which the piston will operate. + * + * @return direction of the piston + */ + public BlockFace getDirection() { + // Both are meh! + // return ((PistonBaseMaterial) block.getType().getNewData(block.getData ())).getFacing(); + return ((PistonBaseMaterial) block.getState().getData()).getFacing(); + } +} diff --git a/src/main/java/org/bukkit/event/block/BlockPistonExtendEvent.java b/src/main/java/org/bukkit/event/block/BlockPistonExtendEvent.java new file mode 100644 index 00000000..1643120d --- /dev/null +++ b/src/main/java/org/bukkit/event/block/BlockPistonExtendEvent.java @@ -0,0 +1,43 @@ +package org.bukkit.event.block; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.bukkit.block.Block; + +public class BlockPistonExtendEvent extends BlockPistonEvent { + private int length; + private List<Block> blocks; + + public BlockPistonExtendEvent(Block block, int length) { + super(Type.BLOCK_PISTON_EXTEND, block); + + this.length = length; + } + + /** + * Get the amount of blocks which will be moved while extending. + * + * @return the amount of moving blocks + */ + public int getLength() { + return this.length; + } + + /** + * Get an immutable list of the blocks which will be moved by the extending. + * + * @return Immutable list of the moved blocks. + */ + public List<Block> getBlocks() { + if (blocks == null) { + ArrayList<Block> tmp = new ArrayList<Block>(); + for (int i = 0; i < this.getLength(); i++) { + tmp.add(block.getRelative(getDirection(), i + 1)); + } + blocks = Collections.unmodifiableList(tmp); + } + return blocks; + } +} diff --git a/src/main/java/org/bukkit/event/block/BlockPistonRetractEvent.java b/src/main/java/org/bukkit/event/block/BlockPistonRetractEvent.java new file mode 100644 index 00000000..d57fc6b7 --- /dev/null +++ b/src/main/java/org/bukkit/event/block/BlockPistonRetractEvent.java @@ -0,0 +1,20 @@ +package org.bukkit.event.block; + +import org.bukkit.Location; +import org.bukkit.block.Block; + +public class BlockPistonRetractEvent extends BlockPistonEvent { + public BlockPistonRetractEvent(Block block) { + super(Type.BLOCK_PISTON_RETRACT, block); + } + + /** + * Gets the location where the possible moving block might be if the retracting + * piston is sticky. + * + * @return The possible location of the possibly moving block. + */ + public Location getRetractLocation() { + return getBlock().getRelative(getDirection(), 2).getLocation(); + } +} diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index e29c3018..e0b3b53e 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -527,6 +527,20 @@ public final class JavaPluginLoader implements PluginLoader { } }; + case BLOCK_PISTON_RETRACT: + return new EventExecutor() { + public void execute(Listener listener, Event event) { + ((BlockListener) listener).onBlockPistonRetract((BlockPistonRetractEvent) event); + } + }; + + case BLOCK_PISTON_EXTEND: + return new EventExecutor() { + public void execute(Listener listener, Event event) { + ((BlockListener) listener).onBlockPistonExtend((BlockPistonExtendEvent) event); + } + }; + // Server Events case PLUGIN_ENABLE: return new EventExecutor() { |