Skip to content

Configurations

Alberto Migliorato edited this page May 4, 2023 · 4 revisions

Configurations Management

Getting the instance

At first, we'll need to instance a new ConfigManager object. It needs the JavaPlugin instance as a parameter in the constructor. In code:

ConfigManager configManager = new BukkitConfigManager(MyPlugin.getInstance()); // BungeeConfigManager for bungee, VelocityConfigManager for velocity!

It should be instanced only ONCE per plugin; so, it needs to be stored in a local field. In code:

public class MyConfig {

    private ConfigManager cm;

    public MyConfig() {
        init();
    }

    public void init() {
        this.cm = new BukkitConfigManager(MyPlugin.getInstance());
    }
}

Creating a .yml file

Now we're able to use the ConfigManager class. To create a .yml file (e.g. config.yml), we can act in two ways:

  • using the create(String) method, it will return the FileConfiguration we're looking for;
  • using the create(String) method without caring about what it returns and then getting our FileConfiguration with the get(String) method;
public class MyConfig {

    private ConfigManager cm;
    private Configuration configuration;

    public MyConfig() {
        init();
    }

    public void init() {
        this.cm = new BukkitConfigManager(MyPlugin.getInstance());
        this.cm.create("config.yml");                                      //OR: configuration = this.cm.create("config.yml");
        this.configuration = this.cm.get("config.yml");
    }

    public Configuration getConfig() {
        return this.configuration;
    }
}

We're at a good step. Now, focusing on getting values inside the .yml shouldn't be necessary, as FileConfiguration is a Bukkit class: instead, we'll focus on how to save and reload our .yml files.

Saving a .yml file

To save a .yml file after some edits, we just have to do what follows:

// Here we get the file and edit a specific value
Configuration fc = this.cm.get("config.yml");
fc.set("Do.I.Love.Firefighters", false);

// Then we save it back
this.cm.save("config.yml", fc);

Reloading a .yml file

Don't worry, reloading is quite easy too! The method's syntax is #reload(String); however, if you are using multiple .yml files and don't want to reload every single file manually, you can simply call the #reloadAll() method, and it will let the trick for you. In code:

// Here we get the file before reloading and getting any edits
Configuration fc = this.cm.get("config.yml");
boolean bool = fc.getBoolean("Do.I.Love.Firefighters");     // currently it would return FALSE

// MANUAL MODIFICATION TO FILE: We edit it manually from FALSE to TRUE
// Then reload it and get it again

this.cm.reload("config.yml");
bool = fc.getBoolean("Do.I.Love.Firefighters");             // Now it returns TRUE

Example Config class

Finally, here's an example of a complete ConfigManager class usage:

public class ExampleConfigManager {

    private ConfigManager cm;

    /**
     * It will use the /plugins/MyPlugin/ folder to create, save and load files
     */
    public void init() {
        cm = new BukkitConfigManager(MyMainClass.getInstance());
    }

    public Configuration createConfig() {
        Configuration config = cm.create("config.yml");
        return config;
    }

    public Configuration getConfig() {
        return cm.get("config.yml");
    }

    public void saveConfig(String config, Configuration fc) {
        cm.save(config, fc);
    }

    public void reloadConfig() {
        cm.reload("config.yml");
    }

    public void reloadFiles() {
        cm.reloadAll();
    }
}