Configuration - litjisz/Astra GitHub Wiki

Configuration

The Astra framework provides a robust configuration management system through the ConfigManager class. This system allows you to easily create, load, modify, and save YAML configuration files.

Basic Usage

Accessing the ConfigManager

You can access the ConfigManager through the Implements system:

ConfigManager configManager = Implements.fetch(ConfigManager.class);

Or in an AbstractModule:

ConfigManager configManager = getConfig();

Loading Configurations

// Load a configuration file (creates it if it doesn't exist)
FileConfiguration config = configManager.loadConfig("settings");

// Load without creating if it doesn't exist
FileConfiguration config = configManager.loadConfig("settings", false);

Getting Configurations

// Get a loaded configuration (loads it if not already loaded)
FileConfiguration config = configManager.getConfig("settings");

Saving Configurations

// Save a specific configuration
configManager.saveConfig("settings");

// Save all configurations
configManager.saveAllConfigs();

Reloading Configurations

// Reload a specific configuration
FileConfiguration config = configManager.reloadConfig("settings");

// Reload all configurations
configManager.reloadAllConfigs();

Working with Configuration Values

Getting Values

// Get a value with a default
String serverName = configManager.getString("config", "server.name", "My Server");
int maxPlayers = configManager.getInt("config", "server.max-players", 20);
double moneyMultiplier = configManager.getDouble("config", "economy.multiplier", 1.0);

// Generic getter with type inference
Boolean enableFeature = configManager.get("config", "features.new-feature", false);
List<String> enabledWorlds = configManager.get("config", "worlds.enabled", new ArrayList<>());

Setting Values

// Set a value
configManager.set("config", "server.name", "New Server Name");
configManager.set("config", "server.max-players", 50);
configManager.set("config", "economy.multiplier", 2.5);

Working with Sections

// Create a configuration section
ConfigurationSection section = configManager.createSection("config", "new-feature");

// Get keys from a configuration
Set<String> topLevelKeys = configManager.getKeys("config");
Set<String> featureKeys = configManager.getKeys("config", "features", true);

Lists

// Get a string list
List<String> allowedCommands = configManager.getStringList("config", "commands.allowed");

Managing Configuration Files

Checking if a Configuration Exists

boolean exists = configManager.exists("settings");

Deleting a Configuration

boolean deleted = configManager.deleteConfig("temporary");

Example Usage

Here's a complete example of using the configuration system in a module:

public class EconomyModule extends AbstractModule {
    private double startingBalance;
    private double interestRate;
    private boolean interestEnabled;
    private List<String> disabledWorlds;
    
    @Override
    public void onEnable() {
        // Load configuration values
        ConfigManager configManager = getConfig();
        
        // This will create economy.yml if it doesn't exist
        startingBalance = configManager.getDouble("economy", "starting-balance", 100.0);
        interestRate = configManager.getDouble("economy", "interest.rate", 0.05);
        interestEnabled = configManager.get("economy", "interest.enabled", true);
        disabledWorlds = configManager.getStringList("economy", "disabled-worlds");
        
        if (disabledWorlds == null) {
            disabledWorlds = new ArrayList<>();
            configManager.set("economy", "disabled-worlds", disabledWorlds);
            configManager.saveConfig("economy");
        }
        
        getLogger().info("Economy module enabled with starting balance: " + startingBalance);
    }
    
    @Override
    public void onReload() {
        // Reload configuration
        ConfigManager configManager = getConfig();
        configManager.reloadConfig("economy");
        
        startingBalance = configManager.getDouble("economy", "starting-balance", 100.0);
        interestRate = configManager.getDouble("economy", "interest.rate", 0.05);
        interestEnabled = configManager.get("economy", "interest.enabled", true);
        disabledWorlds = configManager.getStringList("economy", "disabled-worlds");
        
        getLogger().info("Economy module reloaded with starting balance: " + startingBalance);
    }
}

Default Configuration Files

If you include a configuration file in your plugin's resources, it will be used as the default:

// If economy.yml exists in your plugin's resources, it will be extracted
// when loadConfig is called if the file doesn't exist in the data folder
FileConfiguration config = configManager.loadConfig("economy");

The default configuration values will be set as defaults for the loaded configuration, but won't overwrite existing values.

⚠️ **GitHub.com Fallback** ⚠️