Reading YAML files

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

  1. Offline

    Erwyn LENS

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    and you shouldn't use == but .equals, better.

    example:

    private String test = "blabla";

    if(test.equals("blabla"){
    System.out.println("this is the case");
    }
    else if(test.equals("lolilol"){
    System.out.println("this isn't the case");
  2. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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?
  3. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  4. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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]"?
  5. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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
  6. Offline

    Sammy

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    root in this case is:
    String root = "items"

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

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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
  8. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  9. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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?
  10. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I tried to do that but it never worked, like it tried to use "put", maybe @Sammy knows :p
  11. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Okay, I'll wait for Sammy then :3
    If he wants to react ofcourse.
  12. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  13. Offline

    Erwyn LENS

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I found a plugin doing, just let me some time to find it back
  14. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I also found a plugin changing yaml files, but I couldn't figure out how it actually worked...
  15. Offline

    Sammy

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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
  16. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If it has the source could you link me to it and I could have a look, thanks.
  17. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yay, thank you Sammy :D
  18. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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?
  19. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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);
  20. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Mmm yes....
    Now the part where you actually add stuff to the file, that's what I really don't understand.
  21. Offline

    Adamki11s

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    :p I'll explain, it's really simple once you get it.

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

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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.
  23. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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?
  24. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  25. Offline

    Jayjay110

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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 :)
  26. Offline

    rcjrrjcr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    List<String> itemList = new ArrayList<String>();
    itemList.add("lava_bucket");
    config.setProperty("items", itemList);
    
    Not an array, but close. :p
  27. Offline

    cholo71796

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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++){}
  28. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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.
  29. Offline

    rcjrrjcr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Just call the load() method again.
  30. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    config.load()?

    EDIT: I think I got it now.

Share This Page