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