Tutorial How to make custom plugin configs easily

Discussion in 'Resources' started by BurnyDaKath, Jul 8, 2015.

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

    BurnyDaKath

    Howdy, today my friend @_Error asked me how to make custom configs, i gave him some things but it didn't work. So now i will tell you how to do it easily.

    First of all, make assign new file values.
    If you are going to use it in new class, other then config class (you can use main class), you add "public static" before these lines, so other classes can access it.

    Code:
    File conf; //File itself
    FileConfiguration config; //Bukkit way of loading it
    Next, you create your own file. It doesn't need to be in your jar, it is being created by our code. If you are using main class, put it in onEnable, if you use it in different class do PluginEnableEvent.

    Code:
    Plugin plugin = this; //Get plugin (if in main class)
    //or
    Plugin plugin = event.getPlugin(); //If used events
    
    if(conf == null) { //Check if config loaded
    conf = new File(plugin.getDataFolder(), "name.yml"); //Create or load config into file name.yml
    } //plugin.getDataFolder() can be without "plugin." if in main class
    config = YamlConfiguration.loadConfiguration(conf); //Load config into usable by bukkit thing
    Then we need a way to save it. I used a way of creating new method.

    Code:
    public static void saveConfig(){ //Making our method
            try { //Check if there will be error, if there no errors do code normally
                config.save(conf); //Save config
            } catch (IOException var3) { //Error name
                Logger.getLogger("Minecraft").severe("[Plugin] Unable to Save Config."); //If any errors, send message to console about unsuccessful save
            }
        }
    To set default settings in config, we first will check if line exists, and if it isn't, we create it. There 2 ways: check for every config line (better when creating plugin so you don't have to reset config every time you change default), or check if directory exist in which one all things.

    Code:
    //First way
    
    if (!config.contains("settings.thing1")){ //Check for exist thing1
    config.set("settings.thing1", "String here or int or double or any other object")
    saveConfig();
    }
    
    if (!config.contains("settings.thing2")){
    config.set("settings.thing2", "String")
    saveConfig();
    }
    
    //Second way
    
    if (!config.contains("settings")){ //Check for existing settings directory
    config.set("settings.thing1", "String here or int or double or any other object")
    config.set("settings.thing2", "String")
    }
    
    //How config will look
    
    settings:
         thing1: "String here or int or double or any other object"
         thing2: "String"
    You better default settings check every time you save config, so put it in saveConfig() method.
    Whenever you want to access (get) things from config you do:
    config.get("settings.thing1")
    or getString()/getDouble()/getInt()

    This tutorial will create config name.yml in plugin directory. Thanks for reading, hope you liked it!
     
  2. Offline

    _Error

    Thanks :D
     
  3. Online

    timtower Administrator Administrator Moderator

    @BurnyDaKath It is a way of doing it, I have my own (less publicly accepted) methods.
    No need for static though, getters and constructors would also do ;)
    Moved to resources
     
  4. Offline

    BurnyDaKath

    Static is best way i found. You can easily in other class do FileConfiguration config = Config.config;
     
  5. Online

    timtower Administrator Administrator Moderator

    Or plugin.getSomeConfig
     
  6. Offline

    _Error

    The problem here is. How do we do comment?
    like #Comment
     
Thread Status:
Not open for further replies.

Share This Page