From 72b7a6a4acea3515ad8c0b0b3b4c9c238280716e Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 11 Oct 2017 08:42:11 +0200 Subject: [PATCH] Protect against BlockExplodeEvent --- .../factions/engine/EngineFlagExplosion.java | 45 ++++++++------- .../factions/integration/V18/EngineV18.java | 55 +++++++++++++++++++ .../integration/V18/IntegrationV18.java | 31 +++++++++++ 3 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 src/com/massivecraft/factions/integration/V18/EngineV18.java create mode 100644 src/com/massivecraft/factions/integration/V18/IntegrationV18.java diff --git a/src/com/massivecraft/factions/engine/EngineFlagExplosion.java b/src/com/massivecraft/factions/engine/EngineFlagExplosion.java index ef264f96..5a32f14e 100644 --- a/src/com/massivecraft/factions/engine/EngineFlagExplosion.java +++ b/src/com/massivecraft/factions/engine/EngineFlagExplosion.java @@ -9,6 +9,7 @@ import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Wither; +import org.bukkit.event.Cancellable; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.entity.EntityChangeBlockEvent; @@ -18,9 +19,11 @@ import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.hanging.HangingBreakEvent; import org.bukkit.event.hanging.HangingBreakEvent.RemoveCause; +import java.util.Collection; import java.util.EnumSet; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; @@ -69,36 +72,38 @@ public class EngineFlagExplosion extends Engine // ... then cancel! event.setCancelled(true); } - + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void blockExplosion(EntityExplodeEvent event) { - // Prepare some variables: - // Current faction - Faction faction = null; - // Current allowed - Boolean allowed = true; + Location location = event.getLocation(); + Cancellable cancellable = event; + Collection blocks = event.blockList(); + + blockExplosion(location, cancellable, blocks); + } + + // Note that this method is used by EngineV18 for the BlockExplodeEvent + public void blockExplosion(Location location, Cancellable cancellable, Collection blocks) + { // Caching to speed things up. Map faction2allowed = new HashMap<>(); - - // If an explosion occurs at a location ... - Location location = event.getLocation(); - + // Check the entity. Are explosions disabled there? - faction = BoardColl.get().getFactionAt(PS.valueOf(location)); - allowed = faction.isExplosionsAllowed(); - if (allowed == false) + Faction faction = BoardColl.get().getFactionAt(PS.valueOf(location)); + Boolean allowed = faction.isExplosionsAllowed(); + if (!allowed) { - event.setCancelled(true); + cancellable.setCancelled(true); return; } faction2allowed.put(faction, allowed); - + // Individually check the flag state for each block - Iterator iter = event.blockList().iterator(); - while (iter.hasNext()) + Iterator iterator = blocks.iterator(); + while (iterator.hasNext()) { - Block block = iter.next(); + Block block = iterator.next(); faction = BoardColl.get().getFactionAt(PS.valueOf(block)); allowed = faction2allowed.get(faction); if (allowed == null) @@ -106,8 +111,8 @@ public class EngineFlagExplosion extends Engine allowed = faction.isExplosionsAllowed(); faction2allowed.put(faction, allowed); } - - if (allowed == false) iter.remove(); + + if (!allowed) iterator.remove(); } } diff --git a/src/com/massivecraft/factions/integration/V18/EngineV18.java b/src/com/massivecraft/factions/integration/V18/EngineV18.java new file mode 100644 index 00000000..5eefe18b --- /dev/null +++ b/src/com/massivecraft/factions/integration/V18/EngineV18.java @@ -0,0 +1,55 @@ +package com.massivecraft.factions.integration.V18; + +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.engine.EngineCanCombatHappen; +import com.massivecraft.factions.engine.EngineFlagExplosion; +import com.massivecraft.massivecore.Engine; +import com.massivecraft.massivecore.MassivePlugin; +import com.massivecraft.massivecore.util.MUtil; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.block.BlockExplodeEvent; +import org.bukkit.event.entity.AreaEffectCloudApplyEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.projectiles.ProjectileSource; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class EngineV18 extends Engine +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static EngineV18 i = new EngineV18(); + public static EngineV18 get() { return i; } + + @Override + public MassivePlugin getActivePlugin() + { + return Factions.get(); + } + + // -------------------------------------------- // + // LISTENER + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void blockExplosion(BlockExplodeEvent event) + { + Location location = event.getBlock().getLocation(); + Cancellable cancellable = event; + Collection blocks = event.blockList(); + + EngineFlagExplosion.get().blockExplosion(location, cancellable, blocks); + } + +} diff --git a/src/com/massivecraft/factions/integration/V18/IntegrationV18.java b/src/com/massivecraft/factions/integration/V18/IntegrationV18.java new file mode 100644 index 00000000..1534f66e --- /dev/null +++ b/src/com/massivecraft/factions/integration/V18/IntegrationV18.java @@ -0,0 +1,31 @@ +package com.massivecraft.factions.integration.V18; + +import com.massivecraft.massivecore.Engine; +import com.massivecraft.massivecore.Integration; + +public class IntegrationV18 extends Integration +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static IntegrationV18 i = new IntegrationV18(); + public static IntegrationV18 get() { return i; } + private IntegrationV18() + { + this.setClassNames( + "org.bukkit.entity.ArmorStand" + ); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Engine getEngine() + { + return EngineV18.get(); + } + +}