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
This commit is contained in:
Brettflan 2011-10-10 21:09:58 -05:00
parent 7d85a478a8
commit f5190db257
4 changed files with 131 additions and 56 deletions

View File

@ -185,6 +185,8 @@ public class Conf
public static boolean spoutFactionAdminCapes = true; // TODO: What are these for? public static boolean spoutFactionAdminCapes = true; // TODO: What are these for?
public static boolean spoutFactionModeratorCapes = true; public static boolean spoutFactionModeratorCapes = true;
public static int spoutTerritoryDisplayPosition = 3; 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 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 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"; public static String capeMember = "https://github.com/MassiveCraft/Factions/raw/master/capes/member.png";

View File

@ -1,35 +1,32 @@
package com.massivecraft.factions.integration; package com.massivecraft.factions.integration;
import com.massivecraft.factions.Board; import java.util.Set;
import com.massivecraft.factions.Conf; import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P; import com.massivecraft.factions.P;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.struct.Role;
import org.getspout.spoutapi.gui.Color; import org.getspout.spoutapi.gui.Color;
import org.getspout.spoutapi.gui.GenericLabel;
import org.getspout.spoutapi.player.AppearanceManager; import org.getspout.spoutapi.player.AppearanceManager;
import org.getspout.spoutapi.player.SpoutPlayer; import org.getspout.spoutapi.player.SpoutPlayer;
import org.getspout.spoutapi.SpoutManager; import org.getspout.spoutapi.SpoutManager;
import org.getspout.spoutapi.gui.WidgetAnchor;
public class SpoutFeatures public class SpoutFeatures
{ {
private transient static AppearanceManager spoutApp; private transient static AppearanceManager spoutApp;
private transient static boolean spoutMe = false; private transient static boolean spoutMe = false;
private transient static Map<String, GenericLabel> territoryLabels = new HashMap<String, GenericLabel>(); private transient static SpoutMainListener mainListener;
private transient static boolean listenersHooked;
// set integration availability // set integration availability
public static void setAvailable(boolean enable, String pluginName) public static void setAvailable(boolean enable, String pluginName)
@ -39,6 +36,13 @@ public class SpoutFeatures
{ {
spoutApp = SpoutManager.getAppearanceManager(); spoutApp = SpoutManager.getAppearanceManager();
P.p.log("Found and will use features of "+pluginName); 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 else
{ {
@ -62,46 +66,12 @@ public class SpoutFeatures
// update displayed current territory for specified player; returns false if unsuccessful // update displayed current territory for specified player; returns false if unsuccessful
public static boolean updateTerritoryDisplay(FPlayer player) public static boolean updateTerritoryDisplay(FPlayer player)
{ {
if (!spoutMe || Conf.spoutTerritoryDisplayPosition == 0) if (!enabled())
{ {
return false; return false;
} }
SpoutPlayer sPlayer = SpoutManager.getPlayer(player.getPlayer()); return mainListener.updateTerritoryDisplay(player);
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;
} }
public static void playerDisconnect(FPlayer player) public static void playerDisconnect(FPlayer player)
@ -110,7 +80,8 @@ public class SpoutFeatures
{ {
return; 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 // 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) if (inColor == null)
{ {

View File

@ -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<String, GenericLabel> territoryLabels = new HashMap<String, GenericLabel>();
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);
}
}

View File

@ -155,16 +155,6 @@ public class FactionsPlayerListener extends PlayerListener
// Run the member auto kick routine. Twice to get to the admins... // Run the member auto kick routine. Twice to get to the admins...
FPlayers.i.autoLeaveOnInactivityRoutine(); FPlayers.i.autoLeaveOnInactivityRoutine();
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 @Override