HashMap Help - Not reading Hashmap Integer

Discussion in 'Plugin Development' started by arkolly, May 10, 2014.

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

    arkolly

    Hello, im new to HashMaps, so what i want, is so that when a player kills 5+ bats, they get a message saying "Well Done, 5+ Bat Deaths!!!", and if they don't have +5 , do <5, they get a message saying "Still Under 5 Bats". Here is the code:
    Code:java
    1. package com.arkolly.tester;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.entity.Bat;
    7.  
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.EntityDeathEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14.  
    15. import java.util.HashMap;
    16.  
    17. public class main extends JavaPlugin implements Listener {
    18.  
    19.  
    20.  
    21. @Override
    22. public void onEnable() {
    23. Bukkit.getPluginManager().registerEvents(this, this);
    24. }
    25.  
    26.  
    27. private HashMap<String, Integer> bats = new HashMap<String, Integer>();
    28.  
    29. @EventHandler
    30. public void onbatkill(EntityDeathEvent event){
    31. Player player = event.getEntity().getKiller();
    32.  
    33. int batkills = 1;
    34.  
    35.  
    36. if (event.getEntity() instanceof Bat){
    37.  
    38. if(bats.get(batkills) < 5){
    39. batkills++;
    40. bats.put(player.getName(), batkills);
    41. player.sendMessage(ChatColor.RED + "Still Under 5 Bats");
    42.  
    43. }
    44. else if(bats.get(batkills) >=5){
    45. batkills++;
    46. bats.put(player.getName(), batkills);
    47. player.sendMessage("Well Done, 5+ Bat Deaths!!!");
    48. }
    49.  
    50. }
    51.  
    52.  
    53. }
    54.  
    55.  
    56.  
    57. }
    58.  


    I know that the hashmap data will be wiped when the server restarts ;) Thanks!! HELP ASAP would be nice!
     
  2. Offline

    Iroh Retired Staff

    Moved to plugin dev.
     
  3. Offline

    glen3b

    Assuming you are fine with map data being wiped on server restart, I think this code will work.

    Code:java
    1. package com.arkolly.tester;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.entity.Bat;
    7.  
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.EntityDeathEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14.  
    15. import java.util.HashMap;
    16.  
    17. public class Main extends JavaPlugin implements Listener {
    18.  
    19.  
    20. @Override
    21. public void onEnable() {
    22. Bukkit.getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25.  
    26. private HashMap<String, Integer> bats = new HashMap<String, Integer>();
    27.  
    28. @EventHandler
    29. public void onbatkill(EntityDeathEvent event){
    30. Player player = event.getEntity().getKiller();
    31.  
    32. if(player == null){
    33. return;
    34. }
    35.  
    36. int batkills = bats.containsKey(player.getName()) ? 0 : bats.get(player.getName()); // If the player already has bat kills, load them, otherwise they have none
    37.  
    38.  
    39. if (event.getEntity() instanceof Bat){
    40.  
    41. bats.put(player.getName(), ++batkills); // Increment bat kills by one
    42.  
    43. if(batkills < 5){
    44. player.sendMessage(ChatColor.RED + "Still Under 5 Bats");
    45.  
    46. }
    47. else{
    48. player.sendMessage("Well Done, 5+ Bat Deaths!!!");
    49.  
    50. // Remove the following line of code if you do NOT want to reset bat kills when players get 5
    51. bats.remove(player.getName());
    52. }
    53.  
    54. }
    55.  
    56.  
    57. }
    58.  
    59.  
    60.  
    61. }


    As a reminder, assuming you leave the code above unmodified, I believe it will work (not tested) given the following items:

    • After a player gets 5 batkills, their batkills will be reset (I showed in code how to change that).
    • The message showing their bat kill status, even if you remove the line of code mentioned above, will be shown after every kat kill.
    • Bat kills are not persisted across server reloads.
    • This system is not guaranteed to work with name changes, as it stores the String names and not the player UUIDs.
     
Thread Status:
Not open for further replies.

Share This Page