azalea_protocol/packets/game/
c_add_entity.rs

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