Refactor scoreboard issue. Resolves issue #80.
This commit is contained in:
parent
25696f32aa
commit
7ada15992b
@ -1,8 +1,10 @@
|
|||||||
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.struct.Permission;
|
import com.massivecraft.factions.struct.Permission;
|
||||||
import com.massivecraft.factions.struct.Relation;
|
import com.massivecraft.factions.struct.Relation;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@ -85,5 +87,18 @@ 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()) {
|
||||||
|
if (!ourMember.isOnline()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (FPlayer theirMember : them.getFPlayers()) {
|
||||||
|
if (!theirMember.isOnline()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
FScoreboard.get(ourMember).updateColor(theirMember);
|
||||||
|
FScoreboard.get(theirMember).updateColor(ourMember);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package com.massivecraft.factions.listeners;
|
package com.massivecraft.factions.listeners;
|
||||||
|
|
||||||
import com.massivecraft.factions.*;
|
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.FScoreboard;
|
||||||
import com.massivecraft.factions.scoreboards.sidebar.FDefaultSidebar;
|
import com.massivecraft.factions.scoreboards.sidebar.FDefaultSidebar;
|
||||||
import com.massivecraft.factions.struct.Permission;
|
import com.massivecraft.factions.struct.Permission;
|
||||||
@ -515,4 +517,16 @@ public class FactionsPlayerListener implements Listener {
|
|||||||
badGuy.detach();
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
package com.massivecraft.factions.scoreboards;
|
package com.massivecraft.factions.scoreboards;
|
||||||
|
|
||||||
import com.massivecraft.factions.FPlayer;
|
import com.massivecraft.factions.FPlayer;
|
||||||
|
import com.massivecraft.factions.FPlayers;
|
||||||
import com.massivecraft.factions.P;
|
import com.massivecraft.factions.P;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
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;
|
||||||
import org.bukkit.scoreboard.Scoreboard;
|
import org.bukkit.scoreboard.Scoreboard;
|
||||||
|
import org.bukkit.scoreboard.Team;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -17,12 +20,16 @@ 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 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) {
|
||||||
@ -37,10 +44,41 @@ public class FScoreboard {
|
|||||||
return fscoreboards.get(player);
|
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) {
|
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()) {
|
||||||
|
Team team = scoreboard.registerNewTeam(color.name());
|
||||||
|
team.setPrefix(color.toString());
|
||||||
|
colorTeams.put(color, team);
|
||||||
|
}
|
||||||
|
|
||||||
fplayer.getPlayer().setScoreboard(scoreboard);
|
fplayer.getPlayer().setScoreboard(scoreboard);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,4 +138,17 @@ public class FScoreboard {
|
|||||||
bufferedObjective.flip();
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user