azalea_client/plugins/packet/config/
events.rs

1use 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    /// The client entity that received the packet.
15    pub entity: Entity,
16    /// The packet that was actually received.
17    pub packet: Arc<ClientboundConfigPacket>,
18}
19
20/// An event for sending a packet to the server while we're in the
21/// `configuration` state.
22#[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/// A Bevy trigger that's sent when our client receives a [`ClientboundPing`]
55/// packet in the config state.
56///
57/// Also see [`GamePingEvent`].
58///
59/// [`ClientboundPing`]: azalea_protocol::packets::config::ClientboundPing
60/// [`GamePingEvent`]: crate::packet::game::GamePingEvent
61#[derive(Event, Debug, Clone)]
62pub struct ConfigPingEvent {
63    pub entity: Entity,
64    pub packet: azalea_protocol::packets::config::ClientboundPing,
65}