Skip to main content

steel_protocol/packets/game/
c_set_entity_data.rs

1//! Clientbound set entity data packet - sent to sync entity metadata.
2
3use std::io::{Result, Write};
4
5use steel_macros::ClientPacket;
6use steel_registry::{
7    entity_data::{DataValue, write_data_values},
8    packets::play::C_SET_ENTITY_DATA,
9};
10use steel_utils::{codec::VarInt, serial::WriteTo};
11
12/// Sent to synchronize entity metadata (health, pose, flags, etc.) with the client.
13///
14/// The packet contains a list of changed metadata values, each with:
15/// - `index`: The field index (0-254)
16/// - `serializer_id`: The type ID from `EntityDataSerializers`
17/// - `value`: The actual data
18///
19/// The list is terminated by a 0xFF byte.
20#[derive(ClientPacket, Clone, Debug)]
21#[packet_id(Play = C_SET_ENTITY_DATA)]
22pub struct CSetEntityData {
23    /// The entity ID whose metadata is being updated.
24    pub entity_id: i32,
25    /// The metadata values to sync.
26    pub packed_items: Vec<DataValue>,
27}
28
29impl CSetEntityData {
30    /// Creates a new set entity data packet.
31    #[must_use]
32    pub const fn new(entity_id: i32, packed_items: Vec<DataValue>) -> Self {
33        Self {
34            entity_id,
35            packed_items,
36        }
37    }
38}
39
40impl WriteTo for CSetEntityData {
41    fn write(&self, writer: &mut impl Write) -> Result<()> {
42        VarInt(self.entity_id).write(writer)?;
43        let mut buf = Vec::new();
44        write_data_values(&self.packed_items, &mut buf)?;
45        writer.write_all(&buf)
46    }
47}