Refreshing Scoreboard

Discussion in 'Plugin Development' started by top2001, Dec 8, 2014.

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

    top2001

    Hello,

    I was wondering if anyone knows how I would refresh a scoreboard to account for the player join count. At the moment, I have it working but it doesn't refresh the player count when someone joins/leaves the game.

    Here's The Code:

    Code:java
    1. package me.top2001.OreCloudSidebar;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import org.bukkit.scoreboard.DisplaySlot;
    11. import org.bukkit.scoreboard.Objective;
    12. import org.bukkit.scoreboard.Score;
    13. import org.bukkit.scoreboard.Scoreboard;
    14. import org.bukkit.scoreboard.ScoreboardManager;
    15.  
    16. public class Main extends JavaPlugin implements Listener{
    17.  
    18. Scoreboard board;
    19.  
    20. public void onEnable() {
    21. getLogger().info("Plugin Enabled!");
    22. PluginManager pm = getServer().getPluginManager();
    23. pm.registerEvents(this, this);
    24. scoreboard();
    25. }
    26.  
    27. public void onDisable() {
    28. getLogger().info("Plugin Disabled!");
    29. }
    30.  
    31. public void scoreboard() {
    32. ScoreboardManager manager = Bukkit.getScoreboardManager();
    33. board = manager.getNewScoreboard();
    34.  
    35. Objective objective = board.registerNewObjective("OreCloud", "OreCloud");
    36. objective.setDisplayName(ChatColor.GRAY + "»" + ChatColor.GOLD + "»" + ChatColor.GRAY + "»" + ChatColor.YELLOW + " OreCloud " + ChatColor.GRAY + "«" + ChatColor.GOLD + "«" + ChatColor.GRAY + "«");
    37. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    38.  
    39. int a = Bukkit.getOnlinePlayers().length;
    40. int b = Bukkit.getMaxPlayers();
    41. String servername = getConfig().getString("servername");
    42.  
    43. Score score1 = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Online Players:"));
    44. score1.setScore(a);
    45.  
    46. Score score2 = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Max Players:"));
    47. score2.setScore(b);
    48.  
    49. Score score3 = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Server: " + servername));
    50. score3.setScore(0);
    51. }
    52. @EventHandler
    53. public void onPlayerJoin(final PlayerJoinEvent e) {
    54. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    55. public void run() {
    56. e.getPlayer().setScoreboard(board);
    57. }
    58. }, 0, 20);
    59. }
    60. }
     
  2. Offline

    bwfcwalshy Retired Staff

    top2001 Why are you using Bukkit.getOfflinePlayer? also why are you refreshing every second. I would apply the scoreboard to everyone when the player leaves. That will update it.
     
  3. Offline

    top2001


    The end last Event Handler I thought would sort the problem out by refreshing the scoreboard and I am using offline players so it will show the text in the scoreboard. Also, if I was going to apply the scoreboard to everyone when a player leaves, would that be a Player Leave Event?
     
  4. Offline

    bwfcwalshy Retired Staff

    top2001 No need to use offline player just put the string. Yes it would, make the PlayerLeaveEvent and do a for loop.
     
  5. Offline

    top2001


    How would I do the loop?
     
  6. Offline

    bwfcwalshy Retired Staff

    top2001 for(Player p : Bukkit.getOnlinePlayers()){
     
  7. Offline

    top2001


    So this should work now?

    Code:java
    1. package me.top2001.OreCloudSidebar;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerQuitEvent;
    9. import org.bukkit.plugin.PluginManager;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.scoreboard.DisplaySlot;
    12. import org.bukkit.scoreboard.Objective;
    13. import org.bukkit.scoreboard.Score;
    14. import org.bukkit.scoreboard.Scoreboard;
    15. import org.bukkit.scoreboard.ScoreboardManager;
    16.  
    17. public class Main extends JavaPlugin implements Listener{
    18.  
    19. Scoreboard board;
    20.  
    21. public void onEnable() {
    22. getLogger().info("Plugin Enabled!");
    23. PluginManager pm = getServer().getPluginManager();
    24. pm.registerEvents(this, this);
    25. scoreboard();
    26. }
    27.  
    28. public void onDisable() {
    29. getLogger().info("Plugin Disabled!");
    30. }
    31.  
    32. public void scoreboard() {
    33. ScoreboardManager manager = Bukkit.getScoreboardManager();
    34. board = manager.getNewScoreboard();
    35.  
    36. Objective objective = board.registerNewObjective("OreCloud", "OreCloud");
    37. objective.setDisplayName(ChatColor.GRAY + "»" + ChatColor.GOLD + "»" + ChatColor.GRAY + "»" + ChatColor.YELLOW + " OreCloud " + ChatColor.GRAY + "«" + ChatColor.GOLD + "«" + ChatColor.GRAY + "«");
    38. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    39.  
    40. int a = Bukkit.getOnlinePlayers().length;
    41. int b = Bukkit.getMaxPlayers();
    42. String servername = getConfig().getString("servername");
    43.  
    44. Score score1 = objective.getScore(ChatColor.GRAY + "Online Players:");
    45. score1.setScore(a);
    46.  
    47. Score score2 = objective.getScore(ChatColor.GRAY + "Max Players:");
    48. score2.setScore(b);
    49.  
    50. Score score3 = objective.getScore(ChatColor.GRAY + "Server: " + servername);
    51. score3.setScore(0);
    52. }
    53. @EventHandler
    54. public void onPlayerQuit(final PlayerQuitEvent e) {
    55. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    56. public void run() {
    57. for(Player player : Bukkit.getOnlinePlayers()) {
    58. player.setScoreboard(board);
    59. }
    60. }
    61. }, 0, 20);
    62. }
    63. }
     
  8. Offline

    bwfcwalshy Retired Staff

    top2001 No need for the delay every second. Also add the scoreboard on Join and leave.
     
  9. Offline

    top2001


    Where about it the delay? I can't see it anywhere.
     
  10. Offline

    bwfcwalshy Retired Staff

    top2001
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. for(Player player : Bukkit.getOnlinePlayers()) {
    4. player.setScoreboard(board);
    5. }
    6. }
    7. }, 0, 20);


    Right there in PlayerLeaveEvent.
     
  11. Offline

    top2001


    So this code should work now?

    Code:java
    1. package me.top2001.OreCloudSidebar;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9. import org.bukkit.event.player.PlayerQuitEvent;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.scoreboard.DisplaySlot;
    13. import org.bukkit.scoreboard.Objective;
    14. import org.bukkit.scoreboard.Score;
    15. import org.bukkit.scoreboard.Scoreboard;
    16. import org.bukkit.scoreboard.ScoreboardManager;
    17.  
    18. public class Main extends JavaPlugin implements Listener{
    19.  
    20. Scoreboard board;
    21.  
    22. public void onEnable() {
    23. getLogger().info("Plugin Enabled!");
    24. PluginManager pm = getServer().getPluginManager();
    25. pm.registerEvents(this, this);
    26. scoreboard();
    27. }
    28.  
    29. public void onDisable() {
    30. getLogger().info("Plugin Disabled!");
    31. }
    32.  
    33. public void scoreboard() {
    34. ScoreboardManager manager = Bukkit.getScoreboardManager();
    35. board = manager.getNewScoreboard();
    36.  
    37. Objective objective = board.registerNewObjective("OreCloud", "OreCloud");
    38. objective.setDisplayName(ChatColor.GRAY + "»" + ChatColor.GOLD + "»" + ChatColor.GRAY + "»" + ChatColor.YELLOW + " OreCloud " + ChatColor.GRAY + "«" + ChatColor.GOLD + "«" + ChatColor.GRAY + "«");
    39. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    40.  
    41. int a = Bukkit.getOnlinePlayers().length;
    42. int b = Bukkit.getMaxPlayers();
    43. String servername = getConfig().getString("servername");
    44.  
    45. Score score1 = objective.getScore(ChatColor.GRAY + "Online Players:");
    46. score1.setScore(a);
    47.  
    48. Score score2 = objective.getScore(ChatColor.GRAY + "Max Players:");
    49. score2.setScore(b);
    50.  
    51. Score score3 = objective.getScore(ChatColor.GRAY + "Server: " + servername);
    52. score3.setScore(0);
    53. }
    54. @EventHandler
    55. public void onPlayerQuit(final PlayerQuitEvent e) {
    56. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    57. public void run() {
    58. for(Player player : Bukkit.getOnlinePlayers()) {
    59. player.setScoreboard(board);
    60. }
    61. }
    62. }, 0, 0);
    63. }
    64. @EventHandler
    65. public void onPlayerJoin(final PlayerJoinEvent e) {
    66. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    67. public void run() {
    68. for(Player player : Bukkit.getOnlinePlayers()) {
    69. player.setScoreboard(board);
    70. }
    71. }
    72. }, 0, 0);
    73. }
    74. }
     
  12. Offline

    bwfcwalshy Retired Staff

    top2001 Without the Schedulers yes.
     
  13. Offline

    top2001


    It still isn't working :(

    Code:java
    1. package me.top2001.OreCloudSidebar;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9. import org.bukkit.event.player.PlayerQuitEvent;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.scoreboard.DisplaySlot;
    13. import org.bukkit.scoreboard.Objective;
    14. import org.bukkit.scoreboard.Score;
    15. import org.bukkit.scoreboard.Scoreboard;
    16. import org.bukkit.scoreboard.ScoreboardManager;
    17.  
    18. public class Main extends JavaPlugin implements Listener{
    19.  
    20. Scoreboard board;
    21.  
    22. public void onEnable() {
    23. getLogger().info("Plugin Enabled!");
    24. PluginManager pm = getServer().getPluginManager();
    25. pm.registerEvents(this, this);
    26. scoreboard();
    27. }
    28.  
    29. public void onDisable() {
    30. getLogger().info("Plugin Disabled!");
    31. }
    32.  
    33. public void scoreboard() {
    34. ScoreboardManager manager = Bukkit.getScoreboardManager();
    35. board = manager.getNewScoreboard();
    36.  
    37. Objective objective = board.registerNewObjective("OreCloud", "OreCloud");
    38. objective.setDisplayName(ChatColor.GRAY + "»" + ChatColor.GOLD + "»" + ChatColor.GRAY + "»" + ChatColor.YELLOW + " OreCloud " + ChatColor.GRAY + "«" + ChatColor.GOLD + "«" + ChatColor.GRAY + "«");
    39. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    40.  
    41. int a = Bukkit.getOnlinePlayers().length;
    42. int b = Bukkit.getMaxPlayers();
    43. String servername = getConfig().getString("servername");
    44.  
    45. Score score1 = objective.getScore(ChatColor.GRAY + "Online Players:");
    46. score1.setScore(a);
    47.  
    48. Score score2 = objective.getScore(ChatColor.GRAY + "Max Players:");
    49. score2.setScore(b);
    50.  
    51. Score score3 = objective.getScore(ChatColor.GRAY + "Server: " + servername);
    52. score3.setScore(0);
    53. }
    54. @EventHandler
    55. public void onPlayerQuit(PlayerQuitEvent e) {
    56. for(Player player : Bukkit.getOnlinePlayers()) {
    57. player.setScoreboard(board);
    58. }
    59. }
    60. @EventHandler
    61. public void onPlayerJoin(PlayerJoinEvent e) {
    62. for(Player player : Bukkit.getOnlinePlayers()) {
    63. player.setScoreboard(board);
    64. }
    65. }
    66. }
     
  14. Offline

    bwfcwalshy Retired Staff

  15. Offline

    top2001


    Yep. Compile the code and see for yourself.
     
  16. Offline

    Tehmaker

    nevermind u fixed it. sec
     
  17. Offline

    moo3oo3oo3

    run scoreboard() to refresh
     
  18. Offline

    top2001


    Please could you give me some detail?
     
  19. Offline

    Skionz

    top2001 What more do you need?
     
  20. Offline

    top2001


    Perhaps where I put it and the exact thing I need to put in?
     
  21. Offline

    Skionz

    top2001 I suggest looking at some basic Java tutorials on oracles website.
     
  22. Offline

    top2001

    Is this what the code should look like?

    Code:
    package me.top2001.OreCloudSidebar;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    public class Main extends JavaPlugin implements Listener{
        Scoreboard board;
        public void onEnable() {
            getLogger().info("Plugin Enabled!");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            scoreboard();
        }
        public void onDisable() {
            getLogger().info("Plugin Disabled!");
        }
        public void scoreboard() {
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            board = manager.getNewScoreboard();
            Objective objective = board.registerNewObjective("OreCloud", "OreCloud");
            objective.setDisplayName(ChatColor.GRAY + "»" + ChatColor.GOLD + "»" + ChatColor.GRAY + "»" + ChatColor.YELLOW + " OreCloud " + ChatColor.GRAY + "«" + ChatColor.GOLD + "«" + ChatColor.GRAY + "«");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            int a = Bukkit.getOnlinePlayers().length;
            int b = Bukkit.getMaxPlayers();
            String servername = getConfig().getString("servername");
            Score score1 = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Online Players:"));
            score1.setScore(a);
            Score score2 = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Max Players:"));
            score2.setScore(b);
            Score score3 = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Server: " + servername));
            score3.setScore(0);
        }
        @EventHandler
        public void onPlayerJoin(final PlayerJoinEvent e) {
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    scoreboard();
                }
            }, 0, 20);
        }
    }
    @Skionz I know how to code. I am just new to scoreboards.
     
  23. Offline

    Monkey_Swag

    @top2001 make the scoreboard method take in a player, then get your for loop for all players online again, after that, you should know what to do.
     
  24. Offline

    Skionz

    @top2001 Usually when people ask where to put certain code they have no idea what they are doing.
     
  25. Offline

    Agentleader1

    All these replies, I don't know what they are exactly saying! I just decided to skip all that! So really, long code short: Create a new scoreboard every time another player joins.

    If you have a scoreboard declared for the whole class, and modify the scores, everyone with the class-declared scoreboard will have it updated. BUT, if you have a new scoreboard made, then they all get DIFFERENT SCOREBOARDS AND THE WHOLE WORLD IS HAPPY! :D

    Try this:
    Code:
    @EventHandler
    public void onJoin(PlayerJoinEvent pje){
    Player player = pje.getPlayer();
    Scoreboard scoreboard =  manager.getNewScoreboard(); //i assume you have a manager declared already, i believe u can have this class-declared! ;)
    //etc.
    player.setScoreboard(scoreboard);
    }
    However, DON'T do this:
    Code:
    //package
    package me.yourmom.plugin
    
    //imports
    import  yourmom.yourdad.sample
    
    public class YourMother extends JavaPlugin{
             //public or private, they both do same result!
             private ScoreboardManager manager = Bukkit.getScoreboardManager();
             private Scoreboard board = manager.getNewScoreboard();
            //etc.
    }
    
     
  26. Offline

    moo3oo3oo3

    Run the same method to recalculate the scores
     
  27. Offline

    fireblast709

    @top2001 don't schedule a repeating task, especially if you never cancel them (they aren't even needed btw). Secondly, only set the player's scoreboard onJoin. Thirdly, only update the scores instead of setting the scoreboard (both on join and quit, since that's when the player count changes).

    @Agentleader1 in his case, sharing a scoreboard among players is desired, since it makes it easier to update the scoreboard.
     
  28. Offline

    Agentleader1

    Oh, in that case.

    Let's make sure you have it set to dummy type here, instead of:
    Code:
    Objective objective = board.registerNewObjective("OreCloud", "OreCloud");
    we do this:
    Code:
    Objective objective = board.registerNewObjective("OreCloud", "dummy");
    And to be honest, the repeating delayed task isn't the best thing to do:
    Code:
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent pje) {
              scoreboard();
              Player player = pje.getPlayer();
              player.setScoreboard(board);
    }
    Let's face it though, I would have scoreboard and it's manager to be declared as private = null; instead of whatever you have.
    Example:
    Code:
    //package goes here
    
    //imports goes here
    public class yourmom(){
               private ScoreboardManager manager = null;
               private Scoreboard board = null;
               @Override
               public void onEnable(){
                       manager = Bukkit.getScoreboardManager();
                       board = manager.getNewScoreboard(); //screwed that method up, whatever it is ;)
                       scoreboard();
                       //etc.
                  }
    }
     
  29. Offline

    fireblast709

    @Agentleader1 I mentioned the tasks because in due time he would have infinite tasks running, just in case he was using it in other plugins (I didn't recommend them :p). Also, storing the ScoreboardManager isn't necessary.
     
  30. Offline

    ItsMattHogan

Thread Status:
Not open for further replies.

Share This Page