azalea_client/plugins/packet/login/
events.rs1use 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 pub entity: Entity,
19 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 pub entity: Entity,
34 pub packet: ClientboundCustomQuery,
35 pub disabled: bool,
41}
42
43#[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}