Create Settings file

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

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

    Zero9195

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

    Jerry Larsson

    Hi.

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


    Code:
    Configuration _config = new Configuration(new File("MyFolder/MyFile.yml"));
    
    _config.load();
     
    // 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);
    
    _config.save();
    There you have some code to read and write to a config file. Quite easy.

    Regards, Jerry
     
  3. Offline

    unkzdomain

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

    mjmr89

    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

    unkzdomain

    Cool! Thanks.
     
  6. Offline

    Drakia

    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 config.save() and it will create the required directory/file.
     
  7. Offline

    Zero9195

    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