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

Add masked chat type/event #118

Merged
merged 2 commits into from
Dec 4, 2023
Merged
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
14 changes: 14 additions & 0 deletions azalea-client/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use azalea_chat::FormattedText;
use azalea_protocol::packets::game::{
clientbound_disguised_chat_packet::ClientboundMaskedChatPacket,
clientbound_player_chat_packet::ClientboundPlayerChatPacket,
clientbound_system_chat_packet::ClientboundSystemChatPacket,
serverbound_chat_command_packet::ServerboundChatCommandPacket,
Expand Down Expand Up @@ -30,6 +31,7 @@ use crate::{
pub enum ChatPacket {
System(Arc<ClientboundSystemChatPacket>),
Player(Arc<ClientboundPlayerChatPacket>),
Masked(Arc<ClientboundMaskedChatPacket>),
}

macro_rules! regex {
Expand All @@ -45,6 +47,7 @@ impl ChatPacket {
match self {
ChatPacket::System(p) => p.content.clone(),
ChatPacket::Player(p) => p.message(),
ChatPacket::Masked(p) => p.message.clone(),
}
}

Expand Down Expand Up @@ -72,6 +75,16 @@ impl ChatPacket {
return (Some(m[1].to_string()), m[2].to_string());
}

(None, message)
}
ChatPacket::Masked(p) => {
let message = p.message.to_string();
// It's a system message, so we'll have to match the content
// with regex
if let Some(m) = regex!("^<([a-zA-Z_0-9]{1,16})> (.+)$").captures(&message) {
return (Some(m[1].to_string()), m[2].to_string());
}

(None, message)
}
}
Expand All @@ -91,6 +104,7 @@ impl ChatPacket {
match self {
ChatPacket::System(_) => None,
ChatPacket::Player(m) => Some(m.sender),
ChatPacket::Masked(_) => None,
}
}

Expand Down
13 changes: 12 additions & 1 deletion azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,18 @@ pub fn process_packet_events(ecs: &mut World) {
ClientboundGamePacket::TabList(_) => {}
ClientboundGamePacket::TagQuery(_) => {}
ClientboundGamePacket::TakeItemEntity(_) => {}
ClientboundGamePacket::DisguisedChat(_) => {}
ClientboundGamePacket::MaskedChat(p) => {
debug!("Got masked chat packet {p:?}");

let mut system_state: SystemState<EventWriter<ChatReceivedEvent>> =
SystemState::new(ecs);
let mut chat_events = system_state.get_mut(ecs);

chat_events.send(ChatReceivedEvent {
entity: player_entity,
packet: ChatPacket::Masked(Arc::new(p.clone())),
});
}
ClientboundGamePacket::Bundle(_) => {}
ClientboundGamePacket::DamageEvent(_) => {}
ClientboundGamePacket::HurtAnimation(_) => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use azalea_buf::McBuf;
use azalea_chat::FormattedText;
use azalea_protocol_macros::ClientboundGamePacket;

#[derive(Clone, Debug, McBuf, ClientboundGamePacket)]
pub struct ClientboundDisguisedChatPacket {
#[derive(Clone, Debug, McBuf, ClientboundGamePacket, PartialEq)]
pub struct ClientboundMaskedChatPacket {
pub message: FormattedText,
pub chat_type: ChatTypeBound,
}
2 changes: 1 addition & 1 deletion azalea-protocol/src/packets/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ declare_state_packets!(
0x19: clientbound_damage_event_packet::ClientboundDamageEventPacket,
0x1a: clientbound_delete_chat_packet::ClientboundDeleteChatPacket,
0x1b: clientbound_disconnect_packet::ClientboundDisconnectPacket,
0x1c: clientbound_disguised_chat_packet::ClientboundDisguisedChatPacket,
0x1c: clientbound_disguised_chat_packet::ClientboundMaskedChatPacket,
0x1d: clientbound_entity_event_packet::ClientboundEntityEventPacket,
0x1e: clientbound_explode_packet::ClientboundExplodePacket,
0x1f: clientbound_forget_level_chunk_packet::ClientboundForgetLevelChunkPacket,
Expand Down
Loading