Skip to content

Latest commit

 

History

History
43 lines (39 loc) · 2.28 KB

README.md

File metadata and controls

43 lines (39 loc) · 2.28 KB

Spigot-CustomConfig

License Pull Requests Issues Lines

Introduction

Bukkit API only allows you to create one config for each plugins, and there is no sign that it will change the situation in the future versions.
Now this piece of code allows you to create custom configs in Bukkit/Spigot plugins, and you can set where the config should be saved.
What's more, the code extends org.bukkit.configuration.file.YamlConfiguration so that you can use methods such as getConfig() and saveConfig() as usual.

However, I didn't rewrite method reloadConfig() so it will remove all the comments as Bukkit API does. Maybe I'll rewrite it next time.🤣
Starting from Minecraft 1.19, reloadConfig() will not remove all comments.

Usage

Create Config

For example, I would like to create a custom YML config named "AConfig" for MyPlugin in "plugins/MyPlugin/settings/".
First, register the config in the main class:

public class MyPlugin extends JavaPlugin {
    public static ConfigReader AConfig;
}

Second, load the config on plugin enable:

public void onEnable() {
    AConfig = new ConfigReader(this,"settings/","AConfig.yml");
	AConfig.saveDefaultConfig();
}

After all that work, you can use all methods in the rest of the plugin.
Here is some examples:

FileConfiguration aconfig = AConfig.getConfig();
List<String> value1 = aconfig.getStringList("First-List");
aconfig.set("First-object","element");
aconfig.reloadConfig();
aconfig.saveConfig();

There are also Javadoc comments before every method.

Save Config

Combining saveConfig() with reloadConfig(), this can be used on plugin disable and after setting some keys in a config.

ConfigReader.save(this.AConfig);