Generate default config.yml from default template

Discussion in 'Plugin Development' started by ddj, Mar 16, 2011.

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


    At this moment I create a default config.xml in my plugin by coding it all out.
    I am sure there is a way to provide a default config.xml with comments, since that is was most plugins actually do.

    I am not experienced enough in java/eclipse to find the proper way to do this. I have found a way to add this config.xml to my project, but how to I access it and output it to the plugin/mypluginname folder in a proper way?

  2. Offline


    Just use the .getConfiguration() function of your main class..

    onEnable() you should do .load()

    onDisable() you should do .save()

    And to change things just use .setProperty("property", value)

    And to load things just use .getProperty("property", defaultvalue)

    The defaultvalue is what will be used if there is no property with the given name :)
  3. Offline


    I think I wasnt clear enough:)

    I use all these methods to load/save/read from and write to my config.yml.
    But, when a plugin.yml doesnt already exist in the ./plugin/mypluginname folder (first time someone starts my plugin), I want to create a default one, with default values & comments.

    I have added such a 'dressed up' config.yml to my project/src structure inside eclipse, but how to I copy that file into the bukkit plugin folder (using the plugin code of course)?

    Never mind, found it out by borrowing some code from worldedit:

    private void CheckConfig() {
        String name = "config.yml";
        File actual = new File(getDataFolder(),name);
        if (!actual.exists()) {
            InputStream input =this.getClass().getResourceAsStream("/config.yml");
            if (input != null) {
                FileOutputStream output = null;
                try {
                    output = new FileOutputStream(actual);
                    byte[] buf = new byte[8192];
                    int length = 0;
                    while ((length = > 0) {
                        output.write(buf, 0, length);
                    AddLog(getDescription().getName() + ": Default configuration file written: " + name);
                } catch (IOException e) {
                } finally {
                    try {
                        if (input != null)
                    } catch (IOException e) {}
                    try {
                        if (output != null)
                    } catch (IOException e) {}
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 12, 2016
Thread Status:
Not open for further replies.

Share This Page