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/// Vanilla: `ClientboundUpdateAttributesPacket`
37#[derive(ClientPacket, WriteTo, Clone, Debug)]
38#[packet_id(Play = C_UPDATE_ATTRIBUTES)]
39pub struct CUpdateAttributes {
40    /// The entity ID whose attributes are being updated.
41    #[write(as = VarInt)]
42    pub entity_id: i32,
43    /// The attribute snapshots to sync.
44    #[write(as = Prefixed(VarInt))]
45    pub attributes: Vec<AttributeSnapshot>,
46}
47
48impl CUpdateAttributes {
49    /// Creates a new update attributes packet.
50    #[must_use]
51    pub const fn new(entity_id: i32, attributes: Vec<AttributeSnapshot>) -> Self {
52        Self {
53            entity_id,
54            attributes,
55        }
56    }
57}