Why doesn't this work?

Discussion in 'Plugin Development' started by The_Punkster, Jun 21, 2014.

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

    The_Punkster

    I know a lot of things can be done better, but the scoreboard isn't showing at all.

    What's wrong, I swear the same code was working earlier.

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3.  
    4. public int survivalPlayers;
    5. public int pvpPlayers;
    6. public int skyworldPlayers;
    7. public int hubPlayers;
    8.  
    9.  
    10.  
    11. @Override
    12. public void onEnable() {
    13. survivalPlayers = 0;
    14. pvpPlayers = 0;
    15. skyworldPlayers = 0;
    16. hubPlayers = 0;
    17. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    18. public void run() {
    19.  
    20.  
    21. Player[] onlinePlayers = Bukkit.getOnlinePlayers();
    22.  
    23. ScoreboardManager sbManager = Bukkit.getScoreboardManager();
    24. Scoreboard sBoard = sbManager.getNewScoreboard();
    25.  
    26. Objective obj = sBoard.registerNewObjective("testObj", "Dimensions");
    27. Score hubScore = obj.getScore(Bukkit.getOfflinePlayer("Hub:"));
    28. hubScore.setScore(hubPlayers);
    29. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    30. obj.setDisplayName("Online Players");
    31.  
    32. for (Player player : onlinePlayers){
    33.  
    34. player.setScoreboard(sBoard);
    35. player.setScoreboard(sbManager.getNewScoreboard());
    36.  
    37. if (player.getWorld().getName().equalsIgnoreCase("pvp")){
    38. pvpPlayers = pvpPlayers +1;
    39.  
    40. } else if (player.getWorld().getName().equalsIgnoreCase("survival")) {
    41. survivalPlayers = survivalPlayers + 1;
    42.  
    43. } else if (player.getWorld().getName().equalsIgnoreCase("skyworld")) {
    44. skyworldPlayers = skyworldPlayers + 1;
    45.  
    46. } else if (player.getWorld().getName().equalsIgnoreCase("newspawn")) {
    47. hubPlayers = hubPlayers + 1;
    48.  
    49. } else {
    50. }
    51. }
    52. }
    53.  
    54.  
    55. }, 0L, 20L);
    56. }
    57.  
     
  2. The_Punkster
    I'm inexperienced with scoreboards, but getNewScoreboard() creates an empty scoreboard. You're using that method after setting the non-empty scoreboard.
    Code:java
    1. player.setScoreboard(sBoard);
    2. player.setScoreboard(sbManager.getNewScoreboard()); // why is this here?
     
  3. Offline

    The_Punkster

    Assist Thank-you, I removed it and it works...... somewhat.

    Now I have the issue that the number keeps increasing and it's going up every time it rechecks.

    I know why but I don't know how to fix it.

    I've tried doing onJoin, but it doesn't work for me.

    Any ideas?
     
  4. The_Punkster
    That's because you're incrementing the integers every time the code runs, e.g.
    Code:
    pvpPlayers = pvpPlayers +1;
    What are you trying to do? Maybe we can figure out a better way.
     
    xTigerRebornx likes this.
  5. Offline

    xTigerRebornx

    Assist Should point out the fact that he is making a new scoreboard every time the run method is called (so every second)
     
  6. Offline

    The_Punkster

    Display the amount of players on in each world on a scoreboard.
     
Thread Status:
Not open for further replies.

Share This Page