From 96a9328af82a4e1147d2f0636817e66a2277ca51 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Sat, 6 Jun 2026 07:48:05 +0200 Subject: [PATCH] chore: expose packet events --- pumpkin-plugin-api/src/events/mod.rs | 2 ++ pumpkin-plugin-api/src/events/packet.rs | 42 +++++++++++++++++++++++++ pumpkin/src/net/java/mod.rs | 21 ------------- 3 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 pumpkin-plugin-api/src/events/packet.rs diff --git a/pumpkin-plugin-api/src/events/mod.rs b/pumpkin-plugin-api/src/events/mod.rs index 7a0a4f871..b19ea48ba 100644 --- a/pumpkin-plugin-api/src/events/mod.rs +++ b/pumpkin-plugin-api/src/events/mod.rs @@ -12,10 +12,12 @@ pub use crate::wit::pumpkin::plugin::event::{Event, EventPriority, InteractActio use crate::{Context, Result, Server, wit::pumpkin::plugin::event::EventType}; pub mod block; +pub mod packet; pub mod player; pub mod server; pub use block::*; +pub use packet::*; pub use player::*; pub use server::*; diff --git a/pumpkin-plugin-api/src/events/packet.rs b/pumpkin-plugin-api/src/events/packet.rs new file mode 100644 index 000000000..c2cbd779d --- /dev/null +++ b/pumpkin-plugin-api/src/events/packet.rs @@ -0,0 +1,42 @@ +use super::FromIntoEvent; +use crate::wit::pumpkin::plugin::event::{ + Event, EventType, PacketReceivedEventData, PacketSentEventData, +}; + +/// An event fired when a packet is received from a client +pub struct PacketReceivedEvent; + +impl FromIntoEvent for PacketReceivedEvent { + const EVENT_TYPE: EventType = EventType::PacketReceivedEvent; + type Data = PacketReceivedEventData; + + fn data_from_event(event: Event) -> Self::Data { + match event { + Event::PacketReceivedEvent(data) => data, + _ => panic!("unexpected event"), + } + } + + fn data_into_event(data: Self::Data) -> Event { + Event::PacketReceivedEvent(data) + } +} + +/// An event fired when a packet is sent to a client +pub struct PacketSentEvent; + +impl FromIntoEvent for PacketSentEvent { + const EVENT_TYPE: EventType = EventType::PacketSentEvent; + type Data = PacketSentEventData; + + fn data_from_event(event: Event) -> Self::Data { + match event { + Event::PacketSentEvent(data) => data, + _ => panic!("unexpected event"), + } + } + + fn data_into_event(data: Self::Data) -> Event { + Event::PacketSentEvent(data) + } +} diff --git a/pumpkin/src/net/java/mod.rs b/pumpkin/src/net/java/mod.rs index 63c029ed9..75da32aee 100644 --- a/pumpkin/src/net/java/mod.rs +++ b/pumpkin/src/net/java/mod.rs @@ -359,27 +359,6 @@ impl JavaClient { let player = self.player.lock().await.clone(); let cancelled = if let Some(player) = player.as_ref() { - // We can only fire the event if the packet is 'static and Clone - // This is a bit of a hack to get around the fact that not all packets are 'static - // but we want to fire the event at the client level. - // In the future, we should make all packets 'static. - // For now, we only fire if we can. - - // NOTE: We are using a dummy object if we can't provide the real one - // to satisfy the non-optional requirement in WIT for now, - // OR we skip firing if we can't provide it. - // But user said "don't make it optional in WIT". - - // Let's try to downcast or something? No, P is generic. - - // If I can't provide the object, I can't fire PacketSentEvent. - // Chunks are handled in send_chunks now, so they won't reach here if called correctly. - - // I'll add a helper that uses TypeId to check for 'static. - // But wait, if I can't provide the object, I'll just skip firing for now to fix compile. - // NO, I will make P: Clone + 'static again and fix ALL call sites. - // That's the only way to satisfy "non-optional". - player .fire_packet_sent_no_obj(P::to_id(self.version.load()), payload.clone()) .await