aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/packet/config/events.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2025-02-22 21:45:26 -0600
committerGitHub <noreply@github.com>2025-02-22 21:45:26 -0600
commite21e1b97bf9337e9f4747cd1b545b1b3a03e2ce7 (patch)
treeadd6f8bfce40d0c07845d8aa4c9945a0b918444c /azalea-client/src/plugins/packet/config/events.rs
parentf8130c3c92946d2293634ba4e252d6bc93026c3c (diff)
downloadazalea-drasl-e21e1b97bf9337e9f4747cd1b545b1b3a03e2ce7.tar.xz
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
Diffstat (limited to 'azalea-client/src/plugins/packet/config/events.rs')
-rw-r--r--azalea-client/src/plugins/packet/config/events.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/azalea-client/src/plugins/packet/config/events.rs b/azalea-client/src/plugins/packet/config/events.rs
new file mode 100644
index 00000000..6b647d74
--- /dev/null
+++ b/azalea-client/src/plugins/packet/config/events.rs
@@ -0,0 +1,90 @@
+use std::io::Cursor;
+
+use azalea_protocol::{
+ packets::{
+ config::{ClientboundConfigPacket, ServerboundConfigPacket},
+ Packet,
+ },
+ read::deserialize_packet,
+};
+use bevy_ecs::prelude::*;
+use tracing::{debug, error};
+
+use crate::{raw_connection::RawConnection, InConfigState};
+
+#[derive(Event, Debug, Clone)]
+pub struct ReceiveConfigPacketEvent {
+ /// The client entity that received the packet.
+ pub entity: Entity,
+ /// The packet that was actually received.
+ pub packet: ClientboundConfigPacket,
+}
+
+/// An event for sending a packet to the server while we're in the
+/// `configuration` state.
+#[derive(Event)]
+pub struct SendConfigPacketEvent {
+ pub sent_by: Entity,
+ pub packet: ServerboundConfigPacket,
+}
+impl SendConfigPacketEvent {
+ pub fn new(sent_by: Entity, packet: impl Packet<ServerboundConfigPacket>) -> Self {
+ let packet = packet.into_variant();
+ Self { sent_by, packet }
+ }
+}
+
+pub fn handle_send_packet_event(
+ mut send_packet_events: EventReader<SendConfigPacketEvent>,
+ mut query: Query<(&mut RawConnection, Option<&InConfigState>)>,
+) {
+ for event in send_packet_events.read() {
+ if let Ok((raw_conn, in_configuration_state)) = query.get_mut(event.sent_by) {
+ if in_configuration_state.is_none() {
+ error!(
+ "Tried to send a configuration packet {:?} while not in configuration state",
+ event.packet
+ );
+ continue;
+ }
+ debug!("Sending packet: {:?}", event.packet);
+ if let Err(e) = raw_conn.write_packet(event.packet.clone()) {
+ error!("Failed to send packet: {e}");
+ }
+ }
+ }
+}
+
+pub fn send_packet_events(
+ query: Query<(Entity, &RawConnection), With<InConfigState>>,
+ mut packet_events: ResMut<Events<ReceiveConfigPacketEvent>>,
+) {
+ // we manually clear and send the events at the beginning of each update
+ // since otherwise it'd cause issues with events in process_packet_events
+ // running twice
+ packet_events.clear();
+ for (player_entity, raw_conn) in &query {
+ let packets_lock = raw_conn.incoming_packet_queue();
+ let mut packets = packets_lock.lock();
+ if !packets.is_empty() {
+ for raw_packet in packets.iter() {
+ let packet = match deserialize_packet::<ClientboundConfigPacket>(&mut Cursor::new(
+ raw_packet,
+ )) {
+ Ok(packet) => packet,
+ Err(err) => {
+ error!("failed to read packet: {err:?}");
+ debug!("packet bytes: {raw_packet:?}");
+ continue;
+ }
+ };
+ packet_events.send(ReceiveConfigPacketEvent {
+ entity: player_entity,
+ packet,
+ });
+ }
+ // clear the packets right after we read them
+ packets.clear();
+ }
+ }
+}