Skip to main content

azalea_protocol/packets/game/
s_player_input.rs

1use std::io::{self, Cursor, Write};
2
3use azalea_buf::{AzBuf, BufReadError};
4use azalea_core::bitset::FixedBitSet;
5use azalea_protocol_macros::ServerboundGamePacket;
6
7#[derive(Clone, Debug, Default, Eq, PartialEq, ServerboundGamePacket)]
8pub struct ServerboundPlayerInput {
9    pub forward: bool,
10    pub backward: bool,
11    pub left: bool,
12    pub right: bool,
13    pub jump: bool,
14    pub shift: bool,
15    pub sprint: bool,
16}
17
18impl AzBuf for ServerboundPlayerInput {
19    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
20        let set = FixedBitSet::<7>::azalea_read(buf)?;
21        Ok(Self {
22            forward: set.index(0),
23            backward: set.index(1),
24            left: set.index(2),
25            right: set.index(3),
26            jump: set.index(4),
27            shift: set.index(5),
28            sprint: set.index(6),
29        })
30    }
31    fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
32        let mut set = FixedBitSet::<7>::new();
33        if self.forward {
34            set.set(0);
35        }
36        if self.backward {
37            set.set(1);
38        }
39        if self.left {
40            set.set(2);
41        }
42        if self.right {
43            set.set(3);
44        }
45        if self.jump {
46            set.set(4);
47        }
48        if self.shift {
49            set.set(5);
50        }
51        if self.sprint {
52            set.set(6);
53        }
54        set.azalea_write(buf)
55    }
56}