[1.9] Saving Hashmaps to config!

Discussion in 'Spigot Plugin Development' started by tokyojack, Jun 1, 2016.

  1. Hey everyone!

    I've recently stumbled into one of my silly problems once again! So, this time I want to save this:
    Code (Text):
    public static HashMap<UUID, Integer> rentalTime = new HashMap<UUID, Integer>();
    Into a config when the plugin is disabled, and when the plugin is enabled again, it puts the config info back into the hashmap. I know this seems quite simple, but I cant seem to figure it out! If people were wondering what I'm trying to do, I have a command that a player can rent, and it removes from the uuid -1 every hour, so when the server is disabled, it saves the rental time, but when the server is turned back on, it puts it back into the hashmap once again!

    Thanks ahead of time! Improvements/Tips are greatly appreciated!

    PS: I did google this problem! So dont say "Google this", or "Good that". Just as a heads up!
     
    #1 tokyojack, Jun 1, 2016
    Last edited: Jun 1, 2016
  2. not sure enough, try some things here:

    Code (Java):
    HashMap<UUID, Integer> rentalTime = new HashMap<>(getConfig().get("path"));
    or try this one

    Code (Java):
    for(String times : rentalTimes.keySet()) {
       getConfig().set(times, rentalTimes.get(times));
      }
     
    • Agree Agree x 1
  3. You can use gson which is in the spigot jar to write the object directly to a file.
     
  4. Or just loop through an entry set.
     
    • Agree Agree x 5
  5. yes another thing I missed.
     
  6. That moment when you just did this like a hour ago but now your at school where I can't access it
     
  7. Just store each UUID in the config with the int value, then on the onLoad, get all the keys inside the config and put the inside the hashmap with the according value ;)
     
  8. Does someone mind on lending me some code? All the code I founds not liking me and not working >.<!
     
  9. That's not how you program. Believe it or not, you have to write it yourself.
     
    • Like Like x 1
  10. codes are given above if you don't understand them talk to Google how to learn Java.
     
  11. NO SPOON FEEDING!

    Tell you what, I'm not going to give you code, but
    I'm more than happy to tell you what you research ^_^

    ...Java Serializable Interface.

    Everyone here.. we cannot assume that this Hashmap
    in question is storing primitive values.
     
  12. Gson is a great way to store this in a readable JSON format in a file. However if you are super lazy serializing it with java is arguably easier and will also work for this case - although you lose readability. If you'd rather save it into a .yml config file though you'll have to probably write your own method for saving and loading the key:value pairs in the config as I don't think the bukkit API includes a method for this.
     
  13. Hey everyone!

    After some more messing around, I finally figured out how to save a Hashmap:
    Code (Text):
        public void saveHashMap(HashMap<UUID, Integer> hm) {
            for (Object key : hm.keySet()) {
                getConfig().set("HashMap." + key, hm.get(key));
            }
    }
    But I still cant figure out how to load/put back the Hashmap from the config back into the original Hashmap, but I got this:
    Code (Text):
    public HashMap<UUID, Integer> loadHashMap() {
            HashMap<UUID, Integer> hm = new HashMap<UUID, Integer>();
            for (String key : getConfig().getConfigurationSection("HashMap").getKeys(false)) {
                hm.put(key, getConfig().get("HashMap." + key));
            }
            return hm;
        }
    Theres compiling error when it puts the config back into the hashmap(this line to be exact : hm.put(key, getConfig().get("HashMap." + key)); )

    PS: Please excuse my arrogance up above, asking for code. I've been using Spigot for quite awhile, and I know that theres no spoon feeding, but it just passed through my head! I think its because it was late and I was tired of school and wanted to complete the plugin I was working on. Once again, forgive me!
     
  14. Omnivion

    Patron

    String is not an instance of UUID; you need to parse the key (String) into a uuid
     
  15. Something like this? Fixed most of it, but theres the problem of the config getting a string and transforming it to a UUID, any ideas?

    Code (Text):
    public HashMap<UUID, Integer> loadHashMap() {
            HashMap<UUID, Integer> hm = new HashMap<UUID, Integer>();
            for (UUID key : getConfig().getConfigurationSection("HashMap").getKeys(false)) {
             //this is the problem ^^^
                hm.put(key, getConfig().getInt("HashMap." + key));
            }
            return hm;
        }
     
  16. Omnivion

    Patron

    UUID#fromString(String)