Skip to main content

azalea_entity/plugin/
mod.rs

1mod components;
2pub mod effect_events;
3pub mod indexing;
4
5use std::collections::HashSet;
6
7use azalea_block::{BlockState, fluid_state::FluidKind, properties};
8use azalea_core::{
9    entity_id::MinecraftEntityId,
10    game_type::GameMode,
11    position::{BlockPos, ChunkPos},
12    tick::GameTick,
13};
14use azalea_registry::{builtin::BlockKind, tags};
15use azalea_world::{ChunkStorage, WorldName, Worlds};
16use bevy_app::{App, Plugin, PostUpdate, Update};
17use bevy_ecs::prelude::*;
18pub use components::*;
19use derive_more::{Deref, DerefMut};
20use indexing::EntityUuidIndex;
21use tracing::debug;
22
23use crate::{
24    FluidOnEyes, LookDirection, Physics, Pose, Position,
25    dimensions::{EntityDimensions, calculate_dimensions},
26    metadata::{self, Health, Player},
27    plugin::effect_events::{handle_add_effect, handle_remove_effects},
28};
29
30/// A Bevy [`SystemSet`] for various types of entity updates.
31#[derive(Clone, Debug, Eq, Hash, PartialEq, SystemSet)]
32pub enum EntityUpdateSystems {
33    /// Create search indexes for entities.
34    Index,
35    /// Remove despawned entities from search indexes.
36    Deindex,
37}
38
39/// A Bevy [`SystemSet`] for post travel update for dimension and boundingbox
40#[derive(Clone, Debug, Eq, Hash, PartialEq, SystemSet)]
41pub struct EntityGeometryUpdateSystems;
42
43/// Plugin handling some basic entity functionality.
44pub struct EntityPlugin;
45impl Plugin for EntityPlugin {
46    fn build(&self, app: &mut App) {
47        // entities get added pre-update
48        // added to indexes during update (done by this plugin)
49        // modified during update
50        // despawned post-update (done by this plugin)
51        app.add_systems(
52            PostUpdate,
53            indexing::remove_despawned_entities_from_indexes.in_set(EntityUpdateSystems::Deindex),
54        )
55        .add_systems(
56            Update,
57            (
58                (
59                    indexing::update_entity_chunk_positions,
60                    indexing::insert_entity_chunk_position,
61                )
62                    .chain()
63                    .in_set(EntityUpdateSystems::Index),
64                (
65                    debug_new_entity,
66                    add_dead,
67                    clamp_look_direction,
68                    update_on_climbable,
69                    (update_dimensions, update_bounding_box).chain(),
70                    update_crouching,
71                ),
72            ),
73        )
74        .add_systems(
75            GameTick,
76            (
77                update_in_loaded_chunk,
78                update_fluid_on_eyes,
79                (update_dimensions, update_bounding_box)
80                    .chain()
81                    .in_set(EntityGeometryUpdateSystems),
82            ),
83        )
84        .add_observer(handle_add_effect)
85        .add_observer(handle_remove_effects)
86        .init_resource::<EntityUuidIndex>();
87    }
88}
89
90fn debug_new_entity(query: Query<(Entity, Option<&LocalEntity>), Added<MinecraftEntityId>>) {
91    for (entity, local) in query.iter() {
92        if local.is_some() {
93            debug!("new local entity: {:?}", entity);
94        } else {
95            debug!("new entity: {:?}", entity);
96        }
97    }
98}
99
100/// System that adds the [`Dead`] marker component if an entity's health is set
101/// to 0 (or less than 0).
102///
103/// This will be present if an entity is doing the death animation.
104///
105/// Entities that are dead cannot be revived.
106pub fn add_dead(mut commands: Commands, query: Query<(Entity, &Health), Changed<Health>>) {
107    for (entity, health) in query.iter() {
108        if **health <= 0.0 {
109            commands.entity(entity).insert(Dead);
110        }
111    }
112}
113
114pub fn update_fluid_on_eyes(
115    mut query: Query<
116        (&mut FluidOnEyes, &Position, &EntityDimensions, &WorldName),
117        With<metadata::AbstractLiving>,
118    >,
119    worlds: Res<Worlds>,
120) {
121    query
122        .par_iter_mut()
123        .for_each(|(mut fluid_on_eyes, position, dimensions, world_name)| {
124            let Some(world) = worlds.get(world_name) else {
125                return;
126            };
127
128            let adjusted_eye_y = position.y + (dimensions.eye_height as f64) - 0.1111111119389534;
129            let eye_block_pos = BlockPos::from(position.with_y(adjusted_eye_y));
130            let fluid_at_eye = world
131                .read()
132                .get_fluid_state(eye_block_pos)
133                .unwrap_or_default();
134            let fluid_cutoff_y = (eye_block_pos.y as f32 + fluid_at_eye.height()) as f64;
135            if fluid_cutoff_y > adjusted_eye_y {
136                **fluid_on_eyes = fluid_at_eye.kind;
137            } else {
138                **fluid_on_eyes = FluidKind::Empty;
139            }
140        });
141}
142
143pub fn update_on_climbable(
144    mut query: Query<(&mut OnClimbable, &Position, &WorldName, &GameMode), With<LocalEntity>>,
145    worlds: Res<Worlds>,
146) {
147    for (mut on_climbable, position, world_name, &game_mode) in query.iter_mut() {
148        if game_mode == GameMode::Spectator {
149            continue;
150        }
151
152        let Some(world) = worlds.get(world_name) else {
153            continue;
154        };
155
156        let world = world.read();
157
158        let block_pos = BlockPos::from(position);
159        let block_at_feet = world.get_block_state(block_pos).unwrap_or_default();
160        let registry_block_at_feet = block_at_feet.as_block_kind();
161
162        **on_climbable = tags::blocks::CLIMBABLE.contains(&registry_block_at_feet)
163            || (tags::blocks::TRAPDOORS.contains(&registry_block_at_feet)
164                && is_trapdoor_usable_as_ladder(block_at_feet, block_pos, &world));
165    }
166}
167
168#[expect(clippy::needless_bool, reason = "Kept for future improvements")]
169fn is_trapdoor_usable_as_ladder(
170    block_state: BlockState,
171    block_pos: BlockPos,
172    world: &azalea_world::World,
173) -> bool {
174    // trapdoor must be open
175    if !block_state
176        .property::<properties::Open>()
177        .unwrap_or_default()
178    {
179        return false;
180    }
181
182    // block below must be a ladder
183    let block_below = world.get_block_state(block_pos.down(1)).unwrap_or_default();
184    let registry_block_below = block_below.as_block_kind();
185    if registry_block_below != BlockKind::Ladder {
186        return false;
187    }
188    // and the ladder must be facing the same direction as the trapdoor
189    let ladder_facing = block_below
190        .property::<properties::FacingCardinal>()
191        .expect("ladder block must have facing property");
192    let trapdoor_facing = block_state
193        .property::<properties::FacingCardinal>()
194        .expect("trapdoor block must have facing property");
195    if ladder_facing != trapdoor_facing {
196        return false;
197    }
198
199    true
200}
201
202/// The local clients that have this entity loaded.
203///
204/// This is used for making sure entities aren't unloaded incorrectly when in
205/// shared worlds.
206///
207/// If this is empty, the entity will be removed from the ECS. If it's not
208/// present on an entity, then that entity won't be unloaded.
209#[derive(Clone, Component, Deref, DerefMut, Default)]
210pub struct LoadedBy(pub HashSet<Entity>);
211
212pub fn clamp_look_direction(mut query: Query<&mut LookDirection>) {
213    for mut look_direction in &mut query {
214        *look_direction = look_direction.clamped();
215    }
216}
217
218/// Sets the position of the entity.
219///
220/// This doesn't update the cache in azalea-world, and should only be used
221/// within azalea-world.
222///
223/// # Safety
224///
225/// Cached position in the world must be updated.
226#[allow(clippy::type_complexity)]
227pub fn update_bounding_box(
228    mut query: Query<
229        (&mut Physics, &Position, &EntityDimensions),
230        Or<(Changed<Position>, Changed<EntityDimensions>)>,
231    >,
232) {
233    for (mut physics, position, dimensions) in query.iter_mut() {
234        let bounding_box = dimensions.make_bounding_box(**position);
235        physics.bounding_box = bounding_box;
236    }
237}
238
239#[allow(clippy::type_complexity)]
240pub fn update_dimensions(
241    mut query: Query<
242        (&mut EntityDimensions, &EntityKindComponent, &Pose),
243        Or<(Changed<EntityKindComponent>, Changed<Pose>)>,
244    >,
245) {
246    for (mut dimensions, kind, pose) in query.iter_mut() {
247        *dimensions = calculate_dimensions(**kind, *pose);
248    }
249}
250
251#[allow(clippy::type_complexity)]
252pub fn update_crouching(
253    query: Query<(&mut Crouching, &Pose), (Without<LocalEntity>, With<Player>)>,
254) {
255    for (mut crouching, pose) in query {
256        let new_crouching = *pose == Pose::Crouching;
257        // avoid triggering change detection
258        if **crouching != new_crouching {
259            **crouching = new_crouching;
260        }
261    }
262}
263
264/// Marks an entity that's in a loaded chunk. This is updated at the beginning
265/// of every tick.
266///
267/// Internally, this is only used for player physics. Not to be confused with
268/// the somewhat similarly named [`LoadedBy`].
269#[derive(Clone, Component, Copy, Debug)]
270pub struct InLoadedChunk;
271
272/// Update the [`InLoadedChunk`] component for all entities in the world.
273pub fn update_in_loaded_chunk(
274    mut commands: bevy_ecs::system::Commands,
275    query: Query<(Entity, &WorldName, &Position, Option<&InLoadedChunk>)>,
276    worlds: Res<Worlds>,
277) {
278    for (entity, world_name, position, last_in_loaded_chunk) in &query {
279        let player_chunk_pos = ChunkPos::from(position);
280        let Some(world_lock) = worlds.get(world_name) else {
281            commands.entity(entity).remove::<InLoadedChunk>();
282            continue;
283        };
284
285        let in_loaded_chunk = world_lock.read().chunks.get(&player_chunk_pos).is_some();
286        if in_loaded_chunk {
287            if last_in_loaded_chunk.is_none() {
288                commands.entity(entity).insert(InLoadedChunk);
289            }
290        } else {
291            if last_in_loaded_chunk.is_some() {
292                commands.entity(entity).remove::<InLoadedChunk>();
293            }
294        }
295    }
296}
297
298/// Get the position of the block below the entity, but a little lower.
299pub fn on_pos_legacy(chunk_storage: &ChunkStorage, position: Position) -> BlockPos {
300    on_pos(0.2, chunk_storage, position)
301}
302
303// int x = Mth.floor(this.position.x);
304// int y = Mth.floor(this.position.y - (double)var1);
305// int z = Mth.floor(this.position.z);
306// BlockPos var5 = new BlockPos(x, y, z);
307// if (this.level.getBlockState(var5).isAir()) {
308//    BlockPos var6 = var5.below();
309//    BlockState var7 = this.level.getBlockState(var6);
310//    if (var7.is(BlockTags.FENCES) || var7.is(BlockTags.WALLS) ||
311// var7.getBlock() instanceof FenceGateBlock) {       return var6;
312//    }
313// }
314// return var5;
315pub fn on_pos(offset: f32, chunk_storage: &ChunkStorage, pos: Position) -> BlockPos {
316    let x = pos.x.floor() as i32;
317    let y = (pos.y - offset as f64).floor() as i32;
318    let z = pos.z.floor() as i32;
319    let pos = BlockPos { x, y, z };
320
321    // TODO: check if block below is a fence, wall, or fence gate
322    let block_pos = pos.down(1);
323    let block_state = chunk_storage.get_block_state(block_pos);
324    if block_state == Some(BlockState::AIR) {
325        let block_pos_below = block_pos.down(1);
326        let block_state_below = chunk_storage.get_block_state(block_pos_below);
327        if let Some(_block_state_below) = block_state_below {
328            // if block_state_below.is_fence()
329            //     || block_state_below.is_wall()
330            //     || block_state_below.is_fence_gate()
331            // {
332            //     return block_pos_below;
333            // }
334        }
335    }
336
337    pos
338}
339
340#[cfg(test)]
341mod tests {
342    use azalea_block::{
343        blocks::{Ladder, OakTrapdoor},
344        properties::{FacingCardinal, TopBottom},
345    };
346    use azalea_core::position::{BlockPos, ChunkPos};
347    use azalea_registry::builtin::BlockKind;
348    use azalea_world::{Chunk, ChunkStorage, PartialWorld, World};
349
350    use super::is_trapdoor_usable_as_ladder;
351
352    #[test]
353    fn test_is_trapdoor_useable_as_ladder() {
354        let mut partial_world = PartialWorld::default();
355        let mut chunks = ChunkStorage::default();
356        partial_world.chunks.set(
357            &ChunkPos { x: 0, z: 0 },
358            Some(Chunk::default()),
359            &mut chunks,
360        );
361        partial_world.chunks.set_block_state(
362            BlockPos::new(0, 0, 0),
363            BlockKind::Stone.into(),
364            &chunks,
365        );
366
367        let ladder = Ladder {
368            facing: FacingCardinal::East,
369            waterlogged: false,
370        };
371        partial_world
372            .chunks
373            .set_block_state(BlockPos::new(0, 0, 0), ladder.into(), &chunks);
374
375        let trapdoor = OakTrapdoor {
376            facing: FacingCardinal::East,
377            half: TopBottom::Bottom,
378            open: true,
379            powered: false,
380            waterlogged: false,
381        };
382        partial_world
383            .chunks
384            .set_block_state(BlockPos::new(0, 1, 0), trapdoor.into(), &chunks);
385
386        let world = World::from(chunks);
387        let trapdoor_matches_ladder = is_trapdoor_usable_as_ladder(
388            world
389                .get_block_state(BlockPos::new(0, 1, 0))
390                .unwrap_or_default(),
391            BlockPos::new(0, 1, 0),
392            &world,
393        );
394
395        assert!(trapdoor_matches_ladder);
396    }
397}