How to load/save data to external file?

Discussion in 'Plugin Development' started by Agent000037, Jun 10, 2017.

Thread Status:
Not open for further replies.
  1. I have a hashmap...

    HashMap<OfflinePlayer, Integer> map = new HashMap<OfflinePlayer, Integer>();

    I understand how to add to it, and read it, but how do I save it to a config, and then load it back in?

    I've been searching for hours and can't figure out how... I really hate to ask but could someone write me some code that will do this? Because.... I don't know how, I'm completely stumped.

    Thanks for all help!
     
  2. We won't spoonfeed you, but if you have basic knowledge of Java, you should be able to do it using the following:

    Saving:
    1. Iterate over map.keySet() (it returns a Set that contains all keys (offlineplayers) of the HashMap)
    2. Set "examplepath."+offlineplayer.getUniqueId()", map.get(offlineplayer) to the config
    3. After the for loop, save the config using saveConfig()
    Loading:
    1. Iterate over config.getConfigurationSection("examplepath").getKeys(false) (it returns a Set that contains all subpaths (in this case the uuids) of "examplepath")
    2. Get the uuid with UUID.fromString(config.getString("examplepath."+subpath"))
    3. Get the offlineplayer with Bukkit.getOfflinePlayer(uuid)
    4. Get the integer with config.getInt("examplepath."+subpath")
    5. Put the offlineplayer and the integer in the HashMap
     
  3. Yeah, that's what I was trying to say xD Thanks for the info
     
Thread Status:
Not open for further replies.

Share This Page