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::{
15 client::InGameState, connection::RawConnection, local_player::WorldHolder, player::PlayerInfo,
16};
17
18#[derive(Clone, Debug, Message)]
36pub struct ReceiveGamePacketEvent {
37 pub entity: Entity,
39 pub packet: Arc<ClientboundGamePacket>,
41}
42
43#[derive(Clone, Debug, EntityEvent)]
45pub struct SendGamePacketEvent {
46 #[event_target]
47 pub sent_by: Entity,
48 pub packet: ServerboundGamePacket,
49}
50impl SendGamePacketEvent {
51 pub fn new(sent_by: Entity, packet: impl Packet<ServerboundGamePacket>) -> Self {
52 let packet = packet.into_variant();
53 Self { sent_by, packet }
54 }
55}
56
57pub fn handle_outgoing_packets_observer(
58 trigger: On<SendGamePacketEvent>,
59 mut query: Query<(
60 &mut RawConnection,
61 Option<&WorldHolder>,
62 Option<&InGameState>,
63 )>,
64) {
65 let event = trigger.event();
66
67 if let Ok((mut raw_connection, world, in_game_state)) = query.get_mut(event.sent_by) {
68 if in_game_state.is_none() {
69 error!(
70 "Tried to send a game packet {:?} while not in game state",
71 event.packet
72 );
73 return;
74 }
75
76 trace!("Sending game packet: {:?}", event.packet);
77
78 let mut write_fn = |ctx: azalea_buf::Ctx| {
79 if let Err(e) = raw_connection.write(ctx, event.packet.clone()) {
80 error!("Failed to send packet: {e}");
81 }
82 };
83 if let Some(world) = world {
84 write_fn(azalea_buf::Ctx::new(&world.shared.read().registries))
85 } else {
86 write_fn(azalea_buf::Ctx::default())
87 };
88 } else {
89 trace!("Not sending game packet: {:?}", event.packet);
90 }
91}
92
93#[derive(Clone, Debug, Message)]
96pub struct AddPlayerEvent {
97 pub entity: Entity,
99 pub info: PlayerInfo,
100}
101#[derive(Clone, Debug, Message)]
104pub struct RemovePlayerEvent {
105 pub entity: Entity,
107 pub info: PlayerInfo,
108}
109#[derive(Clone, Debug, Message)]
112pub struct UpdatePlayerEvent {
113 pub entity: Entity,
115 pub info: PlayerInfo,
116}
117
118#[derive(Clone, Debug, Message)]
123pub struct DeathEvent {
124 pub entity: Entity,
125 pub packet: Option<ClientboundPlayerCombatKill>,
126}
127
128#[derive(Clone, Debug, EntityEvent)]
131pub struct KeepAliveEvent {
132 pub entity: Entity,
133 pub id: u64,
138}
139
140#[derive(Clone, Debug, Message)]
141pub struct ResourcePackEvent {
142 pub entity: Entity,
143 pub id: Uuid,
148 pub url: String,
149 pub hash: String,
150 pub required: bool,
151 pub prompt: Option<FormattedText>,
152}
153
154#[derive(Clone, Debug, Message)]
159pub struct WorldLoadedEvent {
160 pub entity: Entity,
161 pub name: WorldName,
162 pub world: Weak<RwLock<World>>,
163}
164#[deprecated = "renamed to `WorldLoadedEvent`."]
165pub type InstanceLoadedEvent = WorldLoadedEvent;
166
167#[derive(Clone, Debug, EntityEvent)]
175pub struct GamePingEvent {
176 pub entity: Entity,
177 pub packet: azalea_protocol::packets::game::ClientboundPing,
178}