levilamina::command¶
Custom commands.
Command registration is one way
Bedrock offers no route to deregister a command, so a registered command lives until the server
stops. While a mod is disabled the host mutes the callback rather than removing it, and re-
enabling resumes it. There is therefore no unregister and no handle that deregisters on drop,
since such a handle would suggest it can be undone when it cannot.
It follows that registering a command inside on_enable has to survive being called several
times, as a hot reload does. A re-registration under the same name only swaps the callback and
does not rebuild the command.
Two shapes
register takes the whole raw line, which suits parsing it yourself. CommandBuilder
declares typed overloads that the engine parses and completes, so a player sees argument hints
on the client.
Functions¶
command::register¶
| Rust | |
|---|---|
Registers a command that takes the whole raw line.
Invocation::raw in the callback is everything after /name, parsed by you.
- Parameters:
- name :
&str - description :
&str - permission :
CommandPermission - handler :
impl FnMut(&mut Invocation<'_>) + Send + 'static
- name :
- Return type:
Result<()> - Slots:
register_command
command::builder¶
| Rust | |
|---|---|
Begins declaring a command with overloads.
- Parameters:
- name :
impl Into<String> - description :
impl Into<String> - permission :
CommandPermission
- name :
- Return type:
CommandBuilder
command::register_enum¶
| Rust | |
|---|---|
Registers a static enum for a ParamType::Enum parameter to reference.
- Parameters:
- name :
&str - values :
&[(&str, u64)]
- name :
- Return type:
Result<()> - Slots:
register_command_enum
command::register_soft_enum¶
| Rust | |
|---|---|
Registers a soft enum that can change at runtime, for a ParamType::SoftEnum
parameter to reference.
- Parameters:
- name :
&str - values :
&[&str]
- name :
- Return type:
Result<()> - Slots:
register_command_soft_enum
command::update_soft_enum¶
| Rust | |
|---|---|
- Parameters:
- name :
&str - op :
SoftEnumOp - values :
&[&str]
- name :
- Return type:
Result<()> - Slots:
update_command_soft_enum
Invocation¶
One command invocation.
Invocation::raw¶
| Rust | |
|---|---|
The raw argument text. For a command registered through CommandBuilder this is the
argument SNBT.
- Return type:
&str
Invocation::success¶
| Rust | |
|---|---|
Returns one success output.
- Parameters:
- msg :
&str
- msg :
Invocation::error¶
| Rust | |
|---|---|
Returns one error output. It is a separate channel from success, the client colors it differently, and a failed command must not use the success channel, which would make a command block decide wrongly.
- Parameters:
- msg :
&str
- msg :
Invocation::origin¶
| Rust | |
|---|---|
The origin.
A command from CommandBuilder carries {name,type,dim,x,y,z}. One from
register carries only the name, so kind is -1 there and CommandOrigin::is_console
and CommandOrigin::player_name cannot tell who sent it; a command that needs to
know is registered through CommandBuilder. The two shapes are told apart by
structure: a bare word such as Steve is itself valid SNBT, so a successful parse
alone does not mean the structured form.
- Return type:
CommandOrigin
Invocation::overload¶
| Rust | |
|---|---|
Which overload matched. Only a command registered through CommandBuilder has one.
- Return type:
Option<i32>
Invocation::arg¶
| Rust | |
|---|---|
Reads a named argument. An omitted optional argument gives None.
- Parameters:
- name :
&str
- name :
- Return type:
Option<&NbtValue>
Invocation::arg_str¶
| Rust | |
|---|---|
- Parameters:
- name :
&str
- name :
- Return type:
Option<&str>
Invocation::arg_i64¶
| Rust | |
|---|---|
- Parameters:
- name :
&str
- name :
- Return type:
Option<i64>
Invocation::arg_f64¶
| Rust | |
|---|---|
- Parameters:
- name :
&str
- name :
- Return type:
Option<f64>
Invocation::arg_bool¶
| Rust | |
|---|---|
- Parameters:
- name :
&str
- name :
- Return type:
Option<bool>
OverloadBuilder¶
The declaration of one overload.
- Implements:
Debug,Clone,Default
OverloadBuilder::required¶
| Rust | |
|---|---|
- Parameters:
- name :
&str - kind :
ParamType
- name :
- Return type:
OverloadBuilder
OverloadBuilder::optional¶
| Rust | |
|---|---|
- Parameters:
- name :
&str - kind :
ParamType
- name :
- Return type:
OverloadBuilder
OverloadBuilder::text¶
| Rust | |
|---|---|
A literal: a fixed word the player types, and a node in the command tree.
Why this is not an enum with one value
An Enum parameter carries EnumAutocompleteExpansion, so the client lists its
values, but its data type is Enum and not ChainedSubcommand: there is no tree
node, and the client has nothing to narrow as the player types. A literal is what
/scoreboard objectives add is made of, and it is the only form that filters.
The word comes back in the arguments under name, so the handler reads it the
same way as any other parameter, rather than keying on an overload index that
moves when an overload is inserted above it.
Sibling words that take the same arguments are one enum instead (CONTRACT.md 6.1).
- Parameters:
- name :
&str - literal :
&str
- name :
- Return type:
OverloadBuilder
OverloadBuilder::required_enum¶
| Rust | |
|---|---|
- Parameters:
- name :
&str - kind :
ParamType - enum_name :
&str
- name :
- Return type:
OverloadBuilder
OverloadBuilder::optional_enum¶
| Rust | |
|---|---|
- Parameters:
- name :
&str - kind :
ParamType - enum_name :
&str
- name :
- Return type:
OverloadBuilder
CommandOrigin¶
Where a command came from.
- Implements:
Debug,Clone,PartialEq
CommandOrigin::is_console¶
| Rust | |
|---|---|
- Return type:
bool
CommandOrigin::player_name¶
| Rust | |
|---|---|
The player name when the origin is a player.
Note this is a name and not an identity: a permission decision uses kind plus a
own player table, for the reason crate::sel gives.
- Return type:
Option<&str>
CommandBuilder¶
A command with typed overloads.
Each overload parses a different input: two that accept the same words leave the engine to pick one, and the handler cannot rely on which (contract Β§6.1). Sibling words with the same arguments are one enum parameter; a word with arguments of its own is a literal.
CommandBuilder::overload¶
| Rust | |
|---|---|
- Parameters:
- build :
impl FnOnce(OverloadBuilder) -> OverloadBuilder,
- build :
- Return type:
CommandBuilder
CommandBuilder::register¶
| Rust | |
|---|---|
Registers it. At least one overload is required, since the host refuses outright with none, so this stops it earlier and says why rather than leaving a bare registration failure to be guessed at.
- Parameters:
- handler :
impl FnMut(&mut Invocation<'_>) + Send + 'static
- handler :
- Return type:
Result<()> - Slots:
register_command_ex
CommandPermission¶
| Rust | |
|---|---|
The permission a command needs. The values mirror CommandPermissionLevel.
- Implements:
Debug,Clone,Copy,PartialEq,Eq
CommandPermission::as_i32¶
| Rust | |
|---|---|
- Return type:
i32
ParamType¶
| Rust | |
|---|---|
The type of one parameter inside one overload.
Enum and SoftEnum also need an enum name, declared through the
OverloadBuilder::required_enum pair of methods.
- Implements:
Debug,Clone,Copy,PartialEq,Eq
ParamType::as_str¶
| Rust | |
|---|---|
The spelling on the ABI. The host dispatches on this string and a typo drops the whole overload.
- Return type:
&'static str
SoftEnumOp¶
Changes the values of a soft enum.
- Implements:
Debug,Clone,Copy,PartialEq,Eq