azalea_client/plugins/packet/login/
events.rs

1use std::sync::Arc;
2
3use azalea_protocol::packets::{
4    Packet,
5    login::{
6        ClientboundCustomQuery, ClientboundHello, ClientboundLoginPacket, ServerboundLoginPacket,
7    },
8};
9use bevy_ecs::prelude::*;
10use tracing::{debug, error};
11
12use super::InLoginState;
13use crate::{account::Account, connection::RawConnection};
14
15#[derive(Clone, Debug, Message)]
16pub struct ReceiveLoginPacketEvent {
17    /// The client entity that received the packet.
18    pub entity: Entity,
19    /// The packet that was actually received.
20    pub packet: Arc<ClientboundLoginPacket>,
21}
22
23#[derive(Clone, Debug, EntityEvent)]
24pub struct ReceiveHelloEvent {
25    pub entity: Entity,
26    pub account: Account,
27    pub packet: ClientboundHello,
28}
29
30#[derive(Clone, Debug, Message)]
31pub struct ReceiveCustomQueryEvent {
32    /// The client entity that received the packet.
33    pub entity: Entity,
34    pub packet: ClientboundCustomQuery,
35    /// A system can set this to `true` to make Azalea not reply to the query.
36    /// You must make sure you modify this before the
37    /// [`reply_to_custom_queries`] system runs.
38    ///
39    /// [`reply_to_custom_queries`]: crate::login::reply_to_custom_queries
40    pub disabled: bool,
41}
42
43/// Event for sending a login packet to the server.
44#[derive(Clone, Debug, EntityEvent)]
45pub struct SendLoginPacketEvent {
46    #[event_target]
47    pub sent_by: Entity,
48    pub packet: ServerboundLoginPacket,
49}
50impl SendLoginPacketEvent {
51    pub fn new(entity: Entity, packet: impl Packet<ServerboundLoginPacket>) -> Self {
52        let packet = packet.into_variant();
53        Self {
54            sent_by: entity,
55            packet,
56        }
57    }
58}
59
60pub fn handle_outgoing_packets_observer(
61    trigger: On<SendLoginPacketEvent>,
62    mut query: Query<(&mut RawConnection, Option<&InLoginState>)>,
63) {
64    let event = trigger.event();
65    if let Ok((mut raw_conn, in_login_state)) = query.get_mut(event.sent_by) {
66        if in_login_state.is_none() {
67            error!(
68                "Tried to send a login packet {:?} while not in login state",
69                event.packet
70            );
71            return;
72        }
73        debug!("Sending login packet: {:?}", event.packet);
74        if let Err(e) = raw_conn.write(event.packet.clone()) {
75            error!("Failed to send packet: {e}");
76        }
77    }
78}