azalea_client/plugins/packet/config/
events.rs1use std::sync::Arc;
2
3use azalea_protocol::packets::{
4 Packet,
5 config::{ClientboundConfigPacket, ServerboundConfigPacket},
6};
7use bevy_ecs::prelude::*;
8use tracing::{debug, error};
9
10use crate::{InConfigState, connection::RawConnection};
11
12#[derive(Message, Debug, Clone)]
13pub struct ReceiveConfigPacketEvent {
14 pub entity: Entity,
16 pub packet: Arc<ClientboundConfigPacket>,
18}
19
20#[derive(EntityEvent, Clone)]
23pub struct SendConfigPacketEvent {
24 #[event_target]
25 pub sent_by: Entity,
26 pub packet: ServerboundConfigPacket,
27}
28impl SendConfigPacketEvent {
29 pub fn new(sent_by: Entity, packet: impl Packet<ServerboundConfigPacket>) -> Self {
30 let packet = packet.into_variant();
31 Self { sent_by, packet }
32 }
33}
34
35pub fn handle_outgoing_packets_observer(
36 send_config_packet: On<SendConfigPacketEvent>,
37 mut query: Query<(&mut RawConnection, Option<&InConfigState>)>,
38) {
39 if let Ok((mut raw_conn, in_configuration_state)) = query.get_mut(send_config_packet.sent_by) {
40 if in_configuration_state.is_none() {
41 error!(
42 "Tried to send a configuration packet {:?} while not in configuration state",
43 send_config_packet.packet
44 );
45 return;
46 }
47 debug!("Sending config packet: {:?}", send_config_packet.packet);
48 if let Err(e) = raw_conn.write(send_config_packet.packet.clone()) {
49 error!("Failed to send packet: {e}");
50 }
51 }
52}
53
54#[derive(Event, Debug, Clone)]
62pub struct ConfigPingEvent {
63 pub entity: Entity,
64 pub packet: azalea_protocol::packets::config::ClientboundPing,
65}