Skip to content

Commit

Permalink
revert packet name to Disguised and make it more like ChatPacket::Player
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Dec 4, 2023
1 parent 888c276 commit 797dd91
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 37 deletions.
36 changes: 16 additions & 20 deletions azalea-client/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use azalea_chat::FormattedText;
use azalea_protocol::packets::game::{
clientbound_disguised_chat_packet::ClientboundMaskedChatPacket,
clientbound_disguised_chat_packet::ClientboundDisguisedChatPacket,
clientbound_player_chat_packet::ClientboundPlayerChatPacket,
clientbound_system_chat_packet::ClientboundSystemChatPacket,
serverbound_chat_command_packet::ServerboundChatCommandPacket,
Expand Down Expand Up @@ -31,7 +31,7 @@ use crate::{
pub enum ChatPacket {
System(Arc<ClientboundSystemChatPacket>),
Player(Arc<ClientboundPlayerChatPacket>),
Masked(Arc<ClientboundMaskedChatPacket>),
Disguised(Arc<ClientboundDisguisedChatPacket>),
}

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

Expand All @@ -57,12 +57,6 @@ impl ChatPacket {
/// None.
pub fn split_sender_and_content(&self) -> (Option<String>, 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.body.content.clone(),
),
ChatPacket::System(p) => {
let message = p.content.to_string();
// Overlay messages aren't in chat
Expand All @@ -77,16 +71,18 @@ impl ChatPacket {

(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)
}
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.body.content.clone(),
),
ChatPacket::Disguised(p) => (
// disguised chat packets are basically the same as player chat packets but without
// the chat signing things
Some(p.chat_type.name.to_string()),
p.message.to_string(),
),
}
}

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

Expand Down
24 changes: 12 additions & 12 deletions azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,18 @@ pub fn process_packet_events(ecs: &mut World) {
packet: ChatPacket::System(Arc::new(p.clone())),
});
}
ClientboundGamePacket::DisguisedChat(p) => {
debug!("Got disguised 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::Disguised(Arc::new(p.clone())),
});
}
ClientboundGamePacket::Sound(_p) => {
// debug!("Got sound packet {p:?}");
}
Expand Down Expand Up @@ -1382,18 +1394,6 @@ pub fn process_packet_events(ecs: &mut World) {
ClientboundGamePacket::TabList(_) => {}
ClientboundGamePacket::TagQuery(_) => {}
ClientboundGamePacket::TakeItemEntity(_) => {}
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
@@ -1,10 +1,41 @@
use super::clientbound_player_chat_packet::ChatTypeBound;
use azalea_buf::McBuf;
use azalea_chat::FormattedText;
use azalea_chat::{
translatable_component::{StringOrComponent, TranslatableComponent},
FormattedText,
};
use azalea_protocol_macros::ClientboundGamePacket;

// A disguised chat packet is basically the same as a normal
// [`ClientboundPlayerChatPacket`], except that it doesn't have any of the chat
// signing things. Vanilla servers use this when messages are sent from the
// console.
#[derive(Clone, Debug, McBuf, ClientboundGamePacket, PartialEq)]
pub struct ClientboundMaskedChatPacket {
pub struct ClientboundDisguisedChatPacket {
pub message: FormattedText,
pub chat_type: ChatTypeBound,
}

impl ClientboundDisguisedChatPacket {
/// Get the full message, including the sender part.
#[must_use]
pub fn message(&self) -> FormattedText {
let sender = self.chat_type.name.clone();
let content = self.message.clone();
let target = self.chat_type.target_name.clone();

let translation_key = self.chat_type.chat_type.chat_translation_key();

let mut args = vec![
StringOrComponent::FormattedText(sender),
StringOrComponent::FormattedText(content),
];
if let Some(target) = target {
args.push(StringOrComponent::FormattedText(target));
}

let component = TranslatableComponent::new(translation_key.to_string(), args);

FormattedText::Translatable(component)
}
}
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::ClientboundMaskedChatPacket,
0x1c: clientbound_disguised_chat_packet::ClientboundDisguisedChatPacket,
0x1d: clientbound_entity_event_packet::ClientboundEntityEventPacket,
0x1e: clientbound_explode_packet::ClientboundExplodePacket,
0x1f: clientbound_forget_level_chunk_packet::ClientboundForgetLevelChunkPacket,
Expand Down
4 changes: 2 additions & 2 deletions azalea-world/src/chunk_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ impl PartialChunkStorage {
}

/// Set a chunk in the shared storage and reference it from the limited
/// storage. Use [`Self::set_with_shared_reference`] if you already have
/// an `Arc<RwLock<Chunk>>`.
/// storage. Use [`Self::limited_set`] if you already have an
/// `Arc<RwLock<Chunk>>`.
///
/// # Panics
/// If the chunk is not in the render distance.
Expand Down

0 comments on commit 797dd91

Please sign in to comment.