SCOREBOARD

Discussion in 'Plugin Development' started by Bernabeast, Sep 30, 2013.

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

    Bernabeast

    Im making a plugin that adds 1 point to your score on the SIDEBAR. Basic right?
    Not for me please help!
    @sgavster
    @
    Gopaintman

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    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 sbs extends JavaPlugin implements Listener {
     
    public void onPlayerJoinEvent() {
     
    ScoreboardManager sbm = Bukkit.getScoreboardManager();
    Scoreboard sb = sbm.getNewScoreboard();
     
    Objective obj = sb.registerNewObjective("123", "deathCount");
    obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    obj.setDisplayName("Kills");
    Score k = obj.getScore(Bukkit.getOfflinePlayer("Kills:")); // Get a fake
    // offline
     
    }
     
    }
     
  2. Offline

    sgavster

    Bernabeast Man, I wish I could help you.. I'm still trying to figure this out (no one has succesfully helped me, either :()
    I'll continue to look around and tell you if I figure anything else, will put this on my favorites tabs thingy

    SkillSam Doesn't work ;(
    It never makes the fille.. or anything :/

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

    sgavster

    SkillSam I did that; It didn't create it, and when I kill/die from people it doesn't work :C

    SkillSam Alright:
    Code:java
    1. public FileConfiguration stats;
    2. public File sfile;
    3.  
    4. public void setup(Plugin p)
    5. {
    6. sfile = new File(p.getDataFolder(), "stats.yml");
    7. if (!sfile.exists())
    8. {
    9. try
    10. {
    11. sfile.createNewFile();
    12. }
    13. catch (IOException e)
    14. {
    15. Bukkit.getLogger().severe("Could not create stats.yml!");
    16. }
    17. }
    18. stats = YamlConfiguration.loadConfiguration(sfile);
    19. }
    20.  
    21.  
    22.  


    on enable:

    Code:java
    1. setup(plugin);



    onDeath:

    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent e)
    3. {
    4. if(e.getEntity() instanceof Player)
    5. {
    6. Player p = e.getEntity();
    7. if(p.getKiller() instanceof Player)
    8. {
    9. int deaths = stats.getInt("Stats." + p.getName() + ".Deaths");
    10. stats.set("Stats." + p.getName() + ".Deaths", deaths + 1);
    11.  
    12. Player k = p.getKiller();
    13. int kills = stats.getInt("Stats." + k.getName() + ".Kills");
    14. stats.set("Stats." + k.getName() + ".Kills", kills + 1);
    15.  
    16. int kd = stats.getInt("Stats." + p.getName() + ".Kills")/stats.getInt("Stats." + p.getName() + ".Deaths");
    17.  
    18. ScoreboardManager sbm = Bukkit.getScoreboardManager();
    19. Scoreboard sb = sbm.getNewScoreboard();
    20. Objective ob = sb.registerNewObjective("stats", "dummy");
    21. ob.setDisplayName(ChatColor.GOLD + "Your Stats: ");
    22. ob.setDisplaySlot(DisplaySlot.SIDEBAR);
    23.  
    24. Score statkills = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Kills: "));
    25. statkills.setScore(kills);
    26.  
    27. Score statdeaths = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Deaths: "));
    28. statdeaths.setScore(deaths);
    29.  
    30. Score statkd = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "K/D "));
    31. statkd.setScore(kd);
    32.  
    33. p.setScoreboard(sb);
    34. k.setScoreboard(sb);
    35. }
    36. }
    37. }
    38. }



    On join:

    Code:java
    1. @EventHandler
    2. public void onScoreboardSet(PlayerJoinEvent e)
    3. {
    4. Player p = e.getPlayer();
    5.  
    6. int kills = QuadularPVP.getInstance().stats.getInt("Stats." + p.getName() + ".Kills");
    7.  
    8. int deaths = QuadularPVP.getInstance().stats.getInt("Stats." + p.getName() + ".Deaths");
    9.  
    10. int kd = QuadularPVP.getInstance().stats.getInt("Stats." + p.getName() + ".Kills")/QuadularPVP.getInstance().stats.getInt("Stats." + p.getName() + ".Deaths");
    11.  
    12. ScoreboardManager sbm = Bukkit.getScoreboardManager();
    13. Scoreboard sb = sbm.getNewScoreboard();
    14. Objective ob = sb.registerNewObjective("stats", "dummy");
    15. ob.setDisplayName(ChatColor.GOLD + "Your Stats: ");
    16. ob.setDisplaySlot(DisplaySlot.SIDEBAR);
    17.  
    18. Score statkills = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Kills: "));
    19. statkills.setScore(kills);
    20.  
    21. Score statdeaths = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Deaths: "));
    22. statdeaths.setScore(deaths);
    23.  
    24. Score statkd = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "K/D "));
    25. statkd.setScore(kd);
    26. }
    27. }


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

    SkillSam

    sgavster Perhaps in your onEnable method, you can use this:

    Code:java
    1. public void onEnable() {
    2. setup(this);
    3. }


    And then I realized that e.getEntity().getKiller() returns a player, so instead you can use null checks:

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath (PlayerDeathEvent e) {
    3. if (e.getEntity().getKiller() == null) return;
    4. }


    If you're using the stats thing in another class, you can create a method in the original stats file location that could help in other classes.

    Code:java
    1. public FileConfiguration getStats() {
    2. return stats;
    3. }


    Other than that, everything else looks fine to me.

    I forgot one more thing as well. At the end of the PlayerJoinEvent, you have to set the player's scoreboard to the one you have created.

    Code:java
    1. p.setScoreboard(sb);


    EDIT: You can also just divide the integers you already declared and initialized in the PlayerJoinEvent:

    Code:java
    1. int kd = kills/deaths;


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

    sgavster

    SkillSam Where would I put
    Code:java
    1. public FileConfiguration getStats() {
    2. return stats;
    3. }


    cause QuadularMC.getInstance()....
    works with arraylists and things
     
  6. Offline

    SkillSam

    Well, it should rather be where you created the file, so in the class QuadularPVP is where you will create the method, and so here is what it will look like when calling the method:

    Code:java
    1. QuadularPVP.getInstance().getStats()...
     
  7. Offline

    sgavster

  8. Offline

    SkillSam

    sgavster That's right! I forgot, at the beginning of the setup method, you must check whether the data folder already exists.

    Code:java
    1. public void setup(Plugin p) {
    2. if (!(p.getDataFolder.exists())) p.getDataFolder.mkdir();
    3. }
     
  9. Offline

    sgavster

    SkillSam
    getDataFolder cannot be resolved or is not a field
    (facepalm) nevermind..
     
  10. Offline

    SkillSam

  11. Offline

    sgavster

    SkillSam IT WORKS!!!! Thank you, so much.
    I have 1 question: Wont it cause a bit of 'lag' if there is a lot of players, cause it's scanning through 1 file? :O
     
  12. Offline

    Plo124

    sgavster No, you will be surprised how much stuff you can run before one's server will lag
     
    sgavster likes this.
  13. Offline

    SkillSam

    sgavster I have absolutely no idea, as I have never ran a server before, so I'll just go along with what Plo124 said.
     
    sgavster likes this.
  14. Offline

    sgavster

    SkillSam Plo124 Thank you guys a lot.. Really.
    I have been playing with scoreboard for about 1+ months.. Thank you, alot.

    SkillSam Hmm, whenever I restart the server and kill someone it goes back to 0 :(

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

    SkillSam

    sgavster No problem, anytime. First time I've helped someone through someone else thread. :D

    sgavster Wow, today is just not my day, I keep forgetting everything. Okay, so you can create another method in your stats file class, which saves it every time an event is fired.

    Code:java
    1. public void saveStats() {
    2. try {
    3. stats.saveFile(sfile);
    4. }
    5. catch (IOException e) {
    6. Bukkit.getLogger().info("Could not save stats.yml!");
    7. }
    8. }


    Example of using this:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath (PlayerDeathEvent e) {
    3. //Code here
    4. QuadularPVP.getInstance().saveStats();
    5. }


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

    DrTURTLE2

    SkillSam
    Hey, could you possible write everything that needs to be coded completely? I just don't understand with all these adding methods.

    IF you are willing so
     
  17. Offline

    SkillSam

    DrTURTLE2 Oh, okay. Sure, sorry if I don't explain the methods very well. Perhaps we could talk on Skype or message me on which parts you don't understand?
     
  18. Offline

    Doodledew


    Could you maybe pastebin the hole thing? :3
    And also, how could you do it if you want all the players to have an individually scoreboard displaying the player's name? :)

    Oh and also SkillSam isn't that two different Scoreboards? I couldn't get that to work because it's two different, but I just made a PlayerMoveEvent which loaded the stats (updated it everytime you move) instead of the onjoin

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

    SkillSam

    DrTURTLE2 Doodledew
    I have created a class with almost everything you need. Feel free to copy it and edit it to your liking. :)

    Code:java
    1. import java.io.File;
    2. import java.io.IOException;
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.PlayerDeathEvent;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.plugin.Plugin;
    15. import org.bukkit.scoreboard.DisplaySlot;
    16. import org.bukkit.scoreboard.Objective;
    17. import org.bukkit.scoreboard.Score;
    18. import org.bukkit.scoreboard.Scoreboard;
    19. import org.bukkit.scoreboard.ScoreboardManager;
    20.  
    21. public class SettingsManager implements Listener {
    22.  
    23. private SettingsManager() { }
    24. private static SettingsManager instance = new SettingsManager();
    25. public static SettingsManager getInstance() { return instance; }
    26.  
    27. File sfile;
    28. FileConfiguration stats;
    29.  
    30. public void setup (Plugin p) {
    31. if (!p.getDataFolder().exists()) p.getDataFolder().mkdir();
    32.  
    33. sfile = new File (p.getDataFolder(), "stats.yml");
    34. if (!sfile.exists()) {
    35. try { sfile.createNewFile(); }
    36. catch (IOException e) { e.printStackTrace(); }
    37. }
    38. stats = YamlConfiguration.loadConfiguration(sfile);
    39.  
    40. Bukkit.getPluginManager().registerEvents(this, p);
    41. }
    42.  
    43. public FileConfiguration getStats() { return stats; }
    44.  
    45. public void saveStats() {
    46. try { stats.save(sfile); }
    47. catch (IOException e) { e.printStackTrace(); }
    48. }
    49.  
    50. public void reloadStats() { stats = YamlConfiguration.loadConfiguration(sfile); }
    51.  
    52. public void setScoreboard (Player p) {
    53. ScoreboardManager sbm = Bukkit.getScoreboardManager();
    54. Scoreboard sb = sbm.getNewScoreboard();
    55. Objective ob = sb.registerNewObjective("test", "dummy");
    56. ob.setDisplayName(ChatColor.GREEN + p.getName() + "'s Stats: ");
    57. ob.setDisplaySlot(DisplaySlot.SIDEBAR);
    58.  
    59. int KDKills = getStats().getInt("Stats." + p.getName() + ".Kills");
    60. int KDDeaths = getStats().getInt("Stats." + p.getName() + ".Deaths");
    61.  
    62. int KD = 0;
    63.  
    64. if (KD <= 0) { KD = KDKills; }
    65. else { KD = KDKills / KDDeaths; }
    66.  
    67. Score kills = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Kills: "));
    68. kills.setScore(getStats().getInt("Stats." + p.getName() + ".Kills"));
    69.  
    70. Score deaths = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Deaths: "));
    71. deaths.setScore(getStats().getInt("Stats." + p.getName() + ".Deaths"));
    72.  
    73. Score killstreak = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "KillStreak: "));
    74. killstreak.setScore(getStats().getInt("Stats." + p.getName() + ".KillStreak"));
    75.  
    76. Score credits = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Credits: "));
    77. credits.setScore(getStats().getInt("Stats." + p.getName() + ".Credits"));
    78.  
    79. Score kd = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "KD: "));
    80. kd.setScore(KD);
    81.  
    82. p.setScoreboard(sb);
    83. }
    84.  
    85. @EventHandler
    86. public void PlayerJoin (PlayerJoinEvent e) {
    87. Player p = e.getPlayer();
    88. if (getStats().getConfigurationSection("Stats." + p.getName()) == null) {
    89. getStats().set("Stats." + p.getName() + ".Kills", 0);
    90. getStats().set("Stats." + p.getName() + ".Deaths", 0);
    91. getStats().set("Stats." + p.getName() + ".KillStreak", 0);
    92. getStats().set("Stats." + p.getName() + ".Credits", 0);
    93. saveStats();
    94. }
    95. setScoreboard(p);
    96. }
    97.  
    98. @EventHandler
    99. public void PlayerDeath (PlayerDeathEvent e) {
    100. if (e.getEntity() == null && e.getEntity().getKiller() == null) return;
    101. Player p = e.getEntity();
    102. Player k = e.getEntity().getKiller();
    103.  
    104. //Adding the Player Death Counter & Resetting KillStreak
    105. int deaths = getStats().getInt("Stats." + p.getName() + ".Deaths");
    106. getStats().set("Stats." + p.getName() + ".Deaths", deaths + 1);
    107. getStats().set("Stats." + p.getName() + ".KillStreak", 0);
    108.  
    109. //Adding the Killer's Kill Count, KillStreak, and Random Amount of Credits
    110. int kills = getStats().getInt("Stats." + k.getName() + ".Kills");
    111. getStats().set("Stats." + k.getName() + ".Kills", kills + 1);
    112.  
    113. int killstreak = getStats().getInt("Stats." + k.getName() + ".KillStreak");
    114. getStats().set("Stats." + k.getName() + ".KillStreak", killstreak + 1);
    115.  
    116. int credits = getStats().getInt("Stats." + k.getName() + ".Credits");
    117. int percentage = new Random().nextInt(50);
    118.  
    119. getStats().set("Stats." + k.getName() + ".Credits", credits + percentage);
    120.  
    121. saveStats();
    122.  
    123. setScoreboard(p);
    124. setScoreboard(k);
    125. }
    126.  
    127. }
    128.  
     
  20. Offline

    Doodledew


    YESSSS! Thank you SO much! Very much appreciated. I'm so happy! :DDD

    EDIT: It didn't work 0.0 Does it work for you?

    SkillSam Can I add you on Skype? :3 I have some questions regarding some code. doodle.dew :)

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

    Doodledew

    SkillSam Added :) And thanks!

    EDIT: Why didn't I see that myself? 0.0 fail xD

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

    SkillSam

    Doodledew Hmm...I hope this class fixes the error. Sorry for the late reply, I had school to tend to.

    Code:java
    1. package me.SkillSam.Stats;
    2. import java.io.File;
    3. import java.io.IOException;
    4. import java.util.Random;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.entity.PlayerDeathEvent;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.event.player.PlayerMoveEvent;
    16. import org.bukkit.plugin.Plugin;
    17. import org.bukkit.scoreboard.DisplaySlot;
    18. import org.bukkit.scoreboard.Objective;
    19. import org.bukkit.scoreboard.Score;
    20. import org.bukkit.scoreboard.Scoreboard;
    21. import org.bukkit.scoreboard.ScoreboardManager;
    22.  
    23. public class SettingsManager implements Listener {
    24.  
    25. private SettingsManager() { }
    26. private static SettingsManager instance = new SettingsManager();
    27. public static SettingsManager getInstance() { return instance; }
    28.  
    29. File sfile;
    30. FileConfiguration stats;
    31.  
    32. public void setup (Plugin p) {
    33. if (!p.getDataFolder().exists()) p.getDataFolder().mkdir();
    34.  
    35. sfile = new File (p.getDataFolder(), "stats.yml");
    36. if (!sfile.exists()) {
    37. try { sfile.createNewFile(); }
    38. catch (IOException e) { e.printStackTrace(); }
    39. }
    40. stats = YamlConfiguration.loadConfiguration(sfile);
    41.  
    42. Bukkit.getPluginManager().registerEvents(this, p);
    43. }
    44.  
    45. public FileConfiguration getStats() { return stats; }
    46.  
    47. public void saveStats() {
    48. try { stats.save(sfile); }
    49. catch (IOException e) { e.printStackTrace(); }
    50. }
    51.  
    52. public void reloadStats() { stats = YamlConfiguration.loadConfiguration(sfile); }
    53.  
    54. public void setScoreboard (Player p) {
    55. ScoreboardManager sbm = Bukkit.getScoreboardManager();
    56. Scoreboard sb = sbm.getNewScoreboard();
    57. Objective ob = sb.registerNewObjective("test", "dummy");
    58. ob.setDisplayName(ChatColor.GREEN + p.getName() + "'s Stats: ");
    59. ob.setDisplaySlot(DisplaySlot.SIDEBAR);
    60.  
    61. int KDKills = getStats().getInt("Stats." + p.getName() + ".Kills");
    62. int KDDeaths = getStats().getInt("Stats." + p.getName() + ".Deaths");
    63.  
    64. int KD = 0;
    65.  
    66. if (KDDeaths == 0) { KD = KDKills; }
    67. else { KD = KDKills / KDDeaths; }
    68.  
    69. Score kills = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Kills: "));
    70. kills.setScore(getStats().getInt("Stats." + p.getName() + ".Kills"));
    71.  
    72. Score deaths = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Deaths: "));
    73. deaths.setScore(getStats().getInt("Stats." + p.getName() + ".Deaths"));
    74.  
    75. Score killstreak = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "KillStreak: "));
    76. killstreak.setScore(getStats().getInt("Stats." + p.getName() + ".KillStreak"));
    77.  
    78. Score credits = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Credits: "));
    79. credits.setScore(getStats().getInt("Stats." + p.getName() + ".Credits"));
    80.  
    81. Score kd = ob.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "KD: "));
    82. kd.setScore(KD);
    83.  
    84. p.setScoreboard(sb);
    85. }
    86.  
    87. @EventHandler
    88. public void PlayerJoin (PlayerJoinEvent e) {
    89. Player p = e.getPlayer();
    90. if (getStats().getConfigurationSection("Stats." + p.getName()) == null) {
    91. getStats().set("Stats." + p.getName() + ".Kills", 0);
    92. getStats().set("Stats." + p.getName() + ".Deaths", 0);
    93. getStats().set("Stats." + p.getName() + ".KillStreak", 0);
    94. getStats().set("Stats." + p.getName() + ".Credits", 0);
    95. saveStats();
    96. }
    97. setScoreboard(p);
    98. }
    99.  
    100. @EventHandler
    101. public void PlayerMove (PlayerMoveEvent e) {
    102. Player p = e.getPlayer();
    103. setScoreboard(p);
    104. }
    105.  
    106. @EventHandler
    107. public void PlayerDeath (PlayerDeathEvent e) {
    108. Player p = e.getEntity();
    109.  
    110. //Adding the Player Death Counter & Resetting KillStreak
    111. int deaths = getStats().getInt("Stats." + p.getName() + ".Deaths");
    112. getStats().set("Stats." + p.getName() + ".Deaths", deaths + 1);
    113. getStats().set("Stats." + p.getName() + ".KillStreak", 0);
    114. saveStats();
    115.  
    116. setScoreboard(p);
    117.  
    118. if (e.getEntity().getKiller() == null) return;
    119.  
    120. Player k = e.getEntity().getKiller();
    121.  
    122. //Adding the Killer's Kill Count, KillStreak, and Random Amount of Credits
    123. int kills = getStats().getInt("Stats." + k.getName() + ".Kills");
    124. getStats().set("Stats." + k.getName() + ".Kills", kills + 1);
    125.  
    126. int killstreak = getStats().getInt("Stats." + k.getName() + ".KillStreak");
    127. getStats().set("Stats." + k.getName() + ".KillStreak", killstreak + 1);
    128.  
    129. int credits = getStats().getInt("Stats." + k.getName() + ".Credits");
    130. int percentage = new Random().nextInt(50);
    131.  
    132. getStats().set("Stats." + k.getName() + ".Credits", credits + percentage);
    133.  
    134. saveStats();
    135.  
    136. setScoreboard(p);
    137. setScoreboard(k);
    138. }
    139.  
    140. }
    141.  
     
Thread Status:
Not open for further replies.

Share This Page