[API] Enhanced YML File Configuration - YML Editing never been this easy!!

Discussion in 'Resources' started by spywhere, Oct 7, 2012.

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

    spywhere

    This is my API for yml file configuration. Easy to use, fully customizable (using native yml file configuration)

    Features
    - Auto create config file if not exist
    - Check if path exist (without case-sensitive)
    - Force save mode (ignored reading)
    - Header set
    - Use native YML file configuration
    - Many more... (see below for example)

    Todo
    - Inventory, Location, etc.

    How to use?
    Well, just create new class and put these code in it!

    Methods
    • boolean hasPath = ymlio.contains(Stringpath);
      • Check if path is exists (not case-sensitive)
    • ymlio.set(String path, Object obj);
      • Add object into path
    • Object obj = ymlio.getObject(Stringpath); //Include specific type
      • Get object from path
    • Object obj = ymlio.get(String path,Object defaultObject); //Include specific type
      • Get object from path but if not exist use defaultObject (and also add it to yml)
    • Map<String,Object> map = ymlio.getAsMap(String path);
      • Get all paths and values into single map
    • List<String> list = ymlio.getAsFullPathList(String path);
      • Get all paths into single list of full paths
    • List<String> list = ymlio.getAsPathList(String path);
      • Get all paths into single list of path names (Warning! In v0.2 or older, this method will return full path instead)
    • List<Object> list = ymlio.getAsValueList(String path);
      • Get all values from path into single list of objects
    • ymlio.save();
      • Save changes
    • ymlio.setForceSave(boolean force);
      • Ignore all object from file and use defaultObject instead (working with get(String path, Object defaultObject);)
    • String header = ymlio.getHeader();
      • Get header
    • String header = ymlio.getHeader(StringdefaultHeader);
      • Get header but if not exist use defaultHeader (and also set as header)
    • ymlio.setHeader(Stringheader);
      • Set header
    • etc. (JavaDocs will helps you do the tricks :cool: )
    v0.3 - Improve on PathList method (kind of) and add JavaDocs
    YMLIO.java

    v0.2 - Supported for Path/Value List, HashMap
    YMLIO.java

    v0.1
    YMLIO.java

    Example usage:
    Code:
    int intNum=10;
    String str="Hello!";
     
    private void configSaveLoad(boolean isSave){
        YMLIO yml=new YMLIO(new File(this.getDataFolder().toString(),"config.yml"));
        yml.setForceSave(isSave);
        intNum=yml.get("Plugin.ThisIsInteger", intNum);
        str=yml.get("Plugin.ThisIsString", str);
        yml.save();
    }
    @Edit
    Oh god! It's too long now... :p All sources now converted into code pastes :D
     
    bobacadodl, itunes89 and devilquak like this.
  2. Offline

    spywhere

    API Updated to v0.2 - Supported for Path/Value List, HashMap

    This version add following methods...
    • getAsMap
    • getAsPathList
    • getAsValueList
    Very useful for get each value from subpath

    Example:
    Code:
    for(String path:yml.getAsPathList("Path.Players")){
                    //Something to do with path like...
                    //boolean isOP = yml.getBoolean(path);
                }
    Example YML for this example:
    Show Spoiler

    Code:
    Path:
      Players:
        Notch: true
        spywhere: true
        Me: true
        You: false
     
  3. Offline

    itunes89

    I love this! I'm getting a error though. When I use this it just keeps getting a error


    Code:
    try {
                                YMLIO yml = new YMLIO(new File(this.getDataFolder().toString(),"config.yml"));
                            } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (InvalidConfigurationException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                              Location l = plr.getLocation();
                              boolean hasPath = yml.contains("lol");
    Error: yml can not be resolved as a type
     
  4. Offline

    spywhere

    Can you post a full stack trace? That'll helps me a lot.
    PS. I'm also check it right now...

    @EDIT
    It's just syntax error... You should code like this...
    Code:
    boolean hasPath = false; //Default value
            try {
                YMLIO yml = new YMLIO(new File(this.getDataFolder().toString(),"config.yml"));
                hasPath = yml.contains("lol");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvalidConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //Do something with hasPath here
     
  5. Offline

    itunes89

    Already fixed. Thanks btw. This is the best YML API I have ever seen. It helps me with 5 of my plugins, and other people. Thanks really :D
     
  6. Offline

    spywhere

    You're welcome :)
     
  7. Offline

    Unknowncmbk

    Okay, I'm still relatively new to Java I'm just wondering if this includes or how do you include the auto generation of the plugin folder (which of course would contain config.yml)
     
  8. Offline

    spywhere

    As default, Bukkit's YML Configuration class will create a plugin folder when you save a file if it's not exist. And this API is use native Bukkit's YML Configuration class to implements... So, only load if file exists and save it (the rest is for Bukkit)... :)

    PS. If I'm misunderstanding anything just tell me...
     
  9. Offline

    bobacadodl

    spywhere
    Can you correctly save ANY object to YML with this? Even Inventory objects, Locations, and such?
     
  10. Offline

    spywhere

    Not at this moment :( But I may add it in next version :)

    @Edit
    And I should add Javadocs within the code too...
     
  11. Offline

    bobacadodl

    Would be really useful if you added it :D I would be using this for 100% of my projects :3 I'll see if I can add it myself...
    Hmm Perhaps you could convert it to bytes then read the bytes?...
     
  12. Offline

    spywhere

    I might use this in the API. May be both readable version and non-readable version. We'll see :p
     
    bobacadodl likes this.
  13. Offline

    p000ison

    heyhey does this support comments between 2 settings?
    for example:

    Code:
    bool: true
    # This is a comment
    string: 'Test'

    EDIT: hm I noticed this only uses the bukkit configuration api :/ So this should not be possible as well :/
     
  14. Offline

    bobacadodl

    Please use this :D. Would be great if you could also add an easy method to save/load readable locations.
     
  15. Offline

    evilmidget38

    I hope you don't plan on saving colored leather armor or fireworks then. Fireworks will lose their effects, and the leather armor will lose its color. Oh, it also doesn't save item name and lore. It doesn't save the contents of books either. (TL;DR - No ItemMeta support as of yet.)

    Perhaps it'd be better for a unique implementation(if you're using YAML, why would you want to save the entire inventory to a single string?), or wait until it has item meta support. I wouldn't suggest waiting, as the author hasn't been on the bukkit forums for a couple of months now.
     
  16. Offline

    spywhere

    You're right. I'd not implements this directly into my YML API. I might add some method for convert those object to simple string (that's my kind of unique implementation :p) or easiest way, create new API just only for convert object to string.

    But as I say, I "might" do what I say, it's not 100% sure. Because I might have some problems while I test it or something...
     
  17. Offline

    itunes89

    Do you mind if I build off this spywhere ? Maybe once I'm done we can finish it and come out with a 3.0,
     
  18. Offline

    spywhere

    Nope. Just use it as you want :)
     
  19. Offline

    itunes89

    Thanks.
     
  20. Offline

    spywhere

    API Updated to v0.3 - Improved on PathList method (kind of) and add JavaDocs

    This version add following methods...
    • getAsFullPathList
    This version also change following methods usage... (see details and example below)
    • getAsPathList

    Changes of getAsPathList:
    As v0.2, this method will return a list of all full paths within specified path. But in v0.3, this method will return a list of all path names within specified path instead. Make it more useful and flexible.


    Example YML for this example:
    Show Spoiler

    Code:
    Path:
      Players:
        Notch: true
        spywhere: true
        Me: true
        You: false


    v0.2 getAsPathList("Path.Players") will return:
    Code:
    Path.Players.Notch
    Path.Players.spywhere
    Path.Players.Me
    Path.Players.You
    
    v0.3 getAsPathList("Path.Players") will return:
    Code:
    Notch
    spywhere
    Me
    You
    
    and getAsFullPathList("Path.Players") will return:
    Code:
    Path.Players.Notch
    Path.Players.spywhere
    Path.Players.Me
    Path.Players.You
    
     
Thread Status:
Not open for further replies.

Share This Page