[RES][Intermediate] Method to store (Hash-)Maps in configurations

Discussion in 'Resources' started by Pandemoneus, Dec 11, 2011.

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

    Pandemoneus

    Hey, I thought I would share this little method that stores a Map in a specified ConfigurationSection.

    Code:java
    1. import java.util.Iterator;
    2. import java.util.Map;
    3. import java.util.Map.Entry;
    4. import java.util.Collection;
    5.  
    6. import org.bukkit.configuration.ConfigurationSection;
    7.  
    8. /**
    9.  * Saves a Map at the specified ConfigurationSection.
    10.  * @param section the ConfigurationSection
    11.  * @param map the Map, note that the String parameter in the map refers to the keys the objects are saved in
    12.  * @throws IllegalArgumentException when either the ConfigurationSection or the Map is null
    13.  */
    14. @SuppressWarnings("unchecked")
    15. public static void saveMap(final ConfigurationSection section, final Map<String, Object> map) throws IllegalArgumentException {
    16. if (section == null || map == null)
    17. throw new IllegalArgumentException("Both the configuration section and the map to save must not be null");
    18.  
    19. final Iterator<Entry<String, Object>> iter = map.entrySet().iterator();
    20.  
    21. while (iter.hasNext()) {
    22. final Entry<String, Object> entry = iter.next();
    23. final Object value = entry.getValue();
    24. final String key = entry.getKey();
    25.  
    26. if (value instanceof Map) {
    27. saveMap(section.createSection(key), (Map<String, Object>) value);
    28. } else if (value instanceof Collection) {
    29. saveCollection(section, (Collection<Object>) value);
    30. } else {
    31. section.set(key, value);
    32. }
    33. }
    34. }
    35.  
    36. /**
    37.  * Saves a Collection at the specified ConfigurationSection.
    38.  * @param section the ConfigurationSection
    39.  * @param collection the Collection
    40.  * @throws IllegalArgumentException when either the ConfigurationSection or the Collection is null
    41.  */
    42. public static void saveCollection(final ConfigurationSection section, final Collection<Object> collection) throws IllegalArgumentException {
    43. if (section == null || collection == null)
    44. throw new IllegalArgumentException("Both the configuration section and the iterable object to save must not be null");
    45.  
    46. final Iterator<Object> iter = collection.iterator();
    47. final String currentSectionPath = section.getCurrentPath();
    48.  
    49. while (iter.hasNext()) {
    50. final Object value = iter.next();
    51.  
    52. section.set(currentSectionPath, value);
    53. }
    54. }


    Loading is a bit harder, as it depends on what you actually save. That's why I am not sharing a common method for that.

    Note that if you want to have a certain order of the keys in your configuration, you have to pass a LinkedHashMap (or any other map that has a predictable iteration order).

    Post questions about the method here.
     
  2. Offline

    LofixDev

    Thank you very much for this method, I searched exactly for something like this :) But I have two questions:
    - Would this work, if I use an ArrayList as Object?
    - Would this work, if I use a Block as Object?

    I hope you can help me with this (and maybe with the appropriate load method).
     
  3. Offline

    Pandemoneus

    1. I edited my original post with new code to save lists.
    2. You can't save Blocks, you must use serializeable objects.
     
  4. Offline

    LofixDev

    Thank you very much, again :D I extended the method, so it works now with blocks (saves the needed data as an extra config-element). But now I have another problem: The config file is only one line... Can you help me with this again?
     
  5. Offline

    Pandemoneus

    What do you mean by "The config file is only one line..."?
    What are you trying to save?
     
  6. Offline

    LofixDev

    I'm trying to save simple data (Only strings etc.), but the config file is only one line, although there are multiple config nodes.
     
  7. Offline

    Pandemoneus

    Send me a PM with your whole code, I don't want to pollute this thread for that problem. :p
     
  8. Offline

    Milkywayz

    I want to make a hash map for spawn egg id's and be able to allow / disallow them in config. Any idea how?
     
Thread Status:
Not open for further replies.

Share This Page