Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented /msg, /tell and /w commands #254

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions pumpkin/src/command/commands/cmd_msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use async_trait::async_trait;
use pumpkin_core::text::{color::NamedColor, TextComponent};

use crate::command::{
args::{
arg_message::MsgArgConsumer, arg_players::PlayersArgumentConsumer, ConsumedArgs, FindArg,
},
tree::CommandTree,
tree_builder::argument,
CommandExecutor, CommandSender, InvalidTreeError,
};

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 targets = PlayersArgumentConsumer::find_arg(args, ARG_TARGET)?;
let msg = MsgArgConsumer::find_arg(args, ARG_MESSAGE)?;

let sender_message = targets
.iter()
.map(|target| format!("You whisper to {}: {}", target.gameprofile.name, msg))
.collect::<Vec<String>>()
.join("\n");
let sender_text = TextComponent::text(&sender_message)
.color_named(NamedColor::Gray)
.italic();

let recipient_message = format!("{sender_name} whispers to you: {msg}");
let recipient_text = TextComponent::text(&recipient_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;
}

Ok(())
}
}

pub fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION).with_child(
argument(ARG_TARGET, &PlayersArgumentConsumer)
.with_child(argument(ARG_MESSAGE, &MsgArgConsumer).execute(&MsgExecutor)),
)
}
1 change: 1 addition & 0 deletions pumpkin/src/command/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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;
Expand Down
3 changes: 2 additions & 1 deletion pumpkin/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_list, cmd_msg, cmd_pumpkin, cmd_say, cmd_stop, cmd_teleport, cmd_worldborder,
};
use dispatcher::InvalidTreeError;
use pumpkin_core::math::vector3::Vector3;
Expand Down Expand Up @@ -86,6 +86,7 @@ pub fn default_dispatcher<'a>() -> Arc<CommandDispatcher<'a>> {
dispatcher.register(cmd_give::init_command_tree());
dispatcher.register(cmd_list::init_command_tree());
dispatcher.register(cmd_clear::init_command_tree());
KaliemSB marked this conversation as resolved.
Show resolved Hide resolved
dispatcher.register(cmd_msg::init_command_tree());

Arc::new(dispatcher)
}
Expand Down