Development Assistance Sending a HashMap's contents to a player

Discussion in 'Plugin Help/Development/Requests' started by haydenaa, Nov 13, 2014.

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

    haydenaa

    Hello. So i'm making a plugin called StatsPro, and in this plugin it stores the player's kills and deaths in a HashMap. The only problem I have in it is when I attempt to retrieve the player's info from the HashMap and send it in a form of a message, Bukkit completely ignores that message. If I attempt to create a integer and make it equal to getting the player's contents in one of the HashMap's, it gives me an internal error. Thanks in advance :p

    Main Class:
    Code:java
    1. package com.guildlaunch.platinumverse.statspro;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. private Kills killsclass;
    12. private Deaths deathsclass;
    13.  
    14. public void onEnable() {
    15. getServer().getPluginManager().registerEvents(new Kills(), this);
    16. getServer().getPluginManager().registerEvents(new FirstJoin(), this);
    17. getServer().getPluginManager().registerEvents(new Deaths(), this);
    18. getConfig();
    19. }
    20.  
    21. public void onDisable() {
    22. getConfig().options().copyDefaults(true);
    23. saveDefaultConfig();
    24. saveConfig();
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    28.  
    29. Player player = (Player) sender;
    30.  
    31. if(cmd.getName().equalsIgnoreCase("stats")) {
    32. player.sendMessage(ChatColor.RED + "[Stats]");
    33. player.sendMessage(ChatColor.GREEN + "Kills: " + ChatColor.AQUA + killsclass.killamount);
    34. player.sendMessage(ChatColor.GREEN + "Deaths: " + ChatColor.AQUA + deathsclass.deathamount);
    35. player.sendMessage(ChatColor.GREEN + "KDR: " + (killsclass.killamount / deathsclass.deathamount));
    36. return true;
    37. }
    38.  
    39. return false;
    40. }
    41.  
    42. }


    First Join Class:
    Code:java
    1. package com.guildlaunch.platinumverse.statspro;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.player.PlayerJoinEvent;
    6.  
    7. public class FirstJoin implements Listener {
    8.  
    9. private Main plugin;
    10. private Kills killsclass;
    11. private Deaths deathsclass;
    12.  
    13. public void onPlayerJoin(PlayerJoinEvent event) {
    14. Player player = event.getPlayer();
    15.  
    16. if(!(player.hasPlayedBefore())) {
    17. killsclass.kills.put(player, killsclass.killamount);
    18. deathsclass.deaths.put(player, deathsclass.deathamount);
    19. plugin.getConfig().set("stats." + player.getName() + ".kills.", killsclass.kills.get(player));
    20. plugin.getConfig().set("stats." + player.getName() + ".deaths.", deathsclass.deaths.get(player));
    21. }
    22. }
    23.  
    24. }


    Kills Class:
    Code:java
    1. package com.guildlaunch.platinumverse.statspro;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.PlayerDeathEvent;
    9.  
    10. public class Kills implements Listener {
    11.  
    12. HashMap<Player, Integer> kills = new HashMap<Player, Integer>();
    13.  
    14. public int killamount = 0;
    15.  
    16. @EventHandler
    17. public void onPlayerKill(PlayerDeathEvent event) {
    18.  
    19. Player player = (Player) event.getEntity().getKiller();
    20.  
    21. if(event.getEntity().getKiller() != null) {
    22.  
    23. killamount++;
    24. kills.remove(player);
    25. kills.put(player, killamount);
    26.  
    27. }
    28.  
    29.  
    30. }
    31. }


    Deaths Class:
    Code:java
    1. package com.guildlaunch.platinumverse.statspro;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8.  
    9. public class Deaths implements Listener {
    10.  
    11. HashMap<Player, Integer> deaths = new HashMap<Player, Integer>();
    12.  
    13. public int deathamount = 0;
    14.  
    15. public void onPlayerDeath(PlayerDeathEvent event) {
    16.  
    17. Player player = (Player) event.getEntity();
    18.  
    19. deathamount++;
    20.  
    21. deaths.remove(player);
    22. deaths.put(player, deathamount);
    23.  
    24. }
    25.  
    26. }


    Oh by the way, in the Main class, I was experimenting so I tried putting the kill amount and the death amount in there, it also gave me the internal error.

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

    haydenaa

  3. Offline

    Cloaking_Ocean

    Code:
    private Kills killsclass;
        private Deaths deathsclass;
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new Kills(), this);
            getServer().getPluginManager().registerEvents(new FirstJoin(), this);
            getServer().getPluginManager().registerEvents(new Deaths(), this);
            getConfig();
        }
    The problem I see here is you're making a variable but you're not assigning it. I think one way you can fix this is before u register all the events, do "killsclass = new Kills();" and same for the others.
     
  4. Offline

    haydenaa

    Ok i'll try that and report back
     
  5. Offline

    haydenaa

    Cloaking_Ocean so now it displays the kills and deaths messages with /stats, but it says I have null kills and deaths.
     
  6. Offline

    Skionz

  7. Offline

    haydenaa

    Skionz Lol, I know your a pro at Bukkit so could you help me a little?
     
  8. Offline

    Cloaking_Ocean

    @ahydenaa I believe that Skionz was talking about how you are casting your Player object from event.getEntity() when you could just as easily make a Player object without casting with event.getPlayer(); Unless I'm super out of it and you can't do that with PlayerDeath (Event thought it specifies player). All I can say for now, Good luck I might be back later to cretique u some more
     
  9. Offline

    AronTheGamer

    Have a hashmap for every stat you want to save.

    Then have one, I repeat, one listener that increases all these values on event. When sending stats, just use for example sendMessage ("Deaths: " + deathStat.get (p));
     
  10. Offline

    Skionz

    haydenaa If your making a plugin to save stats then I would suggest using a database or a file to save data as a hash map will be lost when the program stops.
     
  11. Offline

    haydenaa

    Skionz I was planning to use a config.yml file, but i'm kind of new to bukkit (not that new) and I don't properly know how to setup and save the files. Do I do something like getConfig().set("stats." + player.getName() + ".kills", kills.get(player)); or something like that. Sorry, like I said i'm kinda new so to get it would I do getConfig().get("stats." + player.getName() + ".kills") or replace the get with getString?

    Cloaking_Ocean I just checked and there is no event.getPlayer(); for PlayerDeathEvents so I was originally right

    Skionz also, should I copy the defaults for the config? I'm not sure what that does.

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

    Skionz

    It creates default values when the config is created.
     
  13. Offline

    haydenaa

    Skionz got it

    Skionz AronTheGamer Cloaking_Ocean It's still telling me in-game that I have null kills and deaths. Can someone attempt to help me?

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

    Skionz

    Post the stack-trace and the class pertaining to it.
     
  15. Offline

    haydenaa

    Skionz Summing it up: It's telling me there's a NullPointerException at line 32 of my Main.java, which happens to be the line where I tell the player how many kills they have. Is there an error in my Kills.java class or something?
     
  16. Offline

    Skionz

    haydenaa Their kills are probably null.
     
  17. Offline

    haydenaa

    Skionz It would say the same for deaths, but that means there's an error in my FirstJoin class because it's supposed to put them at 0 deaths and kills if it's their first time joining. Sorry if i'm bugging you, but can you help me with this?
     
  18. Offline

    Skionz

    When getting a value check if it is null first.
     
  19. Offline

    haydenaa

    Skionz so just if(kills.get(player) != null) ?
     
  20. Offline

    Cloaking_Ocean

    haydenaa It would be very useful if you could post code from the class you are having trouble with and any other classes that are linked from that code. There could be many issues that we could think of with the obscurity you have shown us.
     
  21. Offline

    Lolmewn Retired Staff

    Unrelated to the actual issue, but please use UUID's for storing their data. Just imagine what would happen if someone were to change their name!

    Also, for Stats, you *could* use my API ;)
     
  22. Offline

    haydenaa

    Lolmewn it's not currently possible to change a name, right?

    Cloaking_Ocean , i've narrowed it down to 2 classes:
    Code:java
    1. package com.guildlaunch.platinumverse.statspro;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. private static Listeners listeners;
    12. public static Main plugin;
    13.  
    14. public void onEnable() {
    15. plugin = this;
    16. getServer().getPluginManager().registerEvents(new Listeners(), this);
    17. getConfig();
    18. }
    19.  
    20. public void onDisable() {
    21. getConfig().options().copyDefaults(true);
    22. saveConfig();
    23. plugin = null;
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27.  
    28. Player player = (Player) sender;
    29.  
    30. if(cmd.getName().equalsIgnoreCase("stats")) {
    31. player.sendMessage(ChatColor.RED + "[Stats]");
    32. player.sendMessage(ChatColor.GREEN + "Kills: " + ChatColor.AQUA + listeners.kills.get(player));
    33. player.sendMessage(ChatColor.GREEN + "Deaths: " + ChatColor.AQUA + listeners.deaths.get(player));
    34. player.sendMessage(ChatColor.GREEN + "KDR: " + (listeners.killamount / listeners.deathamount));
    35. return true;
    36. }
    37.  
    38. return false;
    39. }
    40.  
    41. }

    And
    Code:java
    1. package com.guildlaunch.platinumverse.statspro;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.PlayerDeathEvent;
    9. import org.bukkit.event.player.PlayerJoinEvent;
    10.  
    11. public class Listeners implements Listener {
    12.  
    13. HashMap<Player, Integer> deaths = new HashMap<Player, Integer>();
    14.  
    15. int deathamount = 0;
    16.  
    17. @EventHandler
    18. public void onPlayerDeath(PlayerDeathEvent event) {
    19.  
    20. Player player = (Player) event.getEntity();
    21.  
    22. deathamount++;
    23. deaths.remove(player);
    24. deaths.put(player, deathamount);
    25. Main.plugin.getConfig().set("stats." + player.getName() + ".deaths", deaths.get(player));
    26.  
    27. }
    28.  
    29. HashMap<Player, Integer> kills = new HashMap<Player, Integer>();
    30.  
    31. int killamount = 0;
    32.  
    33. @EventHandler
    34. public void onPlayerKill(PlayerDeathEvent event) {
    35.  
    36. Player player = (Player) event.getEntity().getKiller();
    37.  
    38. if(event.getEntity().getKiller() != null) {
    39.  
    40. killamount++;
    41. kills.remove(player);
    42. kills.put(player, killamount);
    43. Main.plugin.getConfig().set("stats." + player.getName() + ".kills", kills.get(player));
    44.  
    45. }
    46.  
    47.  
    48. }
    49.  
    50. @EventHandler
    51. public void onPlayerJoin(PlayerJoinEvent event) {
    52. Player player = event.getPlayer();
    53.  
    54. if(!player.hasPlayedBefore()) {
    55. kills.put(player, killamount);
    56. deaths.put(player, deathamount);
    57. Main.plugin.getConfig().set("stats." + player.getName() + ".kills", kills.get(player));
    58. Main.plugin.getConfig().set("stats." + player.getName() + ".deaths", deaths.get(player));
    59. }
    60. }
    61.  
    62. }
    63.  

    I am still getting an internal error when I type /stats, it displays the first message, [Stats], but follows with an internal error.

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

    Lolmewn Retired Staff

    No, but it will be. Very soon. Very, very soon. And when it does, you better be prepared ;)
     
  24. Offline

    haydenaa

  25. Offline

    Cloaking_Ocean

    Lol I just barely saw another problem like this. What you're doing is your referencing a variable you declared at the top right?: private static Listeners listeners;. The problem is when you go to register these listeners instead of assigning the variable to a new Listeners class you just call the constrctor for a new listeners class. Simply add listeners = new Listeners() in your Plugin.getPluginManager.registerEvents()

    haydenaa

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

    haydenaa

  27. Offline

    haydenaa

  28. Offline

    Cloaking_Ocean

  29. Offline

    haydenaa

    Cloaking_Ocean Skionz Lolmewn AronTheGamer Thanks for the help guys, I greatly appreciate the help. I've managed to fix most of the bugs, and since this thread is on sending a hashmap's contents to a player, and i've figured out how to do this, I'm probably not going to be posting on here anymore. Again, thanks for the help :D
     
    Skionz likes this.
  30. Offline

    Cloaking_Ocean

    @haydenaa Your welcome, glad I could help!
     
Thread Status:
Not open for further replies.

Share This Page