Edit sync-async scoreboard

This commit is contained in:
Fabrizio La Rosa 2020-07-12 19:42:56 +02:00
parent 80cc409899
commit 4452fa33fc

View File

@ -37,7 +37,7 @@ public class ScoreboardManager {
this.plugin = plugin; this.plugin = plugin;
this.playerDataManager = plugin.getPlayerDataManager(); this.playerDataManager = plugin.getPlayerDataManager();
Bukkit.getScheduler().runTask(plugin, () -> reloadScoreboards(true)); Bukkit.getScheduler().runTask(plugin, () -> reloadScoreboards(true));
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::updateScoreboards, 20L, 40L); Bukkit.getScheduler().runTaskTimer(plugin, this::updateScoreboards, 20L, 40L);
} }
private synchronized void updateScoreboards() { private synchronized void updateScoreboards() {
@ -45,104 +45,106 @@ public class ScoreboardManager {
final Set<Objective> objectives = primary.getObjectives(); final Set<Objective> objectives = primary.getObjectives();
final Set<Team> teams = primary.getTeams(); final Set<Team> teams = primary.getTeams();
/* Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
* Unregister all teams or objectives that are no longer present in the main /*
* scoreboard. * Unregister all teams or objectives that are no longer present in the main
*/ * scoreboard.
for (UUID uuid : scoreboardStorage.keySet()) { */
Player player = Bukkit.getPlayer(uuid); for (UUID uuid : scoreboardStorage.keySet()) {
if(player != null) { Player player = Bukkit.getPlayer(uuid);
final org.bukkit.scoreboard.Scoreboard board = player.getScoreboard(); if(player != null) {
final org.bukkit.scoreboard.Scoreboard board = player.getScoreboard();
for (String name : objectiveNames) {
if (primary.getObjective(name) != null) continue; for (String name : objectiveNames) {
final Objective objective = board.getObjective(name); if (primary.getObjective(name) != null) continue;
if (objective != null) objective.unregister(); final Objective objective = board.getObjective(name);
}
if (objective != null) objective.unregister();
for (String name : teamNames) {
if (primary.getTeam(name) != null) continue;
final Team team = board.getTeam(name);
if (team != null) team.unregister();
}
}
}
/*
* Update the objective/team names.
*/
objectiveNames.clear();
teamNames.clear();
for(Objective objective : objectives) {
if (primary.getObjective(objective.getName()) != null) {
objectiveNames.add(objective.getName());
}
}
for(Team team : teams) {
if (primary.getTeam(team.getName()) != null) {
teamNames.add(team.getName());
}
}
/*
* Update or add any missing information to the player's scoreboard.
*/
for (UUID uuid : scoreboardStorage.keySet()) {
Player player = Bukkit.getPlayer(uuid);
if(player != null) {
PlayerData pd = playerDataManager.getPlayerData(player);
if(pd != null && pd.isScoreboard()){
final org.bukkit.scoreboard.Scoreboard playerBoard = player.getScoreboard();
for (Objective primaryObjective : objectives) {
Objective obj = playerBoard.getObjective(primaryObjective.getName());
if (obj == null)
obj = playerBoard.registerNewObjective(primaryObjective.getName(), primaryObjective.getCriteria());
obj.setDisplayName(primaryObjective.getDisplayName());
obj.setDisplaySlot(primaryObjective.getDisplaySlot());
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) obj.setRenderType(primaryObjective.getRenderType());
} }
for (String name : teamNames) {
if (primary.getTeam(name) != null) continue;
final Team team = board.getTeam(name);
if (team != null) team.unregister();
}
}
}
for (Team primaryTeam : teams) { /*
* Update the objective/team names.
Team obj = playerBoard.getTeam(primaryTeam.getName()); */
if (obj == null) obj = playerBoard.registerNewTeam(primaryTeam.getName()); objectiveNames.clear();
teamNames.clear();
obj.setAllowFriendlyFire(primaryTeam.allowFriendlyFire());
obj.setCanSeeFriendlyInvisibles(primaryTeam.canSeeFriendlyInvisibles()); for(Objective objective : objectives) {
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) obj.setColor(primaryTeam.getColor()); if (primary.getObjective(objective.getName()) != null) {
obj.setDisplayName(primaryTeam.getDisplayName()); objectiveNames.add(objective.getName());
obj.setNameTagVisibility(primaryTeam.getNameTagVisibility()); }
obj.setPrefix(primaryTeam.getPrefix()); }
obj.setSuffix(primaryTeam.getSuffix());
for(Team team : teams) {
for (String primaryEntry : primaryTeam.getEntries()) { if (primary.getTeam(team.getName()) != null) {
obj.addEntry(primaryEntry); teamNames.add(team.getName());
}
}
/*
* Update or add any missing information to the player's scoreboard.
*/
for (UUID uuid : scoreboardStorage.keySet()) {
Player player = Bukkit.getPlayer(uuid);
if(player != null) {
PlayerData pd = playerDataManager.getPlayerData(player);
if(pd != null && pd.isScoreboard()){
final org.bukkit.scoreboard.Scoreboard playerBoard = player.getScoreboard();
for (Objective primaryObjective : objectives) {
Objective obj = playerBoard.getObjective(primaryObjective.getName());
if (obj == null)
obj = playerBoard.registerNewObjective(primaryObjective.getName(), primaryObjective.getCriteria());
obj.setDisplayName(primaryObjective.getDisplayName());
obj.setDisplaySlot(primaryObjective.getDisplaySlot());
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) obj.setRenderType(primaryObjective.getRenderType());
} }
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) { for (Team primaryTeam : teams) {
for (Option option : Option.values()) {
obj.setOption(option, primaryTeam.getOption(option)); Team obj = playerBoard.getTeam(primaryTeam.getName());
if (obj == null) obj = playerBoard.registerNewTeam(primaryTeam.getName());
obj.setAllowFriendlyFire(primaryTeam.allowFriendlyFire());
obj.setCanSeeFriendlyInvisibles(primaryTeam.canSeeFriendlyInvisibles());
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) obj.setColor(primaryTeam.getColor());
obj.setDisplayName(primaryTeam.getDisplayName());
obj.setNameTagVisibility(primaryTeam.getNameTagVisibility());
obj.setPrefix(primaryTeam.getPrefix());
obj.setSuffix(primaryTeam.getSuffix());
for (String primaryEntry : primaryTeam.getEntries()) {
obj.addEntry(primaryEntry);
}
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) {
for (Option option : Option.values()) {
obj.setOption(option, primaryTeam.getOption(option));
}
} }
} }
} }
} }
} }
} });
} }
public synchronized void reloadScoreboards(boolean createNew) { public synchronized void reloadScoreboards(boolean createNew) {