use azalea_entity::metadata::Health; use bevy_app::{App, Plugin, Update}; use bevy_ecs::{ prelude::*, system::{SystemParam, SystemState}, }; use self::game::DeathEvent; use crate::client_chat::ChatReceivedEvent; pub mod config; pub mod game; pub mod login; pub mod relative_updates; pub struct PacketPlugin; pub fn death_event_on_0_health( query: Query<(Entity, &Health), Changed>, mut death_events: MessageWriter, ) { 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_systems( Update, relative_updates::debug_detect_updates_received_on_local_entities, ) .add_observer(game::handle_outgoing_packets_observer) .add_observer(config::handle_outgoing_packets_observer) .add_observer(login::handle_outgoing_packets_observer) .add_systems(Update, death_event_on_0_health) .add_message::() .add_message::() .add_message::() // .add_message::() .add_message::() .add_message::() .add_message::() .add_message::() .add_message::() .add_message::() .add_message::(); } } pub(crate) use azalea_protocol::azalea_protocol_macros::declare_packet_handlers; #[derive(Resource)] struct CachedSystemState(SystemState); pub(crate) fn as_system(ecs: &mut World, f: impl FnOnce(T::Item<'_, '_>)) where T: SystemParam + 'static, { // creating a new SystemState is expensive, so we save them as a Resource in the // ecs let mut system_state = match ecs.remove_resource::>() { Some(s) => s.0, None => SystemState::::new(ecs), }; let values = system_state.get_mut(ecs); f(values); system_state.apply(ecs); ecs.insert_resource(CachedSystemState(system_state)); }