⚙️Advanced Configurations

In case you need more flexibility or customization, the configuration system has you covered!

Getting a configuration

To get a configuration from a file, you just have to use the configuration extensions in your plugin class. You can see how in the following example:

MyFirstPlugin.cs
public class MyFirstPlugin : Plugin
{
    // LevelingConfig is a serializable class.
    public LevelingConfig LevelsConfig;
    
    public override void LoadConfigs()
    {
        base.LoadConfigs();
        // This will read the LevelingConfig configuration from the lvls.yml file
        // In case the file doesn't exist, it will be created with its default values
        LevelsConfig = this.LoadConfig<LevelingConfig>("lvls.yml");  
    }
}

By default, if the user has broken the configuration file, an error will appear in the console and LoadConfig will return null. You can catch this with TryLoadConfig:

MyFirstPlugin.cs
private bool _hasIncorrectSettings = false;

public override void LoadConfigs()
{
    base.LoadConfigs();
    // We could, for example, avoid to enable the plugin at all.
    _hasIncorrectSettings = !this.TryLoadConfig("lvls.yml", out LevelsConfig);
}

public override void Enable()
{
    // Enable is called after the settings are loaded.
    // We can directly check if the user has incorrect settings.
    if (_hasIncorrectSettings)
    {
        Logger.Error("Detected incorrect settings, not loading");
        return;
    }

Saving a configuration

To save a configuration, you just have to call the SaveConfig<T>(T, string) method inside your plugin class:

In this case, the configuration file would be overridden with the property NeededExp changed to 50.

And as easy as that, you now are an advanced configuration system user!

Last updated