valence_server/
hand_swing.rs

1use bevy_app::prelude::*;
2use bevy_ecs::prelude::*;
3use valence_entity::{EntityAnimation, EntityAnimations};
4use valence_protocol::packets::play::HandSwingC2s;
5use valence_protocol::Hand;
6
7use crate::event_loop::{EventLoopPreUpdate, PacketEvent};
8
9pub struct HandSwingPlugin;
10
11impl Plugin for HandSwingPlugin {
12    fn build(&self, app: &mut App) {
13        app.add_event::<HandSwingEvent>()
14            .add_systems(EventLoopPreUpdate, handle_hand_swing);
15    }
16}
17
18#[derive(Event, Copy, Clone, PartialEq, Eq, Debug)]
19pub struct HandSwingEvent {
20    pub client: Entity,
21    pub hand: Hand,
22}
23
24fn handle_hand_swing(
25    mut packets: EventReader<PacketEvent>,
26    mut clients: Query<&mut EntityAnimations>,
27    mut events: EventWriter<HandSwingEvent>,
28) {
29    for packet in packets.read() {
30        if let Some(pkt) = packet.decode::<HandSwingC2s>() {
31            if let Ok(mut anim) = clients.get_mut(packet.client) {
32                anim.trigger(match pkt.hand {
33                    Hand::Main => EntityAnimation::SwingMainHand,
34                    Hand::Off => EntityAnimation::SwingOffHand,
35                });
36            }
37
38            events.send(HandSwingEvent {
39                client: packet.client,
40                hand: pkt.hand,
41            });
42        }
43    }
44}