[Tutorial] Custom .yml files

Discussion in 'Resources' started by Alan26, Apr 19, 2014.

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

    Alan26

    First, set File and FileConfiguration:
    Code:java
    1. File customYml = new File(plugin.getDataFolder()+"/customYmlFile.yml");
    2. FileConfiguration customConfig = YamlConfiguration.loadConfiguration(customYml);

    Then create a function to save this config:
    Code:java
    1. public void saveCustomYml(FileConfiguration ymlConfig, File ymlFile) {
    2. try {
    3. ymlConfig.save(ymlFile);
    4. } catch (IOException e) {
    5. e.printStackTrace();
    6. }
    7. }

    Okay, this created a custom config.
    To save this, use:
    Code:java
    1. saveCustomYml(customConfig, customYml);

    Now you can use the custom config as a default config.
    Ex:
    Code:java
    1. customConfig.getString("path");

    How to use custom config:
    Setting a fields value (open)
    Set a boolean value:
    Code:java
    1. plugin.customConfig.set("path.to.boolean", true);

    Would produce:
    Code:
    path:
      to:
        boolean: true
    Set a string value:
    Code:java
    1. plugin.customConfig.set("path.to.string", "This is a custom yml file :D");

    Would produce
    Code:
    path:
      to:
        string: This is a custom yml file :D
    Set a int value:
    Code:java
    1. plugin.customConfig.set("path.to.int", 10);

    Would produce
    Code:
    path:
      to:
        int: 10

    Getting a fields value (open)

    Get a boolean value:
    Code:java
    1. plugin.customConfig.getBoolean("path.to.boolean");
    So if had:
    Code:
    path:
      to:
        boolean: true
    It's get true

    Get a string value:
    Code:java
    1. plugin.customConfig.getString("path.to.string");
    So if had:
    Code:
    path:
      to:
        string: This is a custom yml file :D
    It's get This is a custom yml file :D

    Get a int value:
    Code:java
    1. plugin.customConfig.getInt("path.to.int");
    So if had:
    Code:
    path:
      to:
        int: 10
    It's get 10
     
  2. Offline

    Gater12

    This should be better suited in the Resources subsection.
     
  3. Offline

    Iroh

    Moved to plugin dev resources.
     
Thread Status:
Not open for further replies.

Share This Page