From 67e5aa8464d07b69b43a5f4c4e556b7680a740ce Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 9 May 2012 05:21:21 +0200 Subject: [PATCH] Fixing a few apperance bugs and stopped updating apparance if it was the same. --- src/com/massivecraft/factions/P.java | 19 ++-- .../factions/cmd/FRelationCommand.java | 1 + .../factions/integration/SpoutFeatures.java | 61 +++++++++---- .../listeners/FactionsAppearanceListener.java | 88 +++++++++++++++++++ .../listeners/FactionsHealthBarListener.java | 57 ------------ .../listeners/FactionsPlayerListener.java | 5 -- 6 files changed, 141 insertions(+), 90 deletions(-) create mode 100644 src/com/massivecraft/factions/listeners/FactionsAppearanceListener.java delete 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 d488a373..38d9f8d7 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -32,7 +32,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.FactionsAppearanceListener; import com.massivecraft.factions.listeners.FactionsPlayerListener; import com.massivecraft.factions.listeners.FactionsServerListener; import com.massivecraft.factions.struct.FFlag; @@ -59,7 +59,7 @@ public class P extends MPlugin public final FactionsExploitListener exploitListener; public final FactionsBlockListener blockListener; public final FactionsServerListener serverListener; - public final FactionsHealthBarListener healthBarListener; + public final FactionsAppearanceListener appearanceListener; // Persistance related private boolean locked = false; @@ -80,7 +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); + this.appearanceListener = new FactionsAppearanceListener(this); } @@ -117,12 +117,13 @@ public class P extends MPlugin startAutoLeaveTask(false); // Register Event Handlers - getServer().getPluginManager().registerEvents(playerListener, this); - getServer().getPluginManager().registerEvents(chatListener, this); - getServer().getPluginManager().registerEvents(entityListener, this); - getServer().getPluginManager().registerEvents(exploitListener, this); - getServer().getPluginManager().registerEvents(blockListener, this); - getServer().getPluginManager().registerEvents(serverListener, this); + getServer().getPluginManager().registerEvents(this.playerListener, this); + getServer().getPluginManager().registerEvents(this.chatListener, this); + getServer().getPluginManager().registerEvents(this.entityListener, this); + getServer().getPluginManager().registerEvents(this.exploitListener, this); + getServer().getPluginManager().registerEvents(this.blockListener, this); + getServer().getPluginManager().registerEvents(this.serverListener, this); + getServer().getPluginManager().registerEvents(this.appearanceListener, this); // since some other plugins execute commands directly through this command interface, provide it this.getCommand(this.refCommand).setExecutor(this); diff --git a/src/com/massivecraft/factions/cmd/FRelationCommand.java b/src/com/massivecraft/factions/cmd/FRelationCommand.java index c1c75ce5..099a497b 100644 --- a/src/com/massivecraft/factions/cmd/FRelationCommand.java +++ b/src/com/massivecraft/factions/cmd/FRelationCommand.java @@ -94,6 +94,7 @@ public abstract class FRelationCommand extends FCommand } SpoutFeatures.updateTitle(myFaction, them); + SpoutFeatures.updateTitle(them, myFaction); SpoutFeatures.updateTerritoryDisplayLoc(null); } } diff --git a/src/com/massivecraft/factions/integration/SpoutFeatures.java b/src/com/massivecraft/factions/integration/SpoutFeatures.java index cb93db27..968e16f2 100644 --- a/src/com/massivecraft/factions/integration/SpoutFeatures.java +++ b/src/com/massivecraft/factions/integration/SpoutFeatures.java @@ -62,7 +62,7 @@ public class SpoutFeatures // -------------------------------------------- // // Capes look the same to everyone. - public static void updateCape(final Object ofrom, final Object oto) + public static void updateCape(final Object ofrom, final Object oto, boolean onlyIfDifferent) { // Enabled and non-null? if ( ! isEnabled()) return; @@ -78,31 +78,38 @@ public class SpoutFeatures Faction faction = fplayer.getFaction(); String cape = faction.getCape(); + if (cape == null) + { + cape = "http://s3.amazonaws.com/MinecraftCloaks/" + player.getName() + ".png"; + } for (Player playerTo : toPlayers) { SpoutPlayer splayerTo = SpoutManager.getPlayer(playerTo); + boolean skip = onlyIfDifferent && cape.equals(splayer.getCape(splayerTo)); + //Bukkit.getConsoleSender().sendMessage(P.p.txt.parse("CAPE SKIP:%s FROM %s TO %s URL %s", String.valueOf(skip), player.getDisplayName(), playerTo.getDisplayName(), cape)); + if (skip) continue; + //Bukkit.getConsoleSender().sendMessage(P.p.txt.parse("CAPE FROM %s TO %s URL %s", player.getDisplayName(), playerTo.getDisplayName(), cape)); + // Set the cape - if (cape != null) + try { - try - { - splayer.setCapeFor(splayerTo, cape); - } - catch (Exception e) - { - - } + splayer.setCapeFor(splayerTo, cape); } - else + catch (Exception e) { - splayer.resetCapeFor(splayerTo); + } } } } + public static void updateCape(final Object ofrom, final Object oto) + { + updateCape(ofrom, oto, true); + } + public static void updateCapeShortly(final Object ofrom, final Object oto) { P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, new Runnable() @@ -110,16 +117,16 @@ public class SpoutFeatures @Override public void run() { - updateCape(ofrom, oto); + updateCape(ofrom, oto, false); } - }, 10); + }, 5); } // -------------------------------------------- // // TITLE // -------------------------------------------- // - public static void updateTitle(final Object ofrom, final Object oto) + public static void updateTitle(final Object ofrom, final Object oto, boolean onlyIfDifferent) { // Enabled and non-null? if ( ! isEnabled()) return; @@ -143,11 +150,22 @@ public class SpoutFeatures ChatColor relationColor = faction.getRelationTo(factionTo).getColor(); String title = generateTitle(player, fplayer, faction, relationColor); + + boolean skip = onlyIfDifferent && title.equals(splayer.getTitleFor(splayerTo)); + //Bukkit.getConsoleSender().sendMessage(P.p.txt.parse("TITLE SKIP:%s FROM %s TO %s TITLE %s", String.valueOf(skip), player.getDisplayName(), playerTo.getDisplayName(), title)); + if (skip) continue; + //Bukkit.getConsoleSender().sendMessage(P.p.txt.parse("TITLE FROM %s TO %s TITLE %s", player.getDisplayName(), playerTo.getDisplayName(), title)); + splayer.setTitleFor(splayerTo, title); } } } + public static void updateTitle(final Object ofrom, final Object oto) + { + updateTitle(ofrom, oto, true); + } + public static void updateTitleShortly(final Object ofrom, final Object oto) { P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, new Runnable() @@ -155,9 +173,9 @@ public class SpoutFeatures @Override public void run() { - updateTitle(ofrom, oto); + updateTitle(ofrom, oto, false); } - }, 10); + }, 5); } public static String generateTitle(Player player, FPlayer fplayer, Faction faction, ChatColor relationColor) @@ -171,7 +189,7 @@ public class SpoutFeatures String addTag = ""; if (Conf.spoutFactionTagsOverNames) { - addTag += relationColor.toString() + "[" + fplayer.getRole().getPrefix() + faction.getTag() + "]"; + addTag += relationColor.toString() + fplayer.getRole().getPrefix() + faction.getTag(); } if (Conf.spoutFactionTitlesOverNames && ! fplayer.getTitle().isEmpty()) @@ -204,7 +222,12 @@ public class SpoutFeatures } else if (o instanceof FPlayer) { - ret.add(((FPlayer)o).getPlayer()); + FPlayer fplayer = (FPlayer)o; + Player player = fplayer.getPlayer(); + if (player != null) + { + ret.add(player); + } } else if (o instanceof Faction) { diff --git a/src/com/massivecraft/factions/listeners/FactionsAppearanceListener.java b/src/com/massivecraft/factions/listeners/FactionsAppearanceListener.java new file mode 100644 index 00000000..9829d25c --- /dev/null +++ b/src/com/massivecraft/factions/listeners/FactionsAppearanceListener.java @@ -0,0 +1,88 @@ +package com.massivecraft.factions.listeners; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +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.PlayerJoinEvent; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.event.player.PlayerTeleportEvent; + +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.P; +import com.massivecraft.factions.integration.SpoutFeatures; + + +public class FactionsAppearanceListener implements Listener +{ + public P p; + public FactionsAppearanceListener(P p) + { + this.p = p; + } + + // -------------------------------------------- // + // FULL TWO-WAYS + // -------------------------------------------- // + + public void fullTwoWay(Player player) + { + SpoutFeatures.updateTitleShortly(player, null); + SpoutFeatures.updateTitleShortly(null, player); + SpoutFeatures.updateCapeShortly(player, null); + SpoutFeatures.updateCapeShortly(null, player); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerJoin(PlayerJoinEvent event) + { + fullTwoWay(event.getPlayer()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerTeleport(PlayerTeleportEvent event) + { + if (event.getFrom().getWorld().equals(event.getTo().getWorld())) return; + fullTwoWay(event.getPlayer()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerRespawn(PlayerRespawnEvent event) + { + fullTwoWay(event.getPlayer()); + } + + // -------------------------------------------- // + // HEALTH BAR + // -------------------------------------------- // + + public static void possiblyUpdateHealthBar(Entity entity) + { + if ( ! Conf.spoutHealthBarUnderNames) return; + if ( ! (entity instanceof Player)) return; + Player player = (Player)entity; + SpoutFeatures.updateTitle(player, null); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void monitorEntityDamageEvent(EntityDamageEvent event) + { + possiblyUpdateHealthBar(event.getEntity()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void monitorEntityRegainHealthEvent(EntityRegainHealthEvent event) + { + possiblyUpdateHealthBar(event.getEntity()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void monitorPlayerRespawnEvent(PlayerRespawnEvent event) + { + possiblyUpdateHealthBar(event.getPlayer()); + } + +} diff --git a/src/com/massivecraft/factions/listeners/FactionsHealthBarListener.java b/src/com/massivecraft/factions/listeners/FactionsHealthBarListener.java deleted file mode 100644 index c52daf03..00000000 --- a/src/com/massivecraft/factions/listeners/FactionsHealthBarListener.java +++ /dev/null @@ -1,57 +0,0 @@ -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.updateTitle(player, null); - } - - @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); - } - -} diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 1c96ae01..7ba67e07 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -52,11 +52,6 @@ public class FactionsPlayerListener implements Listener // Update the lastLoginTime for this fplayer me.setLastLoginTime(System.currentTimeMillis()); - - SpoutFeatures.updateTitleShortly(event.getPlayer(), null); - SpoutFeatures.updateTitleShortly(null, event.getPlayer()); - SpoutFeatures.updateCapeShortly(event.getPlayer(), null); - SpoutFeatures.updateCapeShortly(null, event.getPlayer()); } @EventHandler(priority = EventPriority.NORMAL)