Solved HashMaps

Discussion in 'Plugin Development' started by Lygophobia, Aug 28, 2014.

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

    Lygophobia

    This is my first Bukkit Plugin, my friend was coding one and I wanted to learn it, so here I am.

    Anyway, i'm having a bit of trouble. I am creating a Hashmap to store two values. It stores the values then they get deleted instantly. When i try to get the values, it doesn't work. I don't want to give out all my code out quite yet even tho it is quite possibly useless. Here is what i am willing to share:

    Code:java
    1. public HashMap<NPCName, Integer> tracker = new HashMap<NPCName, Integer>();
    2.  
    3. public void addKill(String NpcValue, Player p){
    4. for(NPCName npc : NPCName.NAMES) {
    5. if(cleanString(npc.toString()).equalsIgnoreCase(cleanString(NpcValue.toString()))){
    6. int kills = (tracker.get(npc) == null ? 0 : tracker.get(npc)) + 1;
    7. tracker.put(npc, kills);
    8. System.out.println("HashMap: "+tracker);
    9. p.sendMessage(ChatColor.WHITE + "You have killed "+ChatColor.RED+cleanString(npc.toString())+" "+ChatColor.BLUE+kills+ChatColor.WHITE+" time(s).");
    10. }
    11. }
    12. }


    HashMap: {PIG=1}
    HashMap: {PIG=1}

    ^ server output after killing two pigs

    Any help would be appreciated.

    Edit: Wrong Section, would a mod mind moving this to bukkit help? unless this is the correct one.
     
  2. Offline

    SmooshCakez

    If you're developing a plugin and need help, this is the correct forum section.

    If you're trying to get it to print out "1" instead of {PIG=1}, print tracker.get(npc); to get its integer value. Otherwise, we can't really help you if we don't know what you're doing (which means by posting the full code).
     
  3. Offline

    Lygophobia


    Yeah, my friend said you would need the full code, I'll edit this with the pastebin of the code.

    Edit2: The problem is that the HashMap gets deleted before i kill another mob.

    Edit:
    http://pastebin.com/YKQmhSdT
    http://pastebin.com/UzE4sPs4
     
  4. Offline

    Rocoty

    Lygophobia Yeah you're right about the problem...in a sense. The thing is, between two kills you are working with two completely different maps altogether. The reason for this is that you are instantiating the NPCDeathTracker once for each time an NPC gets killed and then you use that NEW instance to call the addKill method. So you're effectively deleting your records as soon as you create them. You should instead store one instance of the NPCDeathTracker class as a member of your main class. And use that one instance every time you need to alter NPC records.

    If you are getting confused about what I am saying you may ask me to explain a bit better. But your best bet would honestly be to learn how OOP works semantically. Trust me, it is likely to be essential knowledge in the future.

    Hope I helped.
     
    Lygophobia likes this.
  5. Offline

    Lygophobia

    You did, Thanks a ton, I understood everything you said. Fixed I feel dumb for my error :p
     
    Rocoty likes this.
Thread Status:
Not open for further replies.

Share This Page