pub struct CommandBuilder;
Expand description
A builder for a command.
§Example
A basic /msg
command:
use quill_prototype::{CommandBuilder, State, EntityRef, SysResult};
CommandBuilder::new("msg")
.alias("m")
.build(on_msg, &mut setup);
fn on_msg(state: &mut State, sender: EntityRef, args: &[&str]) -> SysResult {
if args.len() < 1 {
sender.send_message("Usage: /msg <player> [message...]")?;
// Note that a command executor should only return an `Err`
// when an internal error occurs, not when user input is incorrect.
// Returning an error will
// send the player a "an internal error occurred while trying to execute
// this command" message and print the error to the console.
// Returning an `Err` is like throwing
// an exception in Bukkit.
return Ok(());
}
let target = match state.player_by_name(args[0]) {
Some(player) => player,
None => {
sender.send_message(&format!("Player {} not found.", args[0]));
return Ok(());
}
};
// Build the message by accumulating `args`.
let mut message = String::new();
for arg in &args[1..] {
message += arg;
message.push(' ');
}
target.send_message(&message)?;
Ok(())
}
Implementations§
Source§impl CommandBuilder
impl CommandBuilder
Sourcepub fn new(_name: &str) -> Self
pub fn new(_name: &str) -> Self
Creates a new CommandBuilder
for a command with
the given name.
§Example
Create a CommandBuilder
for the /msg
command:
use quill_prototype::CommandBuilder;
CommandBuilder::new("msg");
(Note the absence of a preceding slash.)
Sourcepub fn build(&mut self, _executor: CommandExecutor, _setup: &mut Setup)
pub fn build(&mut self, _executor: CommandExecutor, _setup: &mut Setup)
Builds this CommandBuilder
.
This function takes:
- The function called when the command is executed
- The
Setup
passed to your plugin’ssetup
function and registers the command with the server.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CommandBuilder
impl RefUnwindSafe for CommandBuilder
impl Send for CommandBuilder
impl Sync for CommandBuilder
impl Unpin for CommandBuilder
impl UnwindSafe for CommandBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more