Skip to main content

azalea_entity/plugin/
components.rs

1use azalea_block::fluid_state::FluidKind;
2use azalea_core::{
3    entity_id::MinecraftEntityId,
4    position::{BlockPos, ChunkPos, Vec3},
5};
6use azalea_registry::builtin::EntityKind;
7use azalea_world::WorldName;
8use bevy_ecs::{bundle::Bundle, component::Component};
9use derive_more::{Deref, DerefMut};
10use uuid::Uuid;
11
12use crate::{
13    ActiveEffects, Attributes, EntityUuid, FluidOnEyes, LookDirection, Physics, Position,
14    dimensions::EntityDimensions, indexing::EntityChunkPos,
15};
16
17/// A bundle of components that every entity has.
18///
19/// This doesn't contain metadata; that has to be added separately.
20#[derive(Bundle)]
21pub struct EntityBundle {
22    pub kind: EntityKindComponent,
23    pub minecraft_id: MinecraftEntityId,
24    pub uuid: EntityUuid,
25    pub world_name: WorldName,
26    pub position: Position,
27    pub last_sent_position: LastSentPosition,
28
29    pub chunk_pos: EntityChunkPos,
30
31    pub physics: Physics,
32    pub direction: LookDirection,
33    pub dimensions: EntityDimensions,
34    pub attributes: Attributes,
35    pub jumping: Jumping,
36    pub crouching: Crouching,
37    pub fluid_on_eyes: FluidOnEyes,
38    pub on_climbable: OnClimbable,
39    pub active_effects: ActiveEffects,
40}
41
42impl EntityBundle {
43    pub fn new(
44        uuid: Uuid,
45        minecraft_id: MinecraftEntityId,
46        pos: Vec3,
47        kind: EntityKind,
48        world_name: WorldName,
49    ) -> Self {
50        let dimensions = EntityDimensions::from(kind);
51
52        Self {
53            kind: EntityKindComponent(kind),
54            minecraft_id,
55            uuid: EntityUuid(uuid),
56            world_name,
57            position: Position(pos),
58            chunk_pos: EntityChunkPos(ChunkPos::from(&pos)),
59            last_sent_position: LastSentPosition(pos),
60            physics: Physics::new(&dimensions, pos),
61            dimensions,
62            direction: LookDirection::default(),
63
64            attributes: Attributes::new(EntityKind::Player),
65
66            jumping: Jumping(false),
67            crouching: Crouching(false),
68            fluid_on_eyes: FluidOnEyes(FluidKind::Empty),
69            on_climbable: OnClimbable(false),
70            active_effects: ActiveEffects::default(),
71        }
72    }
73}
74
75/// Marker component for entities that are dead.
76///
77/// "Dead" means that the entity has 0 health.
78#[derive(Clone, Component, Copy, Default)]
79pub struct Dead;
80
81/// A component NewType for [`EntityKind`].
82///
83/// Most of the time, you should be using `azalea_registry::EntityKind`
84/// directly instead.
85#[derive(Clone, Component, Copy, Debug, Deref, PartialEq)]
86pub struct EntityKindComponent(pub EntityKind);
87
88/// A marker component that signifies that this entity is "local" and shouldn't
89/// be updated by other clients.
90///
91/// If this is for a client then all of our clients will have this.
92///
93/// This component is not removed from clients when they disconnect.
94#[derive(Clone, Component, Copy, Debug, Default)]
95pub struct LocalEntity;
96
97impl FluidOnEyes {
98    pub fn new(fluid: FluidKind) -> Self {
99        Self(fluid)
100    }
101}
102
103#[derive(Clone, Component, Copy, Debug, Deref, DerefMut, PartialEq)]
104pub struct OnClimbable(bool);
105
106/// A component that indicates whether the player is currently sneaking.
107///
108/// If the entity is a player but isn't a local player, then this is just a
109/// shortcut for checking if the [`Pose`] is `Crouching`.
110///
111/// If you need to modify this value, use
112/// `azalea_client::PhysicsState::trying_to_crouch` or `Client::set_crouching`
113/// instead.
114///
115/// [`Pose`]: crate::data::Pose
116#[derive(Clone, Component, Copy, Default, Deref, DerefMut)]
117pub struct Crouching(bool);
118
119/// A component that indicates whether the client has loaded.
120///
121/// This is updated by a system in `azalea-client`.
122#[derive(Component)]
123pub struct HasClientLoaded;
124
125/// The second most recent position of the entity that was sent over the
126/// network.
127///
128/// This is currently only updated for our own local player entities.
129#[derive(Clone, Copy, Component, Debug, Default, Deref, DerefMut, PartialEq)]
130pub struct LastSentPosition(Vec3);
131impl From<&LastSentPosition> for Vec3 {
132    fn from(value: &LastSentPosition) -> Self {
133        value.0
134    }
135}
136impl From<LastSentPosition> for ChunkPos {
137    fn from(value: LastSentPosition) -> Self {
138        ChunkPos::from(&value.0)
139    }
140}
141impl From<LastSentPosition> for BlockPos {
142    fn from(value: LastSentPosition) -> Self {
143        BlockPos::from(&value.0)
144    }
145}
146impl From<&LastSentPosition> for ChunkPos {
147    fn from(value: &LastSentPosition) -> Self {
148        ChunkPos::from(value.0)
149    }
150}
151impl From<&LastSentPosition> for BlockPos {
152    fn from(value: &LastSentPosition) -> Self {
153        BlockPos::from(value.0)
154    }
155}
156
157/// A component for entities that can jump.
158///
159/// If this is true, the entity will try to jump every tick. It's equivalent to
160/// the space key being held in vanilla.
161#[derive(Clone, Copy, Component, Debug, Default, Deref, DerefMut, Eq, PartialEq)]
162pub struct Jumping(pub bool);