Added Block.getPistonMoveReaction, BlockPistonExtend and BlockPistonRetractEvent

By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
Bukkit/Spigot
2011-07-15 23:59:12 +02:00
parent 402794beff
commit 86ab32254f
9 changed files with 205 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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) {}
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}