diff --git a/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java b/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java index d79338fe..fe53630d 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java +++ b/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java @@ -1,8 +1,10 @@ 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; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Relation; import org.bukkit.Bukkit; @@ -85,5 +87,18 @@ 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); + } + } + } } diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 105ea564..bf9a8cb8 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -1,6 +1,8 @@ package com.massivecraft.factions.listeners; import com.massivecraft.factions.*; +import com.massivecraft.factions.event.FPlayerJoinEvent; +import com.massivecraft.factions.event.FPlayerLeaveEvent; import com.massivecraft.factions.scoreboards.FScoreboard; import com.massivecraft.factions.scoreboards.sidebar.FDefaultSidebar; import com.massivecraft.factions.struct.Permission; @@ -515,4 +517,16 @@ public class FactionsPlayerListener implements Listener { badGuy.detach(); } } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + final public void onFactionJoin(FPlayerJoinEvent event) { + FScoreboard.updateColorToAllLater(event.getfPlayer()); + FScoreboard.updateColorsFromAllLater(event.getfPlayer()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onFactionLeave(FPlayerLeaveEvent event) { + FScoreboard.updateColorToAllLater(event.getfPlayer()); + FScoreboard.updateColorsFromAllLater(event.getfPlayer()); + } } diff --git a/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java b/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java index 233c98bd..c59eaba5 100644 --- a/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java +++ b/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java @@ -1,12 +1,15 @@ package com.massivecraft.factions.scoreboards; import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.P; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.Scoreboard; +import org.bukkit.scoreboard.Team; import java.util.HashMap; import java.util.Map; @@ -17,12 +20,16 @@ public class FScoreboard { private final Scoreboard scoreboard; private final FPlayer fplayer; private final BufferedObjective bufferedObjective; + private final Map colorTeams = new HashMap(); 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) { @@ -37,10 +44,41 @@ 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) + Bukkit.getScheduler().runTask(P.p, new Runnable() { + @Override + public void run() { + for (FPlayer other : FPlayers.i.getOnline()) { + get(other).updateColor(fplayer); + } + } + }); + } + + 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); + } + } + }); + } + private FScoreboard(FPlayer fplayer) { this.fplayer = fplayer; 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); + } + fplayer.getPlayer().setScoreboard(scoreboard); } @@ -100,4 +138,17 @@ public class FScoreboard { bufferedObjective.flip(); } } + + public void updateColor(FPlayer other) { + if (!other.isOnline()) { + return; + } + + ChatColor newColor = fplayer.getRelationTo(other).getColor(); + Team team = colorTeams.get(newColor); + + if (!team.hasPlayer(other.getPlayer())) { + team.addPlayer(other.getPlayer()); + } + } }