Config.yml code help

Discussion in 'Spigot Plugin Development' started by loonaticop, May 23, 2015.

  1. Hello everyone,

    recently i've coding some plugins for fun/personal use and since they are "custom" i've never used the config.yml thingy.

    Well, now i'm with 2 ideas for public distribution and after reading configapi and some threads about it i'm still unable to interpreting it the right way (english isn't my main language and is causing me some trouble about this issue).

    What i want to implement on config.yml and what i "have":

    groups:
    #to apply more groups just copy/paste this default section
    default:
    one: true
    two: Awesome plugin!
    three:
    three.one: false
    three.two: BLACK


    i have created this on the config.yml (the upper example is just random but follows the structure i want) manually but i'm unable to find how to code this and use the values.

    public void onEnable()
    {
    this.saveDefaultConfig();
    this.getConfig().options().copyDefaults(true)
    }

    Besides this i'm "stuck", and this can be wrong :p

    If anyone could help me would be awesome.

    Thank you for your time,
    loonaticOP
     
  2. I would just use a variable FileConfiguration conf = getConfig();

    And when you need to set it use conf.set(path, object)
    Getting:
    conf.get(path). Use getString for a string, getInt for a int etcetera.

    Or make a hashmap for what you want and just when you leave/restart save it to the config (saveConfig();)

    and when you join/start, get it from the config and put it in the Hashmap.

    Succes! (when u set something in the config u need to put saveConfig(); afterwards!
     
    • Like Like x 1
  3. In that case, i would havê to create the getters setters or just use conf.set(path, object) ? :)

    Btw ty so much for the response, it was enlighten for sure o/
     
  4. Yes, there's

    FileConfiguration conf = getConfig();

    and then you can do:

    conf.set(path, value); // This sets a value in your config file
    conf.getString(path); // This gets a string from the config.yml
    conf.getInt(path); and conf.getDouble(path); // This gets numbers
    conf.getBoolean(path) // This gets a boolean

    conf.saveConfig(); // Saves the config.yml after changes have been made
    conf.reloadConfig(); // Reloads the config.yml
     
  5. No problem, you don't have to create getters and setters, they already exist. Just type conf.(you get a whole list).
     
  6. the setters aren't needed right? if i update the config manually it will update automatically and be saved?
     
  7. If you edit the config manually I suggest automatically reloading it or add a reload command which reloads you config
     
  8. i have implemented the reload comand already :)

    about the automatically reload hows that work?
     
  9. Have you added commands so that you can change your config from ingame? If so the configs will automatically reload once the command has been executed. However, if you do it manually ie in the config.yml yourself you will have to manually reload it for things to take effect.
     
  10. no i havent, it's a good idea however :p

    ok i thought i would have to write code about that but than reload is enough.

    Thank you so much guys o/
     
  11. Just because the plugin is custom doesn't mean you should hard code all of it :p