From 10522e70484f2121520d44cbf72d59426eef69df Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 1 Apr 2012 15:08:20 +0200 Subject: [PATCH] Made a sepparate health bar listener, reduced duplicate code and added respawn listener. --- src/com/massivecraft/factions/P.java | 3 + .../listeners/FactionsBlockListener.java | 1 - .../listeners/FactionsEntityListener.java | 28 --------- .../listeners/FactionsHealthBarListener.java | 57 +++++++++++++++++++ 4 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 src/com/massivecraft/factions/listeners/FactionsHealthBarListener.java diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index ae95ec9e..0c28d636 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -31,6 +31,7 @@ import com.massivecraft.factions.listeners.FactionsBlockListener; import com.massivecraft.factions.listeners.FactionsChatListener; import com.massivecraft.factions.listeners.FactionsEntityListener; import com.massivecraft.factions.listeners.FactionsExploitListener; +import com.massivecraft.factions.listeners.FactionsHealthBarListener; import com.massivecraft.factions.listeners.FactionsPlayerListener; import com.massivecraft.factions.listeners.FactionsServerListener; import com.massivecraft.factions.struct.ChatMode; @@ -58,6 +59,7 @@ public class P extends MPlugin public final FactionsExploitListener exploitListener; public final FactionsBlockListener blockListener; public final FactionsServerListener serverListener; + public final FactionsHealthBarListener healthBarListener; // Persistance related private boolean locked = false; @@ -78,6 +80,7 @@ public class P extends MPlugin this.exploitListener = new FactionsExploitListener(); this.blockListener = new FactionsBlockListener(this); this.serverListener = new FactionsServerListener(this); + this.healthBarListener = new FactionsHealthBarListener(this); } diff --git a/src/com/massivecraft/factions/listeners/FactionsBlockListener.java b/src/com/massivecraft/factions/listeners/FactionsBlockListener.java index 19caf19f..836ef7c9 100644 --- a/src/com/massivecraft/factions/listeners/FactionsBlockListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsBlockListener.java @@ -1,7 +1,6 @@ package com.massivecraft.factions.listeners; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; diff --git a/src/com/massivecraft/factions/listeners/FactionsEntityListener.java b/src/com/massivecraft/factions/listeners/FactionsEntityListener.java index cf095cb4..4ddb8156 100644 --- a/src/com/massivecraft/factions/listeners/FactionsEntityListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsEntityListener.java @@ -26,7 +26,6 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.EntityTargetEvent; import org.bukkit.event.entity.PotionSplashEvent; import org.bukkit.event.painting.PaintingBreakByEntityEvent; @@ -42,7 +41,6 @@ import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.Faction; import com.massivecraft.factions.P; -import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.struct.FFlag; import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.util.MiscUtil; @@ -407,30 +405,4 @@ public class FactionsEntityListener implements Listener event.setCancelled(true); } - - // -------------------------------------------- // - // HEALTH BAR - // -------------------------------------------- // - - @EventHandler(priority = EventPriority.MONITOR) - public void monitorEntityDamageEvent(EntityDamageEvent event) - { - if (event.isCancelled()) return; - if ( ! Conf.spoutHealthBarUnderNames) return; - Entity entity = event.getEntity(); - if ( ! (entity instanceof Player)) return; - Player player = (Player)entity; - SpoutFeatures.updateMyAppearance(player); - } - - @EventHandler(priority = EventPriority.MONITOR) - public void monitorEntityRegainHealthEvent(EntityRegainHealthEvent event) - { - if (event.isCancelled()) return; - if ( ! Conf.spoutHealthBarUnderNames) return; - Entity entity = event.getEntity(); - if ( ! (entity instanceof Player)) return; - Player player = (Player)entity; - SpoutFeatures.updateMyAppearance(player); - } } diff --git a/src/com/massivecraft/factions/listeners/FactionsHealthBarListener.java b/src/com/massivecraft/factions/listeners/FactionsHealthBarListener.java new file mode 100644 index 00000000..32eae52d --- /dev/null +++ b/src/com/massivecraft/factions/listeners/FactionsHealthBarListener.java @@ -0,0 +1,57 @@ +package com.massivecraft.factions.listeners; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.bukkit.event.player.PlayerRespawnEvent; + +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.P; +import com.massivecraft.factions.integration.SpoutFeatures; + + +public class FactionsHealthBarListener implements Listener +{ + public P p; + public FactionsHealthBarListener(P p) + { + this.p = p; + } + + // -------------------------------------------- // + // HEALTH BAR + // -------------------------------------------- // + + public static void possiblyUpdateHealthBar(Entity entity, Cancellable event) + { + if (event != null && event.isCancelled()) return; + if ( ! Conf.spoutHealthBarUnderNames) return; + if ( ! (entity instanceof Player)) return; + Player player = (Player)entity; + SpoutFeatures.updateMyAppearance(player); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void monitorEntityDamageEvent(EntityDamageEvent event) + { + possiblyUpdateHealthBar(event.getEntity(), event); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void monitorEntityRegainHealthEvent(EntityRegainHealthEvent event) + { + possiblyUpdateHealthBar(event.getEntity(), event); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void monitorPlayerRespawnEvent(PlayerRespawnEvent event) + { + possiblyUpdateHealthBar(event.getPlayer(), null); + } + +}