From 7ad4b227267e3bb2da99ef5d76c30cd54d040157 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Fri, 18 Nov 2022 21:52:09 -0600 Subject: Add functions to get ChatPacket author and content (#42) * Add functions to get ChatPacket author and content * add ChatPacket::username and ChatPacket::content --- azalea-client/src/chat.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ azalea-client/src/client.rs | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) (limited to 'azalea-client/src') diff --git a/azalea-client/src/chat.rs b/azalea-client/src/chat.rs index 24ccbfd5..3a0efa95 100755 --- a/azalea-client/src/chat.rs +++ b/azalea-client/src/chat.rs @@ -9,6 +9,8 @@ use azalea_protocol::packets::game::{ serverbound_chat_command_packet::ServerboundChatCommandPacket, serverbound_chat_packet::ServerboundChatPacket, }; +use lazy_static::lazy_static; +use regex::Regex; use std::time::{SystemTime, UNIX_EPOCH}; /// A chat packet, either a system message or a chat message. @@ -26,6 +28,53 @@ impl ChatPacket { ChatPacket::Player(p) => p.message(false), } } + + /// Determine the username of the sender and content of the message. This + /// does not preserve formatting codes. If it's not a player-sent chat + /// message or the sender couldn't be determined, the username part will be + /// None. + pub fn split_sender_and_content(&self) -> (Option, String) { + match self { + ChatPacket::Player(p) => ( + // If it's a player chat packet, then the sender and content + // are already split for us. + Some(p.chat_type.name.to_string()), + p.message.content(false).to_string(), + ), + ChatPacket::System(p) => { + let message = p.content.to_string(); + // Overlay messages aren't in chat + if p.overlay { + return (None, message); + } + // It's a system message, so we'll have to match the content + // with regex + lazy_static! { + static ref ANGLE_BRACKETS_RE: Regex = + Regex::new("^<([a-zA-Z_0-9]{1,16})> (.+)$").unwrap(); + } + if let Some(m) = ANGLE_BRACKETS_RE.captures(&message) { + return (Some(m[1].to_string()), m[2].to_string()); + } + + (None, message) + } + } + } + + /// Get the username of the sender of the message. If it's not a + /// player-sent chat message or the sender couldn't be determined, this + /// will be None. + pub fn username(&self) -> Option { + self.split_sender_and_content().0 + } + + /// Get the content part of the message as a string. This does not preserve + /// formatting codes. If it's not a player-sent chat message or the sender + /// couldn't be determined, this will contain the entire message. + pub fn content(&self) -> String { + self.split_sender_and_content().1 + } } impl Client { diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 326ba002..e271065c 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -64,7 +64,7 @@ pub enum Event { /// A player that you control that is currently in a Minecraft server. #[derive(Clone)] pub struct Client { - game_profile: GameProfile, + pub game_profile: GameProfile, pub read_conn: Arc>>, pub write_conn: Arc>>, pub player: Arc>, -- cgit v1.2.3