Reading YAML files

Discussion in 'Plugin Development' started by Randy Schouten, Apr 28, 2011.

Thread Status:
Not open for further replies.
  1. root would be items and null is the default value and of course the list doesn't have a value by itself so it gives it a default value of null :p. Understand?

    It's still unfinished but this is what I've done so far :)

    http://forums.bukkit.org/threads/plugin-development-a-huge-tutorial-status-under-development.15167/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  2. Offline

    Randy Schouten

    I got the file in sync with the plugin.

    Now as I said, the only thing to do is check every array...
    There's no function for that then?

    The splitString worked, but I have to point out a single array every time.
    Is there no way to check every array at once?
    Something such as "<variable>[ANY]"?
     
  3. You could use a for loop.

    int i;
    for(i=0; i <= yourarray.length();i++){
    }

    This checks if i is smaller than or equal to your arrays length(number of items/index's) then check whatever.

    I will be making a tutorial on this, it's a simple concept but difficult to understand unless tought :p
     
  4. Offline

    Sammy

    root in this case is:
    String root = "items"

    null its what it returns if the list is empty
     
  5. Offline

    Randy Schouten

    Thanks alot everyone for all the help here :D.

    When I added some more stuff to allow easier editing, I will publish the plugin and put you all in the credits.
    You're the best :3

    Again, if I need help, I'll just come back again :p
     
  6. Offline

    Randy Schouten

    Right, now there's another thing I don't know how to do.

    Putting stuff in the yaml file.
    I made it so the plugin creates the yml file, but then I want it to add the structure as well.
    It's really easy:

    Items:
    - tnt
    - bedrock

    Something like this, for instance.
    Anyone knows how to do this?
     
  7. I tried to do that but it never worked, like it tried to use "put", maybe @Sammy knows :p
     
  8. Offline

    Randy Schouten

    Okay, I'll wait for Sammy then :3
    If he wants to react ofcourse.
     
  9. Offline

    Erwyn LENS

    I found a plugin doing, just let me some time to find it back
     
  10. Offline

    Randy Schouten

    I also found a plugin changing yaml files, but I couldn't figure out how it actually worked...
     
  11. Offline

    Sammy

    I do it like this:

    Code:
        public Configuration load() {
            try {
                Configuration PluginPropConfig = new Configuration(Ranksfile);
                PluginPropConfig.load();
                return PluginPropConfig;
    
            } catch (Exception e) {
            }
            return null;
        }
     public void saveRaffleMaker(String maker) {
            Configuration config = load();
            config.setProperty("RaffleMaker", maker);
            config.save();
        }
    
        public void saveTickets(ArrayList<String> Tick) {
            Configuration config = load();
            config.setProperty("UnSoldTickets", Tick);
            config.save();
        }
    If you want minecraft item you need to store the info, like Material ID or Type and ItemStack amount
     
  12. If it has the source could you link me to it and I could have a look, thanks.

    Yay, thank you Sammy :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  13. Offline

    Randy Schouten

    I'm trying to figure out what everything means...
    Having some trouble with it though.

    For instance:
    Code:
    Configuration PluginPropConfig = new Configuration(Ranksfile);

    What's Ranksfile supposed to be?
    I tried inserting my config.yml file there, but that didn't work.
    And well... I don't really understand anything of this code :/

    Could you maybe annotate it for me?
    What every line does?
     
  14. I'll be adding this to my tutorial so I'll explain it in depth there but it's similar to how the properties files works.

    Ranksfile would be the location of the file, I think Sammy forgot to include it in his code, but it would probably look like this.

    Code:
    static File Ranksfile = new File("plugins/" + "Ranks" + File.separator + "Ranks.config");
    Configuration PluginPropConfig = new Configuration(Ranksfile);
     
  15. Offline

    Randy Schouten

    Mmm yes....
    Now the part where you actually add stuff to the file, that's what I really don't understand.
     
  16. :p I'll explain, it's really simple once you get it.

    Did you understand my tutorial on Filehanding though?
     
  17. Offline

    Randy Schouten

    That's a whole different kind of file, right?
    YML files uses a certain format for it to work (ie: you can't use tabs, it has to be 4 spaces").

    So I suppose there is a special way of putting stuff in a certain branch.

    Alright, I got one part figured out now.

    I know how to make the plugin write in a file, now I want it to list a few items.

    This is what my config file looks like after creation:
    Code:
    items: '
    
        - lava_bucket'
    
    And this is the code:
    Code:
    config.setProperty("items", "\n- lava_bucket");
    When I removed the \n, it didnt give the ', but it put lava_bucket right next to it. Like so:
    Code:
    items: - lava_bucket
    
    What I want it to become is:
    Code:
    items:
        - lava_bucket
    The syntax for "setProperty" is:
    Code:
    String arg0, Obj arg0
    Anyone has an idea here?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  18. Offline

    Randy Schouten

  19. Offline

    Jayjay110

    to list multiple items in the yml file, you must send an array in the second argument:
    config.setProperty("items", SEND ARRAY HERE);

    its easy dw :)
     
  20. Offline

    rcjrrjcr

    Code:
    List<String> itemList = new ArrayList<String>();
    itemList.add("lava_bucket");
    config.setProperty("items", itemList);
    
    Not an array, but close. :p
     
  21. Offline

    cholo71796

    To keep the whole idea concise you can simultaneously declare and initialize the variable within the for loop info:
    for(int i = 0 ; i <= yourarray.length() ; i++){}
     
  22. Offline

    Randy Schouten

    Hey guys,
    I've already figured this out (see my plugin) :p

    There's one other thing I don't know though, I don't know how to reload a config file... :p
    I've made a different thread about it, but didn't get a reaction on it.
     
  23. Offline

    rcjrrjcr

    Just call the load() method again.
     
  24. Offline

    Randy Schouten

    config.load()?

    EDIT: I think I got it now.
     
  25. Offline

    xXCryptoFreakXx

    Hey, i'm working on my first plugin too, and I already have some commands down and working, and I decided to make a config folder to go along with it. I made the folder, make the yml, and had no clue where to go from there. I found this place and it helped, but when I did

    private static final File myfile= new File("plugins/CryptosPluginKit/config.yml");
    public Configuration load() {
    try {
    Configuration PluginPropConfig = new Configuration(myfile);
    PluginPropConfig.load();
    return PluginPropConfig;

    } catch (Exception e) {
    }
    return null;
    }

    The line

    Configuration PluginPropConfig = new Configuration(myfile);

    Had an error, saying: "Cannot instantiate the type Configuration"

    Any help?
     
Thread Status:
Not open for further replies.

Share This Page