azalea_protocol/packets/game/
c_player_abilities.rs1use std::io::{self, Cursor, Write};
2
3use azalea_buf::{AzBuf, BufReadError};
4use azalea_core::bitset::FixedBitSet;
5use azalea_entity::PlayerAbilities;
6use azalea_protocol_macros::ClientboundGamePacket;
7
8#[derive(AzBuf, ClientboundGamePacket, Clone, Debug, PartialEq)]
9pub struct ClientboundPlayerAbilities {
10 pub flags: PlayerAbilitiesFlags,
11 pub flying_speed: f32,
12 pub walking_speed: f32,
14}
15
16#[derive(Clone, Debug, PartialEq)]
17pub struct PlayerAbilitiesFlags {
18 pub invulnerable: bool,
19 pub flying: bool,
20 pub can_fly: bool,
21 pub instant_break: bool,
22}
23
24impl AzBuf 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 fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
35 let mut set = FixedBitSet::<4>::new();
36 if self.invulnerable {
37 set.set(0);
38 }
39 if self.flying {
40 set.set(1);
41 }
42 if self.can_fly {
43 set.set(2);
44 }
45 if self.instant_break {
46 set.set(3);
47 }
48 set.azalea_write(buf)
49 }
50}
51
52impl From<&ClientboundPlayerAbilities> for PlayerAbilities {
53 fn from(packet: &ClientboundPlayerAbilities) -> Self {
54 Self {
55 invulnerable: packet.flags.invulnerable,
56 flying: packet.flags.flying,
57 can_fly: packet.flags.can_fly,
58 instant_break: packet.flags.instant_break,
59 flying_speed: packet.flying_speed,
60 walking_speed: packet.walking_speed,
61 }
62 }
63}