Reloadable Plugin

How to create a reloadable Plugin

Synapse has a reload Command which will reload all the configs file, but that doesn't mean that all Plugins are reloadable automatic, since some Plugins creates Custom Config or something else that can be reloaded during runtime.

In order to create now a Reloadable Plugin must we go back to your PluginClass and inherit ReloadablePlugin instead of Plugin. This however means we have to rename our Enable method to EnablePlugin

using Neuron.Core.Plugins;
using Ninject;
using Synapse3.SynapseModule;
using Synapse3.SynapseModule.Player;

namespace DocsExample;

[Plugin(
    Author = "Dimenzio",
    Name = "ExamplePlugin",
    Description = "An Example Plugin for the Docs",
    Version = "1.0.0"
)]
public class ExamplePlugin : ReloadablePlugin
{
    //The Inject Attribute will causes Neuron to automatically sets the PlayerService
    //This is a injected field like we mentioned on the previous page
    [Inject]
    public PlayerService PlayerService { get; set; }
    
    public override void Load()
    {
        Logger.Info("My Plugin Loaded");
    }

    public override void EnablePlugin()
    {
        Logger.Info("My Plugin was Enabled");
    }
}

This Class will now also have a new method to override Named Reload()

using Neuron.Core.Plugins;
using Ninject;
using Synapse3.SynapseModule;
using Synapse3.SynapseModule.Events;
using Synapse3.SynapseModule.Player;

namespace DocsExample;

[Plugin(
    Author = "Dimenzio",
    Name = "ExamplePlugin",
    Description = "An Example Plugin for the Docs",
    Version = "1.0.0"
)]
public class ExamplePlugin : ReloadablePlugin
{
    //The Inject Attribute will causes Neuron to automatically sets the PlayerService
    //This is a injected field like we mentioned on the previous page
    [Inject]
    public PlayerService PlayerService { get; set; }
    
    public override void Load()
    {
        Logger.Info("My Plugin Loaded");
    }

    public override void EnablePlugin()
    {
        Logger.Info("My Plugin was Enabled");
    }

    public override void Reload(ReloadEvent _ = null)
    {
        Logger.Info("Reload My Plugin!");
    }
}

This method can now be used for reloading everything you need

However there is also a second ReloadablePlugin Type that automatically reloads Config and Translation for you which would look like this

using Neuron.Core.Plugins;
using Ninject;
using Synapse3.SynapseModule;
using Synapse3.SynapseModule.Events;
using Synapse3.SynapseModule.Player;

namespace DocsExample;

[Plugin(
    Author = "Dimenzio",
    Name = "ExamplePlugin",
    Description = "An Example Plugin for the Docs",
    Version = "1.0.0"
)]
public class ExamplePlugin : ReloadablePlugin<MyConfig, MyTranslation>
{
    //The Inject Attribute will causes Neuron to automatically sets the PlayerService
    //This is a injected field like we mentioned on the previous page
    [Inject]
    public PlayerService PlayerService { get; set; }
    
    public override void Load()
    {
        Logger.Info("My Plugin Loaded");
    }

    public override void EnablePlugin()
    {
        Logger.Info("My Plugin was Enabled");
    }

    public override void OnReload()
    {
        Logger.Info("Reload My Plugin!");
    }
}

This Class now also has the field Config and Translation that can be accessed to get always the current Config and Translation

[Automatic]
public class EventHandlersExample : Listener
{
    private readonly ExamplePlugin _pluginClass;

    public EventHandlersExample(ExamplePlugin pluginClass)
    {
        _pluginClass = pluginClass;
    }

    [EventHandler]
    public void Waiting(RoundWaitingEvent _)
    {
        NeuronLogger.For<ExamplePlugin>().Warn("Y Active: " + _pluginClass.Config.EnableY);
    }
}

For reference, here is the code of the Reloadable Plugin what it is doing

public class ReloadablePlugin<TConfig,TTranslation> : ReloadablePlugin
    where TConfig : IDocumentSection
    where TTranslation : Translations<TTranslation>, new()
{
    public TConfig Config { get; private set; }
    public TTranslation Translation { get; private set; }

    public sealed override void FirstSetUp()
    {
        Config = Synapse.Get<TConfig>();
        Translation = Synapse.Get<TTranslation>();
        OnFirstSetUp();
    }
    
    public sealed override void Reload(ReloadEvent _ = null)
    {
        Config = Synapse.Get<TConfig>();
        Translation = Synapse.Get<TTranslation>();
        OnReload();
    }
    
    public virtual void OnReload() { }
    
    public virtual void OnFirstSetUp() { }
}

Last updated