Update Scoreboard Sync

Discussion in 'Plugin Development' started by BrennyMullan, Jul 3, 2014.

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


    I have a scoreboard above the players head that keeps count of there level

    1. ScoreboardManager manager = Bukkit.getScoreboardManager();
    2. Scoreboard board = manager.getNewScoreboard();
    3. Objective objective = board.registerNewObjective("level", "dummy");
    4. objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
    5. objective.setDisplayName("%");

    Say the player moved up a level by destroying a block, they start off with 0% level and when they destroy a block it increments by 1, so the display above there head would show 1% after they destroy it. What is happening though is the players level goes up 1 after destroying which is fine but the scoreboard doesnt update until they destroyed another one, its kind of out of sync.

    This is what i use to update
    1. for(Player online : Bukkit.getOnlinePlayers()){
    2. Score score = objective.getScore(online);
    3. score.setScore(online.getLevel());
    4. online.setScoreboard(board);
    5. }
  2. Offline


    What I usually do is put the scoreboard in a timer that will run every second. What it will do every second, is it will keep adding the scoreboard, at the same time updating it.

    This is an old scoreboard I used for a plugin I made:
    1. final Player p = e.getPlayer();
    2. new BukkitRunnable() {
    3. public void run() {
    4. if(p.isOnline()) {
    5. ScoreboardManager manager = Bukkit.getScoreboardManager();
    6. Scoreboard sb = manager.getNewScoreboard();
    7. Objective obj = sb.registerNewObjective("stats", "dummy");
    8. obj.setDisplayName("§b§lStats");
    9. Score gold = obj.getScore(Bukkit.getOfflinePlayer("§2§lGold"));
    10. gold.setScore(6);
    11. Score pgold = obj.getScore(Bukkit.getOfflinePlayer("§6" + Main.config.getString(p.getName() + ".Gold")));
    12. pgold.setScore(5);
    13. Score kills = obj.getScore(Bukkit.getOfflinePlayer("§2§lKills"));
    14. kills.setScore(4);
    15. Score pkills = obj.getScore(Bukkit.getOfflinePlayer("§6" + Main.config.getString(p.getName() + ".Kills")));
    16. pkills.setScore(3);
    17. Score deaths = obj.getScore(Bukkit.getOfflinePlayer("§2§lDeaths"));
    18. deaths.setScore(2);
    19. Score pdeaths = obj.getScore(Bukkit.getOfflinePlayer("§6" + Main.config.getString(p.getName() + ".Deaths")));
    20. pdeaths.setScore(1);
    21. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    22. p.setScoreboard(sb);
    23. } else {
    24. cancel();
    25. }
    26. }
    27. }.runTaskTimer(plugin, 20, 20L);

    I put this into PlayerJoinEvent. But you can put it wherever you want.
  3. Offline


    Epicballzy Will that cause any lag though? running a scheduler every second without it ever stopping?
  4. Offline


    BrennyMullan It never lagged for me. But make sure to check if the player the player is online, if he is, run the timer, if he isn't then cancel the timer. Just like in the example I showed you.
  5. Offline


    @Epicballzy Yeah its a minigame im creating so i could just check if they were in the game​
Thread Status:
Not open for further replies.

Share This Page