From bf508c2842825c09abb986261342f53e38953af7 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 18:45:39 +0100 Subject: [PATCH] Let mobs, dropped items and projectiles trigger pressure plates and tripwires --- crates/horizon-client/src/game.rs | 91 ++++++++++++++++++++++-- crates/horizon-client/src/projectiles.rs | 17 +++++ 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index ddb07912a..1cd69e037 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -546,6 +546,11 @@ struct GameLevel<'w> { cart_boxes: Vec<([f64; 3], [f64; 3])>, /// The other entities that keep blocks from being placed into them. blockers: Blockers<'w>, + /// Item entities, experience orbs and projectiles, which trigger plates and + /// tripwires but never keep a block from being placed. + dropped: &'w [ItemEntity], + orbs: &'w [ExperienceOrb], + projectiles: &'w [horizon_client::projectiles::Projectile], biomes: &'w Biomes, seed: i64, behaviours: &'w Behaviours, @@ -615,6 +620,53 @@ impl EntityObstructionWorld for BuildingObstruction { } } impl GameLevel<'_> { + /// The mobs that trigger plates and tripwires: living entities that don't + /// ignore block triggers (`Entity.isIgnoringBlockTriggers`: bats; armor + /// stands are not in `mobs`). + fn mob_trigger_boxes(&self) -> impl Iterator + '_ { + self.blockers + .mobs + .iter() + .filter(|m| !m.removed && m.species() != horizon_client::mobs::Species::Bat) + .map(Mob::bounds) + } + /// The non-living entities that trigger plates and tripwires (boats, + /// crystals, primed TNT, falling blocks, item entities, experience orbs and + /// projectiles). + fn other_trigger_boxes(&self) -> impl Iterator + '_ { + let b = self.blockers; + let square = |p: Vec3, size: f64| { + Aabb::new( + Vec3::new(p.x - size / 2.0, p.y, p.z - size / 2.0), + Vec3::new(p.x + size / 2.0, p.y + size, p.z + size / 2.0), + ) + }; + b.boats + .iter() + .map(boats::Boat::bounds) + .chain(b.crystals.iter().map(|c| { + // EntityType.END_CRYSTAL: 2 x 2. + let p = c.position; + Aabb::new( + Vec3::new(p.x - 1.0, p.y, p.z - 1.0), + Vec3::new(p.x + 1.0, p.y + 2.0, p.z + 1.0), + ) + })) + .chain(b.tnts.iter().map(tnt::PrimedTnt::bounds)) + .chain( + b.falling + .iter() + .map(|f| horizon_client::falling_block::bounds(f.position)), + ) + .chain(self.dropped.iter().map(ItemEntity::bounds)) + // EntityType.EXPERIENCE_ORB: 0.5 x 0.5. + .chain(self.orbs.iter().map(move |o| square(o.position, 0.5))) + .chain( + self.projectiles + .iter() + .map(move |p| square(p.position, p.kind.size())), + ) + } /// Every entity box that blocks building: the players who aren't /// spectators, the minecarts and the `Blockers` (a removed mob or a marker /// armor stand, whose box is empty, doesn't). @@ -838,7 +890,8 @@ impl LevelAccess for GameLevel<'_> { self.tickets.block_ticking(chunk.x, chunk.z) && ticking_chunk(self.world, chunk.x, chunk.z) } /// Entities that trigger pressure plates, tripwires and detector rails: - /// the players (the host and the LAN guests, mobs) and the minecarts. + /// the players (the host and the LAN guests), the mobs, the minecarts and + /// the other entities that don't ignore block triggers. fn count_entities(&self, area: Aabb, query: EntityQuery) -> usize { let hit = |(lo, hi): &([f64; 3], [f64; 3])| { area.intersects(Aabb::new( @@ -847,6 +900,16 @@ impl LevelAccess for GameLevel<'_> { )) }; let carts = || self.cart_boxes.iter().filter(|b| hit(b)).count(); + let mobs = || { + self.mob_trigger_boxes() + .filter(|b| area.intersects(*b)) + .count() + }; + let others = || { + self.other_trigger_boxes() + .filter(|b| area.intersects(*b)) + .count() + }; let players = || { self.player_box .iter() @@ -856,8 +919,8 @@ impl LevelAccess for GameLevel<'_> { }; match query { EntityQuery::Minecart => carts(), - EntityQuery::Mobs => players(), - EntityQuery::Everything => players() + carts(), + EntityQuery::Mobs => players() + mobs(), + EntityQuery::Everything => players() + carts() + mobs() + others(), } } fn emit(&mut self, event: LevelEvent) { @@ -2495,6 +2558,9 @@ impl Game<'_> { let mut level = GameLevel { cart_boxes, blockers, + dropped: &self.dropped, + orbs: &self.orbs, + projectiles: &self.creatures.projectiles, world: &mut self.world, facts: self.res.facts(), collision: self.collision, @@ -2625,7 +2691,8 @@ impl Game<'_> { .filter(|&(x, z)| levels.random_ticking(x, z) && ticking_chunk(&self.world, x, z)) .collect(); // Entity.checkInsideBlocks for the plates, tripwires and detector rails - // under the players (the host and the LAN guests) and the minecarts. + // under the players (the host and the LAN guests) and the minecarts + // (the mobs and other entities are added in `with_level` below). let mut inside = Vec::new(); let guests = self.lan_block_trigger_boxes(); let boxes = self @@ -2686,7 +2753,21 @@ impl Game<'_> { } } self.with_level(|b, level, ticks| { - for pos in inside { + // The mobs and the other entities (items, orbs, projectiles...) too. + let mut touched: Vec = Vec::new(); + for bounds in level.mob_trigger_boxes().chain(level.other_trigger_boxes()) { + let (lo, hi) = (bounds.min_position(), bounds.max_position()); + for x in (lo.x + 1e-7).floor() as i32..=(hi.x - 1e-7).floor() as i32 { + for y in (lo.y + 1e-7).floor() as i32..=(hi.y - 1e-7).floor() as i32 { + for z in (lo.z + 1e-7).floor() as i32..=(hi.z - 1e-7).floor() as i32 { + touched.push(BlockPos::new(x, y, z)); + } + } + } + } + touched.sort_unstable_by_key(|p| (p.x, p.y, p.z)); + touched.dedup(); + for pos in inside.into_iter().chain(touched) { b.entity_inside(level, ticks, pos); } for (pos, on_ground, feet_y) in standing { diff --git a/crates/horizon-client/src/projectiles.rs b/crates/horizon-client/src/projectiles.rs index 219d59b6b..a1d6e5215 100644 --- a/crates/horizon-client/src/projectiles.rs +++ b/crates/horizon-client/src/projectiles.rs @@ -62,6 +62,23 @@ pub enum ProjectileKind { }, } impl ProjectileKind { + /// The width and height of its entity type's `sized(...)`. + pub fn size(&self) -> f64 { + match self { + Self::Arrow { .. } => 0.5, + Self::Snowball + | Self::Egg + | Self::EnderPearl + | Self::LlamaSpit + | Self::Potion { .. } => 0.25, + Self::SmallFireball + | Self::WindCharge + | Self::BreezeWindCharge + | Self::ShulkerBullet { .. } + | Self::WitherSkull { .. } => 0.3125, + Self::LargeFireball { .. } => 1.0, + } + } /// Its entity type. pub fn id(&self) -> &'static str { match self {