Skip to content

Config<T>

Namespace Mc.Scripting.Configuration · Kind class (sealed)

public sealed class Config<T> : IDisposable, IPluginResource where T : class, new()

A typed, JSON-backed configuration file of a plugin (plugins/<id>/config.json by default). Defaults (the values of a freshly constructed T, including property initialisers) are written on first run; the file is read with comments and trailing commas allowed. Describe T with System.Text.Json source generation so loading needs no reflection.

Example

public sealed class HomesConfig { public int MaxHomes { get; set; } = 3; public int CooldownSeconds { get; set; } = 5; }
[JsonSerializable(typeof(HomesConfig))] partial class Json : JsonSerializerContext;
var config = Config.Load(Json.Default.HomesConfig); // config.Value.MaxHomes
Member Description
string Path { get; } The path of the file.
T Value { get; } The current values: an immutable snapshot replaced as a whole on Config.Reload (safe to read from any thread).
bool Watch { get; set; } Whether the file is watched: edits on disk are picked up (debounced) and reported through Config.Changed.
public string Path { get; }

The path of the file.

public T Value { get; }

The current values: an immutable snapshot replaced as a whole on Config.Reload (safe to read from any thread).

public bool Watch { get; set; }

Whether the file is watched: edits on disk are picked up (debounced) and reported through Config.Changed.

Member Description
void Dispose()
bool Reload() Re-reads the file.
void Save() Writes the current value to the file (pretty-printed).
void Update(T value) Replaces the value (and saves it).
public void Dispose()
public bool Reload()

Re-reads the file. A parse error keeps the previous value, logs the file and line, and never overwrites the file.

Returns bool: True when the file was read successfully.

public void Save()

Writes the current value to the file (pretty-printed). Use it after changing a copy of Config.Value with Config.Update.

public void Update(T value)

Replaces the value (and saves it).

Parameters

Name Type Description
value T The new value.
Member Description
event Action<T>? Changed Raised after a reload that produced a new value (also when the file changed on disk with Config.Watch on), on the tick thread.
public event Action<T>? Changed;

Raised after a reload that produced a new value (also when the file changed on disk with Config.Watch on), on the tick thread.

Returns Action<T>?

IPluginResource