Skip to content

CommandBase

Axwabo edited this page Oct 30, 2023 · 7 revisions

CommandBase

The Axwabo.CommandSystem.CommandBase class is the core of creating commands.

See also: targeting commands container commands

You can specify common properties using attributes, with resolvers or by overriding them.

Register your command in the Remote Admin, Server Console or the Client Console (what's the difference?) using the Axwabo.CommandSystem.Attributes.CommandTargetAttribute

There is an attribute to set each of the following properties: Name Description Aliases Usage MinArguments

Alternatively, the Axwabo.CommandSystem.Attributes.CommandPropertiesAttribute allows you to set Name Description Aliases MinArguments in one attribute.

A command must specify its name in one of the ways described above (otherwise, an exception is thrown) and override the Execute method. This takes in an ArraySegment<string> and a CommandSender - and returns a CommandResult

Example:

using System;
using Axwabo.CommandSystem;
using Axwabo.CommandSystem.Attributes;

[CommandProperties(CommandHandlerType.RemoteAdmin, "myCommand", "My description", "myAlias")]
public sealed class MyCommand : CommandBase
{

    protected override CommandResult Execute(ArraySegment<string> arguments, CommandSender sender)
    {
        return "Hello, World!"; // implicitly converted to a CommandResult
    }

}

Same command with overrides:

using System;
using Axwabo.CommandSystem;
using Axwabo.CommandSystem.Attributes;

[RemoteAdminCommand] // handler types must be specified using attributes
[CommandTarget(CommandHandlerType.RemoteAdmin)] // same as above
public sealed class MyCommand : CommandBase
{

    public override string Name => "myCommand";

    public override string Description => "My description";

    public override string[] Aliases { get; } = {"myAlias"};

    protected override CommandResult Execute(ArraySegment<string> arguments, CommandSender sender)
    {
        return CommandResult.Succeeded("Hello, World!");
    }

}

CommandResult

Multiple implicit casts make it easier to create a CommandResult

  • If a string begins with an !, it will be marked as a failed result (with the exclamation point trimmed from the start).
  • (string, bool) (bool, string)
  • Enum conversion happens using result translation
    • Up to 4 objects can be passed in using a tuple, for example: (Enum, object, object)

Execution sequence

  1. Ensure that the sender is a player if the command is player-only
  2. Permissions are checked
  3. A pre-execution filter may intercept the execution here
  4. Minimum argument count is checked
  5. The overriding command's Execute method is invoked

Player only

Add the PlayerOnlyCommandAttribute to the class to specify that the command can only be executed by players.

Implement the IPlayerOnlyCommand to specify a custom message when the sender is not a player. If the OnNotPlayer method returns CommandResult.Null, the execution sequence continues at step 4.

WARNING: only return CommandResult.Null if there won't be an attempt to get the player from the sender.

Pre execution filter

Implement the IPreExecutionFilter interface in the command class to add certain custom conditions before the command is executed. If the OnBeforeExecuted method returns CommandResult.Null, the execution sequence continues at step 4.

Command Handlers

Remote Admin

A command registered in this handler can be executed through the Text-Based Remote Admin. The base-game handler is RemoteAdminCommandHandler

The attribute is RemoteAdminCommand which uses CommandHandlerType.RemoteAdmin

Server Console

These commands can only executed by someone who has access to the dedicated server's console. Also known as the GameConsoleCommandHandler and not to be confused with the ClientCommandHandler

The attribute is ServerCommand which uses CommandHandlerType.ServerConsole

Client Console

Client commands are invoked by opening the game console (~ or `) on the client, then typing the command with a . prefix.

The attribute is ClientCommand which uses CommandHandlerType.Client

Permissions

IMPORTANT: The NWAPIPermissionSystem plugin is required if you're using the Northwood Plugin API instead of EXILED.

Most commands are useful if they are restricted to a group of users.

You can specify a required permission by creating your own permission checker and overriding the Permissions property or by adding one of the following attributes from the Axwabo.CommandSystem.Permissions namespace:

  • StringPermissionsAttribute
  • VanillaPermissionsAttribute
  • OneOfVanillaPermissionsAttribute
  • AllVanillaPermissionsAttribute

Multiple permission attributes can be specified and if there is more than one, they will be combined into a CombinedPermissionChecker which ensures all permissions are present.

Custom Permissions

A CommandBase itself may implement the IPermissionChecker interface and will also be resolved as a permission checker.

The CheckPermission method takes in a CommandSender and returns a CommandResult

If the returned result's Succeeded field is false, that signifies a failed permission check.

Attribute resolvers can be used to add permission checkers to a command.

Clone this wiki locally