Skip to content

Adding Custom Permissions

Jorteck edited this page Apr 14, 2022 · 1 revision

Adding permissions to your own plugin

To start, you will need to add the NWN.Toolbox plugin as a reference in NuGet. This can be done by navigating to your plugin, and entering the follow command:

dotnet add package NWN.Toolbox -v <desired version>

Where <desired version> is the version of NWN.Toolbox you would like to to use.

Once installed, you will have access to the NWN.Toolbox APIs. NWN.Toolbox has a very simple interface for querying player permissions.

Simply reference Jorteck.Toolbox.Features.Permissions.PermissionsService in your service constructor, and you can check your custom permissions using PermissionService.HasPermission():

using Anvil.API;
using Anvil.API.Events;
using Anvil.Services;
using Jorteck.Toolbox.Features.Permissions;

[ServiceBinding(typeof(CoolCommandService))]
public sealed class CoolCommandService
{
  // The permission key to check.
  private const string MyPermissionKey = "server.usecoolcommand";

  private readonly PermissionsService permissionsService;

  public CoolCommandService(PermissionsService permissionsService)
  {
    this.permissionsService = permissionsService;
    NwModule.Instance.OnPlayerChat += OnPlayerChat;
  }

  private void OnPlayerChat(ModuleEvents.OnPlayerChat eventData)
  {
    // Check if the message sent was the cool command.
    if (eventData.Message == "/cool")
    {
      // Check to see if the sender of this message has permission to use the command.
      if (permissionsService.HasPermission(eventData.Sender, MyPermissionKey))
      {
        // Tell them they are cool!
        eventData.Sender.SendServerMessage("You are awesome!");
      }
    }
  }
}

Clone this wiki locally