Skip to content

Commit

Permalink
Command /say (#211)
Browse files Browse the repository at this point in the history
* add say command
  • Loading branch information
suprohub authored Oct 29, 2024
1 parent abbb76a commit 89640fe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pumpkin/src/commands/cmd_say.rs
Original file line number Diff line number Diff line change
@@ -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 {})),
)
}
2 changes: 2 additions & 0 deletions pumpkin/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -71,6 +72,7 @@ pub fn default_dispatcher<'a>() -> Arc<CommandDispatcher<'a>> {
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());
Expand Down

0 comments on commit 89640fe

Please sign in to comment.