Skip to content

Configuration API

SirBlobman edited this page Apr 29, 2023 · 1 revision

Information

BlueSlimeCore uses ConfigurationManager objects to assist you with configuration files.

ConfigurationManager Instance

Option 01) Pre-Built Plugin

You can make your plugin's main class extend com.github.sirblobman.api.plugin.ConfigurablePlugin instead of JavaPlugin to use the built-in method: getConfigurationManager()

Option 02) Custom Instance

You can create a custom instance in your own plugin class if you don't want to use the ConfigurablePlugin as a parent.

import org.bukkit.plugin.java.JavaPlugin;
import com.github.sirblobman.api.configuration.ConfigurationManager;

public final class YourPlugin extends JavaPlugin {
    private final ConfigurationManager configurationManager;
    
    public YourPlugin() {
        this.configurationManager = new ConfigurationManager(this);    
    }
    
    public ConfigurationManager getConfigurationManager() {
        return this.configurationManager;
    }
}

Using ConfigurationManager

Saving Default Configurations

Default configurations are files that are inside of the plugin jar and contain default values. You can create them at startup by using the following method in your plugin class. Default configurations will not override existing files.

@Override
public void onLoad() {
    ConfigurationManager configurationManager = getConfigurationManager();
    configurationManager.saveDefault("example.yml");
}

Reloading Configurations

Reloading a configuration means overriding the values in memory with values currently on the disk. Reloading is usually done after a user finishes editing a configuration. You can reload configuration files with the following method:

public void reloadConfiguration() {
    ConfigurationManager configurationManager = getConfigurationManager();
    configurationManager.reload("example.yml");
}

Reading Configuration Values

Reading a configuration allows you to get values. ConfigurationManager uses the Bukkit YamlConfiguration objects, so you don't have to learn anything new if you already know this system.

public void readValues() {
    ConfigurationManager configurationManager = getConfigurationManager();
    YamlConfiguration configuration = configurationManager.get("example.yml");
    
    String exampleValue = configuration.getString("example-path");
    // Do something with the value
}

Saving Configuration Values

Sometimes you need to save values in memory to the disk. You should not do this for user-editable files, but for data files it is sometimes necessary.

public void setCustomValues() {
    ConfigurationManager configurationManager = getConfigurationManager();
    YamlConfiguration configuration = configurationManager.get("example.yml");
    configuration.set("example-path", "example value");
    configurationManager.save("example.yml")
}

Extra Information

You can find out more information about the configuration API in BlueSlimeCore by reviewing the source files