How do I auto update scoreboards?

Discussion in 'Plugin Development' started by legoman519, Feb 17, 2014.

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

    legoman519

    Hello I have been trying to find out how to update my scoreboard when someone joins.

    Code:java
    1. package me.legoman519.plugins.ScoreBoards;
    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. PluginManager pm = getServer().getPluginManager();
    22. pm.registerEvents(this, this);
    23.  
    24. scoreboard();
    25. }
    26.  
    27. public void scoreboard() {
    28. ScoreboardManager manager = Bukkit.getScoreboardManager();
    29. board = manager.getNewScoreboard();
    30.  
    31. Objective objective = board.registerNewObjective("Test", "Test2");
    32. objective.setDisplayName(ChatColor.DARK_RED + "┬žlCrimsonRealm");
    33. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    34.  
    35. int a = Bukkit.getOnlinePlayers().length;
    36. int b = Bukkit.getMaxPlayers();
    37.  
    38. Score onlineplayers = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Online Players"));
    39. onlineplayers.setScore(a);
    40.  
    41. Score maxplayers = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Max Players"));
    42. maxplayers.setScore(b);
    43.  
    44. }
    45.  
    46. @EventHandler
    47. public void onJoin(PlayerJoinEvent e) {
    48. e.getPlayer().setScoreboard(board);
    49.  
    50. }
    51.  
    52. public void onDisable() {
    53. }
    54. }
    55.  


    I have an int called a for online players but when someone logs in it doesnt update.
     
  2. Offline

    Stealth2800

    You're currently just setting the joined player's scoreboard to the scoreboard variable you have defined. You're not updating it. Move the last part of your 'scoreboard()' method to its own method and then call it whenever a player joins.

    Code:
    public void updateScoreboard() {
      int a = Bukkit.getOnlinePlayers().length;
      int b = Bukkit.getMaxPlayers();
     
      Score onlineplayers = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Online Players"));
      onlineplayers.setScore(a);
     
      Score maxplayers = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Max Players"));
      maxplayers.setScore(b);
    }
     
    GrandmaJam likes this.
Thread Status:
Not open for further replies.

Share This Page