Pumpkin/pumpkin/src/command/commands/say.rs
Alexander Medvedev 71392b4a38 remove async_trait from Commands
This decreased compile time by 30%, Plugins also are not required anymore to use async_trait when adding custom Commands
2025-12-03 18:25:58 +01:00

48 lines
1.3 KiB
Rust

use pumpkin_data::world::SAY_COMMAND;
use pumpkin_util::text::TextComponent;
use crate::command::{
CommandError, CommandExecutor, CommandResult, CommandSender,
args::{Arg, ConsumedArgs, message::MsgArgConsumer},
tree::{CommandTree, builder::argument},
};
use CommandError::InvalidConsumption;
const NAMES: [&str; 1] = ["say"];
const DESCRIPTION: &str = "Broadcast a message to all Players.";
const ARG_MESSAGE: &str = "message";
struct Executor;
impl CommandExecutor for Executor {
fn execute<'a>(
&'a self,
sender: &'a CommandSender,
server: &'a crate::server::Server,
args: &'a ConsumedArgs<'a>,
) -> CommandResult<'a> {
Box::pin(async move {
let Some(Arg::Msg(msg)) = args.get(ARG_MESSAGE) else {
return Err(InvalidConsumption(Some(ARG_MESSAGE.into())));
};
server
.broadcast_message(
&TextComponent::text(msg.clone()),
&TextComponent::text(format!("{sender}")),
SAY_COMMAND,
None,
)
.await;
Ok(())
})
}
}
pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION)
.then(argument(ARG_MESSAGE, MsgArgConsumer).execute(Executor))
}