From c049768e217959b3b20c13237afbe67c11337b87 Mon Sep 17 00:00:00 2001 From: eueln Date: Mon, 20 Oct 2014 14:55:16 -0500 Subject: [PATCH] Add faction tag to team prefix --- .../com/massivecraft/factions/cmd/CmdTag.java | 2 + .../factions/cmd/FRelationCommand.java | 16 +--- .../listeners/FactionsPlayerListener.java | 6 +- .../factions/scoreboards/FScoreboard.java | 79 +++++++++++-------- 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdTag.java b/src/main/java/com/massivecraft/factions/cmd/CmdTag.java index da86d37d..b2c4e57f 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdTag.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdTag.java @@ -4,6 +4,7 @@ import com.massivecraft.factions.Conf; import com.massivecraft.factions.Faction; import com.massivecraft.factions.Factions; import com.massivecraft.factions.event.FactionRenameEvent; +import com.massivecraft.factions.scoreboards.FScoreboard; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.util.MiscUtil; import org.bukkit.Bukkit; @@ -73,6 +74,7 @@ public class CmdTag extends FCommand { faction.msg("The faction %s changed their name to %s.", fme.getColorTo(faction) + oldtag, myFaction.getTag(faction)); } + FScoreboard.applyUpdates(myFaction); } } diff --git a/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java b/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java index fe53630d..e84fd28c 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java +++ b/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java @@ -1,7 +1,6 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Conf; -import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Faction; import com.massivecraft.factions.event.FactionRelationEvent; import com.massivecraft.factions.scoreboards.FScoreboard; @@ -87,18 +86,7 @@ public abstract class FRelationCommand extends FCommand { myFaction.msg("This will have no effect while your faction is peaceful."); } - for (FPlayer ourMember : myFaction.getFPlayers()) { - if (!ourMember.isOnline()) { - continue; - } - for (FPlayer theirMember : them.getFPlayers()) { - if (!theirMember.isOnline()) { - continue; - } - FScoreboard.get(ourMember).updateColor(theirMember); - FScoreboard.get(theirMember).updateColor(ourMember); - } - } - + FScoreboard.applyUpdates(myFaction); + FScoreboard.applyUpdates(them); } } diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 2b7610cc..da606a30 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -526,13 +526,11 @@ public class FactionsPlayerListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) final public void onFactionJoin(FPlayerJoinEvent event) { - FScoreboard.updateColorToAllLater(event.getfPlayer()); - FScoreboard.updateColorsFromAllLater(event.getfPlayer()); + FScoreboard.applyUpdatesLater(event.getFaction()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onFactionLeave(FPlayerLeaveEvent event) { - FScoreboard.updateColorToAllLater(event.getfPlayer()); - FScoreboard.updateColorsFromAllLater(event.getfPlayer()); + FScoreboard.applyUpdatesLater(event.getFaction()); } } diff --git a/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java b/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java index c59eaba5..45b89335 100644 --- a/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java +++ b/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java @@ -1,10 +1,8 @@ package com.massivecraft.factions.scoreboards; -import com.massivecraft.factions.FPlayer; -import com.massivecraft.factions.FPlayers; -import com.massivecraft.factions.P; +import com.massivecraft.factions.*; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scoreboard.DisplaySlot; @@ -20,16 +18,14 @@ public class FScoreboard { private final Scoreboard scoreboard; private final FPlayer fplayer; private final BufferedObjective bufferedObjective; - private final Map colorTeams = new HashMap(); + private final Map factionTeams = new HashMap(); + private int factionTeamPtr; private FSidebarProvider defaultProvider; private FSidebarProvider temporaryProvider; private boolean removed = false; public static void init(FPlayer fplayer) { fscoreboards.put(fplayer.getPlayer(), new FScoreboard(fplayer)); - - updateColorToAllLater(fplayer); - updateColorsFromAllLater(fplayer); } public static void remove(FPlayer fplayer) { @@ -44,28 +40,19 @@ public class FScoreboard { return fscoreboards.get(player); } - public static void updateColorToAllLater(final FPlayer fplayer) { - // We're delaying by a tick here to simplify logic in other areas - // (e.g. for FPlayer{Join,Leave}Event handlers; CmdDisband) + public static void applyUpdatesLater(final Faction faction) { Bukkit.getScheduler().runTask(P.p, new Runnable() { @Override public void run() { - for (FPlayer other : FPlayers.i.getOnline()) { - get(other).updateColor(fplayer); - } + applyUpdates(faction); } }); } - public static void updateColorsFromAllLater(final FPlayer fplayer) { - Bukkit.getScheduler().runTask(P.p, new Runnable() { - @Override - public void run() { - for (FPlayer other : FPlayers.i.getOnline()) { - get(fplayer).updateColor(other); - } - } - }); + public static void applyUpdates(Faction faction) { + for (FScoreboard fscoreboard : fscoreboards.values()) { + fscoreboard.updateFactionTeam(faction); + } } private FScoreboard(FPlayer fplayer) { @@ -73,10 +60,8 @@ public class FScoreboard { this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); this.bufferedObjective = new BufferedObjective(scoreboard); - for (ChatColor color : ChatColor.values()) { - Team team = scoreboard.registerNewTeam(color.name()); - team.setPrefix(color.toString()); - colorTeams.put(color, team); + for (Faction faction : Factions.i.get()) { + updateFactionTeam(faction); } fplayer.getPlayer().setScoreboard(scoreboard); @@ -139,16 +124,44 @@ public class FScoreboard { } } - public void updateColor(FPlayer other) { - if (!other.isOnline()) { + public void updateFactionTeam(Faction faction) { + Team team = factionTeams.get(faction); + + if (!Factions.i.get().contains(faction)) { + // Faction was disbanded + if (team != null) { + factionTeams.remove(faction); + team.unregister(); + } return; } - ChatColor newColor = fplayer.getRelationTo(other).getColor(); - Team team = colorTeams.get(newColor); + if (team == null) { + team = scoreboard.registerNewTeam("faction_" + (factionTeamPtr++)); + factionTeams.put(faction, team); + } - if (!team.hasPlayer(other.getPlayer())) { - team.addPlayer(other.getPlayer()); + for (OfflinePlayer player : team.getPlayers()) { + 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); } } }