From 3ac432ac3d168c27b238d6db330e288544521ec5 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 18 Apr 2013 16:11:45 +0200 Subject: [PATCH] Clean up the Monsters flag logic. --- .../listeners/FactionsListenerMain.java | 45 +++++++++++++++++++ .../listeners/TodoFactionsEntityListener.java | 40 ++--------------- .../listeners/TodoFactionsPlayerListener.java | 5 +-- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java index b30f726f..ee706070 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java @@ -7,6 +7,7 @@ import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.Enderman; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.entity.Wither; import org.bukkit.event.Cancellable; @@ -22,10 +23,12 @@ import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockSpreadEvent; import org.bukkit.event.block.BlockIgniteEvent.IgniteCause; +import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.bukkit.event.entity.EntityTargetEvent; import org.bukkit.event.hanging.HangingBreakByEntityEvent; import org.bukkit.event.hanging.HangingBreakEvent; import org.bukkit.event.hanging.HangingPlaceEvent; @@ -38,6 +41,7 @@ import org.bukkit.event.server.PluginEnableEvent; import com.massivecraft.factions.BoardColl; import com.massivecraft.factions.ConfServer; +import com.massivecraft.factions.Const; import com.massivecraft.factions.FFlag; import com.massivecraft.factions.FPerm; import com.massivecraft.factions.FPlayer; @@ -65,6 +69,47 @@ public class FactionsListenerMain implements Listener Bukkit.getPluginManager().registerEvents(this, Factions.get()); } + // -------------------------------------------- // + // FLAG: MONSTERS + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void blockMonsters(CreatureSpawnEvent event) + { + // If a monster is spawning ... + if ( ! Const.ENTITY_TYPES_MONSTERS.contains(event.getEntityType())) return; + + // ... at a place where monsters are forbidden ... + PS ps = PS.valueOf(event.getLocation()); + Faction faction = BoardColl.get().getFactionAt(ps); + if (faction.getFlag(FFlag.MONSTERS)) return; + + // ... block the spawn. + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void blockMonsters(EntityTargetEvent event) + { + // If a monster is targeting something ... + if ( ! Const.ENTITY_TYPES_MONSTERS.contains(event.getEntityType())) return; + + // ... at a place where monsters are forbidden ... + PS ps = PS.valueOf(event.getTarget()); + Faction faction = BoardColl.get().getFactionAt(ps); + if (faction.getFlag(FFlag.MONSTERS)) return; + + // ... then if ghast target nothing ... + if (event.getEntityType() == EntityType.GHAST) + { + event.setTarget(null); + return; + } + + // ... otherwise simply cancel. + event.setCancelled(true); + } + // -------------------------------------------- // // FLAG: EXPLOSIONS // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/listeners/TodoFactionsEntityListener.java b/src/com/massivecraft/factions/listeners/TodoFactionsEntityListener.java index 62443ee7..c55ccb7b 100644 --- a/src/com/massivecraft/factions/listeners/TodoFactionsEntityListener.java +++ b/src/com/massivecraft/factions/listeners/TodoFactionsEntityListener.java @@ -15,11 +15,9 @@ import org.bukkit.entity.Projectile; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.EntityCombustByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityTargetEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PotionSplashEvent; import org.bukkit.potion.PotionEffect; @@ -27,7 +25,6 @@ import org.bukkit.potion.PotionEffectType; import com.massivecraft.factions.BoardColl; import com.massivecraft.factions.ConfServer; -import com.massivecraft.factions.Const; import com.massivecraft.factions.FFlag; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayerColl; @@ -110,8 +107,9 @@ public class TodoFactionsEntityListener implements Listener { EntityDamageByEntityEvent sub = new EntityDamageByEntityEvent(event.getCombuster(), event.getEntity(), EntityDamageEvent.DamageCause.FIRE, 0); if ( ! this.canDamagerHurtDamagee(sub, false)) + { event.setCancelled(true); - sub = null; + } } private static final Set badPotionEffects = new LinkedHashSet(Arrays.asList( @@ -276,38 +274,6 @@ public class TodoFactionsEntityListener implements Listener return true; } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onCreatureSpawn(CreatureSpawnEvent event) - { - if (event.getLocation() == null) return; - - PS ps = PS.valueOf(event.getLocation()); - Faction faction = BoardColl.get().getFactionAt(ps); - - if (faction.getFlag(FFlag.MONSTERS)) return; - if ( ! Const.ENTITY_TYPES_MONSTERS.contains(event.getEntityType())) return; - - event.setCancelled(true); - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onEntityTarget(EntityTargetEvent event) - { - // if there is a target - Entity target = event.getTarget(); - if (target == null) return; - - // We are interested in blocking targeting for certain mobs: - - - if ( ! Const.ENTITY_TYPES_MONSTERS.contains(event.getEntity().getType())) return; - - PS ps = PS.valueOf(target.getLocation()); - Faction faction = BoardColl.get().getFactionAt(ps); - - if (faction.getFlag(FFlag.MONSTERS)) return; - - event.setCancelled(true); - } + } diff --git a/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java index 5e5b9794..f33e1278 100644 --- a/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java @@ -267,10 +267,7 @@ public class TodoFactionsPlayerListener implements Listener public void onPlayerKick(PlayerKickEvent event) { FPlayer badGuy = FPlayerColl.get().get(event.getPlayer()); - if (badGuy == null) - { - return; - } + if (badGuy == null) return; SpoutFeatures.playerDisconnect(badGuy);