From a783d0dfc8f4bfeb1a7f7116d17b2cb84187d72b Mon Sep 17 00:00:00 2001 From: Cory Redmond Date: Sun, 4 Oct 2015 21:39:22 +0100 Subject: [PATCH] Monitor falling blocks. --- .../nossr50/listeners/BlockListener.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 817f7abb0..e1c285ce6 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -11,6 +11,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; import org.bukkit.block.BrewingStand; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -20,6 +21,7 @@ import org.bukkit.event.block.BlockDamageEvent; 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.inventory.ItemStack; import com.gmail.nossr50.mcMMO; @@ -49,6 +51,8 @@ import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.skills.SkillUtils; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; public class BlockListener implements Listener { private final mcMMO plugin; @@ -124,6 +128,34 @@ public class BlockListener implements Listener { //new StickyPistonTrackerTask(direction, event.getBlock(), movedBlock).runTaskLater(plugin, 2); } + /** + * Monitor falling blocks. + * + * @param event The event to watch + */ + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onFallingBlock(EntityChangeBlockEvent event) { + + if (BlockUtils.shouldBeWatched(event.getBlock().getState()) && event.getEntityType().equals(EntityType.FALLING_BLOCK)) { + if (event.getTo().equals(Material.AIR) && mcMMO.getPlaceStore().isTrue(event.getBlock())) { + event.getEntity().setMetadata("mcMMOBlockFall", new FixedMetadataValue( plugin, event.getBlock().getLocation())); + } else { + List values = event.getEntity().getMetadata( "mcMMOBlockFall" ); + + if (!values.isEmpty()) { + + if (values.get(0).value() == null) return; + Block spawn = ((org.bukkit.Location) values.get(0).value()).getBlock(); + + + mcMMO.getPlaceStore().setTrue( event.getBlock() ); + mcMMO.getPlaceStore().setFalse( spawn ); + + } + } + } + } + /** * Monitor BlockPlace events. *