From 2a6937554eaeef0c840b5464d99d67d887fae5e8 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 22 Sep 2013 11:34:21 +0200 Subject: [PATCH] Cache explosion allowed results. Fixes #423 --- .../listeners/FactionsListenerMain.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java index d81d3f58..ac95eb1b 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java @@ -2,8 +2,10 @@ package com.massivecraft.factions.listeners; import java.text.MessageFormat; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -604,6 +606,14 @@ public class FactionsListenerMain implements Listener @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void blockExplosion(EntityExplodeEvent event) { + // Prepare some variables: + // Current faction + Faction faction = null; + // Current allowed + Boolean allowed = true; + // Caching to speed things up. + Map faction2allowed = new HashMap(); + // If an explosion occurs at a location ... Location location = event.getLocation(); @@ -611,20 +621,29 @@ public class FactionsListenerMain implements Listener if (UConf.isDisabled(location)) return; // Check the entity. Are explosions disabled there? - - if (BoardColls.get().getFactionAt(PS.valueOf(location)).isExplosionsAllowed() == false) + faction = BoardColls.get().getFactionAt(PS.valueOf(location)); + allowed = faction.isExplosionsAllowed(); + if (allowed == false) { event.setCancelled(true); return; } + faction2allowed.put(faction, allowed); // Individually check the flag state for each block Iterator iter = event.blockList().iterator(); while (iter.hasNext()) { Block block = iter.next(); - Faction faction = BoardColls.get().getFactionAt(PS.valueOf(block)); - if (faction.isExplosionsAllowed() == false) iter.remove(); + faction = BoardColls.get().getFactionAt(PS.valueOf(block)); + allowed = faction2allowed.get(faction); + if (allowed == null) + { + allowed = faction.isExplosionsAllowed(); + faction2allowed.put(faction, allowed); + } + + if (allowed == false) iter.remove(); } }