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!
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)); }
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
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.
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.
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!
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; }