Create Settings file

Discussion in 'Plugin Development' started by Zero9195, Feb 12, 2011.

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


    Hi Guys, have some Problems creating a settings file with some simple true and false statements.
    It should look like this
    also I want to know how I can get the settings, if it is true or false.
    Thanks for helping ;)
  2. Offline

    Jerry Larsson


    You can use the built-in Configuration class if you want to read/write yaml configuration files.

    Configuration _config = new Configuration(new File("MyFolder/MyFile.yml"));
    // Reading from yml file
    Boolean elevators = _config.getBoolean("mysettings.enableElevators", false);
    Boolean gravesigns = _config.getBoolean("mysettings.enableGravesigns", false);
    Boolean buildings = _config.getBoolean("mysettings.enableBuildings", false);
    Boolean wirestone = _config.getBoolean("mysettings.enableWirestone", false);
    //Writing to yml file
    _config.setProperty("mysettings.enableElevators", true);
    _config.setProperty("mysettings.enableGravesigns", true);
    _config.setProperty("mysettings.enableBuildings", true);
    _config.setProperty("mysettings.enableWirestone", true);;
    There you have some code to read and write to a config file. Quite easy.

    Regards, Jerry
  3. Offline


    Which directory is "MyFolder/MyFile.yml" in? I'm assuming the "plugins" directory?
  4. Offline


    No, the root is going to be where your craftbook.jar is. So if you want it to be in the plugins folder, you have to have "plugins/MyFolder/MyFile.yml"
  5. Offline


    Cool! Thanks.
  6. Offline


    I highly recommend using the default config file for your plugin (this.getConfiguration() in the onEnable method). That will access plugins/[YourPlugin]/config.yml which is the default, and is where you should store your config stuff.
    To create the default file, just have a method that does config.setProperty() for all of the options you want, and then run and it will create the required directory/file.
  7. Offline


    Thanks!! this sounds much easier than everything I have tried so far xD
    --- merged: Feb 13, 2011 9:08 AM ---
    Is there any way to check if the file already exists? Because now it overwrites the settings everytime. Thanks for Help^^
Thread Status:
Not open for further replies.

Share This Page