Problem with Scoreboards

Discussion in 'Spigot Plugin Development' started by Acer_Mortem, Apr 1, 2014.

  1. Right now, I'm trying to have two objectives, a Health objective that displays a players health underneath their username, and a Cooldown objective on the side bar. Unfortunately, I've run into a problem.

    In method one (shown below), the cooldown objective is NOT seperate for each player, as in, Player A's cooldowns show on Player B's scoreboard (this is not desired). However, the health system works perfectly, and players health levels are correctly shown.

    Code (Text):
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
            for(Player p : Bukkit.getOnlinePlayers()){
                Objective objective = board.registerNewObjective(p.getName(), "dummy"); //Health objective
                objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
                objective.setDisplayName(ChatColor.RED + "❤");
                Score score = objective.getScore(p);
                score.setScore(dfs.getCustomConfig().getInt(p.getUniqueId() + ".Health"));
             
                Objective obj = board.registerNewObjective(p.getName() + "-", "dummy"); //Cooldown Objective
                obj.setDisplaySlot(DisplaySlot.SIDEBAR);
                obj.setDisplayName(ChatColor.AQUA + "~~" + ChatColor.YELLOW + dfs.getClass(p.getUniqueId()) + ChatColor.AQUA + "~~");
             
                p.setScoreboard(board);
    In method two (shown below), the cooldown objective is seperate for each person, as in, Player A's cooldowns aren't shown on Player B's board (this is desired), however, instead of showing a players health, it only shows zero:

    Code (Text):
        public void resetPlayerStats(){
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            for(Player p : Bukkit.getOnlinePlayers()){
                Scoreboard board = manager.getNewScoreboard();
                Objective objective = board.registerNewObjective(p.getName(), "dummy"); //Health objective
                objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
                objective.setDisplayName(ChatColor.RED + "❤");
                Score score = objective.getScore(p);
                score.setScore(dfs.getCustomConfig().getInt(p.getUniqueId() + ".Health"));
             
                Objective obj = board.registerNewObjective(p.getName() + "-", "dummy"); //Cooldown Objective
                obj.setDisplaySlot(DisplaySlot.SIDEBAR);
                obj.setDisplayName(ChatColor.AQUA + "~~" + ChatColor.YELLOW + dfs.getClass(p.getUniqueId()) + ChatColor.AQUA + "~~");
             
                p.setScoreboard(board);

    The only difference between the two pieces of code is the creation of a new Scoreboard in/out of the for loop. Anyone know as to what's going wrong? Thanks!
     
  2. You don't need the enhanced for loop, keep the code, but then use the enhanced for loop when adding the scoreboard.