Correct way to update scoreboard?

Discussion in 'Plugin Development' started by Simo389, Oct 27, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Simo389

    Hey, so I have a scoreboard that has specific values depending on each player. But currently it flashes every time it's updated, so I assume I'm updating it incorrectly?

    This is what I have currently:
    Code:java
    1. while(/*Cycle through all players in arena*/) {
    2.  
    3. //currentPlayer is a custom class I created
    4.  
    5. Player player = Bukkit.getPlayer(currentPlayer.getName());
    6. String playerString = currentPlayer.getName();
    7.  
    8. Scoreboard board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
    9. Objective o = board.registerNewObjective("timer", "dummy");
    10. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    11.  
    12. o.setDisplayName("Time Left: " + ChatColor.GREEN + /*Display time left*/);
    13.  
    14. //Leave a space between time and scores
    15. Score space = o.getScore(Bukkit.getOfflinePlayer(""));
    16. space.setScore(2);
    17.  
    18. //Display players' kills and deaths
    19. Score kills = o.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "" + /*Players' kills*/ + " kills"));
    20. kills.setScore(1);
    21. Score deaths = o.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "" + /*Players' deaths*/ + " deaths"));
    22. deaths.setScore(0);
    23. }

    I'm having to create the scoreboard when I update each player, because otherwise the scoreboard for all players gets set to whatever it was for the last player in the while loop. What's the correct thing to do here?

    Currently with the scoreboard flashing I assume there's a better way to have per player scoreboards of this type?

    I used to have this (but it showed all players whatever the value was for the last in the while loop):
    Code:java
    1. Scoreboard board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
    2. Objective o = board.registerNewObjective("timer", "dummy");
    3. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    4.  
    5. while(/*Cycle through all players in arena*/) {
    6.  
    7. //currentPlayer is a custom class I created
    8.  
    9. Player player = Bukkit.getPlayer(currentPlayer.getName());
    10. String playerString = currentPlayer.getName();
    11.  
    12. o.setDisplayName("Time Left: " + ChatColor.GREEN + /*Display time left*/);
    13.  
    14. //Leave a space between time and scores
    15. Score space = o.getScore(Bukkit.getOfflinePlayer(""));
    16. space.setScore(2);
    17.  
    18. //Display players' kills and deaths
    19. Score kills = o.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "" + /*Players' kills*/ + " kills"));
    20. kills.setScore(1);
    21. Score deaths = o.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "" + /*Players' deaths*/ + " deaths"));
    22. deaths.setScore(0);
    23. }
     
  2. Offline

    chasechocolate

    I believe it's because you are constantly setting a player's scoreboard. For my KitPVP server, I just use a custom class which stores all the different logged stats in a HashMap<StatType, Score> (StatType is an enum). Then, I just use score.setScore(someNumber) to update it.
     
    njb_said likes this.
  3. Offline

    Simo389


    Thanks for the suggestion. My issue is that I store the score in the score name, and use the score itself to organise things in a specific order. So I can't really use your suggestion. :-(
     
  4. Offline

    d33k40

  5. there is a solution, instead of making a new scoreboard each time (and having scoreboard packets send again, do it in the flowing order:
    Create new objective with correct setting
    set the sidebar to that objective
    remove old objective

    If you create a scoreboard each time, it will first send packets to fully destruct the old scoreboard
     
Thread Status:
Not open for further replies.

Share This Page