From e7ef6af0127612b9f5771c23cdc1055b5dd40756 Mon Sep 17 00:00:00 2001 From: wizjany Date: Sun, 12 May 2019 00:17:34 -0400 Subject: [PATCH] Handle BlockExplodeEvent as other explosion. Also misc cleanup. Closes #407, WORLDGUARD-3843. --- .../listener/EventAbstractionListener.java | 6 ++- .../listener/WorldGuardBlockListener.java | 41 ++++++++++++++----- .../listener/WorldGuardEntityListener.java | 4 +- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java index 9119b74e..3c1f7e57 100644 --- a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java +++ b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java @@ -960,8 +960,10 @@ public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event){ @EventHandler(ignoreCancelled = true) public void onBlockExplode(BlockExplodeEvent event) { - Events.fireBulkEventToCancel(event, new BreakBlockEvent(event, create(event.getBlock()), - event.getBlock().getLocation().getWorld(), event.blockList(), Material.AIR)); + final BreakBlockEvent eventToFire = new BreakBlockEvent(event, create(event.getBlock()), + event.getBlock().getLocation().getWorld(), event.blockList(), Material.AIR); + eventToFire.getRelevantFlags().add(Flags.OTHER_EXPLOSION); + Events.fireBulkEventToCancel(event, eventToFire); } /** diff --git a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardBlockListener.java b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardBlockListener.java index a2b2aa16..77c45319 100644 --- a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardBlockListener.java +++ b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardBlockListener.java @@ -32,7 +32,6 @@ import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.util.SpongeUtil; import org.bukkit.Material; -import org.bukkit.Tag; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -42,6 +41,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBurnEvent; +import org.bukkit.event.block.BlockExplodeEvent; import org.bukkit.event.block.BlockFadeEvent; import org.bukkit.event.block.BlockFormEvent; import org.bukkit.event.block.BlockFromToEvent; @@ -54,6 +54,8 @@ import org.bukkit.event.block.EntityBlockFormEvent; import org.bukkit.event.block.LeavesDecayEvent; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; /** * The listener for block events. @@ -86,7 +88,7 @@ public void registerEvents() { * @param world The world to get the configuration for. * @return The configuration for {@code world} */ - protected WorldConfiguration getWorldConfig(World world) { + private WorldConfiguration getWorldConfig(World world) { return WorldGuard.getInstance().getPlatform().getGlobalStateManager().get(BukkitAdapter.adapt(world)); } @@ -96,7 +98,7 @@ protected WorldConfiguration getWorldConfig(World world) { * @param player The player to get the wold from * @return The {@link BukkitWorldConfiguration} for the player's world */ - protected WorldConfiguration getWorldConfig(Player player) { + private WorldConfiguration getWorldConfig(Player player) { return getWorldConfig(player.getWorld()); } @@ -109,10 +111,12 @@ public void onBlockBreak(BlockBreakEvent event) { WorldConfiguration wcfg = getWorldConfig(player); if (!wcfg.itemDurability) { - ItemStack held = player.getItemInHand(); - if (held.getType() != Material.AIR) { - held.setDurability((short) 0); - player.setItemInHand(held); + ItemStack held = player.getInventory().getItemInMainHand(); + ItemMeta meta = held.getItemMeta(); + if (meta != null) { + ((Damageable) meta).setDamage(0); + held.setItemMeta(meta); + player.getInventory().setItemInMainHand(held); } } } @@ -170,7 +174,7 @@ public void onBlockFromTo(BlockFromToEvent event) { // Check the fluid block (from) whether it is air. // If so and the target block is protected, cancel the event - if (wcfg.preventWaterDamage.size() > 0) { + if (!wcfg.preventWaterDamage.isEmpty()) { Material targetId = blockTo.getType(); if ((isAir || isWater) && @@ -180,7 +184,7 @@ public void onBlockFromTo(BlockFromToEvent event) { } } - if (wcfg.allowedLavaSpreadOver.size() > 0 && isLava) { + if (!wcfg.allowedLavaSpreadOver.isEmpty() && isLava) { Material targetId = blockTo.getRelative(0, -1, 0).getType(); if (!wcfg.allowedLavaSpreadOver.contains(BukkitAdapter.asBlockType(targetId).getId())) { @@ -254,7 +258,7 @@ public void onBlockIgnite(BlockIgniteEvent event) { return; } - if (wcfg.disableFireSpreadBlocks.size() > 0 && isFireSpread) { + if (!wcfg.disableFireSpreadBlocks.isEmpty() && isFireSpread) { int x = block.getX(); int y = block.getY(); int z = block.getZ(); @@ -325,7 +329,7 @@ public void onBlockBurn(BlockBurnEvent event) { return; } - if (wcfg.disableFireSpreadBlocks.size() > 0) { + if (!wcfg.disableFireSpreadBlocks.isEmpty()) { Block block = event.getBlock(); if (wcfg.disableFireSpreadBlocks.contains(BukkitAdapter.asBlockType(block.getType()).getId())) { @@ -661,4 +665,19 @@ public void onBlockFade(BlockFadeEvent event) { } } + @EventHandler(ignoreCancelled = true) + public void onBlockExplode(BlockExplodeEvent event) { + ConfigurationManager cfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager(); + + if (cfg.activityHaltToggle) { + event.setCancelled(true); + return; + } + + WorldConfiguration wcfg = getWorldConfig(event.getBlock().getWorld()); + if (wcfg.blockOtherExplosions) { + event.setCancelled(true); + } + } + } diff --git a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java index 1b6015a7..8064cdf4 100644 --- a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java +++ b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java @@ -454,9 +454,7 @@ public void onEntityExplode(EntityExplodeEvent event) { Entity ent = event.getEntity(); if (cfg.activityHaltToggle) { - if (ent != null) { - ent.remove(); - } + ent.remove(); event.setCancelled(true); return; }