-
Notifications
You must be signed in to change notification settings - Fork 34
Advanced Configurations
ced777ric edited this page Feb 10, 2025
·
1 revision
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:
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:
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;
}To save a configuration, you just have to call the SaveConfig<T>(T, string) method inside your plugin class:
public override void Enable()
{
// We can edit the configuration
LevelingConfiguration.NeededExp = 50;
// And we call this.SaveConfig to save our changes to the configuration file!
this.SaveConfig("lvls.yml", LevelingConfiguration);
}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!
- Making Plugins
- Features
- Guides