Solved How to use config.yml in java code

Discussion in 'Plugin Development' started by TheMintyMate, Apr 25, 2014.

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

    TheMintyMate

    I have recently finished developing 3 plugins concerning player interaction in the world. I am now developing a mini game for my server, and have come to the conclusion that I will need to save some specific data to a file. After some investigation, I discovered that I would need to use .yml , or more specifically, config.yml. However, from my findings, I couldn't find any clear explanations of how to use it. Below are the things I would like to be "spoon fed". Thank-you in advance.

    1. How to create a .yml file to store data in (when plugin is deactivated, or otherwise)
    2. How to append to said .yml file
    3. How to read said .yml file
    4. How to save/update/reload said .yml file
    5. Any other suggestions you may have.

    I would like to add that I struggled to understand what the bukkit wiki was trying to convey, and from the threads I have searched (none really helped) I am not the only one. So I hope this thread helps others as well as me.
    Thank-you,
    Minty

    Edit: All of the steps above (except 5) refer to using code, not directly accessing the .yml file
     
  2. Offline

    Gater12

    TheMintyMate
    Create:
    Invoke in your onEnable() it will copy the config.yml from your resources into your data folder.
    Code:java
    1. saveDefaultConfig();


    Set:
    Code:java
    1. getConfig().set("path.to.variable", variable);
    2. saveConfig();

    What it will look like in your config.yml:
    Code:
    path:
      to:
        variable: variable
    Get:
    Code:java
    1. /* You can get a String, Integer, double, ItemStack whatever is provided */
    2. getConfig().get("path.to.variable");


    Example usage:
    Code:java
    1. @Override
    2. public void onEnable(){
    3. saveDefaultConfig();
    4. getServer().getPluginManager().registerEvents(this, this);
    5. }
    6.  
    7. @EventHandler
    8. public void onJoin(PlayerJoinEvent e){
    9. if(getConfig().getString("my.motd") == null){
    10. getConfig().set("my.motd", "Hello, player!");
    11. saveConfig();
    12. }
    13. e.getPlayer().sendMessage(getConfig().getString("my.motd"));
    14. }
    15.  


    If you would like to mirror JavaPlugin implementation of the config.yml to create your own .yml files please refer to: http://wiki.bukkit.org/Configuration_API_Reference
     
    TheMintyMate likes this.
  3. If you are saving data to a .yml file, I wouldn't create a config since owners will try to edit stuff in there.
    Read this to figure out how to make a .yml file that is NOT a config. It also allows you to make as much .yml files as you wish:
    CLICK HERE
     
    TheMintyMate likes this.
  4. Offline

    TheMintyMate

    Gater12
    WD_MUFFINFLUFFER

    Thanks for your help, I have now started to implement it into my plugin. I just wanted to check a few things first:
    1. Does using saveDefaultConfig(); overwrite any previous config.yml
    2. How can I detect if the config.yml is empty

    Again, thanks guys :)
     
  5. saveDefaultConfig() will override the config with the default config you have packaged with your plugin.
    I usually check if config.yml exists, else saveDefaultConfig()
    You can check by doing
    File configFile = new File(getDataFolder().toString() + "/config.yml");
    if (!configFile.exists()) {
    this.saveDefaultConfig();
    }

    For your other question, do you want to check if a certain path is empty? Or the entire config file
     
  6. Offline

    Gater12

    TheMintyMate likes this.
  7. Interesting, then I guess you do not need to make a file to check if it exists, it does it for you. Ok cool :)
     
  8. Offline

    TheMintyMate

    Gater12
    WD_MUFFINFLUFFER
    Ok, thank-you guys :)
    For anyone else who may come across this post, you can detect if a path is empty by:
    Code:
    if (getConfig().getString("path.path") == null){
    //do stuff
    }
    else {
    //do stuff here if the string contains data
    }
    
    Above clarifies Gater12's first post.
     
Thread Status:
Not open for further replies.

Share This Page