Solved Save HashMaps into configuration files

Discussion in 'Plugin Development' started by derin38, Dec 30, 2012.

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

    derin38

    Hello again Bukkit!

    I have new question for you. I now have two HashMaps. One is structured as <Integer, String> and the other one is <String, Integer>. I would now like to know a way to save these maps (on shutdown) so that I can load them again (on startup). I know how to save an ArrayList but not a HashMap.

    Many thanks,
    derin38
     
  2. Offline

    RainoBoy97

    I havent tried to save HashMaps etc but i will think its the same as with ArrayList.
     
  3. Offline

    fireblast709

    There is something about it on the configuration page on the wiki
     
  4. Offline

    derin38

    I presume you are referring to this?

    I have read it through many times but either I am not right or I think that tutorial is about something else.

    It talks about setting a HashMap to either the key or the value of a config. But I want to set the key of the HashMap to the key of the config and the value of the HashMap to the value of the config. Then when I load it, the key of the config to the key of the HashMap etc.

    If the tutorial do cover what I am on about, please explain a little because I'm not that great on Java yet for this is my first plugin I code.
     
  5. Offline

    fireblast709

    Code:java
    1. for(Entry<K,V> entry : someHashMap.entrySet())
    2. {
    3. // entry.getKey() for the K key
    4. // entry.getValue() for V value
    5. }
     
    StefanXX and Hoolean like this.
  6. Offline

    derin38

    Please explain further. What will that do and what should I write inside the loop?
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    The section you are looking for is this. It talks about the restrictions on the types that can compose the Map.
     
  8. Offline

    fireblast709

    First of all, don't forget to replace K and V for the HashMaps <this, and this>. Secondly, you can get the key and set it to the value in the config. Example:
    Code:java
    1. getConfig().set(entry.getKey(), entry.getValue());
    This would create a plain config like
    Code:
    firstkey: firstvalue
    secondkey: secondvalue
    ...
    nthkey: nthvalue
     
    MrBluebear3 likes this.
  9. Offline

    derin38

    fireblast,
    It worked with saving the String, Integer map then I tried writing it for the Integer, String map but it says I can't use '.set' there because it is only applicable for String, Object. Are there any workarounds?
     
  10. Offline

    fireblast709

    use entry.getKey().toString()
     
  11. Offline

    derin38

    Thanks it worked.
    The thing left is how I can load the config into a map. I hope you could help me with that.
     
  12. Offline

    fireblast709

    Same way, only problem is that you should differentiate between Integers and Strings. You just read all the keys (in my simple example, getConfig().getKeys(false)), and get the values. Then, somehow choose Integer or String, and .put(key, value) it
     
  13. Offline

    derin38

    Please, could you quickly write a code?

    Could someone please help me? I get a NullPointerException with whatever I try which means that I have clearly not understood correctly.

    Do I .getKeys(false) and .getValues(false)?
    If I do, what is the next step? How do actually put those values into my map?

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

    Hoolean

    Show us ya code :p

    It might just be easier for us to add it in to give you an example of how it works ;)
     
  15. Offline

    derin38

    Firstly, I can show you the saving code. It worked fine and I thank fireblast for helping me with that one. (it's in the onDisable)
    Code:java
    1.  
    2. for(Entry<String, Integer> entry : map1.entrySet())
    3. {
    4. dmapDataConfig.set(entry.getKey(), entry.getValue());
    5.  
    6. }
    7.  
    8. for(Entry<Integer, String> entry2 : map2.entrySet())
    9. {
    10. pmapDataConfig.set(entry2.getKey().toString(), entry2.getValue());
    11.  
    12. }
    13.  


    The problem is, I don't know how to load again, not even the slightest. But from what fireblast said I did something like this: (in the onEnable)

    Code:java
    1.  
    2. for(Entry<String, Integer> entry : map1.entrySet())
    3. {
    4.  
    5. dmapDataConfig.getKeys(false);
    6. dmapDataConfig.getValues(false);
    7. map1.put(I do not know what to put here!);
    8.  
    9. }
    10.  
    11. for(Entry<Integer, String> entry2 : map2.entrySet()){
    12.  
    13. pmapDataConfig.getKeys(false);
    14. pmapDataConfig.getValues(false);
    15. map2.put(entry2.getKey(), entry2.getValue() <-- This obviously did not work :/);
    16.  
    17. }
    18.  


    And I do not either know how to make the String into Integer again.
     
  16. Offline

    fireblast709

    Code:
    String to int (which you can use as Integer)
    int number = Integer.parseInt(someString);
     
    // Integer to string
    String string = Integer.toString();
     
  17. Offline

    derin38

    The thing is... how are the values of the config defined? If I get the keys/values from the config with .getKeys(false)/Values(false), what then? Where are they? How do I use them?
     
  18. Offline

    fireblast709

    getKeys(false) returns a Set<String> with all keys. Then use those keys to get its value.
    Code:
    for(String key : someConfig.getKeys(false))
    {
        int value = someConfig().getInt(key);
    }
     
  19. Offline

    derin38

    fireblast, how do I put it into the HashMap? (together with the value)
     
  20. Offline

    fireblast709

    yourHashMap.put(key, value). In my previous post that would be a HashMap<String, Integer>
     
  21. Offline

    derin38

    How do I get the values? I'm sorry if I tend to be annoying right now.
     
  22. Offline

    fireblast709

    derin38
     
  23. Offline

    derin38

    I think I understand now! So the int value is the value and the String key is the key! :)
    What confused me before was the .getInt(key), I thought it meant that int value was the key.

    It didn't work :( . NullPointerException at map.put(key, value); .

    My code:
    Code:java
    1. for(String key : pmapDataConfig.getKeys(false))
    2. {
    3. int value = pmapDataConfig.getInt(key);
    4. map.put(key, value);
    5.  
    6. }


    EDIT: Ooops I think I know why.

    EDIT2: Nope didn't fix.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  24. Offline

    fireblast709

    did you initialize map?
     
  25. Offline

    derin38

    Oops, I initialized the maps after I loaded the config (later on). It's at least loading for now, let's see if it is working correctly.

    It's working! Fireblast, you have been very helpful to me and I thank you very much for that.

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

    fireblast709

  27. Offline

    YoFuzzy3

    fireblast709
    I was looking to do a very similar thing but save it to a certain configuration section but it doesn't seem to load the data correctly. It's a simple <String, Long> HashMap that I use for a cooldown in a command.

    In my onDisable:
    Code:java
    1. for(Entry<String, Long> getBooksCommandCooldownData: getBooksCommandCooldown.entrySet()){
    2. getConfig().set("GetBooksCommandCooldownData." + getBooksCommandCooldownData.getKey(), getBooksCommandCooldownData.getValue());
    3. }
    4. saveConfig();

    The data saves correctly, it would look like this:
    Code:
    GetBooksCommandCooldownData:
      YoFuzzy3: 1357597376116
    But when I try to load it seems the value variable is always 0, well at least that's what the debug says.
    In my onEnable:
    Code:java
    1. try{
    2. for(String key : getConfig().getConfigurationSection("GetBooksCommandCooldownData").getKeys(false)){
    3. long value = getConfig().getLong(key);
    4. getBooksCommandCooldown.put(key, value);
    5. logger.log(Level.INFO, key + value);
    6. }
    7. }catch(NullPointerException e){ }


    Hope you can tell me when I'm doing wrong, thanks! :)
     
  28. Offline

    fireblast709

    getConfig().getLong("GetBooksCommandCooldownData."+key)
     
    YoFuzzy3 likes this.
  29. Offline

    YoFuzzy3

    Oh I see, silly me! Thank you very much. :)
     
  30. Offline

    imaboy321

    ik this thread is old but i just wanted to say thank you for making it!!!
     
Thread Status:
Not open for further replies.

Share This Page