Skip to content

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.


🚀 Features

  • Automatically creates a new .yml file if it doesn’t exist.
  • Loads the configuration into a FileConfiguration object.
  • Provides methods to:
    • ✅ Get the configuration
    • ✅ Save changes to disk
    • ✅ Reload configuration from disk

📂 Class Overview

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);
    }
}

🛠️ Usage Example

1. Create a custom messages.yml

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!"

2. Initialize the ConfigManager

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;
    }
}

3. Modify and Save

ConfigManager messages = plugin.getMessagesConfig();

// Change a value in memory
messages.getConfig().set("hello", "&bHi there!");

// Save changes to file
messages.save();

4. Reload Configuration

plugin.getMessagesConfig().reload();

⚡ Benefits

  • 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.

✅ Best Practices

  • 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 ConfigManager instances if your plugin uses multiple .yml files.

Clone this wiki locally