Add faction tag to team prefix

This commit is contained in:
eueln 2014-10-20 14:55:16 -05:00 committed by t00thpick1
parent 51f1ceee14
commit c049768e21
4 changed files with 52 additions and 51 deletions

View File

@ -4,6 +4,7 @@ import com.massivecraft.factions.Conf;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.factions.event.FactionRenameEvent; import com.massivecraft.factions.event.FactionRenameEvent;
import com.massivecraft.factions.scoreboards.FScoreboard;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.util.MiscUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -73,6 +74,7 @@ public class CmdTag extends FCommand {
faction.msg("<i>The faction %s<i> changed their name to %s.", fme.getColorTo(faction) + oldtag, myFaction.getTag(faction)); faction.msg("<i>The faction %s<i> changed their name to %s.", fme.getColorTo(faction) + oldtag, myFaction.getTag(faction));
} }
FScoreboard.applyUpdates(myFaction);
} }
} }

View File

@ -1,7 +1,6 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Conf; import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.event.FactionRelationEvent; import com.massivecraft.factions.event.FactionRelationEvent;
import com.massivecraft.factions.scoreboards.FScoreboard; import com.massivecraft.factions.scoreboards.FScoreboard;
@ -87,18 +86,7 @@ public abstract class FRelationCommand extends FCommand {
myFaction.msg("<i>This will have no effect while your faction is peaceful."); myFaction.msg("<i>This will have no effect while your faction is peaceful.");
} }
for (FPlayer ourMember : myFaction.getFPlayers()) { FScoreboard.applyUpdates(myFaction);
if (!ourMember.isOnline()) { FScoreboard.applyUpdates(them);
continue;
}
for (FPlayer theirMember : them.getFPlayers()) {
if (!theirMember.isOnline()) {
continue;
}
FScoreboard.get(ourMember).updateColor(theirMember);
FScoreboard.get(theirMember).updateColor(ourMember);
}
}
} }
} }

View File

@ -526,13 +526,11 @@ public class FactionsPlayerListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
final public void onFactionJoin(FPlayerJoinEvent event) { final public void onFactionJoin(FPlayerJoinEvent event) {
FScoreboard.updateColorToAllLater(event.getfPlayer()); FScoreboard.applyUpdatesLater(event.getFaction());
FScoreboard.updateColorsFromAllLater(event.getfPlayer());
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onFactionLeave(FPlayerLeaveEvent event) { public void onFactionLeave(FPlayerLeaveEvent event) {
FScoreboard.updateColorToAllLater(event.getfPlayer()); FScoreboard.applyUpdatesLater(event.getFaction());
FScoreboard.updateColorsFromAllLater(event.getfPlayer());
} }
} }

View File

@ -1,10 +1,8 @@
package com.massivecraft.factions.scoreboards; package com.massivecraft.factions.scoreboards;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.*;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.P;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.DisplaySlot;
@ -20,16 +18,14 @@ public class FScoreboard {
private final Scoreboard scoreboard; private final Scoreboard scoreboard;
private final FPlayer fplayer; private final FPlayer fplayer;
private final BufferedObjective bufferedObjective; private final BufferedObjective bufferedObjective;
private final Map<ChatColor, Team> colorTeams = new HashMap<ChatColor, Team>(); private final Map<Faction, Team> factionTeams = new HashMap<Faction, Team>();
private int factionTeamPtr;
private FSidebarProvider defaultProvider; private FSidebarProvider defaultProvider;
private FSidebarProvider temporaryProvider; private FSidebarProvider temporaryProvider;
private boolean removed = false; private boolean removed = false;
public static void init(FPlayer fplayer) { public static void init(FPlayer fplayer) {
fscoreboards.put(fplayer.getPlayer(), new FScoreboard(fplayer)); fscoreboards.put(fplayer.getPlayer(), new FScoreboard(fplayer));
updateColorToAllLater(fplayer);
updateColorsFromAllLater(fplayer);
} }
public static void remove(FPlayer fplayer) { public static void remove(FPlayer fplayer) {
@ -44,39 +40,28 @@ public class FScoreboard {
return fscoreboards.get(player); return fscoreboards.get(player);
} }
public static void updateColorToAllLater(final FPlayer fplayer) { public static void applyUpdatesLater(final Faction faction) {
// We're delaying by a tick here to simplify logic in other areas
// (e.g. for FPlayer{Join,Leave}Event handlers; CmdDisband)
Bukkit.getScheduler().runTask(P.p, new Runnable() { Bukkit.getScheduler().runTask(P.p, new Runnable() {
@Override @Override
public void run() { public void run() {
for (FPlayer other : FPlayers.i.getOnline()) { applyUpdates(faction);
get(other).updateColor(fplayer);
}
} }
}); });
} }
public static void updateColorsFromAllLater(final FPlayer fplayer) { public static void applyUpdates(Faction faction) {
Bukkit.getScheduler().runTask(P.p, new Runnable() { for (FScoreboard fscoreboard : fscoreboards.values()) {
@Override fscoreboard.updateFactionTeam(faction);
public void run() {
for (FPlayer other : FPlayers.i.getOnline()) {
get(fplayer).updateColor(other);
} }
} }
});
}
private FScoreboard(FPlayer fplayer) { private FScoreboard(FPlayer fplayer) {
this.fplayer = fplayer; this.fplayer = fplayer;
this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
this.bufferedObjective = new BufferedObjective(scoreboard); this.bufferedObjective = new BufferedObjective(scoreboard);
for (ChatColor color : ChatColor.values()) { for (Faction faction : Factions.i.get()) {
Team team = scoreboard.registerNewTeam(color.name()); updateFactionTeam(faction);
team.setPrefix(color.toString());
colorTeams.put(color, team);
} }
fplayer.getPlayer().setScoreboard(scoreboard); fplayer.getPlayer().setScoreboard(scoreboard);
@ -139,16 +124,44 @@ public class FScoreboard {
} }
} }
public void updateColor(FPlayer other) { public void updateFactionTeam(Faction faction) {
if (!other.isOnline()) { Team team = factionTeams.get(faction);
if (!Factions.i.get().contains(faction)) {
// Faction was disbanded
if (team != null) {
factionTeams.remove(faction);
team.unregister();
}
return; return;
} }
ChatColor newColor = fplayer.getRelationTo(other).getColor(); if (team == null) {
Team team = colorTeams.get(newColor); team = scoreboard.registerNewTeam("faction_" + (factionTeamPtr++));
factionTeams.put(faction, team);
}
if (!team.hasPlayer(other.getPlayer())) { for (OfflinePlayer player : team.getPlayers()) {
team.addPlayer(other.getPlayer()); if (!player.isOnline() || !faction.getFPlayers().contains(FPlayers.i.get(player.getPlayer()))) {
// Player is offline or no longer in faction
team.removePlayer(player);
}
}
for (FPlayer fmember : faction.getFPlayers()) {
if (!fmember.isOnline()) {
continue;
}
if (!team.hasPlayer(fmember.getPlayer())) {
// Scoreboard team doesn't have player; add him/her
team.addPlayer(fmember.getPlayer());
}
}
// Update faction prefix
String prefix = faction.getTag().substring(0, Math.min(13, faction.getTag().length())) + " " + faction.getRelationTo(this.fplayer).getColor();
if (team.getPrefix() == null || !team.getPrefix().equals(prefix)) {
team.setPrefix(prefix);
} }
} }
} }