use azalea_entity::metadata::Health; use bevy_app::{App, Plugin, Update}; use bevy_ecs::{ prelude::*, system::{SystemParam, SystemState}, }; use self::game::DeathEvent; use crate::{chat::ChatReceivedEvent, events::death_listener}; pub mod config; pub mod game; pub mod login; pub struct PacketPlugin; pub fn death_event_on_0_health( query: Query<(Entity, &Health), Changed>, mut death_events: EventWriter, ) { for (entity, health) in query.iter() { if **health == 0. { death_events.write(DeathEvent { entity, packet: None, }); } } } impl Plugin for PacketPlugin { fn build(&self, app: &mut App) { app.add_observer(game::handle_outgoing_packets_observer) .add_observer(config::handle_outgoing_packets_observer) .add_observer(login::handle_outgoing_packets_observer) .add_systems( Update, ( ( config::handle_outgoing_packets, game::handle_outgoing_packets, login::handle_outgoing_packets, ) .chain(), death_event_on_0_health.before(death_listener), ), ) .add_event::() .add_event::() .add_event::() // .add_event::() .add_event::() .add_event::() // .add_event::() .add_event::() .add_event::() .add_event::() .add_event::() .add_event::() .add_event::() .add_event::() .add_event::(); } } #[macro_export] macro_rules! declare_packet_handlers { ( $packetenum:ident, $packetvar:expr, $handler:ident, [$($packet:path),+ $(,)?] ) => { paste::paste! { match $packetvar { $( $packetenum::[< $packet:camel >](p) => $handler.$packet(p), )+ } } }; } pub(crate) fn as_system(ecs: &mut World, f: impl FnOnce(T::Item<'_, '_>)) where T: SystemParam + 'static, { let mut system_state = SystemState::::new(ecs); let values = system_state.get_mut(ecs); f(values); system_state.apply(ecs); }