KIlls Scoreboard help!

Discussion in 'Plugin Development' started by xCalib0r, Apr 10, 2014.

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

    xCalib0r

    So I setup a scoreboard and it's working fine BUT I don't know how to set the score for it, when I set it to killCount.setScore(Kills.get(p.getName())); Minecraft gives me tons of errors. Here's my code, please help.

    Code:
     
    public static HashMap<String, Integer> kills = new HashMap<String, Integer>();
     
    public static void updateScoreboard(final Player p) {
    Bukkit.getScheduler().scheduleSyncRepeatingTask(Wrapper.instance,
    new Runnable() {
    public void run() {
     
    Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
    Objective obj = scoreboard.registerNewObjective("scoreboard", "dummy");
    obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    obj.setDisplayName(ChatColor.RED + p.getName());
     
    Score killCount = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Kills:"));
    //HELP ! killCount.setScore();
     
     
     
    p.setScoreboard(scoreboard);
     
     
    }
     
    }, 0, 10);
     
    }
     
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
    Player p = (Player) e.getPlayer();
     
    updateScoreboard(p);
    }
     
    @EventHandler
    public void onPlayerDeath(EntityDeathEvent e) {
     
    Player p = (Player) e.getEntity();
    Player killer = p.getKiller();
     
    kills.put(killer.getName(), kills.get(killer.getName()) + 1);
     
     
    }
     
     
    
     
  2. Offline

    MrInspector

    Post the error log here with a tag or on pastebin.
     
  3. Offline

    xCalib0r

    MrInspector I have a scoreboard also display players money and its working fab, this is the only thing giving me errors.

    MrInspector when the player joins, it throws a nullpointerexception

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

    MrInspector

    I uh need the error log to see where the NPE is coming from :p
     
  5. Offline

    xCalib0r

  6. Offline

    Dahwn

    xCalib0r
    Could you post your code as Java code?

    -Dahwn
     
  7. Offline

    xCalib0r

    bump

    anyone?

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

    coasterman10

    What is line 35 from your code?

    Also, I highly recommend rewriting the whole method from scratch, since it is very inefficient. You should always take youtube tutorials with a grain of salt, because I know a lot of them to grossly misuse the Bukkit API and Java in general.
     
  9. Offline

    xCalib0r

  10. Offline

    coasterman10

    xCalib0r The stacktrace is telling us that the error is on the 35th line of code. Does your IDE have line numbers enabled?
     
  11. Offline

    itzrobotix

    It will probably be updateScoreboard(p); but obviously it is the method causing this.

    Code:java
    1. public static HashMap<String, Integer> kills = new HashMap<String, Integer>();
    2.  
    3. private Scoreboard scoreboard;
    4. private Objective obj;
    5. private Score killCount;
    6.  
    7. onEnable method; //I would instantiate it in your on enable and outside the onEnable create the variables.
    8. Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
    9. Objective obj = scoreboard.registerNewObjective("scoreboard", "dummy");
    10. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    11. obj.setDisplayName(ChatColor.RED + p.getName());
    12.  
    13. Score killCount = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Kills:"));
    14. killCount.setScore(*Insert score here*);
    15. }
    16.  
    17. @EventHandler
    18. public void onPlayerJoin(PlayerJoinEvent e) {
    19. Player p = e.getPlayer();
    20.  
    21. p.setScoreboard(scoreboard);
    22. }
    23.  
    24. @EventHandler
    25. public void onPlayerDeath(PlayerDeathEvent e) {
    26. Player p = e.getPlayer();
    27. if(e.getEntity().getKiller() instanceof Player){
    28. Player killer = e.getEntity().getKiller();
    29.  
    30. kills.put(killer.getName(), kills.get(killer.getName()) + 1);
    31. }
    32. }


    This code should be a lot cleaner and efficient. Wether it works or not I don't know, I wasn't trying to make it.
     
  12. Offline

    xCalib0r

    itzrobotix updatescoreboard was being called in onEnable so it's essentially the same thing, though this is what I want to know. What do I put in setscore to display the score. I did kills.get(p.getName())) and thats what caused it all.
    1. killCount.setScore(*Insert score here*);

    O ya one thing, when the player dies their's also ONE MORE null pointer exception, and that's this.

    Code:java
    1.  
    2. kills.put(killer.getName(), kills.get(killer.getName()) + 1);
    3.  


    I think I fixed it with a simple check though
    Code:java
    1.  
    2. if(killer instanceof Player) {
    3. kills.put(killer.getName(), kills.get(killer.getName()) + 1);
    4. }
    5.  


    still don't know what to put in setScore.

    bump

    anyon?

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

    Dahwn

    xCalib0r
    Look at what itzrobotix posted: the killCount is your scoreboard object. Now you have to set it's score to how many kills the person has:
    Code:java
    1. killCount.setScore("your int score here");

    Try that as your integer:
    Code:java
    1. kills.get(p.getName()).intValue();

    It works for me!

    -Dahwn
     
  14. Offline

    itzrobotix

    I fixed a lot of the code for you... Also the nullpointer may be caused by the fact you are setting there score by getting it in the hashmap, but if they are not in the hashmap it will return null.
    So change the code to this;
    Code:java
    1. if(kills.get(killer.getName() != null){
    2. //Insert code here
    3. return;
    4. }
    5. else kills.put(killer.getName(), 1); return;
     
  15. Offline

    xCalib0r

    Guys how do I make sure it's ALWAY updating? You know for credits and stuff.

    Still getting null pointer exception, FULL CODE:

    ScoreboardManager:
    Code:java
    1.  
    2. public class ScoreboardManager implements Listener {
    3.  
    4. public static HashMap<String, Integer> kills = new HashMap<String, Integer>();
    5.  
    6. public static void updateScoreboard(final Player p) {
    7. Bukkit.getScheduler().scheduleSyncRepeatingTask(Wrapper.instance,
    8. new Runnable() {
    9. public void run() {
    10.  
    11. Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
    12. Objective obj = scoreboard.registerNewObjective("scoreboard", "dummy");
    13. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    14. obj.setDisplayName(ChatColor.RED + p.getName());
    15.  
    16. Score killCount = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Kills:"));
    17.  
    18. killCount.setScore(kills.get(p.getName()).intValue());
    19.  
    20. p.setScoreboard(scoreboard);
    21.  
    22.  
    23. }
    24.  
    25. }, 0, 10);
    26.  
    27. }
    28.  
    29. @EventHandler
    30. public void onPlayerJoin(PlayerJoinEvent e) {
    31. Player p = (Player) e.getPlayer();
    32.  
    33. updateScoreboard(p);
    34. }
    35.  
    36. @EventHandler
    37. public void onPlayerDeath(EntityDeathEvent e) {
    38.  
    39. Player p = (Player) e.getEntity();
    40. Player killer = p.getKiller();
    41.  
    42. if (kills.get(killer.getName()) != null) {
    43. kills.put(killer.getName(), kills.get(killer.getName()) + 1);
    44. return;
    45. } else {
    46. kills.put(killer.getName(), 1);
    47. return;
    48. }
    49.  
    50.  
    51. }
    52.  
    53.  
    54. }
    55.  


    MAIN CLASS (onEnable):
    Code:java
    1.  
    2. AutoBroadcastManager.AutoBroadcastMessage();
    3. for (Player p : Bukkit.getOnlinePlayers()) {
    4. ScoreboardManager.updateScoreboard(p);
    5. }
    6.  


    anyone?

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

    coasterman10

    Stacktrace? It's important to know where the NPE is originating.
     
  17. Offline

    deathline75

    Try adding this line on your onPlayerJoin:
    Code:java
    1. kills.put(p.getName(), 0);

    I suspect you did not put your player into your HashMap, that is why it is giving a NULL.

    Also, don't use player names anymore. It has been deprecated since 1.7.5 as Mojang is releasing a way to change player names. Use player UUID instead.
     
  18. Offline

    Dahwn

    deathline75
    You can use player names for sessions like the killstreaks but not if you want to save data for example in a MySQL database or config file! :)

    -Dahwn
     
  19. Offline

    deathline75

    Dahwn
    Oh. Then I think it should be fine. Thanks for the info! :D
     
  20. Offline

    xCalib0r

    deathline75 nope, still getting the null pointer exception and the scoreboard isn't even showing up.
    Code:java
    1.  
    2. public class ScoreboardManager implements Listener {
    3.  
    4. public static HashMap<String, Integer> kills = new HashMap<String, Integer>();
    5. public static Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
    6.  
    7.  
    8. public static void updateScoreboard(final Player p) {
    9. Objective obj = scoreboard.registerNewObjective("stats", "dummy");
    10. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    11. obj.setDisplayName(ChatColor.RED + p.getName());
    12.  
    13.  
    14. Score killCount = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Kills:"));
    15.  
    16. killCount.setScore(kills.get(p.getName()).intValue());
    17.  
    18. p.setScoreboard(scoreboard);
    19.  
    20. }
    21.  
    22. @EventHandler
    23. public void onPlayerJoin(PlayerJoinEvent e) {
    24. Player p = (Player) e.getPlayer();
    25.  
    26. kills.put(p.getName(), 0);
    27. updateScoreboard(p);
    28. }
    29.  
    30. @EventHandler
    31. public void onPlayerDeath(EntityDeathEvent e) {
    32.  
    33. Player p = (Player) e.getEntity();
    34. Player killer = p.getKiller();
    35. if (e.getEntity().getKiller() instanceof Player) {
    36. kills.put(killer.getName(), kills.get(killer.getName()) + 1);
    37. }
    38. }
    39.  
    40. }
    41.  


    The error is this guys : killCount.setScore(kills.get(p.getName())); when I take it out and just put 0 the scoreboard shows.

    It has something to do with the hashmap for sure :/

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page