Skip to main content

steel_protocol/packets/game/
c_player_abilities.rs

1use steel_macros::{ClientPacket, WriteTo};
2use steel_registry::packets::play::C_PLAYER_ABILITIES;
3
4/// Flags for player abilities bitfield.
5/// These match vanilla Minecraft's ability flags.
6pub mod ability_flags {
7    pub const INVULNERABLE: u8 = 0x01;
8    pub const FLYING: u8 = 0x02;
9    pub const MAY_FLY: u8 = 0x04;
10    pub const INSTABUILD: u8 = 0x08;
11}
12
13/// Sent by the server to update the player's abilities.
14/// This tells the client whether the player can fly, is invulnerable, etc.
15#[derive(ClientPacket, WriteTo, Clone, Debug)]
16#[packet_id(Play = C_PLAYER_ABILITIES)]
17pub struct CPlayerAbilities {
18    /// Bitfield of ability flags (invulnerable, flying, `may_fly`, instabuild)
19    pub flags: u8,
20    /// Flying speed (default 0.05)
21    pub flying_speed: f32,
22    /// Field of view modifier / walking speed (default 0.1)
23    pub walking_speed: f32,
24}
25
26impl CPlayerAbilities {
27    /// Default flying speed in vanilla Minecraft
28    pub const DEFAULT_FLYING_SPEED: f32 = 0.05;
29    /// Default walking speed in vanilla Minecraft
30    pub const DEFAULT_WALKING_SPEED: f32 = 0.1;
31
32    /// Creates abilities for survival mode
33    #[must_use]
34    pub const fn survival() -> Self {
35        Self {
36            flags: 0,
37            flying_speed: Self::DEFAULT_FLYING_SPEED,
38            walking_speed: Self::DEFAULT_WALKING_SPEED,
39        }
40    }
41
42    /// Creates abilities for creative mode
43    #[must_use]
44    pub const fn creative() -> Self {
45        Self {
46            flags: ability_flags::INVULNERABLE | ability_flags::MAY_FLY | ability_flags::INSTABUILD,
47            flying_speed: Self::DEFAULT_FLYING_SPEED,
48            walking_speed: Self::DEFAULT_WALKING_SPEED,
49        }
50    }
51
52    /// Creates abilities for adventure mode
53    #[must_use]
54    pub const fn adventure() -> Self {
55        Self {
56            flags: 0,
57            flying_speed: Self::DEFAULT_FLYING_SPEED,
58            walking_speed: Self::DEFAULT_WALKING_SPEED,
59        }
60    }
61
62    /// Creates abilities for spectator mode
63    #[must_use]
64    pub const fn spectator() -> Self {
65        Self {
66            // Spectators: invulnerable, can fly, and are currently flying
67            flags: ability_flags::INVULNERABLE | ability_flags::MAY_FLY | ability_flags::FLYING,
68            flying_speed: Self::DEFAULT_FLYING_SPEED,
69            walking_speed: Self::DEFAULT_WALKING_SPEED,
70        }
71    }
72}