-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add say command
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {})), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters