azalea_protocol/packets/game/
c_player_abilities.rs

1use std::io::{self, Cursor, Write};
2
3use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
4use azalea_core::bitset::FixedBitSet;
5use azalea_entity::PlayerAbilities;
6use azalea_protocol_macros::ClientboundGamePacket;
7
8#[derive(Clone, Debug, AzBuf, ClientboundGamePacket)]
9pub struct ClientboundPlayerAbilities {
10    pub flags: PlayerAbilitiesFlags,
11    pub flying_speed: f32,
12    /// Used for the fov
13    pub walking_speed: f32,
14}
15
16#[derive(Clone, Debug)]
17pub struct PlayerAbilitiesFlags {
18    pub invulnerable: bool,
19    pub flying: bool,
20    pub can_fly: bool,
21    pub instant_break: bool,
22}
23
24impl AzaleaRead for PlayerAbilitiesFlags {
25    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
26        let set = FixedBitSet::<4>::azalea_read(buf)?;
27        Ok(PlayerAbilitiesFlags {
28            invulnerable: set.index(0),
29            flying: set.index(1),
30            can_fly: set.index(2),
31            instant_break: set.index(3),
32        })
33    }
34}
35
36impl AzaleaWrite for PlayerAbilitiesFlags {
37    fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
38        let mut set = FixedBitSet::<4>::new();
39        if self.invulnerable {
40            set.set(0);
41        }
42        if self.flying {
43            set.set(1);
44        }
45        if self.can_fly {
46            set.set(2);
47        }
48        if self.instant_break {
49            set.set(3);
50        }
51        set.azalea_write(buf)
52    }
53}
54
55impl From<&ClientboundPlayerAbilities> for PlayerAbilities {
56    fn from(packet: &ClientboundPlayerAbilities) -> Self {
57        Self {
58            invulnerable: packet.flags.invulnerable,
59            flying: packet.flags.flying,
60            can_fly: packet.flags.can_fly,
61            instant_break: packet.flags.instant_break,
62            flying_speed: packet.flying_speed,
63            walking_speed: packet.walking_speed,
64        }
65    }
66}