Skip to main content

steel_protocol/packets/game/
c_swing_animation.rs

1//! Clientbound swing animation packet - sent when an entity swings a hand.
2
3use steel_macros::{ClientPacket, WriteTo};
4use steel_registry::data_components::vanilla_components::SwingAnimation;
5use steel_registry::packets::play::C_SWING_ANIMATION;
6use steel_utils::types::InteractionHand;
7
8/// Sent to play an arm swing on an entity.
9#[derive(ClientPacket, WriteTo, Clone, Debug)]
10#[packet_id(Play = C_SWING_ANIMATION)]
11pub struct CSwingAnimation {
12    /// The entity ID that is swinging.
13    #[write(as = VarInt)]
14    pub entity_id: i32,
15    /// The hand being swung.
16    pub hand: InteractionHand,
17    /// The animation style and its duration in ticks.
18    pub animation: SwingAnimation,
19}
20
21impl CSwingAnimation {
22    /// Creates a new swing animation packet.
23    #[must_use]
24    pub const fn new(entity_id: i32, hand: InteractionHand, animation: SwingAnimation) -> Self {
25        Self {
26            entity_id,
27            hand,
28            animation,
29        }
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use steel_registry::data_components::components::SwingAnimationType;
36    use steel_utils::codec::VarInt;
37    use steel_utils::serial::WriteTo as _;
38
39    use super::{CSwingAnimation, InteractionHand, SwingAnimation};
40
41    #[test]
42    fn writes_entity_hand_and_animation() {
43        let mut bytes = Vec::new();
44        CSwingAnimation::new(
45            7,
46            InteractionHand::OffHand,
47            SwingAnimation::new(SwingAnimationType::Stab, 13),
48        )
49        .write(&mut bytes)
50        .expect("write should succeed");
51
52        let mut expected = Vec::new();
53        VarInt(7).write(&mut expected).unwrap();
54        VarInt(1).write(&mut expected).unwrap();
55        VarInt(2).write(&mut expected).unwrap();
56        VarInt(13).write(&mut expected).unwrap();
57        assert_eq!(bytes, expected);
58    }
59}