'public' scoreboards?

Discussion in 'Plugin Development' started by sgavster, Sep 23, 2013.

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

    sgavster

    Hi everyone.
    I can't seem to figure this out, and I really need to.

    I need it so I can have objectives like
    'money'
    that display on sidebar, but I have no idea how to access it, I have this:
    Code:java
    1. @Override
    2. public void onEnable()
    3. {
    4. log.info("QuaduarPVP has been enabled!");
    5.  
    6. PluginManager pm = Bukkit.getPluginManager();
    7. pm.registerEvents(this.dmh, this);
    8. pm.registerEvents(this.ok, this);
    9. pm.registerEvents(this.ss, this);
    10. pm.registerEvents(this.km, this);
    11. pm.registerEvents(this.ovodh, this);
    12.  
    13. this.saveDefaultConfig();
    14.  
    15. instance = this;
    16.  
    17. ScoreboardManager sbm = Bukkit.getScoreboardManager();
    18. Scoreboard sb = sbm.getNewScoreboard();
    19.  
    20. Objective obj = sb.registerNewObjective("Stats", "dummy");
    21.  
    22. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    23. obj.setDisplayName("§6Stats");
    24.  
    25. Score k = obj.getScore(Bukkit.getOfflinePlayer("Kills:"));
    26. Score d = obj.getScore(Bukkit.getOfflinePlayer("Deaths:"));
    27. Score kd = obj.getScore(Bukkit.getOfflinePlayer("K/D:"));
    28. Score kk = obj.getScore(Bukkit.getOfflinePlayer("K/K"));
    29. Score m = obj.getScore(Bukkit.getOfflinePlayer("Money:"));
    30. Score c = obj.getScore(Bukkit.getOfflinePlayer("Kit:"));
    31.  
    32. for(Player p : Bukkit.getOnlinePlayers())
    33. {
    34. p.setScoreboard(sb);
    35. }
    36. }


    But I can't figure out how to set the score for
    k
    d
    kd
    kk
    m
    c

    from somewhere else than onEnable().

    Thanks for any help in advance!
     
  2. Offline

    Cybermaxke

    You will need to make a field for the scoreboard and set the scoreboard for the players when they join.
     
  3. Offline

    sgavster

    Cybermaxke Could I get an example of the code, please?
     
  4. Offline

    Cybermaxke

    Something like this:
    Code:
    public class MyPlugin extends JavaPlugin implements Listener {
     
        private Scoreboard scoreboard;
     
        @Override
        public void onEnable() {
            this.scoreboard = ...;
     
            this.getServer().getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            event.getPlayer().setScoreboard(this.scoreboard);
        }
    }
     
  5. Offline

    sgavster

    Cybermaxke
    But how would I set the score?
    like say I wanted to give them 10 points on the 'money' Score when they kill someone, how would I do that?

    Any help? @Tzeentchful

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  6. Offline

    Rockslide

    You need to make a separate scoreboard for each player
     
  7. Offline

    sgavster

    Rockslide that is not my issue. Did you even read the post? I need to set the score for each objective. But no on my onEnable, and I don't know how to do it.
     
  8. Offline

    Rockslide

    I did read your post. I though I'd start by pointing out that you need a seperate scoreboard for every player, because your code sets the same board for every player, which won't work.

    Anyway, is this what you mean?
    Code:java
    1. Score k = obj.getScore(Bukkit.getOfflinePlayer("Kills:"));
    2.  
    3. k.setScore(1);


     
  9. Offline

    DevRosemberg

    sgavster Create a method and name it as you want for example:

    Code:java
    1. loadScoreboardForPlayer(Player p) {
    2. p.setScoreboard(whatuwant);
    3. p.getScoreboard.getOfflinePlayer("Kills or what you need which you have alredy set up before").setScore(the int you need);
    4. }


    If what you are asking is how to actually save the kills and all that take a look at my FileUtil and if not please specify exactly what you want cause as it seems no one understood.
     
  10. Offline

    sgavster

    Rockslide well, sorta..
    I need it so I can set the number like that, but somewhere that isn't where onEnable is, so for example in another class, or just somewhere else in the class
     
  11. Offline

    Rockslide

    Then you need to declare the "sb" and "obj" objects globally, instead of in the onEnable() method. Like this:

    Code:java
    1. public class MyPlugin extends JavaPlugin {
    2.  
    3. Scoreboard sb;
    4. Objective obj;
    5.  
    6. @Override
    7. public void onEnable(){
    8. sb = getnewscoreblalalalah
    9. obj = sb.registernewbalalala
    10. }
    11. }
     
  12. Offline

    sgavster

    Rockslide Okay, so how do I set the score with that? I'd assume this.obj.? and this.sb.?
     
  13. Offline

    DevRosemberg

    sgavster use something named Methods and please learn more java before starting with Bukkit, its an advice, i also did this when i didnt start java.

    in that method you can define what you want do something like this:

    Code:java
    1. public void setScoreboardScores() {
    2. /**
    3.   * This is a method, the name is "setScoreboardScores()"
    4.   * Btw: This is a comment ;)
    5.   */
    6.  
    7. int scoreForWhatYouWant = this.plugin.getConfig().getInt("Kills");
    8.  
    9. scboardName.setScore(scoreForWhatYouWant);
    10.  
    11.  
    12. }
    13.  
    14.  


    Then to actually make that method run you do:

    Code:java
    1. this.setScoreboardScores();


    or:

    Code:java
    1. [yourclassname].this.setScoreboardScores();
     
  14. Offline

    sgavster

    DevRosemberg I know, I'm learning java.. I just do bukkit to 'lol' around.. and learning as I go.. Sorry..

    DevRosemberg I'm confused now.. How do I use what you gave me for scoreboard?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  15. Offline

    DevRosemberg

    sgavster Use what Rockslide told you to set the score. I am just giving you an example on how to actually set the scores not onEnable() or in other Class. Just creating a method. And dont be sorry, its just an Advice because you have 15 plugins that actually are just RAndom code that comes to your mind in the moment you code it. I have been around here and on java for a long time and i only have 2 plugins which are Extremely easy to make because i dont like much doing public plugins. I like doing private Minigames and plugins for other people.

    Regards
     
  16. Offline

    sgavster

    DevRosemberg Alright
    @Rockslide You said something about setting it per player.. Could you explain that to me?
     
  17. Offline

    DevRosemberg

    sgavster Use the PlayerJoinEvent and use this or something similar:

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin1(PlayerJoinEvent event) {
    3. Player player = event.getPlayer();
    4. int kills = this.plugin.getConfig().getInt("kills." + player.getName().toLowerCase());
    5. player.setScoreboard(this.plugin.getServer().getScoreboardManager().getNewScoreboard());
    6. Scoreboard scoreboard = player.getScoreboard();
    7. Objective objective = scoreboard.registerNewObjective("Stats", "dummy");
    8. objective.setDisplayName(ChatColor.GREEN + player.getName() + "'s Stats");
    9. objective.getScore(Bukkit.getOfflinePlayer("Kills")).setScore(kills);
    10. /**
    11.   * FYI: The name of the objective in this case Kills cant excede the 16 Characters and
    12.   * Colors add 2 Characters
    13.   */
    14. }
     
  18. Offline

    sgavster

    DevRosemberg I did it, but it doesn't work.

    This is my main code (with Methods)

    Code:java
    1. public static final QuadularPVP getInstance()
    2. {
    3. return instance;
    4. }
    5.  
    6. @Override
    7. public void onEnable()
    8. {
    9. log.info("QuaduarPVP has been enabled!");
    10.  
    11. PluginManager pm = Bukkit.getPluginManager();
    12. pm.registerEvents(this.dmh, this);
    13. pm.registerEvents(this.ok, this);
    14. pm.registerEvents(this.ss, this);
    15. pm.registerEvents(this.km, this);
    16. pm.registerEvents(this.ovodh, this);
    17.  
    18. this.saveDefaultConfig();
    19.  
    20. instance = this;
    21.  
    22. ScoreboardManager sbm = Bukkit.getScoreboardManager();
    23. Scoreboard sb = sbm.getNewScoreboard();
    24.  
    25. Objective obj = sb.registerNewObjective("Stats", "dummy");
    26.  
    27. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    28. }
    29.  
    30. public void kill()
    31. {
    32. Score k = obj.getScore(Bukkit.getOfflinePlayer("Kills:"));
    33. k.setScore(k.getScore() + 1);
    34. }
    35.  
    36. public int getKills()
    37. {
    38. Score k = obj.getScore(Bukkit.getOfflinePlayer("Kills:"));
    39. return k.getScore();
    40. }
    41.  
    42. public void death()
    43. {
    44. Score d = obj.getScore(Bukkit.getOfflinePlayer("Deaths:"));
    45. d.setScore(d.getScore() + 1);
    46. }
    47.  
    48. public int getDeaths()
    49. {
    50. Score d = obj.getScore(Bukkit.getOfflinePlayer("Deaths:"));
    51. return d.getScore();
    52. }
    53.  
    54. public void killd()
    55. {
    56. Score kd = obj.getScore(Bukkit.getOfflinePlayer("K/D:"));
    57. double kdr = 0;
    58. int deathz = getKills();
    59. int killz = getDeaths();
    60. if(deathz != 0)
    61. {
    62. kdr = killz/deathz;
    63. kd.setScore((int) kdr);
    64. }
    65. }
    66.  
    67. public int getKillD()
    68. {
    69. Score kd = obj.getScore(Bukkit.getOfflinePlayer("K/D:"));
    70. return kd.getScore();
    71. }
    72.  
    73. public void killk()
    74. {
    75. Score kk = obj.getScore(Bukkit.getOfflinePlayer("K/K:"));
    76. kk.setScore(kk.getScore() + 1);
    77. }
    78.  
    79. public int getKillK()
    80. {
    81. Score kk = obj.getScore(Bukkit.getOfflinePlayer("K/K:"));
    82. kk.getScore();
    83. return kk.getScore();
    84. }
    85.  
    86. public void kills()
    87. {
    88. Score ks = obj.getScore(Bukkit.getOfflinePlayer("KillStreak:"));
    89. ks.setScore(ks.getScore() + 1);
    90. }
    91.  
    92. public int getKillS()
    93. {
    94. Score ks = obj.getScore(Bukkit.getOfflinePlayer("KillStreak:"));
    95. ks.getScore();
    96. return ks.getScore();
    97. }
    98.  
    99. public void killsclear()
    100. {
    101. Score ks = obj.getScore(Bukkit.getOfflinePlayer("KillStreak:"));
    102. ks.setScore(0);
    103. }
    104.  
    105. public int money(Player p)
    106. {
    107. Score m = obj.getScore(Bukkit.getOfflinePlayer("Money:"));
    108. m.setScore(getConfig().getInt(p.getName() + ".money"));
    109. return m.getScore();
    110. }


    And this is my PlayerJoinEvent:


    Code:java
    1. @EventHandler
    2. public void onScoreboardSet(PlayerJoinEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. p.setScoreboard(this.plugin.getServer().getScoreboardManager().getNewScoreboard());
    6. Scoreboard scoreboard = p.getScoreboard();
    7. Objective objective = scoreboard.registerNewObjective("Stats", "dummy");
    8. objective.setDisplayName(ChatColor.GREEN + p.getName() + "§6's Stats");
    9. objective.getScore(Bukkit.getOfflinePlayer("Kills:")).setScore(QuadularPVP.getInstance().getKills());
    10. objective.getScore(Bukkit.getOfflinePlayer("Deaths:")).setScore(QuadularPVP.getInstance().getDeaths());
    11. objective.getScore(Bukkit.getOfflinePlayer("K/D")).setScore(QuadularPVP.getInstance().getKillD());
    12. objective.getScore(Bukkit.getOfflinePlayer("K/K:")).setScore(QuadularPVP.getInstance().getKillK());
    13. objective.getScore(Bukkit.getOfflinePlayer("KillStreak:")).setScore(QuadularPVP.getInstance().getKillS());
    14. objective.getScore(Bukkit.getOfflinePlayer("Money:")).setScore(QuadularPVP.getInstance().money(p));
    15.  
    16. }
    17. }


    I'm not sure what I'm doing wrong.

    Thanks for your help so far, and any in advance :D

    DevRosemberg Also, I thought I'd note, the 'money' display should work; in the config there is stuff in it so I have NO idea why it isn't wokring

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  19. Offline

    DevRosemberg

  20. Offline

    sgavster

  21. Offline

    DevRosemberg

    sgavster Done, finished the util. Take a look at it in the resource section.
     
  22. Offline

    sgavster

    DevRosemberg So how would this be added to scoreboard.. Basicly I just need scoreboard, cause I also need it for things like my Paintball Plugin. :/
     
  23. Offline

    sgavster

  24. Offline

    DevRosemberg

  25. Offline

    sgavster

    DevRosemberg You gave me a util for player files, but I also need other things than just players
     
Thread Status:
Not open for further replies.

Share This Page