use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use valence_entity::EntityManager;
pub use valence_protocol::packets::play::player_interact_entity_c2s::EntityInteraction;
use valence_protocol::packets::play::PlayerInteractEntityC2s;
use crate::event_loop::{EventLoopPreUpdate, PacketEvent};
pub struct InteractEntityPlugin;
impl Plugin for InteractEntityPlugin {
fn build(&self, app: &mut App) {
app.add_event::<InteractEntityEvent>()
.add_systems(EventLoopPreUpdate, handle_interact_entity);
}
}
#[derive(Event, Copy, Clone, Debug)]
pub struct InteractEntityEvent {
pub client: Entity,
pub entity: Entity,
pub sneaking: bool,
pub interact: EntityInteraction,
}
fn handle_interact_entity(
mut packets: EventReader<PacketEvent>,
entities: Res<EntityManager>,
mut events: EventWriter<InteractEntityEvent>,
) {
for packet in packets.read() {
if let Some(pkt) = packet.decode::<PlayerInteractEntityC2s>() {
if let Some(entity) = entities.get_by_id(pkt.entity_id.0) {
events.send(InteractEntityEvent {
client: packet.client,
entity,
sneaking: pkt.sneaking,
interact: pkt.interact,
});
}
}
}
}