From f5190db257200b79b4822325744bfd6021357d63 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Mon, 10 Oct 2011 21:09:58 -0500 Subject: [PATCH] Moved some Spout code out to a separate listener, for slightly better organization and more importantly to prevent NoClassDefFoundErrors on servers not running Spout The text scale (size) can now be set for the territory display using conf.json setting "spoutTerritoryDisplaySize" (default 1.0), and "spoutTerritoryDisplayShowDescription" (default true) can be disabled to have only the faction tag/name displayed and not the description as well --- src/com/massivecraft/factions/Conf.java | 2 + .../factions/integration/SpoutFeatures.java | 63 +++------- .../integration/SpoutMainListener.java | 112 ++++++++++++++++++ .../listeners/FactionsPlayerListener.java | 10 -- 4 files changed, 131 insertions(+), 56 deletions(-) create mode 100644 src/com/massivecraft/factions/integration/SpoutMainListener.java diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 0744001f..a76404c6 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -185,6 +185,8 @@ public class Conf public static boolean spoutFactionAdminCapes = true; // TODO: What are these for? public static boolean spoutFactionModeratorCapes = true; public static int spoutTerritoryDisplayPosition = 3; + public static float spoutTerritoryDisplaySize = 1.0f; + public static boolean spoutTerritoryDisplayShowDescription = true; public static String capeAlly = "https://github.com/MassiveCraft/Factions/raw/master/capes/ally.png"; public static String capeEnemy = "https://github.com/MassiveCraft/Factions/raw/master/capes/enemy.png"; public static String capeMember = "https://github.com/MassiveCraft/Factions/raw/master/capes/member.png"; diff --git a/src/com/massivecraft/factions/integration/SpoutFeatures.java b/src/com/massivecraft/factions/integration/SpoutFeatures.java index 4fbc90b6..59ffc7f9 100644 --- a/src/com/massivecraft/factions/integration/SpoutFeatures.java +++ b/src/com/massivecraft/factions/integration/SpoutFeatures.java @@ -1,35 +1,32 @@ package com.massivecraft.factions.integration; -import com.massivecraft.factions.Board; +import java.util.Set; + import com.massivecraft.factions.Conf; -import com.massivecraft.factions.FLocation; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.Faction; import com.massivecraft.factions.P; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import org.bukkit.event.Event; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Role; import org.getspout.spoutapi.gui.Color; -import org.getspout.spoutapi.gui.GenericLabel; import org.getspout.spoutapi.player.AppearanceManager; import org.getspout.spoutapi.player.SpoutPlayer; import org.getspout.spoutapi.SpoutManager; -import org.getspout.spoutapi.gui.WidgetAnchor; public class SpoutFeatures { private transient static AppearanceManager spoutApp; private transient static boolean spoutMe = false; - private transient static Map territoryLabels = new HashMap(); + private transient static SpoutMainListener mainListener; + private transient static boolean listenersHooked; // set integration availability public static void setAvailable(boolean enable, String pluginName) @@ -39,6 +36,13 @@ public class SpoutFeatures { spoutApp = SpoutManager.getAppearanceManager(); P.p.log("Found and will use features of "+pluginName); + + if (!listenersHooked) + { + listenersHooked = true; + mainListener = new SpoutMainListener(); + P.p.getServer().getPluginManager().registerEvent(Event.Type.CUSTOM_EVENT, mainListener, Event.Priority.Normal, P.p); + } } else { @@ -62,46 +66,12 @@ public class SpoutFeatures // update displayed current territory for specified player; returns false if unsuccessful public static boolean updateTerritoryDisplay(FPlayer player) { - if (!spoutMe || Conf.spoutTerritoryDisplayPosition == 0) + if (!enabled()) { return false; } - SpoutPlayer sPlayer = SpoutManager.getPlayer(player.getPlayer()); - if (!sPlayer.isSpoutCraftEnabled()) - { - return false; - } - - GenericLabel label; - if (territoryLabels.containsKey(player.getName())) - { - label = territoryLabels.get(player.getName()); - } - else - { - label = new GenericLabel(); - sPlayer.getMainScreen().attachWidget(P.p, label); - switch (Conf.spoutTerritoryDisplayPosition) - { - case 1: label.setAlign(WidgetAnchor.TOP_LEFT).setAnchor(WidgetAnchor.TOP_LEFT); break; - case 2: label.setAlign(WidgetAnchor.TOP_CENTER).setAnchor(WidgetAnchor.TOP_CENTER); break; - default: label.setAlign(WidgetAnchor.TOP_RIGHT).setAnchor(WidgetAnchor.TOP_RIGHT); - } - territoryLabels.put(player.getName(), label); - } - - Faction factionHere = Board.getFactionAt(new FLocation(player)); - String msg = factionHere.getTag(); - if (factionHere.getDescription().length() > 0) - { - msg += " - "+factionHere.getDescription(); - } - label.setTextColor(getSpoutColor(player.getRelationColor(factionHere), 0)); - label.setText(msg); - label.setDirty(true); - - return true; + return mainListener.updateTerritoryDisplay(player); } public static void playerDisconnect(FPlayer player) @@ -110,7 +80,8 @@ public class SpoutFeatures { return; } - territoryLabels.remove(player.getName()); + + mainListener.removeTerritoryLabel(player.getName()); } @@ -294,7 +265,7 @@ public class SpoutFeatures } // method to convert a Bukkit ChatColor to a Spout Color - private static Color getSpoutColor(ChatColor inColor, int alpha) + protected static Color getSpoutColor(ChatColor inColor, int alpha) { if (inColor == null) { diff --git a/src/com/massivecraft/factions/integration/SpoutMainListener.java b/src/com/massivecraft/factions/integration/SpoutMainListener.java new file mode 100644 index 00000000..ef7ff520 --- /dev/null +++ b/src/com/massivecraft/factions/integration/SpoutMainListener.java @@ -0,0 +1,112 @@ +package com.massivecraft.factions.integration; + +import java.util.HashMap; +import java.util.Map; + +import com.massivecraft.factions.Board; +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.FLocation; +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.FPlayers; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.P; + +import org.getspout.spoutapi.event.spout.SpoutCraftEnableEvent; +import org.getspout.spoutapi.event.spout.SpoutListener; +import org.getspout.spoutapi.gui.GenericLabel; +import org.getspout.spoutapi.player.SpoutPlayer; +import org.getspout.spoutapi.SpoutManager; +//import org.getspout.spoutapi.gui.WidgetAnchor; + + +public class SpoutMainListener extends SpoutListener +{ + @Override + public void onSpoutCraftEnable(SpoutCraftEnableEvent event) + { + final FPlayer me = FPlayers.i.get(event.getPlayer()); + + SpoutFeatures.updateAppearances(me.getPlayer()); + updateTerritoryDisplay(me); + } + + + //-----------------------------------------------------------------------------------------// + // Everything below this is handled in here to prevent errors on servers not running Spout + //-----------------------------------------------------------------------------------------// + + private transient static Map territoryLabels = new HashMap(); + private final static int SCREEN_WIDTH = 427; +// private final static int SCREEN_HEIGHT = 240; + + + public boolean updateTerritoryDisplay(FPlayer player) + { + SpoutPlayer sPlayer = SpoutManager.getPlayer(player.getPlayer()); + if (!sPlayer.isSpoutCraftEnabled() || Conf.spoutTerritoryDisplaySize <= 0) + { + return false; + } + + GenericLabel label; + if (territoryLabels.containsKey(player.getName())) + { + label = territoryLabels.get(player.getName()); + } else { + label = new GenericLabel(); + label.setScale(Conf.spoutTerritoryDisplaySize); +/* // this should work once the Spout team fix it to account for text scaling; we can then get rid of alignLabel method added below + switch (Conf.spoutTerritoryDisplayPosition) { + case 1: label.setAlign(WidgetAnchor.TOP_LEFT).setAnchor(WidgetAnchor.TOP_LEFT); break; + case 2: label.setAlign(WidgetAnchor.TOP_CENTER).setAnchor(WidgetAnchor.TOP_CENTER); break; + default: label.setAlign(WidgetAnchor.TOP_RIGHT).setAnchor(WidgetAnchor.TOP_RIGHT); + } + */ + sPlayer.getMainScreen().attachWidget(P.p, label); + territoryLabels.put(player.getName(), label); + } + + Faction factionHere = Board.getFactionAt(new FLocation(player)); + String msg = factionHere.getTag(); + + if (Conf.spoutTerritoryDisplayShowDescription && factionHere.getDescription().length() > 0) + { + msg += " - "+factionHere.getDescription(); + } + + label.setTextColor(SpoutFeatures.getSpoutColor(player.getRelationColor(factionHere), 0)); + label.setText(msg); + alignLabel(label, msg); + label.setDirty(true); + + return true; + } + + // this is only necessary because Spout text size scaling is currently bugged and breaks their built-in alignment methods + public void alignLabel(GenericLabel label, String text) + { + int labelWidth = (int)((float)GenericLabel.getStringWidth(text) * Conf.spoutTerritoryDisplaySize); + if (labelWidth > SCREEN_WIDTH) + { + label.setX(0); + return; + } + + switch (Conf.spoutTerritoryDisplayPosition) + { + case 1: // left aligned + label.setX(0); + break; + case 2: // center aligned + label.setX((SCREEN_WIDTH - labelWidth) / 2); + break; + default: // right aligned + label.setX(SCREEN_WIDTH - labelWidth); + } + } + + public void removeTerritoryLabel(String playerName) + { + territoryLabels.remove(playerName); + } +} \ No newline at end of file diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index f03d5dbb..ca4ff039 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -155,16 +155,6 @@ public class FactionsPlayerListener extends PlayerListener // Run the member auto kick routine. Twice to get to the admins... FPlayers.i.autoLeaveOnInactivityRoutine(); FPlayers.i.autoLeaveOnInactivityRoutine(); - - SpoutFeatures.updateTerritoryDisplay(me); - - // Appearance updates which are run when a player joins don't apply properly for other clients, so they need to be delayed slightly - P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, new Runnable() { - public void run() { - SpoutFeatures.updateAppearances(me.getPlayer()); - SpoutFeatures.updateTerritoryDisplay(me); - } - }, 20); } @Override