Solved Scoreboard not showing?

Discussion in 'Plugin Development' started by KarimAKL, Jun 4, 2018.

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

    KarimAKL

    I just tried this:
    onJoin Class (open)

    Code:Java
    1.  
    2. public class PlayerJoin implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerJoin(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onJoin(PlayerJoinEvent e) {
    13. Player p = e.getPlayer();
    14. if (p.getWorld().getName().equals("world")) {
    15. ScoreboardUtil scoreboard = new ScoreboardUtil();
    16. scoreboard.setScoreboard(p);
    17. new BukkitRunnable() {
    18. @Override
    19. public void run() {
    20. scoreboard.updateScoreboard(p);
    21. }
    22. }.runTaskTimer(plugin, 20*1, 20*1);
    23. }
    24. }
    25. }
    26.  


    And the scoreboard doesn't show when i join in the world "world". :7
    Main Class (open)

    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. public Economy econ = null;
    5.  
    6. public void onEnable() {
    7. if (setupEconomy()) {
    8. new PlayerJoin(this);
    9. Bukkit.getConsoleSender().sendMessage(Color.here("&aSuccessfully enabled Scoreboard."));
    10. } else {
    11. Bukkit.getConsoleSender().sendMessage(Color.here("&cEconomy plugin missing!"));
    12. }
    13. }
    14.  
    15. private boolean setupEconomy() {
    16. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    17. if (economyProvider != null) {
    18. econ = economyProvider.getProvider();
    19. }
    20.  
    21. return (econ != null);
    22. }
    23. }
    24.  


    ScoreboardUtil Class (open)

    Code:Java
    1.  
    2. public class ScoreboardUtil {
    3.  
    4. private Main plugin;
    5.  
    6. public void setScoreboard(Player p) {
    7. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    8. Objective obj = board.registerNewObjective("test", "dummy");
    9. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    10. obj.setDisplayName(Color.here("&b&lTest"));
    11. Team money = board.registerNewTeam("money");
    12. money.addEntry(Color.here("&a"));
    13. money.setPrefix(Color.here("&cMoney: "+plugin.econ.getBalance(p)));
    14. obj.getScore(Color.here("&a")).setScore(10);;
    15. p.setScoreboard(board);
    16. }
    17.  
    18. public void updateScoreboard(Player p) {
    19. Scoreboard board = p.getScoreboard();
    20. board.getTeam("money").setPrefix(Color.here("&cMoney: "+plugin.econ.getBalance(p)));
    21. }
    22. }
    23.  


    What am i doing wrong? Did i forget something?
     
  2. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Does the plugin register correctly?
     
  3. Offline

    KarimAKL

    @timtower You mean if it comes with any errors when i load the plugin? If so then no and when i do /plugins in-game it shows the plugin as green.
     
  4. Online

    timtower Administrator Administrator Moderator

    @KarimAKL This one:
    • Bukkit.getConsoleSender().sendMessage(Color.here("&aSuccessfully enabled Scoreboard."));
    • } else {
    • Bukkit.getConsoleSender().sendMessage(Color.here("&cEconomy plugin missing!"));
     
  5. Offline

    KarimAKL

    @timtower Oh, the message "Successfully enabled Scoreboard." is the one getting send in the console when i load the plugin. So it should register correctly.
     
  6. Try to use Scoreboard with packets...
     
  7. Online

    timtower Administrator Administrator Moderator

    Why would you bypass a system that does the exact same thing but easier?

    @KarimAKL Can you get any scoreboard to show?
     
  8. Offline

    KarimAKL

    @timtower Not when i try this, it worked when not using teams but then it jumped around. :7
     
  9. Online

    timtower Administrator Administrator Moderator

  10. Offline

    KarimAKL

    @timtower This is what i did before:
    PlayerJoin Class (open)

    Code:Java
    1.  
    2. public class PlayerJoin implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerJoin(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onJoin(PlayerJoinEvent e) {
    13. Player p = e.getPlayer();
    14. if (p.getWorld().getName().equals("world")) {
    15. new BukkitRunnable() {
    16. @Override
    17. public void run() {
    18. ScoreboardManager manager = Bukkit.getScoreboardManager();
    19. Scoreboard board = manager.getNewScoreboard();
    20. Objective obj = board.registerNewObjective("test", "dummy");
    21. obj.setDisplayName(Color.here("&b&lTest"));
    22. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    23. DecimalFormat df = new DecimalFormat("#.##");
    24. String balance = df.format(plugin.econ.getBalance(p));
    25. Score score = obj.getScore(Color.here("&cMoney: &a"+balance));
    26. score.setScore(7);
    27. p.setScoreboard(board);
    28. }
    29. }.runTaskTimer(plugin, 20*1, 20*1);
    30. }
    31. }
    32. }
    33.  


    This shows the scoreboard but it also does this weird thing where it blinks. :7
    EDIT: The reason why it
     
  11. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Because you keep making a new one.
    You need to get the old scoreboard
     
  12. Offline

    KarimAKL

    @timtower When do i make the new scoreboard then? And if it's possible how would i check if they have a scoreboard?
    Maybe something like this?:
    Maybe? (open)

    Code:Java
    1.  
    2. if (player.hasScoreboard) {
    3. scoreboard.create(board);
    4. } else {
    5. scoreboard.update(board);
    6. }
    7.  

    This is just an example of what i think i might need to do, i don't think those methods exists.
     
  13. Online

    timtower Administrator Administrator Moderator

  14. Offline

    KarimAKL

    @timtower What about that? Do you mean that i should check if it is null or something?
    If yes then like this?:
    Like this? (open)

    Code:Java
    1.  
    2. if (board != null) {
    3. p.updateScoreboard(board); //I don't even know if i should do anything here if this is correct.
    4. } else {
    5. p.setScoreboard(board);
    6. }
    7.  

     
  15. Online

    timtower Administrator Administrator Moderator

    @KarimAKL 1. Please stop with the spoilers.
    2. There is a method to get the scoreboard, use it, modify that scoreboard
     
  16. Offline

    KarimAKL

    @timtower
    1. Okay, i just thought it would be nice if it didn't take as much space.
    2. So i should do this?:
    Code:Java
    1.  
    2. public class PlayerJoin implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerJoin(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onJoin(PlayerJoinEvent e) {
    13. Player p = e.getPlayer();
    14. if (p.getWorld().getName().equals("world")) {
    15. //Create scoreboard here
    16. new BukkitRunnable() {
    17. @Override
    18. public void run() {
    19. //Get and modify scoreboard here
    20. }
    21. }.runTaskTimer(plugin, 20*1, 20*1);
    22. }
    23. }
    24. }
    25.  

    Or is that wrong? Anyway, i'll try that now, thanks. :)
     
  17. Online

    timtower Administrator Administrator Moderator

    @KarimAKL 1. Taking much space is not something you should look at.
    2. No, you should get the old one in the runnable, if it is null: make one.
     
  18. Offline

    KarimAKL

    @timtower Oh, i see. Thanks. :p I'll try that now. :)
    EDIT: Did i do it correctly? If so i'll go ahead and try it:
    Code:Java
    1.  
    2. public class PlayerJoin implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerJoin(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onJoin(PlayerJoinEvent e) {
    13. Player p = e.getPlayer();
    14. if (p.getWorld().getName().equals("world")) {
    15. new BukkitRunnable() {
    16. @Override
    17. public void run() {
    18. if (p.getScoreboard() == null) {
    19. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    20. Objective obj = board.registerNewObjective("test", "dummy");
    21. obj.setDisplayName(Color.here("&b&lTest"));
    22. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    23. DecimalFormat df = new DecimalFormat("#.##");
    24. String balance = df.format(plugin.econ.getBalance(p));
    25. Score money = obj.getScore(Color.here("&cMoney: &a"+balance));
    26. money.setScore(7);
    27. p.setScoreboard(board);
    28. } else {
    29. Scoreboard board = p.getScoreboard();
    30. Objective obj = board.registerNewObjective("test", "dummy");
    31. DecimalFormat df = new DecimalFormat("#.##");
    32. String balance = df.format(plugin.econ.getBalance(p));
    33. Score money = obj.getScore(Color.here("&cMoney: &a"+balance));
    34. money.setScore(7);
    35. p.setScoreboard(board);
    36.  
    37. }
    38. }
    39. }.runTaskTimer(plugin, 0, 20*1);
    40. }
    41. }
    42. }
    43.  
     
    Last edited by a moderator: Jun 5, 2018
  19. Online

    timtower Administrator Administrator Moderator

    @KarimAKL And why not try before you ask?
     
  20. Offline

    KarimAKL

    @timtower I wasn't able to try before now, that's why. :p Anyway, i'll go ahead and try it now. EDIT: It doesn't work, still not showing when join the server. I'll try putting some messages to see if it goes through correctly, i'll get back here after that. EDIT2: Just tried putting some messages like this:
    Didn't work (open)

    Code:Java
    1.  
    2. public class PlayerJoin implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerJoin(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onJoin(PlayerJoinEvent e) {
    13. Player p = e.getPlayer();
    14. if (p.getWorld().getName().equals("world")) {
    15. new BukkitRunnable() {
    16. @Override
    17. public void run() {
    18. p.sendMessage(Color.here("before if getScoreboard null"));
    19. if (p.getScoreboard() == null) {
    20. p.sendMessage(Color.here("if scoreboard is null"));
    21. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    22. Objective obj = board.registerNewObjective("test", "dummy");
    23. obj.setDisplayName(Color.here("&b&lTest"));
    24. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    25. DecimalFormat df = new DecimalFormat("#.##");
    26. String balance = df.format(plugin.econ.getBalance(p));
    27. Score money = obj.getScore(Color.here("&cMoney: &a"+balance));
    28. money.setScore(7);
    29. p.setScoreboard(board);
    30. } else {
    31. p.sendMessage(Color.here("else if scoreboard is not null"));
    32. Scoreboard board = p.getScoreboard();
    33. Objective obj = board.registerNewObjective("test", "dummy");
    34. DecimalFormat df = new DecimalFormat("#.##");
    35. String balance = df.format(plugin.econ.getBalance(p));
    36. Score money = obj.getScore(Color.here("&cMoney: &a"+balance));
    37. money.setScore(7);
    38. p.setScoreboard(board);
    39.  
    40. }
    41. }
    42. }.runTaskTimer(plugin, 0, 20*1);
    43. }
    44. }
    45. }
    46.  


    Result on join:
    I get these messages every second:
    "before if getScoreboard null"
    "else if scoreboard is not null"
    But no scoreboard shows up. :7

    EDIT3: Nvm, this worked for showing it:
    Code:Java
    1.  
    2. public class PlayerJoin implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public PlayerJoin(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onJoin(PlayerJoinEvent e) {
    13. Player p = e.getPlayer();
    14. if (p.getWorld().getName().equals("world")) {
    15. new BukkitRunnable() {
    16. @Override
    17. public void run() {
    18. if (p.getScoreboard().getObjective("test") == null) {
    19. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    20. Objective obj = board.registerNewObjective("test", "dummy");
    21. obj.setDisplayName(Color.here("&b&lTest"));
    22. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    23. DecimalFormat df = new DecimalFormat("#.##");
    24. String balance = df.format(plugin.econ.getBalance(p));
    25. Score money = obj.getScore(Color.here("&cMoney: &a"+balance));
    26. money.setScore(7);
    27. p.setScoreboard(board);
    28. } else {
    29. Scoreboard board = p.getScoreboard();
    30. Objective obj = board.getObjective("test");
    31. DecimalFormat df = new DecimalFormat("#.##");
    32. String balance = df.format(plugin.econ.getBalance(p));
    33. Score money = obj.getScore(Color.here("&cMoney: &a"+balance));
    34. money.setScore(7);
    35. p.setScoreboard(board);
    36.  
    37. }
    38. }
    39. }.runTaskTimer(plugin, 0, 20*10);
    40. }
    41. }
    42. }
    43.  
    44.  

    But now that it shows it creates a new score everytime my money amount changes. :7 My guess is it does this because the balance is not the same and therefore makes a new score. :/ But how would i change that? Do i need to remove the score and then make a new one? If i need to remove the score then how would i do that? I don't see any "objective.removeScore" or anything like that. :7
     
    Last edited by a moderator: Jun 6, 2018
  21. Offline

    KarimAKL

    bump.
    Why does it do this and what should i do to fix it? :7
     
  22. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You need to get and remove the old score first
     
  23. Offline

    KarimAKL

    @timtower Would that mean i should save the score(money in this case) in a HashMap and then get the HashMap so i can remove the old balance first? If so then how would i remove it after doing that?
     
  24. Online

    timtower Administrator Administrator Moderator

    @KarimAKL That is a way. Think that setting the score to 0 works for that.
    Not sure though, don't really mess around with scoreboards.
     
  25. Offline

    KarimAKL

    @timtower From what i've read on other threads setting it to 0 won't work because it can go in negatives as well. :7
    EDIT: I think i've found a way, haven't tested it yet tho. Making a HashMap with the old balance(the current one on scoreboard) and then use "board.resetScores("&cMoney: &a"+HashMapBalance);" and then i'll set the new one.
    EDIT2: Seems the "board.resetScores("&cMoney: &a"+oldbal);" does not remove the old balance from the scoreboard for some reason. :/
    EDIT3: Nvm, just realized that i forgot to translateAlternateColorCodes on the resetScores, my bad. Anyway, it works now. :) Thanks for all the help. :D Changed to Solved now.
     
    Last edited by a moderator: Jun 8, 2018
Thread Status:
Not open for further replies.

Share This Page