Solved Scoreboards keep flashing, am I doing something wrong?

Discussion in 'Plugin Development' started by Wizardo367, Sep 12, 2013.

Thread Status:
Not open for further replies.
  1. Hi, I'm currently trying to get this scoreboard to update for each and every person on the server, the scoreboard varies for each player and currently am just recreating the entire scoreboard using a runnable and setting it to the player. The runnable does this every 20 ticks or 1 second and the scoreboard just flashes randomly every 2-3 seconds. Any help on resolving this matter is appreciated. :)
     
  2. Offline

    metalhedd

    we can't tell you what you're doing wrong without seeing what you're doing.. where's the code?
     
  3. My apologies, here you go. :)

    Code:java
    1. @Override
    2. public void run() {
    3.  
    4. String playerName = null;
    5. int level = 0;
    6.  
    7. for (Player player : Bukkit.getOnlinePlayers()){
    8. playerName = player.getName();
    9. level = plugin.extendedDatabaseFunctions.getLevel(player);
    10. }
    11.  
    12. Scoreboard scoreboard = scoreboardManager.getNewScoreboard();
    13. Objective sidebar = scoreboard.registerNewObjective("sidebar", "dummy");
    14. Objective belowName = scoreboard.registerNewObjective("belowName", "dummy");
    15.  
    16. sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
    17. sidebar.setDisplayName(ChatColor.YELLOW + " " + playerName + " ");
    18. belowName.setDisplaySlot(DisplaySlot.BELOW_NAME);
    19. belowName.setDisplayName(ChatColor.GREEN + "Level : " + level);
    20.  
    21. for (Player player : Bukkit.getOnlinePlayers()){
    22. Score playerLevel = sidebar.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Level:")); //Get a fake offline player
    23. playerLevel.setScore(level);
    24. //Set the scoreboard for the player
    25. player.setScoreboard(scoreboard);
    26. }
    27.  
    28. }
     
  4. Offline

    metalhedd

    You shouldn't be doing all of that work every second, its flickering because you create and destroy the players scoreboard every second. you should really only do that once, when they join. I've not used the scoreboard a lot so I'm not sure the correct pattern to achieve the per-player scoreboard, but recreating it in a repeating task is not the right way to go.
     
  5. Thought so but this is the first time I'm using a scoreboard and I'm unsure on how to update them for each player without having to destroy them.
     
  6. Offline

    metalhedd

    this should help:

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent event) {
    4. Scoreboard board = getServer().getScoreboardManager().getNewScoreboard();
    5. Objective obj = board.registerNewObjective("coins", "dummy");
    6. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    7. obj.setDisplayName(event.getPlayer().getName());
    8. obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Coins")).setScore((int) getPlayerBalance(event.getPlayer()));
    9. event.getPlayer().setScoreboard(board);
    10.  
    11. }
    12. ...
    13. ...
    14.  
    15. private void updateScoreboard(String playerName, Integer balance) {
    16. Player player = plugin.getServer().getPlayerExact(playerName);
    17. Scoreboard board = player.getScoreboard();
    18. Objective coinsObj = board.getObjective(DisplaySlot.SIDEBAR);
    19. coinsObj.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Coins")).setScore(balance);
    20. }
    21.  
    22.  
     
    br456 and Wizardo367 like this.
  7. Offline

    Rprrr

    Wizardo367
    Update your scoreboards only when the amount of coins changes, not every tick. :) Way more efficient & it won't flicker.
     
  8. Thanks, worked like a treat. :D
     
  9. Offline

    MayoDwarf

    Please marked this as filled. Thank you.
     
  10. What do you mean, it's already marked as solved? what's filled?
     
  11. Offline

    MayoDwarf

    I meant solved, lol sorry. When I saw this it wasn't mark as solved sorry.
     
  12. It was... I marked it as solved yesterday.
     
  13. Offline

    MayoDwarf

    It wasn't marked as solved yesterday...
     
  14. ...it was, but it doesn't matter whether or not it was anyways it's still marked as solved.
     
Thread Status:
Not open for further replies.

Share This Page