[Tutorial] Using config.yml

Discussion in 'Resources' started by GyllieGyllie, May 11, 2014.

?

Was this tutorial usefull

  1. Yes

    50.0%
  2. Yes, but needs more info

    50.0%
  3. No

    0 vote(s)
    0.0%
Thread Status:
Not open for further replies.
  1. Offline

    GyllieGyllie

    This tutorial is still being finished!!!
    Using Config.yml


    imports:

    You need to import the next things:


    Code:java
    1. import java.io.File;


    creating file/checking important settings

    now when the plugin is enabled you have to possible situations.

    A. Config file doesn't exist
    B. Config file does exist

    This is the code you'll need to put in the onEnable() part:

    Code:java
    1. public void onEnable() {
    2.  
    3. File file = new File(getDataFolder() + File.separator + "config.yml"); //This will get the config file
    4.  
    5.  
    6. if (!file.exists()){ //This will check if the file exist
    7. //Situation A, File doesn't exist
    8.  
    9. getConfig().addDefault("Name", "Value"); //adding default settings
    10.  
    11. //Save the default settings
    12. getConfig().options().copyDefaults(true);
    13. saveConfig();
    14. } else {
    15. //situation B, Config does exist
    16. CheckConfig(); //function to check the important settings
    17. saveConfig(); //saves the config
    18. reloadConfig(); //reloads the config
    19.  
    20. }
    21. }


    Now as you can see, in situation B there is a function CheckConfig(); This funcion will check of important settings aren't deleted

    Code:java
    1. public void CheckConfig() {
    2.  
    3. if(getConfig().get("Name") == null){ //if the setting has been deleted it will be null
    4. getConfig().set("Name", "Value"); //reset the setting
    5. saveConfig();
    6. reloadConfig();
    7.  
    8. }
    9.  
    10. }


    Getting information from config file

    Now to get the information from the config file you need to know what kind of info you need.

    Code:java
    1. //get String
    2. String string = getConfig().getString("Name");
    3.  
    4. //get Integer
    5. int integer = getConfig().getInt("Name");
    6.  
    7. //get Boolean
    8. Boolean boolean = getConfig().getBoolean("Name");


    Saving information

    Now to save information you just need to do

    Code:java
    1. getConfig().set("Name", "Value");
    2. saveConfig();
    3. reloadConfig();


    Now the saveConfig(); and reloadConfig(); are very important! If you don't place these lines your settings won't be updated.

    Naming your settings

    What is the best way to choose your names for your settings?

    The best way to choose a name:
    1) think what settings you all want to save in the config
    2) divide them into several big groups. Example: Default, Players, Messages ...
    3) now per group think what will be placed in the group and maybe make more little groups in the bigger groups.
    4) Start making the names for your settings using "." to make it this way it will be organised using the groups from above.
    Example:
    • Group1.Group12.Setting1, Group1.Group12.Setting2
    • Default.Enabled, Default.Online ...
    • Players.GyllieGyllie.ip, Players.GyllieGyllie.rank, Players.GyllieGyllie.money ...
    If you then open the config file you see the settings will be organised like this:
    [​IMG]

     
Thread Status:
Not open for further replies.

Share This Page