[Tutorial] Create A Configuration File with YAML!

Discussion in 'Resources' started by Jayjay110, May 4, 2011.

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


    Now how would I use this for example in my blocklistener?
  2. Offline


    Here's how I'm currently handling my config:

        private static final Map<String, Object> CONFIG_DEFAULTS = new HashMap<String, Object>();
        private Configuration myConfig;
        static {
            CONFIG_DEFAULTS.put("goblinevent.torchthief.torchesmin", 5);
            CONFIG_DEFAULTS.put("goblinevent.torchthief.torchesmax", 10);
            CONFIG_DEFAULTS.put("goblinevent.torchthief.torchdelay", 20);
            CONFIG_DEFAULTS.put("goblinevent.moneyfinder.moneymin", 10);
            CONFIG_DEFAULTS.put("goblinevent.moneyfinder.moneymax", 20);
            CONFIG_DEFAULTS.put("goblinevent.moneythief.moneymin", 10);
            CONFIG_DEFAULTS.put("goblinevent.moneythief.moneymax", 20);
    Then in onEnable I call this:

        private void loadConfig() {
            File configFile = new File(this.getDataFolder(), "config.yml");
            if (configFile.exists()) {
                myConfig = new Configuration(configFile);
                myConfig.setHeader("# Configuration file for ServerGoblins\r\n# Please see https://github.com/Pandarr/ServerGoblins/wiki for configuration details.");
                for (String prop : CONFIG_DEFAULTS.keySet()) {
                    if (myConfig.getProperty(prop) == null) {
                        myConfig.setProperty(prop, CONFIG_DEFAULTS.get(prop));
            } else {
                try {
                    myConfig = new Configuration(configFile);
                    // default values
                    myConfig.setHeader("# Configuration file for ServerGoblins\r\n# Please see https://github.com/Pandarr/ServerGoblins/wiki for configuration details.");
                    for (String prop : CONFIG_DEFAULTS.keySet()) {
                        myConfig.setProperty(prop, CONFIG_DEFAULTS.get(prop));
                } catch (IOException e) {
    If the file doesn't exist, it grabs all the defaults. If the file does exist, it loads the YAML and then ensures all the keys are there. If not, it defaults them. This allows for upgrading. The only thing it doesn't do at the moment is remove unused nodes.

    I also have some methods like this:

        public int getConfigInt(final String path) {
            return myConfig.getInt(path, (Integer) CONFIG_DEFAULTS.get(path));
    onDisable I just call myConfig.save();
    garbagemule likes this.
  3. Offline


    Are you asking how to remove unused nodes? Do you mean nodes that are removed from the plugin or from the user?

    If its the user just make it when u read it, if it is null remove it, no other way I guess, (this may remove things like unentered values u dont wanna remove(mysql settings or sumthin))

    to write it, do write("list path", ???) actually I dont know :3, try putting list.toString or sumthing in there

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


    Nope. Was just demonstrating a different way to do it.
  5. Offline


    I'm having a really silly problem.
    I'm doing this:
        private void addDefaults() {
    And it generates the default config.yml that looks like this:
    world: pvpworldbanTime: 30
    The code works, but how do I get it to put each item on a new line, so it would look like this instead:
    world: pvpworld
    Everything works fine, it just looks ugly, lol.
  6. Offline


    I really don't see this as a tutorial as much as you just handing out a script.
  7. Offline


    Aha but it is exactly like this, and its a tutorial: http://forums.bukkit.org/threads/sqlite-and-mysql-tutorial-library.17034

    Thats really wierd? are you opening it with a yml editor or notepad? notepad (the windows thingy doesnt format it correctly i belive...

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


    @DrAgonmoray notepad does not format correctly in a lot of cases, usually with unknown file extensions. when opening it notepad does not know what to do with the line breaks and instead of moving onto the next line it just makes a character that looks equivalent to nothing. If you move your cursor to a few letters before the separator and then use the right arrow keys you'll see that when you get to it you have to hit the button twice to get it to move one character, while it's actually moving two. Like Jayjay said, just download Notepad++, it's a very nice program that allows you to not only read .ymls formatted correctly but script in Java html c++ batch vb vbs and almost all languages I've ever heard of..

    @Jayjay110 I should have rephrased my post, this is a tutorial, but not an extremely good one, you show hot to do something, but in a tutorial there is usually a lot of explaination of what each part does, here is what you do:

    now we do this
    now we do this
    now we do this

    what a good tutorial would be is:

    now we do this... because this does this and this does this...
    now we do this... because this does this and this does this...
    now we do this... because this does this and this does this...

    I'm not saying this is bad, I'm just saying people will just copy the code instead of actually learning how it works so they can make it themselves. And the old tutorial was much better than the new one, the new one you practically make a new class without explaining anything about it and then show how to use it, if people are supposed to just copy the contents of that php code then you could just make a Jar file with that class in it and give it out to people.
  9. Offline


    Oh, thanks! I didn't know that because all my other yaml files opened correctly with Notepad. :D
    I don't know about others, but I took the time to go through each piece and understand it. ^_^
  10. Offline


    @DrAgonmoray good job that you take the time to learn, I know plenty of people that would just copy this into a notepad save it and paste it anytime they need it.
  11. Offline


    But while we're on the topic, there was one part I couldn't figure out how to use; the lists. The plugin I made currently only has support for 1 world, but I want to make it have multiple. So instead of just having
    world: world_name_here
    I want to be able to do
    worlds: world1,world2,world3,etc

    I can't figure out how to use the list object.
  12. Offline


    explain further how you intend to get the info. to get a list use the getStringList(String path) then:

    for(String worldname : srtinglist){
    World world = getServer().getWorld(worldname)

    putting a list of strings is similar, I belive if you assign a property to a List<String> then it will write all the items on a separate line so you can get them by getStringList, I could be wrong.
  13. Offline



    I'm not sure I'm understanding. In the yml file, is the StringList like this: test: foo,bar,etc
    Or are they like this?
    - foo
    - bar
    - etc

    And I don't know how to iterate through the List<String> object. I want to iterate through everything in the list, and check if it equals something else. Ex:
    [loop through List<String> as i] {
      if (i.equals("foo")) {
        return true;
    return false;
  14. Offline


    second one, foo, bar, tct would literally return "foo, bar, etc" instead of returning:

    to iterate through a List do the same thing as an array, an advanced for loop:

    List<String> list = new ArrayList<String>(5);
    add("First String");
    add("Second String");
    add("This is a test");
    add("Forth String");
    add("Fifth String");

    String s : list){

    First String
    Second String
    This is a test
    Forth String
    Fifth String
  15. Offline


    Ah, okai, got the iteration working, thanks. But I'm having some stupid problems with the config part.
    In my main class, I define public List<String> pvpWorlds;
    And in my config class I have this:
        private void addDefaults() {
    write("worlds""- pvpworld");
    write("deathMessage""You died in the PvP world. You have been exiled for %time% minutes.");
    write("timeMessage""You are exiled for %time% more minutes.");

    void loadkeys() {
    plugin.banType readBoolean("banFromServer");
    plugin.banLength readInt("banTime");
    plugin.pvpWorlds readStringList("worlds");
    plugin.deathMessage readString("deathMessage");
    plugin.timeMessage readString("timeMessage");
    1)When adding defaults, how would I make it default to putting
    - pvpworld
    Instead of worlds: - pvpworld

    Aaand this line is giving me an error in Eclipse: plugin.pvpWorlds = readStringList("worlds");
    Type mismatch: cannot convert from String to List<String>

    I'm probably doing something really obvious wrong.
  16. Offline


    like said before, it's probably the editor you are using, download Notepad++ it's free and open up the yml and see if they are still on the same line, also I do not think you need the "- "
  17. Offline


    That's not what I'm asking. The method write() can't tell the difference between-
    Nevermind, I just had an idea. I won't use the list at all; I'll just make a string and parse it myself.
  18. Offline


    that is one way, that would work, parse the string bt "," and then for all of the strings trim()
  19. Offline


    Well the basic concept is read through it and ask me if you want to know what certain parts mean, its really all self explanatory but I probably should have said "If you dont understand what a certain part does, reply and ask" in the main post or something
  20. Offline


    This is getting outdated(i think).
    Config should be Configuration and at the end of the code instead of readB its readBoolean,isn't it?
    I tried it out,with these are fixed and it isnt works for me :(
    I simply want to store some booleans and strings,someone can help me?
  21. Offline


    lol yes silly me, re-download it
  22. Offline



    I is a noob..
  23. Offline

    Devil Boy

    Replace "test" and "MyPlugin" with the name of your plugin class.
    With the code you showed me you would replace them with "ytMain".
    Jayjay110 likes this.
  24. Offline


    wow creating configs are extremely difficult for me.

    2 of my plugins are requiring Configs.

    EvilBed needs a message color/ Message/ and Damage Config
    Shattered needs Message and Damage Config.

    Afaik damage is a int and the message is a string. But both can be string am i right?


    String Damage EvilBed.Settings.getString("1");
    String Message EvilBed.Settings.getString("Giggty");
    String Color EvilBed.Settings.getString("RED");
    But my issue is how do i create the config file and connect to the damage/message/color
  25. Offline


    it can be a string if you input it in the config file like so:
    1: '1'

    Is that what your looking for?
  26. Offline


    I've just started out with java and bukkit plugins, so please excuse my ignorance.

    Say I had the following yaml file:

      username: 'herp'
      code: '1a'
      username: 'derp'
      code: '2b'
    How would I go about searching for a user in the file?
  27. Offline

    Kalman Olah

    You check if users.username.herp is null.
  28. Offline


    I might be the first but I seriously can't understand why you wouldn't just use Bukkit's standard Configuration on its own.
  29. Offline


    It's what I'm wondering too ...
    It's really easy to use :
    2. public class MyPlugin extends JavaPlugin {
    4. public void onEnable() {
    5. //getting the conf.yml, if not exist, bukkit will create it.
    6. Configuration conf = this.getConfiguration();
    7. //Check if the property exists to avoid override it
    8. if(!propertyExists("myProp")){
    9. //the property don't exist, we set it.
    10. conf.setProperty("myProp","test");
    11. }
    12. //we write the change to the file
    13. conf.save();
    15. //getting the property that we set, if not set, it's returning null
    16. String testProp = conf.getString("myProp");
    17. }
    19. //check if the property exist to not override it (like if the server admin change it)
    20. boolean propertyExists(String path) {
    21. return this.getConfiguration().getProperty(path) !=null ;
    22. }

    I don't see any difficulties ... and why using a configuration handler or anything else than Configuration object

    You have all the doc about this object here : http://jd.bukkit.org/doxygen/df/dd2/classorg_1_1bukkit_1_1util_1_1config_1_1Configuration.html
  30. Offline


    @Dark_Balor Why would you need to use if(!propertyExists)? If you use config.getString("Derp", "Herp"); and then save the config it will return the string that the Server Admin set for Derp: or if it isn't there it will set Derp to Derp: Herp.
Thread Status:
Not open for further replies.

Share This Page