basalt_api/command/dispatch.rs
1//! Command trait for server commands.
2//!
3//! Each command implements this trait and is registered on a
4//! [`CommandRegistry`](super::CommandRegistry). When a player
5//! types `/name args`, the registry looks up the command by name
6//! and calls `execute` with the arguments and context.
7
8use crate::context::Context;
9
10use super::args::CommandArgs;
11
12/// A server command that can be executed by players or the console.
13///
14/// # Example
15///
16/// ```ignore
17/// use basalt_api::command::Command;
18/// use crate::context::Context;
19///
20/// pub struct PingCommand;
21///
22/// impl Command for PingCommand {
23/// fn name(&self) -> &str { "ping" }
24/// fn description(&self) -> &str { "Responds with pong" }
25/// fn execute(&self, _args: &CommandArgs, ctx: &dyn Context) {
26/// ctx.chat().send("Pong!");
27/// }
28/// }
29/// ```
30pub trait Command: Send + Sync {
31 /// The command name without the leading `/`.
32 fn name(&self) -> &str;
33
34 /// A short description for the help listing.
35 fn description(&self) -> &str;
36
37 /// Executes the command with parsed arguments.
38 fn execute(&self, args: &CommandArgs, ctx: &dyn Context);
39}