azalea_protocol/common/
movements.rs

1use std::io::{self, Cursor, Write};
2
3use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
4use azalea_core::{bitset::FixedBitSet, position::Vec3};
5use azalea_entity::LookDirection;
6
7/// The updated position, velocity, and rotations for an entity.
8///
9/// Often, this field comes alongside a [`RelativeMovements`] field, which
10/// specifies which parts of this struct should be treated as relative.
11#[derive(AzBuf, Clone, Debug)]
12pub struct PositionMoveRotation {
13    pub pos: Vec3,
14    /// The updated delta movement (velocity).
15    pub delta: Vec3,
16    pub look_direction: LookDirection,
17}
18
19#[derive(Debug, Clone)]
20pub struct RelativeMovements {
21    pub x: bool,
22    pub y: bool,
23    pub z: bool,
24    pub y_rot: bool,
25    pub x_rot: bool,
26    pub delta_x: bool,
27    pub delta_y: bool,
28    pub delta_z: bool,
29    pub rotate_delta: bool,
30}
31
32impl AzaleaRead for RelativeMovements {
33    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
34        // yes minecraft seriously wastes that many bits, smh
35        let set = FixedBitSet::<{ 32_usize.div_ceil(8) }>::azalea_read(buf)?;
36        Ok(RelativeMovements {
37            x: set.index(0),
38            y: set.index(1),
39            z: set.index(2),
40            y_rot: set.index(3),
41            x_rot: set.index(4),
42            delta_x: set.index(5),
43            delta_y: set.index(6),
44            delta_z: set.index(7),
45            rotate_delta: set.index(8),
46        })
47    }
48}
49
50impl AzaleaWrite for RelativeMovements {
51    fn azalea_write(&self, buf: &mut impl Write) -> Result<(), io::Error> {
52        let mut set = FixedBitSet::<{ 32_usize.div_ceil(8) }>::new();
53        let mut set_bit = |index: usize, value: bool| {
54            if value {
55                set.set(index);
56            }
57        };
58
59        set_bit(0, self.x);
60        set_bit(1, self.y);
61        set_bit(2, self.z);
62        set_bit(3, self.y_rot);
63        set_bit(4, self.x_rot);
64        set_bit(5, self.delta_x);
65        set_bit(6, self.delta_y);
66        set_bit(7, self.delta_z);
67        set_bit(8, self.rotate_delta);
68
69        set.azalea_write(buf)
70    }
71}