azalea_client/plugins/packet/game/
events.rs1use std::sync::{Arc, Weak};
2
3use azalea_chat::FormattedText;
4use azalea_protocol::packets::{
5 Packet,
6 game::{ClientboundGamePacket, ClientboundPlayerCombatKill, ServerboundGamePacket},
7};
8use azalea_world::{World, WorldName};
9use bevy_ecs::prelude::*;
10use parking_lot::RwLock;
11use tracing::{error, trace};
12use uuid::Uuid;
13
14use crate::{client::InGameState, connection::RawConnection, player::PlayerInfo};
15
16#[derive(Clone, Debug, Message)]
34pub struct ReceiveGamePacketEvent {
35 pub entity: Entity,
37 pub packet: Arc<ClientboundGamePacket>,
39}
40
41#[derive(Clone, Debug, EntityEvent)]
43pub struct SendGamePacketEvent {
44 #[event_target]
45 pub sent_by: Entity,
46 pub packet: ServerboundGamePacket,
47}
48impl SendGamePacketEvent {
49 pub fn new(sent_by: Entity, packet: impl Packet<ServerboundGamePacket>) -> Self {
50 let packet = packet.into_variant();
51 Self { sent_by, packet }
52 }
53}
54
55pub fn handle_outgoing_packets_observer(
56 trigger: On<SendGamePacketEvent>,
57 mut query: Query<(&mut RawConnection, Option<&InGameState>)>,
58) {
59 let event = trigger.event();
60
61 if let Ok((mut raw_connection, in_game_state)) = query.get_mut(event.sent_by) {
62 if in_game_state.is_none() {
63 error!(
64 "Tried to send a game packet {:?} while not in game state",
65 event.packet
66 );
67 return;
68 }
69
70 trace!("Sending game packet: {:?}", event.packet);
71 if let Err(e) = raw_connection.write(event.packet.clone()) {
72 error!("Failed to send packet: {e}");
73 }
74 } else {
75 trace!("Not sending game packet: {:?}", event.packet);
76 }
77}
78
79#[derive(Clone, Debug, Message)]
82pub struct AddPlayerEvent {
83 pub entity: Entity,
85 pub info: PlayerInfo,
86}
87#[derive(Clone, Debug, Message)]
90pub struct RemovePlayerEvent {
91 pub entity: Entity,
93 pub info: PlayerInfo,
94}
95#[derive(Clone, Debug, Message)]
98pub struct UpdatePlayerEvent {
99 pub entity: Entity,
101 pub info: PlayerInfo,
102}
103
104#[derive(Clone, Debug, Message)]
109pub struct DeathEvent {
110 pub entity: Entity,
111 pub packet: Option<ClientboundPlayerCombatKill>,
112}
113
114#[derive(Clone, Debug, Message)]
117pub struct KeepAliveEvent {
118 pub entity: Entity,
119 pub id: u64,
124}
125
126#[derive(Clone, Debug, Message)]
127pub struct ResourcePackEvent {
128 pub entity: Entity,
129 pub id: Uuid,
134 pub url: String,
135 pub hash: String,
136 pub required: bool,
137 pub prompt: Option<FormattedText>,
138}
139
140#[derive(Clone, Debug, Message)]
145pub struct WorldLoadedEvent {
146 pub entity: Entity,
147 pub name: WorldName,
148 pub world: Weak<RwLock<World>>,
149}
150#[deprecated = "renamed to `WorldLoadedEvent`."]
151pub type InstanceLoadedEvent = WorldLoadedEvent;
152
153#[derive(Clone, Debug, EntityEvent)]
161pub struct GamePingEvent {
162 pub entity: Entity,
163 pub packet: azalea_protocol::packets::game::ClientboundPing,
164}