docs: rewrite README for the TextCommandCallingArgs API
This commit is contained in:
@@ -16,7 +16,7 @@ Add it as a dependency in your `modinfo.json`:
|
|||||||
```json
|
```json
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"game": "1.22.3",
|
"game": "1.22.3",
|
||||||
"commandhook": "1.0.0"
|
"commandhook": "2.0.0"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -31,16 +31,24 @@ A listener provides:
|
|||||||
- `Commands`, the command names you want to watch, without the leading slash
|
- `Commands`, the command names you want to watch, without the leading slash
|
||||||
- `Registration`, a `Before` delegate, an `After` delegate, or both
|
- `Registration`, a `Before` delegate, an `After` delegate, or both
|
||||||
|
|
||||||
`Before` runs before the command executes. Set `data.Cancel = true` inside it
|
`Before` receives the engine's own `TextCommandCallingArgs` for the
|
||||||
to stop the command from running. If you cancel, any remaining `Before`
|
invocation, live and unmodified. Return a non-null `TextCommandResult` to
|
||||||
listeners for that command are skipped, and `After` never fires for that
|
cancel the command and report that result to the caller, use the engine's
|
||||||
invocation.
|
own factories, e.g. `TextCommandResult.Error(...)` for a visible reason, or
|
||||||
|
`TextCommandResult.Deferred` to cancel silently (per its own doc comment,
|
||||||
|
this prints no output). Return `null` to let the command run normally. If
|
||||||
|
you cancel, any remaining `Before` listeners for that command are skipped,
|
||||||
|
and `After` never fires for that invocation.
|
||||||
|
|
||||||
`After` runs once the command has produced a result. It only fires on the
|
`After` receives the same `TextCommandCallingArgs` object plus the
|
||||||
path where the command actually ran.
|
`TextCommandResult` the command actually produced. It only fires on the
|
||||||
|
path where the command ran.
|
||||||
|
|
||||||
`CommandData` is passed by `ref` the whole way through, no allocation per
|
### A note on `TextCommandCallingArgs` at Before vs After
|
||||||
command.
|
|
||||||
|
This is the engine's own live object, not a copy, what's safe to read
|
||||||
|
depends on timing and on how the specific command you're watching is
|
||||||
|
implemented internally.
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
@@ -56,23 +64,26 @@ public class MyListener : ICommandHookListener
|
|||||||
// Wire up Before, After, or both.
|
// Wire up Before, After, or both.
|
||||||
public CommandRegistration Registration => new(Before, After);
|
public CommandRegistration Registration => new(Before, After);
|
||||||
|
|
||||||
private void Before(ref CommandData data)
|
private TextCommandResult? Before(TextCommandCallingArgs args)
|
||||||
{
|
{
|
||||||
// data.Sender is null for console invocations.
|
// args.Caller.Player is null for console invocations.
|
||||||
if (data.Sender != null && !IsAllowed(data.Sender))
|
if (args.Caller.Player is IServerPlayer player && !IsAllowed(player))
|
||||||
{
|
{
|
||||||
// Stops the command from running and skips any remaining
|
// Cancels the command, skips any remaining Before listeners,
|
||||||
// Before listeners. After never fires for this invocation.
|
// and After never fires for this invocation. The caller sees
|
||||||
data.Cancel = true;
|
// this exact result.
|
||||||
|
return TextCommandResult.Error("You're not allowed to do that", "notallowed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void After(ref CommandData data, TextCommandResult result)
|
private void After(TextCommandCallingArgs args, TextCommandResult result)
|
||||||
{
|
{
|
||||||
// Only runs if nothing cancelled. result is whatever the command
|
// Only runs if nothing cancelled. result is whatever the command
|
||||||
// actually produced, check result.Status for success/error/deferred.
|
// actually produced, check result.Status for success/error/deferred.
|
||||||
if (result.Status != EnumCommandStatus.Success)
|
if (result.Status != EnumCommandStatus.Success)
|
||||||
Logger.Warn($"/{data.CommandName} failed: {result.StatusMessage}");
|
Logger.Warn($"A watched command failed: {result.StatusMessage}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user