From e21e1b97bf9337e9f4747cd1b545b1b3a03e2ce7 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sat, 22 Feb 2025 21:45:26 -0600 Subject: Refactor azalea-client (#205) * start organizing packet_handling more by moving packet handlers into their own functions * finish writing all the handler functions for packets * use macro for generating match statement for packet handler functions * fix set_entity_data * update config state to also use handler functions * organize az-client file structure by moving things into plugins directory * fix merge issues --- azalea-client/src/plugins/chat/handler.rs | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 azalea-client/src/plugins/chat/handler.rs (limited to 'azalea-client/src/plugins/chat/handler.rs') diff --git a/azalea-client/src/plugins/chat/handler.rs b/azalea-client/src/plugins/chat/handler.rs new file mode 100644 index 00000000..d598acdb --- /dev/null +++ b/azalea-client/src/plugins/chat/handler.rs @@ -0,0 +1,61 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +use azalea_protocol::packets::{ + game::{s_chat::LastSeenMessagesUpdate, ServerboundChat, ServerboundChatCommand}, + Packet, +}; +use bevy_ecs::prelude::*; + +use super::ChatKind; +use crate::packet::game::SendPacketEvent; + +/// Send a chat packet to the server of a specific kind (chat message or +/// command). Usually you just want [`SendChatEvent`] instead. +/// +/// Usually setting the kind to `Message` will make it send a chat message even +/// if it starts with a slash, but some server implementations will always do a +/// command if it starts with a slash. +/// +/// If you're wondering why this isn't two separate events, it's so ordering is +/// preserved if multiple chat messages and commands are sent at the same time. +#[derive(Event)] +pub struct SendChatKindEvent { + pub entity: Entity, + pub content: String, + pub kind: ChatKind, +} + +pub fn handle_send_chat_kind_event( + mut events: EventReader, + mut send_packet_events: EventWriter, +) { + for event in events.read() { + let content = event + .content + .chars() + .filter(|c| !matches!(c, '\x00'..='\x1F' | '\x7F' | 'ยง')) + .take(256) + .collect::(); + let packet = match event.kind { + ChatKind::Message => ServerboundChat { + message: content, + timestamp: SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Time shouldn't be before epoch") + .as_millis() + .try_into() + .expect("Instant should fit into a u64"), + salt: azalea_crypto::make_salt(), + signature: None, + last_seen_messages: LastSeenMessagesUpdate::default(), + } + .into_variant(), + ChatKind::Command => { + // TODO: chat signing + ServerboundChatCommand { command: content }.into_variant() + } + }; + + send_packet_events.send(SendPacketEvent::new(event.entity, packet)); + } +} -- cgit v1.2.3