-
Notifications
You must be signed in to change notification settings - Fork 0
ConfigManager
Larrox edited this page Aug 29, 2025
·
2 revisions
The ConfigManager is a simple utility class to handle custom configuration files (.yml) in your Spigot/Bukkit plugin.
It allows you to easily load, save, and reload YAML configuration files beyond the default config.yml.
- Automatically creates a new
.ymlfile if it doesn’t exist. - Loads the configuration into a
FileConfigurationobject. - Provides methods to:
- ✅ Get the configuration
- ✅ Save changes to disk
- ✅ Reload configuration from disk
package dev.larrox;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
public class ConfigManager {
private final JavaPlugin plugin;
private File file;
private FileConfiguration config;
public ConfigManager(JavaPlugin plugin, String name) {
this.plugin = plugin;
this.file = new File(plugin.getDataFolder(), name + ".yml");
if (!file.exists()) {
plugin.saveResource(name + ".yml", false);
}
this.config = YamlConfiguration.loadConfiguration(file);
}
public FileConfiguration getConfig() {
return config;
}
public void save() {
try {
config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void reload() {
config = YamlConfiguration.loadConfiguration(file);
}
}Place a messages.yml file inside your plugin’s resources/ folder (next to plugin.yml):
prefix: "&7[&aMyPlugin&7] "
no-permission: "&cYou don't have permission to do this!"
hello: "&aHello, world!"Inside your JavaPlugin class:
public class MyPlugin extends JavaPlugin {
private ConfigManager messagesConfig;
@Override
public void onEnable() {
// Load messages.yml
messagesConfig = new ConfigManager(this, "messages");
// Example: get a string
String helloMsg = messagesConfig.getConfig().getString("hello");
getLogger().info("Loaded message: " + helloMsg);
}
public ConfigManager getMessagesConfig() {
return messagesConfig;
}
}ConfigManager messages = plugin.getMessagesConfig();
// Change a value in memory
messages.getConfig().set("hello", "&bHi there!");
// Save changes to file
messages.save();plugin.getMessagesConfig().reload();- Keeps your configuration files organized (separate configs like
messages.yml,data.yml, etc.). - No need to manually handle file paths or YAML parsing.
- Reusable across multiple projects.
- Always call
save()after making changes, otherwise they’ll be lost on restart. - Use
reload()only if you expect changes from outside (e.g., when editing files while the server is running). - Store multiple
ConfigManagerinstances if your plugin uses multiple.ymlfiles.