Skip to content
GrafDimenzio edited this page Mar 11, 2026 · 1 revision

Creating a Config

Configurations allow plugin users to customize the behavior of your plugin without modifying the code.

To create a configuration file, you need to create a class that implements IConfig and add the Config attribute to it.

The Name property of the attribute determines the file name inside the configs folder.

💡 Tip
It is good practice to use lowercase names for config files.

Inside the class you can define properties which represent your configuration values.
Default values can be assigned directly and will be used when the config file is generated for the first time.

Example:

using DSMOOFramework.Config;

[Config(Name = "example")]
public class ExampleConfig : IConfig
{
    public bool Enabled { get; set; } = true;
    
    public string Name { get; set; } = "Example";

    public HashSet<string> MyList { get; set; } = ["1", "2", "3"];
}

Config Holder

When your plugin loads, the configuration file will automatically be created and loaded.

You could inject the config class directly through dependency injection. However, this is not recommended.

Instead, you should use the ConfigHolder object.

Why use ConfigHolder?

When configs are reloaded, the framework replaces the old config instance with a new one.

  • ConfigHolder always points to the newest config
  • directly injected configs do not update

ConfigHolder also provides useful methods:

  • SaveConfig() → writes the current config to disk
  • LoadConfig() → reloads the config from file

Direct Config Injection (Not Recommended)

The following example shows direct config injection.
This works, but configs will not update when they are reloaded.

using DSMOOFramework.Logger;
using DSMOOFramework.Managers;
using DSMOOFramework.Plugins;
using DSMOOServer.API.Events;
using DSMOOServer.API.Events.Args;

[Plugin(
    Name = "ExamplePlugin",
    Author = "Dimenzio",
    Description = "Small Example Plugin",
    Version = "1.0.0",
    Repository = "None"
    )]
public class ExampleManager(ILogger logger, EventManager eventManager, ExampleConfig config) : Manager
{
    public override void Initialize()
    {
        logger.Info("Loaded ExampleManager");
        logger.Info(config.Name);
        eventManager.OnPlayerJoined.Subscribe(OnPlayerJoined);
    }

    private void OnPlayerJoined(PlayerJoinedEventArgs eventArgs)
    {
        logger.Warn($"Player {eventArgs.Player.Name} joined the Server!!!");
    }
}

Using ConfigHolder (Recommended)

The recommended approach is to use ConfigHolder<T>.

This ensures that your plugin always accesses the newest config instance, even after reloads.

using DSMOOFramework.Config;
using DSMOOFramework.Logger;
using DSMOOFramework.Managers;
using DSMOOFramework.Plugins;
using DSMOOServer.API.Events;
using DSMOOServer.API.Events.Args;

namespace RandomTeleports;

[Plugin(
    Name = "ExamplePlugin",
    Author = "Dimenzio",
    Description = "Small Example Plugin",
    Version = "1.0.0",
    Repository = "None"
    )]
public class ExampleManager(ILogger logger, EventManager eventManager, ConfigHolder<ExampleConfig> configHolder) : Manager
{
    public override void Initialize()
    {
        logger.Info("Loaded ExampleManager");
        logger.Info(configHolder.Config.Name);
        eventManager.OnPlayerJoined.Subscribe(OnPlayerJoined);
    }

    private void OnPlayerJoined(PlayerJoinedEventArgs eventArgs)
    {
        logger.Warn($"Player {eventArgs.Player.Name} joined the Server!!!");
    }
}

Plugin Base Class

Since most plugins frequently use configs, DSMOO provides a helpful base class:

Plugin<T>

This class already includes:

  • a Config object
  • a ConfigHolder
  • a Logger
  • a SaveConfig() helper method

Implementation:

public abstract class Plugin<T> : Manager, IInject where T : IConfig
{
  public T Config => (T) this.ConfigHolder.ConfigObject;

  [Inject]
  public DSMOOFramework.Config.ConfigHolder<T> ConfigHolder { get; set; }

  [Inject]
  public ILogger Logger { get; set; }

  public virtual void AfterInject()
  {
  }

  public void SaveConfig() => this.ConfigHolder.SaveConfig();
}

Instead of inheriting from Manager, you can inherit from Plugin<T>.

This allows you to access the config directly through Config and the logger through Logger, without manually injecting them.

💡 Note
This class uses several framework features that are explained later in the documentation. You do not need to fully understand how everything works internally yet in order to use it.


Example Plugin Using Plugin

Here is an example plugin using the Plugin<T> base class.

using DSMOOFramework.Plugins;
using DSMOOServer.API.Events;
using DSMOOServer.API.Events.Args;

namespace RandomTeleports;

[Plugin(
    Name = "ExamplePlugin",
    Author = "Dimenzio",
    Description = "Small Example Plugin",
    Version = "1.0.0",
    Repository = "None"
    )]
public class ExampleManager(EventManager eventManager) : Plugin<ExampleConfig>
{
    public override void Initialize()
    {
        Logger.Info("Loaded ExampleManager");
        Logger.Info(Config.Name);
        eventManager.OnPlayerJoined.Subscribe(OnPlayerJoined);
    }

    private void OnPlayerJoined(PlayerJoinedEventArgs eventArgs)
    {
        Logger.Warn($"Player {eventArgs.Player.Name} joined the Server!!!");
    }
}

Next Steps

The next topic to learn about is creating commands for your plugin:

➡ https://github.com/GrafDimenzio/DSMOO/wiki/Commands

Clone this wiki locally