From f6f94571ea1fa9b1f3a61bb245f2a9de4cea20cd Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 18 Aug 2014 21:37:41 -0700 Subject: [PATCH] Handle pistons in the event abstraction. --- .../listener/EventAbstractionListener.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java index 03229407..d7c86244 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java @@ -60,6 +60,8 @@ import org.bukkit.event.block.BlockFromToEvent; import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockIgniteEvent.IgniteCause; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.entity.CreatureSpawnEvent; @@ -95,6 +97,7 @@ import org.bukkit.util.Vector; import javax.annotation.Nullable; +import java.util.ArrayList; import java.util.List; import static com.sk89q.worldguard.bukkit.cause.Cause.create; @@ -200,7 +203,27 @@ public void onEntityExplode(EntityExplodeEvent event) { Events.fireBulkEventToCancel(event, new BreakBlockEvent(event, create(entity), event.getLocation().getWorld(), event.blockList(), Material.AIR)); } - // TODO: Handle pistons + @EventHandler + public void onBlockPistonRetract(BlockPistonRetractEvent event) { + if (event.isSticky()) { + Cause cause = create(event.getBlock()); + Events.fireToCancel(event, new BreakBlockEvent(event, cause, event.getRetractLocation(), Material.AIR)); + Events.fireToCancel(event, new PlaceBlockEvent(event, cause, event.getBlock().getRelative(event.getDirection()))); + } + } + + @EventHandler + public void onBlockPistonExtend(BlockPistonExtendEvent event) { + // A hack for now + List blocks = new ArrayList(event.getBlocks()); + Block lastBlock = event.getBlock().getRelative(event.getDirection(), event.getLength() + 1); + blocks.add(lastBlock); + int originalLength = blocks.size(); + Events.fireBulkEventToCancel(event, new PlaceBlockEvent(event, create(event.getBlock()), event.getBlock().getWorld(), blocks, Material.STONE)); + if (blocks.size() != originalLength) { + event.setCancelled(true); + } + } //------------------------------------------------------------------------- // Block external interaction