diff --git a/src/main/java/com/songoda/skyblock/scoreboard/ScoreboardManager.java b/src/main/java/com/songoda/skyblock/scoreboard/ScoreboardManager.java index 33b102dc..60f57dfd 100644 --- a/src/main/java/com/songoda/skyblock/scoreboard/ScoreboardManager.java +++ b/src/main/java/com/songoda/skyblock/scoreboard/ScoreboardManager.java @@ -37,118 +37,112 @@ public class ScoreboardManager { this.plugin = plugin; this.playerDataManager = plugin.getPlayerDataManager(); Bukkit.getScheduler().runTask(plugin, () -> reloadScoreboards(true)); - Bukkit.getScheduler().runTaskTimer(plugin, this::updateScoreboards, 20L, 40L); + Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::updateScoreboards, 20L, 40L); } private synchronized void updateScoreboards() { - Bukkit.getScheduler().runTask(plugin, () -> { - final org.bukkit.scoreboard.Scoreboard primary = Bukkit.getScoreboardManager().getMainScoreboard(); - final Set objectives = primary.getObjectives(); - final Set teams = primary.getTeams(); - - /* - * Unregister all teams or objectives that are no longer present in the main - * scoreboard. - */ - for (UUID uuid : scoreboardStorage.keySet()) { - Player player = Bukkit.getPlayer(uuid); - if(player != null) { - final org.bukkit.scoreboard.Scoreboard board = player.getScoreboard(); + final org.bukkit.scoreboard.Scoreboard primary = Bukkit.getScoreboardManager().getMainScoreboard(); + final Set objectives = primary.getObjectives(); + final Set teams = primary.getTeams(); + + /* + * Unregister all teams or objectives that are no longer present in the main + * scoreboard. + */ + for (UUID uuid : scoreboardStorage.keySet()) { + Player player = Bukkit.getPlayer(uuid); + if(player != null) { + final org.bukkit.scoreboard.Scoreboard board = player.getScoreboard(); + + for (String name : objectiveNames) { - for (String name : objectiveNames) { - - if (primary.getObjective(name) != null) continue; - - final Objective objective = board.getObjective(name); - - if (objective != null) objective.unregister(); - } + if (primary.getObjective(name) != null) continue; - for (String name : teamNames) { - - if (primary.getTeam(name) != null) continue; - - final Team team = board.getTeam(name); - - if (team != null) team.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(); - Bukkit.getScheduler().runTask(plugin, () -> { - /* - * Update the objective/team names. - */ - - objectiveNames.clear(); - teamNames.clear(); - - for(Objective objective : objectives) { - if (primary.getObjective(objective.getName()) != null) { - objectiveNames.add(objective.getName()); + 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(Team team : teams) { - if (primary.getTeam(team.getName()) != null) { - teamNames.add(team.getName()); - } - } - - Bukkit.getScheduler().runTask(plugin, () -> { - /* - * 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 (Team primaryTeam : teams) { - - 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)); - } - } - } + for (Team primaryTeam : teams) { + + 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) {