From 89640fe0c0990746f6e58cb0f5fc13b9da47e081 Mon Sep 17 00:00:00 2001 From: suprohub <125716028+suprohub@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:07:53 +0300 Subject: [PATCH] Command /say (#211) * add say command --- pumpkin/src/commands/cmd_say.rs | 48 +++++++++++++++++++++++++++++++++ pumpkin/src/commands/mod.rs | 2 ++ 2 files changed, 50 insertions(+) create mode 100644 pumpkin/src/commands/cmd_say.rs diff --git a/pumpkin/src/commands/cmd_say.rs b/pumpkin/src/commands/cmd_say.rs new file mode 100644 index 000000000..3a58d69b3 --- /dev/null +++ b/pumpkin/src/commands/cmd_say.rs @@ -0,0 +1,48 @@ +use super::{ + arg_simple::SimpleArgConsumer, + tree::CommandTree, + tree_builder::{argument, require}, + CommandExecutor, CommandSender, +}; +use async_trait::async_trait; +use pumpkin_core::text::TextComponent; +use pumpkin_protocol::client::play::CSystemChatMessage; + +const NAMES: [&str; 1] = ["say"]; + +const DESCRIPTION: &str = "Broadcast a message to all Players."; + +const ARG_MESSAGE: &str = "message"; + +struct SayExecutor {} + +#[async_trait] +impl CommandExecutor for SayExecutor { + async fn execute<'a>( + &self, + sender: &mut super::CommandSender<'a>, + server: &crate::server::Server, + args: &super::tree::ConsumedArgs<'a>, + ) -> Result<(), super::dispatcher::InvalidTreeError> { + let sender = match sender { + CommandSender::Console => "Console", + CommandSender::Rcon(_) => "Rcon", + CommandSender::Player(player) => &player.gameprofile.name, + }; + + server + .broadcast_packet_all(&CSystemChatMessage::new( + &TextComponent::text(&format!("[{}] {}", sender, args.get("message").unwrap())), + false, + )) + .await; + Ok(()) + } +} + +pub fn init_command_tree<'a>() -> CommandTree<'a> { + CommandTree::new(NAMES, DESCRIPTION).with_child( + require(&|sender| sender.permission_lvl() >= 2) + .with_child(argument(ARG_MESSAGE, &SimpleArgConsumer {}).execute(&SayExecutor {})), + ) +} diff --git a/pumpkin/src/commands/mod.rs b/pumpkin/src/commands/mod.rs index e3b8c4183..a1caab06f 100644 --- a/pumpkin/src/commands/mod.rs +++ b/pumpkin/src/commands/mod.rs @@ -19,6 +19,7 @@ mod cmd_help; mod cmd_kick; mod cmd_kill; mod cmd_pumpkin; +mod cmd_say; mod cmd_stop; mod cmd_worldborder; @@ -71,6 +72,7 @@ pub fn default_dispatcher<'a>() -> Arc> { let mut dispatcher = CommandDispatcher::default(); dispatcher.register(cmd_pumpkin::init_command_tree()); + dispatcher.register(cmd_say::init_command_tree()); dispatcher.register(cmd_gamemode::init_command_tree()); dispatcher.register(cmd_stop::init_command_tree()); dispatcher.register(cmd_help::init_command_tree());