From 23525692e12afa2d08396a83be10370d34075c1d Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Thu, 26 Sep 2013 10:02:29 +0200 Subject: [PATCH] Handle falling Blocks (Sand/Gravel) --- .../limitedcreative/ModBlockStates.java | 2 + .../blockstate/BlockListener.java | 40 +++++++++++++++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java index fcd8515..9a72994 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java @@ -5,6 +5,7 @@ import org.bukkit.event.Listener; import de.jaschastarke.bukkit.lib.CoreModule; import de.jaschastarke.bukkit.lib.commands.AliasHelpedCommand; import de.jaschastarke.bukkit.lib.modules.AdditionalBlockBreaks; +import de.jaschastarke.bukkit.lib.modules.BlockFall; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockListener; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockStateCommand; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockStateConfig; @@ -47,6 +48,7 @@ public class ModBlockStates extends CoreModule { if (plugin.getModule(AdditionalBlockBreaks.class) == null) { plugin.addModule(new AdditionalBlockBreaks(plugin)); } + addModule(new BlockFall(plugin)); listeners.addListener(new BlockListener(this)); listeners.addListener(new HangingListener(this)); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java index 7d4e5c0..818a7c2 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java @@ -8,6 +8,8 @@ import java.util.Map; import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.FallingBlock; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -15,17 +17,23 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPistonExtendEvent; import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; import de.jaschastarke.bukkit.lib.events.BlockDestroyedEvent; +import de.jaschastarke.bukkit.lib.events.BlockMovedEvent; import de.jaschastarke.minecraft.limitedcreative.ModBlockStates; import de.jaschastarke.minecraft.limitedcreative.blockstate.DBModel.DBTransaction; public class BlockListener implements Listener { private ModBlockStates mod; + private MetadataValue blockAlreadExtended; + public BlockListener(ModBlockStates mod) { this.mod = mod; + blockAlreadExtended = new FixedMetadataValue(mod.getPlugin(), new Boolean(true)); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -34,11 +42,9 @@ public class BlockListener implements Listener { return; if (mod.getModel().isRestricted(event.getBlock())) { if (mod.isDebug()) - mod.getLog().debug("Breaking bad, err.. block: " + event.getBlock().getLocation().toString()); + mod.getLog().debug("Breaking bad, err.. block: " + event.getBlock().getLocation().toString() + " was placed by creative. Drop prevented"); if (event.getPlayer().getGameMode() != GameMode.CREATIVE) { - if (mod.isDebug()) - mod.getLog().debug("... was placed by creative. Drop prevented"); mod.getBlockSpawn().block(event.getBlock(), event.getPlayer()); event.setExpToDrop(0); } @@ -52,14 +58,32 @@ public class BlockListener implements Listener { return; if (mod.getModel().isRestricted(event.getBlock())) { if (mod.isDebug()) - mod.getLog().debug("Breaking attached block: " + event.getBlock().getLocation().toString()); + mod.getLog().debug("Breaking attached block: " + event.getBlock().getLocation().toString() + " was placed by creative. Drop prevented"); - if (mod.isDebug()) - mod.getLog().debug("... was placed by creative. Drop prevented"); mod.getBlockSpawn().block(event.getBlock()); } mod.getModel().removeState(event.getBlock()); } + + @EventHandler + public void onBlockMoved(BlockMovedEvent event) { + if (mod.getConfig().getIgnoredWorlds().contains(event.getBlock().getWorld().getName())) + return; + mod.getModel().moveState(event.getSource(), event.getBlock()); + } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityChangeBlock(final EntityChangeBlockEvent event) { + if (event.getEntityType() == EntityType.FALLING_BLOCK) { + if (event.getTo() == Material.AIR) { + if (mod.getModel().isRestricted(event.getBlock())) { + if (mod.isDebug()) + mod.getLog().debug("Falling block: " + event.getBlock().getLocation().toString() + " was placed by creative (drop prevented)"); + FallingBlock fe = (FallingBlock) event.getEntity(); + fe.setDropItem(false); + } + } + } + } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlocksBreakByExplosion(EntityExplodeEvent event) { @@ -69,7 +93,7 @@ public class BlockListener implements Listener { DBTransaction update = mod.getModel().groupUpdate(); for (Block block : event.blockList()) { if (mod.isDebug()) - mod.getLog().debug("Breaking bad, err.. block: " + block.getLocation().toString()); + mod.getLog().debug("Breaking bad, err.. block by explosion: " + block.getLocation().toString()); if (states.get(block)) { if (mod.isDebug()) @@ -102,7 +126,7 @@ public class BlockListener implements Listener { return; if (event.getBlock().getMetadata("LCBS_pistonIsAlreadyExtended").size() > 0) // Fixes long known Bukkit issue return; - event.getBlock().setMetadata("LCBS_pistonIsAlreadyExtended", new FixedMetadataValue(mod.getPlugin(), new Boolean(true))); + event.getBlock().setMetadata("LCBS_pistonIsAlreadyExtended", blockAlreadExtended); Block source = event.getBlock().getRelative(event.getDirection()); /*if (mod.isDebug())