aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/packet/login/events.rs
blob: 5b44e60ca3b2fa218efd50328f32c442f633db6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::sync::Arc;

use azalea_protocol::packets::{
    Packet,
    login::{
        ClientboundCustomQuery, ClientboundHello, ClientboundLoginPacket, ServerboundLoginPacket,
    },
};
use bevy_ecs::prelude::*;
use tracing::{debug, error};

use super::InLoginState;
use crate::{account::Account, connection::RawConnection};

#[derive(Clone, Debug, Message)]
pub struct ReceiveLoginPacketEvent {
    /// The client entity that received the packet.
    pub entity: Entity,
    /// The packet that was actually received.
    pub packet: Arc<ClientboundLoginPacket>,
}

#[derive(Clone, Debug, EntityEvent)]
pub struct ReceiveHelloEvent {
    pub entity: Entity,
    pub account: Account,
    pub packet: ClientboundHello,
}

#[derive(Clone, Debug, Message)]
pub struct ReceiveCustomQueryEvent {
    /// The client entity that received the packet.
    pub entity: Entity,
    pub packet: ClientboundCustomQuery,
    /// A system can set this to `true` to make Azalea not reply to the query.
    /// You must make sure you modify this before the
    /// [`reply_to_custom_queries`] system runs.
    ///
    /// [`reply_to_custom_queries`]: crate::login::reply_to_custom_queries
    pub disabled: bool,
}

/// Event for sending a login packet to the server.
#[derive(Clone, Debug, EntityEvent)]
pub struct SendLoginPacketEvent {
    #[event_target]
    pub sent_by: Entity,
    pub packet: ServerboundLoginPacket,
}
impl SendLoginPacketEvent {
    pub fn new(entity: Entity, packet: impl Packet<ServerboundLoginPacket>) -> Self {
        let packet = packet.into_variant();
        Self {
            sent_by: entity,
            packet,
        }
    }
}

pub fn handle_outgoing_packets_observer(
    trigger: On<SendLoginPacketEvent>,
    mut query: Query<(&mut RawConnection, Option<&InLoginState>)>,
) {
    let event = trigger.event();
    if let Ok((mut raw_conn, in_login_state)) = query.get_mut(event.sent_by) {
        if in_login_state.is_none() {
            error!(
                "Tried to send a login packet {:?} while not in login state",
                event.packet
            );
            return;
        }
        debug!("Sending login packet: {:?}", event.packet);
        if let Err(e) = raw_conn.write(event.packet.clone()) {
            error!("Failed to send packet: {e}");
        }
    }
}