aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs
blob: 03a2658e4160d81e89049a1ac5c26f1cb6b0a72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use azalea_buf::{BufReadError, McBuf};
use azalea_buf::{McBufReadable, McBufWritable};
use azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write};

#[derive(Clone, Debug, McBuf, ClientboundGamePacket)]
pub struct ClientboundPlayerPositionPacket {
    pub x: f64,
    pub y: f64,
    pub z: f64,
    pub y_rot: f32,
    pub x_rot: f32,
    pub relative_arguments: RelativeArguments,
    /// Client should confirm this packet with Teleport Confirm containing the
    /// same Teleport ID.
    #[var]
    pub id: u32,
    pub dismount_vehicle: bool,
}

#[derive(Debug, Clone)]
pub struct RelativeArguments {
    pub x: bool,
    pub y: bool,
    pub z: bool,
    pub y_rot: bool,
    pub x_rot: bool,
}

impl McBufReadable for RelativeArguments {
    fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
        let byte = u8::read_from(buf)?;
        Ok(RelativeArguments {
            x: byte & 0b1 != 0,
            y: byte & 0b10 != 0,
            z: byte & 0b100 != 0,
            y_rot: byte & 0b1000 != 0,
            x_rot: byte & 0b10000 != 0,
        })
    }
}

impl McBufWritable for RelativeArguments {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        let mut byte = 0;
        if self.x {
            byte |= 0b1;
        }
        if self.y {
            byte |= 0b10;
        }
        if self.z {
            byte |= 0b100;
        }
        if self.y_rot {
            byte |= 0b1000;
        }
        if self.x_rot {
            byte |= 0b10000;
        }
        u8::write_into(&byte, buf)
    }
}