Best Way To Update A Scorebaord

Discussion in 'Plugin Development' started by ItsMattHogan, Oct 25, 2014.

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

    ItsMattHogan

    Hello, I was wondering what's the best way to update the values on a scoreboard...

    Here's an example of what I currently do;
    Code:java
    1. public class ScoreboardHandler {
    2.  
    3. private static HashMap<Player, IScoreboardHandler> playerScoreboards = new HashMap<>();
    4.  
    5. /**
    6. * Creates the player's scoreboard and adds
    7. * the player's scoreboard to the list of boards
    8. * to be updated.
    9. */
    10. public static void createScoreboard(final Player player) {
    11. playerScoreboards.put(player, () -> {
    12.  
    13. Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
    14. Objective objective = scoreboard.registerNewObjective("test", "dummy");
    15.  
    16. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    17. objective.setDisplayName("title");
    18.  
    19. //... scores...
    20.  
    21. player.setScoreboard(scoreboard);
    22. });
    23. }
    24.  
    25. /**
    26. * Begins the loop that updates the players
    27. * scoreboards.
    28. */
    29. public static void beginUpdating() {
    30. Util.getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), () -> {
    31. for (Player online : Bukkit.getOnlinePlayers()) {
    32. playerScoreboards.get(online).update();
    33. }
    34. }, 0, 20);
    35. }
    36. }
    37.  
    38. interface IScoreboardHandler {
    39.  
    40. /**
    41. *
    42. */
    43. public void update();
    44. }

    Please excuse any silly mistakes; I wrote it directly into the code insert thingy and it's 12:45am ;D

    Could this cause a noticeable amount of lag since all of the players scorebaord's are getting updated every second even if they don't need to be? If it does - what's a better way to do it?
     
  2. Offline

    kreashenz

  3. Offline

    mine-care

    Also please don't abuse static :3
    And the way I used to do it but sometimes in lagy conditions it flickers is to send player the scoreboard again so it overrides the one they currently had
     
Thread Status:
Not open for further replies.

Share This Page