azalea_protocol/packets/game/
c_add_entity.rs

1use azalea_buf::AzBuf;
2use azalea_core::{delta::LpVec3, position::Vec3};
3use azalea_entity::{EntityBundle, metadata::apply_default_metadata};
4use azalea_protocol_macros::ClientboundGamePacket;
5use azalea_registry::{builtin::EntityKind, identifier::Identifier};
6use azalea_world::MinecraftEntityId;
7use uuid::Uuid;
8
9#[derive(Clone, Debug, AzBuf, PartialEq, ClientboundGamePacket)]
10pub struct ClientboundAddEntity {
11    /// The numeric ID of the entity being added to the world.
12    #[var]
13    pub id: MinecraftEntityId,
14    pub uuid: Uuid,
15    pub entity_type: EntityKind,
16    pub position: Vec3,
17    pub movement: LpVec3,
18    pub x_rot: i8,
19    pub y_rot: i8,
20    pub y_head_rot: i8,
21    /// The entity's "object data". This is unused for most entities.
22    ///
23    /// Projectiles and fishing hooks treat this as an entity ID, which you're
24    /// encouraged to use [`MinecraftEntityId::from`] for. Other entities may
25    /// treat it as a block state or enum variant.
26    ///
27    /// See [the wiki](https://minecraft.wiki/w/Java_Edition_protocol/Object_data)
28    /// for more information about this field.
29    #[var]
30    pub data: u32,
31}
32
33impl ClientboundAddEntity {
34    /// Make the entity into a bundle that can be inserted into the ECS.
35    ///
36    /// You must apply the metadata after inserting the bundle with
37    /// [`Self::apply_metadata`].
38    pub fn as_entity_bundle(&self, world_name: Identifier) -> EntityBundle {
39        EntityBundle::new(self.uuid, self.position, self.entity_type, world_name)
40    }
41
42    /// Apply the default metadata for the given entity.
43    pub fn apply_metadata(&self, entity: &mut bevy_ecs::system::EntityCommands) {
44        apply_default_metadata(entity, self.entity_type);
45    }
46}