nwn-dotnet / Anvil

A Neverwinter Nights Server API
https://nwn-dotnet.github.io/Anvil
MIT License
24 stars 9 forks source link

Feature request: Subscribe to Polymorph Events #750

Closed cltalmadge closed 8 months ago

cltalmadge commented 9 months ago

Currently, the only way to subscribe to Polymorph events is to use the EventsPlugin and leverage scripthandlers. A "nice to have" here would be to have a more Anvil-specific way to register to polymorph and unpolymorph events.

Here is a current example of how I am doing it:

public class CharacterPolymorphService
{
    private readonly Logger Log = LogManager.GetCurrentClassLogger();

    public CharacterPolymorphService()
    {
        Log.Info("CharacterPolymorphService initialized.");

        EventsPlugin.SubscribeEvent(EventsPlugin.NWNX_ON_POLYMORPH_BEFORE, "polymorph_before");
        EventsPlugin.SubscribeEvent(EventsPlugin.NWNX_ON_UNPOLYMORPH_AFTER, "unpolymorph_after");
    }

    [ScriptHandler("unpolymorph_after")]
    private void OnUnpolymorphAfter(CallInfo info)
    {
        if (!info.ObjectSelf.IsPlayerControlled(out NwPlayer? player)) return;

        RestoreSpells(player);
    }

    [ScriptHandler("polymorph_before")]
    private void OnPolymorphBefore(CallInfo info)
    {
        if (!info.ObjectSelf.IsPlayerControlled(out NwPlayer? player)) return;

        SaveSpells(player);
    }

    // other stuff