HashMap issue in a guild plugin?

Discussion in 'Plugin Development' started by Gonmarte, Nov 13, 2015.

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

    Gonmarte

    Hi,
    Im making a guild plugin and i need your help. Im working on the GuildsManager, and in that class i have all the methods that i need to add to the commands.
    Firstoble i created a hashmap called guilds, to store the name of the players and the name of the guilds using in both Strings.
    The issue is that in them method deleteGuild() i use the guilds.remove(player.getName()); and this is getting the value of player.getName() that it is the guild name, and im not sure if it is removing the guild.. IT IS?
    The other issue is in removePlayer() from an clan, and guilds.remove() is "returning" the value to be removed so i dont have any idea how would i remove players from the clan.
    Finally, do i need to save the hashmap in a config?

    If its confuse and you need some code tell-me the methods that you need and ill add it here.
    Thank you.
     
  2. Offline

    teej107

    Removing the key removes the value associated with it. However there may be the same value for multiple keys and removing one key won't remove all the values.
    If your Map is the one representing who is in a clan, then you did just remove the player from the clan. The value (if there is only one reference to it) is removed from the Map and the method just returns the Object removed or null if nothing was removed.
    Do you want the data to persist after reloads and restarts?
     
  3. Offline

    Gonmarte

    So i can use guilds.remove(player.getName()) and it will remove and the keys there i put in that value(name of clan) and it will remove the clan as well right?

    So, if i use in the removePlayer method guilds.remove(player.getName()) it will only remove that certain player from the keys of the value? But you sad that in deleteClan the methods remove() would remove the value and all the keys right? Im a bit confuse :/

    Persisnt Of course.
     
  4. Offline

    teej107

    http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#remove(java.lang.Object) I assume you are using a HashMap.

    No. Read the JavaDocs link


    Then yes you do need to save the data to a file.
     
  5. Offline

    Gonmarte

  6. Offline

    teej107

    @Gonmarte It removes the key and the mapping to the value.
     
  7. Offline

    Gonmarte

    @teej107 what is the meaning of mapping to the value?
     
  8. Offline

    Scimiguy

    @Gonmarte
    When you remove a Key, it removes that key and its associated value.
     
  9. Offline

    Gonmarte

    @Scimiguy "its associated value" or is associated value" ? Its different if it removes that key from that value, or if it removes the key and the value of the key as well. Its just because English is not my native language and sometimes and dont understand exactly what you mean :/
     
  10. Offline

    Scimiguy

    Removes both the key and the value stored under that key
     
  11. Offline

    Gonmarte

    @Scimiguy So the method remove() its perfect to detele the guild because it will delete the keys ( the players name) and it will delete the value( clans name). The problem is that how i delete only 1 key from a value? because i need that in removePlayer() method;
     
  12. Offline

    Scimiguy

    1 key from a value...?
     
  13. Offline

    Gonmarte

    @Scimiguy yes, when the player type the commands, lets say /guild remove <player to be removed> <guild> i need to remove that key from the value, what i mean is, remove that player to be removed, from the guild, using the hashmap.
     
  14. Offline

    Scimiguy

    Well that depends on how your guilds are set up
     
  15. Offline

    Gonmarte

  16. Offline

    Scimiguy

    How is the list of players in a guild kept?
     
  17. Offline

    Gonmarte

    @Scimiguy i dont have any list, only a hasmap, why?
     
  18. Offline

    Scimiguy

    What an odd way to do it

    So is the player the key, or the value?
     
  19. Offline

    Gonmarte

    @Scimiguy The key, and the value the clan.
    private HashMap<String,String > clan = new HashMap<>();

    PS: clan == guild
     
  20. Offline

    Scimiguy

    Yeah I'm not sure you've picked such a great way to implement what you're trying to do.. but to clarify:

    If I put an entry in the map like this:
    String clan = "someClan";
    String player = "aPlayer";

    HashMap#put(player, clan);
    I assume that's how you're handling it currently

    If I then used HashMap#get(player);
    I'd get clan.

    If I removed the key:
    HashMap#remove(player);
    Then
    HashMap(player, clan) would no longer exist.
     
  21. Offline

    Gonmarte

    @Scimiguy So if i remove 1 key ALL the hashmao will no longer exists?
     
  22. Offline

    Mrs. bwfctower

    @Gonmarte No. If you remove one key, then that key and the clan associated with it are removed the the map.
     
  23. Offline

    teej107

    @Gonmarte
    Lets say I have players Gary, Larry, and Mary.
    Lets also say that there are two clans named Cow and Sheep

    Now lets say I do this. (pseudo code)
    Code:
    myMap.put(Gary, Cow);
    myMap.put(Larry, Cow);
    myMap.put(Mary, Sheep);
    If I call
    Code:
    myMap.get(Gary);
    It will return Cow
    If I call
    Code:
    myMap.get(Larry);
    It will return Cow
    If I call
    Code:
    myMap.get(Mary);
    It will return Sheep
    If I call
    Code:
    myMap.get(Bubcus);
    It will return null
    If I call
    Code:
    myMap.remove(Gary);
    It will return Cow
    NOW If I call
    Code:
    myMap.get(Gary);
    It will return null
    BUT If I call
    Code:
    myMap.get(Larry);
    It will return Cow

    Does this help?
     
    Mrs. bwfctower likes this.
  24. Offline

    Gonmarte

    @teej107 @Mrs. bwfctower myMap.remove(Gary); it will return the clan Cow and delete the clan cow and Gary and Larry from hashmap right? So in hashmap it will just be Marry in the sheep clan. It is this true?
     
  25. Offline

    teej107

    No. Look at the examples again.
     
  26. Offline

    Mrs. bwfctower

    @Gonmarte Honestly I would recommend changing how you store guilds/clans. I'd probably create a Guild object, which has a Set of players, and a map of String to Guild, so you can get the Guild from the name.
     
  27. Offline

    Gonmarte

    @teej107 AHHH So the remove() removes only the selected key, so if i use myMap.remove(Gary) it will remove Garry from the hashmap because if i make myMap.get(Gary) it will return null because it was removed, but the larry is still on the Cow clan. Is that true?

    @Mrs. bwfctower I can get the guild by the name
     
    Last edited by a moderator: Nov 14, 2015
  28. Offline

    Mrs. bwfctower

  29. Offline

    teej107

    Kind of. Maps store their keys and values in Map.Entry objects (look at the link). Removing something from a Map removes that Map.Entry Object from the Map. It removes the key from the Map and the reference to the value associated with the key! It doesn't remove every value. A different key could still have that reference to the value.
    Yes
     
  30. Offline

    Gonmarte

    @Mrs. bwfctower What the hell ? I dont need that! In fact, i have already learnt java oriented to objects, and we are talking about an hashap problem now ok?​
     
Thread Status:
Not open for further replies.

Share This Page