HashMaps and .yml files

Discussion in 'Plugin Development' started by RealDope, Sep 27, 2012.

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

    RealDope

    I want to store a HashMap in a .yml file so that it can be retrieved after reloads/restarts.

    I have this:
    Code:
    public class HashMapController {
        public static void save(HashMap<String, String> map, String path)
        {
            try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeObject(map);
            oos.flush();
            oos.close();
            }
            catch(Exception e) {e.printStackTrace();}
     
        }
        public static HashMap<String, String> load(String path) throws Exception
        {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
            Object result = ois.readObject();
            ois.close();
            return (HashMap<String, String>) result;
        }
    }
    And this:
    Code:
    public class ResidenceVisit extends JavaPlugin {
        String path = getDataFolder() + File.separator + "TeleportLocations.yml";
        File tpLocs = new File(path);
        HashMap<String, String> tpPosLocs;
        public void onEnable() {
            HashMapController.save(tpPosLocs, getDataFolder() + File.separator + "TeleportLocations.yml");
            if(tpLocs.exists()) {
            try {
            tpPosLocs = (HashMap<String, String>)HashMapController.load(path);
            }
            catch(Exception e) {e.printStackTrace();}
        }
    }
        }

    Currently if I do not create the TeleportLocations.yml manually, it gives me an error about the directory not existing, so how do I have it generate that directory?

    Second, I need it to be a in a format the is readable / writable by server owners. Something along the lines of:
    Code:
    name: location
    name: location
    name: location
    etc
    However what it puts in the file is a random string of symbols that won't even paste into this post.


    Sorry for the "noobish" questions, but I'm pretty new to Java and Bukkit, and this is my first go at HashMaps.
     
  2. Offline

    Builder4Free

    HashMaps and Yaml files are pretty difficult. I'm not particularly sure how to do it. But, I suggest you don't use HashMaps, they can get buggy when storing certain data types.
     
  3. Offline

    RealDope

    Well I need a way to store names and locations, pretty much the same way Essentials does with warps.
     
  4. Offline

    MrFigg

    If you don't mind using a Lib you might want to check out my SuperEasyConfig. It can handle HashMaps quite nicely. Even nested HashMaps if you need it.
     
  5. Offline

    Sagacious_Zed Bukkit Docs

  6. Offline

    MrFigg

  7. Offline

    andf54

    Or you can use gson, because it can save/load basically anything.
     
  8. Offline

    RealDope

    Okay that SuperEasyConfig looks like exactly what I need.

    Sorry for the dumb question but:

    How do I use it? Can I just put that code right in or so I have to reference a jar or something?

    Thanks for all the help
     
  9. Offline

    MrFigg

    You just put the Config.java and ConfigObject.java anywhere you want in your code, usually a Config sub-package. Then you just make a class that extends Config, add your variables, and initialize it anywhere you want.

    I really should get around to making a proper guide on the thread.
     
  10. Offline

    RealDope

    So SuperEasyConfig has built in functions for saving/reloading/loading, but I can't call any of them. Whenever I try to call them (for instance in my onEnable() of main. class), it says I can't reference them because they are not static.

    Also:

    Being a noob to java, I'm confused on how I actually tell the HashMaps to save into the yaml file.
     
  11. Offline

    MrFigg

  12. Offline

    Technius

    You can save by doing something like this:
    Code:
    Map<String, String> yourMap = //Your map
    File f = //Your file
    try
    {
        FileOutputStream fos = new FileOutputStream(f);
        fos.flush();
        fos.close();
        PrintWriter pw = new PrintWriter(f);
        for(Map.Entry<String, String> entry:yourMap.entrySet())pw.println(entry.getKey() + ":" + entry.getValue());
        pw.flush();
        pw.close;
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }
    
    and then load by doing this:
    Code:
    HashMap<String, String> map = new HashMap<String, String>();
    File f = //your file
    if(!f.exists())return;
    try
    {
        BufferedReader br = new BufferedReader(new FileReader(f));
        String l;
        while((l=br.readLine()) != null)
        {
            String[] t = l.split(":", 2); //Split it into two strings
            if(t.length != 2)continue; //Ignore it if it isn't seperated by a colon
            map.put(t[0], t[1]);
        }
        br.close();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }
    
     
Thread Status:
Not open for further replies.

Share This Page