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, local_player::WorldHolder};
11
12#[derive(Clone, Debug, Message)]
13pub struct ReceiveConfigPacketEvent {
14 pub entity: Entity,
16 pub packet: Arc<ClientboundConfigPacket>,
18}
19
20#[derive(Clone, EntityEvent)]
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<(
38 &mut RawConnection,
39 Option<&WorldHolder>,
40 Option<&InConfigState>,
41 )>,
42) {
43 if let Ok((mut raw_conn, world, in_configuration_state)) =
44 query.get_mut(send_config_packet.sent_by)
45 {
46 if in_configuration_state.is_none() {
47 error!(
48 "Tried to send a configuration packet {:?} while not in configuration state",
49 send_config_packet.packet
50 );
51 return;
52 }
53 debug!("Sending config packet: {:?}", send_config_packet.packet);
54 let mut write_fn = |ctx: azalea_buf::Ctx| {
55 if let Err(e) = raw_conn.write(ctx, send_config_packet.packet.clone()) {
56 error!("Failed to send packet: {e}");
57 }
58 };
59 if let Some(world) = world {
60 write_fn(azalea_buf::Ctx::new(&world.shared.read().registries));
61 } else {
62 write_fn(azalea_buf::Ctx::default());
63 };
64 }
65}
66
67#[derive(Clone, Debug, Event)]
75pub struct ConfigPingEvent {
76 pub entity: Entity,
77 pub packet: azalea_protocol::packets::config::ClientboundPing,
78}