Skip to main content

steel_protocol/packets/game/
c_update_attributes.rs

1//! Clientbound update attributes packet - sent to sync entity attributes with modifiers.
2
3use steel_macros::{ClientPacket, WriteTo};
4use steel_registry::packets::play::C_UPDATE_ATTRIBUTES;
5use steel_utils::Identifier;
6
7pub use steel_registry::attribute::AttributeModifierOperation;
8
9/// Represents a single attribute modifier within an attribute snapshot.
10#[derive(WriteTo, Clone, Debug)]
11pub struct AttributeModifierData {
12    /// The resource location identifier for this modifier (e.g. `minecraft:sprinting`).
13    pub id: Identifier,
14    /// The modifier amount.
15    pub amount: f64,
16    /// The operation type for this modifier.
17    pub operation: AttributeModifierOperation,
18}
19
20/// A snapshot of a single attribute's state, including its base value and active modifiers.
21#[derive(WriteTo, Clone, Debug)]
22pub struct AttributeSnapshot {
23    /// The registry ID of the attribute (`VarInt` on the wire).
24    #[write(as = VarInt)]
25    pub attribute_id: i32,
26    /// The base value of the attribute.
27    pub base_value: f64,
28    /// Active modifiers on this attribute.
29    #[write(as = Prefixed(VarInt))]
30    pub modifiers: Vec<AttributeModifierData>,
31}
32
33/// Clientbound packet sent to update entity attributes and their modifiers.
34///
35/// Used for things like sprint speed modifiers, potion effects on speed/health, etc.
36#[derive(ClientPacket, WriteTo, Clone, Debug)]
37#[packet_id(Play = C_UPDATE_ATTRIBUTES)]
38pub struct CUpdateAttributes {
39    /// The entity ID whose attributes are being updated.
40    #[write(as = VarInt)]
41    pub entity_id: i32,
42    /// The attribute snapshots to sync.
43    #[write(as = Prefixed(VarInt))]
44    pub attributes: Vec<AttributeSnapshot>,
45}
46
47impl CUpdateAttributes {
48    /// Creates a new update attributes packet.
49    #[must_use]
50    pub const fn new(entity_id: i32, attributes: Vec<AttributeSnapshot>) -> Self {
51        Self {
52            entity_id,
53            attributes,
54        }
55    }
56}