From 064fbb4322b8a8333bc46425d57f1c33c3d72d6e Mon Sep 17 00:00:00 2001 From: KaliemSB Date: Mon, 11 Nov 2024 23:19:16 +0000 Subject: [PATCH 1/5] Commands: implemented msg, tell and w --- pumpkin/src/command/commands/cmd_msg.rs | 71 +++++++++++++++++++++++++ pumpkin/src/command/commands/mod.rs | 2 +- pumpkin/src/command/mod.rs | 4 +- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 pumpkin/src/command/commands/cmd_msg.rs diff --git a/pumpkin/src/command/commands/cmd_msg.rs b/pumpkin/src/command/commands/cmd_msg.rs new file mode 100644 index 000000000..bbb95d1fd --- /dev/null +++ b/pumpkin/src/command/commands/cmd_msg.rs @@ -0,0 +1,71 @@ +use async_trait::async_trait; +use pumpkin_core::text::{color::NamedColor, TextComponent}; + +use crate::command::{ + args::{arg_message::MsgArgConsumer, arg_players::PlayersArgumentConsumer, Arg, ConsumedArgs}, + tree::CommandTree, + tree_builder::{argument, require}, + CommandExecutor, CommandSender, InvalidTreeError, +}; +use InvalidTreeError::InvalidConsumptionError; + +const NAMES: [&str; 3] = ["msg", "tell", "w"]; + +const DESCRIPTION: &str = "Sends a private message to one or more players."; + +const ARG_TARGET: &str = "target"; +const ARG_MESSAGE: &str = "message"; + +struct MsgExecutor; + +#[async_trait] +impl CommandExecutor for MsgExecutor { + async fn execute<'a>( + &self, + sender: &mut CommandSender<'a>, + _server: &crate::server::Server, + args: &ConsumedArgs<'a>, + ) -> Result<(), InvalidTreeError> { + let sender_name = match sender { + CommandSender::Console => "Console", + CommandSender::Rcon(_) => "Rcon", + CommandSender::Player(player) => &player.gameprofile.name.clone(), + }; + + let Some(Arg::Players(targets)) = args.get(&ARG_TARGET) else { + return Err(InvalidConsumptionError(Some(ARG_TARGET.into()))); + }; + + let Some(Arg::Msg(msg)) = args.get(ARG_MESSAGE) else { + return Err(InvalidConsumptionError(Some(ARG_MESSAGE.into()))); + }; + + for target_player in targets { + let recipient_message = format!("{sender_name} whispers to you: {msg}"); + let sender_message = + format!("you whisper to {}: {msg}", target_player.gameprofile.name); + + let recipient_text = TextComponent::text(&recipient_message) + .color_named(NamedColor::Gray) + .italic(); + + let sender_text = TextComponent::text(&sender_message) + .color_named(NamedColor::Gray) + .italic(); + + target_player.send_system_message(&recipient_text).await; + sender.send_message(sender_text).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_TARGET, &PlayersArgumentConsumer) + .with_child(argument(ARG_MESSAGE, &MsgArgConsumer).execute(&MsgExecutor)), + ), + ) +} diff --git a/pumpkin/src/command/commands/mod.rs b/pumpkin/src/command/commands/mod.rs index 330e46d50..8bff123c0 100644 --- a/pumpkin/src/command/commands/mod.rs +++ b/pumpkin/src/command/commands/mod.rs @@ -1,4 +1,3 @@ -pub mod cmd_clear; pub mod cmd_echest; pub mod cmd_gamemode; pub mod cmd_give; @@ -6,6 +5,7 @@ pub mod cmd_help; pub mod cmd_kick; pub mod cmd_kill; pub mod cmd_list; +pub mod cmd_msg; pub mod cmd_pumpkin; pub mod cmd_say; pub mod cmd_stop; diff --git a/pumpkin/src/command/mod.rs b/pumpkin/src/command/mod.rs index 2eda4f282..a58f4209c 100644 --- a/pumpkin/src/command/mod.rs +++ b/pumpkin/src/command/mod.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use args::ConsumedArgs; use async_trait::async_trait; use commands::{ - cmd_clear, cmd_echest, cmd_gamemode, cmd_give, cmd_help, cmd_kick, cmd_kill, cmd_list, + cmd_echest, cmd_gamemode, cmd_give, cmd_help, cmd_kick, cmd_kill, cmd_list, cmd_msg, cmd_pumpkin, cmd_say, cmd_stop, cmd_teleport, cmd_worldborder, }; use dispatcher::InvalidTreeError; @@ -84,7 +84,7 @@ pub fn default_dispatcher<'a>() -> Arc> { dispatcher.register(cmd_teleport::init_command_tree()); dispatcher.register(cmd_give::init_command_tree()); dispatcher.register(cmd_list::init_command_tree()); - dispatcher.register(cmd_clear::init_command_tree()); + dispatcher.register(cmd_msg::init_command_tree()); Arc::new(dispatcher) } From f59ab9ce0a3fb676f76d86ef93aa5b5d6a7a4f94 Mon Sep 17 00:00:00 2001 From: KaliemSB Date: Mon, 11 Nov 2024 23:40:29 +0000 Subject: [PATCH 2/5] chore: permission requirement removed --- pumpkin/src/command/commands/cmd_msg.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pumpkin/src/command/commands/cmd_msg.rs b/pumpkin/src/command/commands/cmd_msg.rs index bbb95d1fd..cbbbc45c4 100644 --- a/pumpkin/src/command/commands/cmd_msg.rs +++ b/pumpkin/src/command/commands/cmd_msg.rs @@ -4,7 +4,7 @@ use pumpkin_core::text::{color::NamedColor, TextComponent}; use crate::command::{ args::{arg_message::MsgArgConsumer, arg_players::PlayersArgumentConsumer, Arg, ConsumedArgs}, tree::CommandTree, - tree_builder::{argument, require}, + tree_builder::argument, CommandExecutor, CommandSender, InvalidTreeError, }; use InvalidTreeError::InvalidConsumptionError; @@ -63,9 +63,7 @@ impl CommandExecutor for MsgExecutor { pub fn init_command_tree<'a>() -> CommandTree<'a> { CommandTree::new(NAMES, DESCRIPTION).with_child( - require(&|sender| sender.permission_lvl() >= 2).with_child( - argument(ARG_TARGET, &PlayersArgumentConsumer) - .with_child(argument(ARG_MESSAGE, &MsgArgConsumer).execute(&MsgExecutor)), - ), + argument(ARG_TARGET, &PlayersArgumentConsumer) + .with_child(argument(ARG_MESSAGE, &MsgArgConsumer).execute(&MsgExecutor)), ) } From 9d7915b2e73e02c32a4232ec4c0d6fe610d646f7 Mon Sep 17 00:00:00 2001 From: KaliemSB Date: Tue, 12 Nov 2024 02:17:07 +0000 Subject: [PATCH 3/5] fix: missing clear command --- pumpkin/src/command/commands/mod.rs | 1 + pumpkin/src/command/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pumpkin/src/command/commands/mod.rs b/pumpkin/src/command/commands/mod.rs index 8bff123c0..1d12c8622 100644 --- a/pumpkin/src/command/commands/mod.rs +++ b/pumpkin/src/command/commands/mod.rs @@ -1,3 +1,4 @@ +pub mod cmd_clear; pub mod cmd_echest; pub mod cmd_gamemode; pub mod cmd_give; diff --git a/pumpkin/src/command/mod.rs b/pumpkin/src/command/mod.rs index a58f4209c..f50d69e1f 100644 --- a/pumpkin/src/command/mod.rs +++ b/pumpkin/src/command/mod.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use args::ConsumedArgs; use async_trait::async_trait; use commands::{ - cmd_echest, cmd_gamemode, cmd_give, cmd_help, cmd_kick, cmd_kill, cmd_list, cmd_msg, + cmd_clear, cmd_echest, cmd_gamemode, cmd_give, cmd_help, cmd_kick, cmd_kill, cmd_list, cmd_msg, cmd_pumpkin, cmd_say, cmd_stop, cmd_teleport, cmd_worldborder, }; use dispatcher::InvalidTreeError; @@ -84,6 +84,7 @@ pub fn default_dispatcher<'a>() -> Arc> { dispatcher.register(cmd_teleport::init_command_tree()); dispatcher.register(cmd_give::init_command_tree()); dispatcher.register(cmd_list::init_command_tree()); + dispatcher.register(cmd_clear::init_command_tree()); dispatcher.register(cmd_msg::init_command_tree()); Arc::new(dispatcher) From 5e55051cafdbc9a125e3c9bf09c6e97c3588b044 Mon Sep 17 00:00:00 2001 From: KaliemSB Date: Tue, 12 Nov 2024 02:17:34 +0000 Subject: [PATCH 4/5] chore: msg command improvements --- pumpkin/src/command/commands/cmd_msg.rs | 39 ++++++++++++------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/pumpkin/src/command/commands/cmd_msg.rs b/pumpkin/src/command/commands/cmd_msg.rs index cbbbc45c4..744db31f4 100644 --- a/pumpkin/src/command/commands/cmd_msg.rs +++ b/pumpkin/src/command/commands/cmd_msg.rs @@ -2,12 +2,13 @@ use async_trait::async_trait; use pumpkin_core::text::{color::NamedColor, TextComponent}; use crate::command::{ - args::{arg_message::MsgArgConsumer, arg_players::PlayersArgumentConsumer, Arg, ConsumedArgs}, + args::{ + arg_message::MsgArgConsumer, arg_players::PlayersArgumentConsumer, ConsumedArgs, FindArg, + }, tree::CommandTree, tree_builder::argument, CommandExecutor, CommandSender, InvalidTreeError, }; -use InvalidTreeError::InvalidConsumptionError; const NAMES: [&str; 3] = ["msg", "tell", "w"]; @@ -32,29 +33,27 @@ impl CommandExecutor for MsgExecutor { CommandSender::Player(player) => &player.gameprofile.name.clone(), }; - let Some(Arg::Players(targets)) = args.get(&ARG_TARGET) else { - return Err(InvalidConsumptionError(Some(ARG_TARGET.into()))); - }; + let targets = PlayersArgumentConsumer::find_arg(args, ARG_TARGET)?; + let msg = MsgArgConsumer::find_arg(args, ARG_MESSAGE)?; - let Some(Arg::Msg(msg)) = args.get(ARG_MESSAGE) else { - return Err(InvalidConsumptionError(Some(ARG_MESSAGE.into()))); - }; + let sender_message = targets + .iter() + .map(|target| format!("You whisper to {}: {}", target.gameprofile.name, msg)) + .collect::>() + .join("\n"); + let sender_text = TextComponent::text(&sender_message) + .color_named(NamedColor::Gray) + .italic(); - for target_player in targets { - let recipient_message = format!("{sender_name} whispers to you: {msg}"); - let sender_message = - format!("you whisper to {}: {msg}", target_player.gameprofile.name); + let recipient_message = format!("{sender_name} whispers to you: {msg}"); + let recipient_text = TextComponent::text(&recipient_message) + .color_named(NamedColor::Gray) + .italic(); - let recipient_text = TextComponent::text(&recipient_message) - .color_named(NamedColor::Gray) - .italic(); - - let sender_text = TextComponent::text(&sender_message) - .color_named(NamedColor::Gray) - .italic(); + sender.send_message(sender_text).await; + for target_player in targets { target_player.send_system_message(&recipient_text).await; - sender.send_message(sender_text).await; } Ok(()) From cf926fa5d7394cf7d66ca7b37c8b6df0c21abc10 Mon Sep 17 00:00:00 2001 From: KaliemSB Date: Tue, 12 Nov 2024 13:47:41 +0000 Subject: [PATCH 5/5] style: code style fixed --- pumpkin/src/command/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pumpkin/src/command/mod.rs b/pumpkin/src/command/mod.rs index b423311f8..9b0497cf9 100644 --- a/pumpkin/src/command/mod.rs +++ b/pumpkin/src/command/mod.rs @@ -4,7 +4,7 @@ use args::ConsumedArgs; use async_trait::async_trait; use commands::{ cmd_clear, cmd_craft, cmd_echest, cmd_gamemode, cmd_give, cmd_help, cmd_kick, cmd_kill, - cmd_list, cmd_pumpkin, cmd_say, cmd_stop, cmd_teleport, cmd_worldborder, cmd_msg + cmd_list, cmd_msg, cmd_pumpkin, cmd_say, cmd_stop, cmd_teleport, cmd_worldborder, }; use dispatcher::InvalidTreeError; use pumpkin_core::math::vector3::Vector3;