How to make a config file?

Discussion in 'Plugin Development' started by imjake9, Apr 22, 2011.

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

    imjake9

    So, most plugins, when installed, will create a config file. It will be placed in the plugins/<PluginName> directory. I want to create something like this for my own plugin, but I'm not completely sure how.
    So, I basically need to know how to get the plugin/<PluginName> directory, how to create a default plugin config file, and how to read it.
    Thanks!
     
  2. Offline

    RonnSama

  3. Offline

    Crash

    To make the path / file use this
    Code:
    new File("plugins/PluginName").mkdir()
    
    File configFile = new File("plugins/PluginName/config.yml");
    
    if(!configFile.exists()){
    
        try { configFile.createNewFile(); } catch(Exception e){ System.out.println("Error when creating config file."); }
    
    }
    
    Then if you wanted to set / get properties,
    Code:
    Configuration config = new Configuration(configFile);
    config.load();
    
    config.get....
    config.setProperty(....);
    
    config.save();
    
    The get... is to get an object from the config file. The first arg is the path to it and the second is the default value if the path doesn't exist. For example if you used config.getBoolean("general.move.set", true); and config.getBoolean("general.move.get", true); on the config file :
    Code:
    general:
      move:
        set: false
    
    The first would return false not using the default value of true and the second returns true because the general.move.get path does not exist.

    Setting is pretty self explanitory.
    Code:
    config.setProperty("general.move.get", 56);
    config.save();
    
    That would save a file like this :
    Code:
    general:
      move:
        get: 56
    
    You can also set / get collections not just Integer, Double, Boolean, etc...
     
  4. Offline

    imjake9

    Thanks, both of you!
    I haven't watched the video yet, but it looks good, so I think I will.
    Crash, thank you so much, this was exactly what I needed. Now my plugin's config works! Yay!
     
  5. Offline

    ashour

    i can use this tooo
     
  6. Offline

    winter4w

    I have a plugin that i am making that they can edit the message in config but idk the code this was help fule but can you show me
     
Thread Status:
Not open for further replies.

Share This Page