Skip to content

CommandRegistry

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

public sealed class CommandRegistry

Command registration, scoped to a plugin (plugin.Commands). Plugin commands are nodes of the server’s real command tree: they appear in the client’s command tree with argument types and suggestions, and run through the same dispatcher as vanilla commands. Everything registered here is removed again when the plugin unloads (the clients’ trees are refreshed once per tick).

Two forms. The tree form Register(root, aliases, description) takes a literal built with Command.Literal and Command.Argument: typed arguments, suggestions, permissions and aliases, shown in the client’s command tree. The flat form Register(name, handler, description) is a shortcut for a name literal followed by a greedy text argument; the handler reads CommandContext.Args. Plugin commands are removed when the plugin unloads. See Commands.

Register a flat command

Commands.Register("hello", ctx =>
{
ctx.Reply($"Hello, {(ctx.Args.Length > 0 ? ctx.Args[0] : "world")}!");
ctx.Result = ctx.Args.Length;
}, "Says hello");

Register a command tree

Commands.Register(Command.Literal("greet").Permission("hello.greet")
.Then(Command.Argument("name", Arg.Word())
.Suggests(ctx => new[] { "world", "steve" })
.Executes(ctx =>
{
ctx.Reply($"Hello, {ctx.GetString("name")}!");
return 1;
})),
description: "Greets someone");
Member Description
Register Registers a command tree.
RegisteredCommand Register(string name, CommandHandler handler, string description = "") Registers a flat command: the word name followed by anything.
public RegisteredCommand Register(CommandNode root, string[]? aliases = null, string description = "")

Registers a command tree.

Parameters

Name Type Description
root CommandNode The root literal; build it with Command.Literal.
aliases string[]? = null Extra names that run the same command (they appear in the client’s tree too).
description string = "" A one-line description for help listings.

Returns RegisteredCommand: The registration handle (dispose to unregister).

Throws

  • System.InvalidOperationException: The name is taken by a vanilla command or another plugin, or the server has no command engine.
public RegisteredCommand Register(string name, CommandHandler handler, string description = "")

Registers a flat command: the word name followed by anything. The handler reads CommandContext.Args. Prefer CommandRegistry.Register for typed arguments, suggestions and permissions.

Parameters

Name Type Description
name string The command name.
handler CommandHandler The command body.
description string = "" A one-line description.

Returns RegisteredCommand: The registration handle.

CommandHandler · CommandNode · RegisteredCommand