From e74a1fe46155aad64f29ac0ae5d05e151632d0a3 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 06:31:46 +0100 Subject: [PATCH] Use every player in the level for patrols, sniffers, allays, trap horses and other world checks on the server thread --- crates/horizon-client/src/entity_nbt.rs | 14 +++++-- crates/horizon-client/src/game/allays.rs | 41 +++++++++++++++++-- crates/horizon-client/src/game/creatures.rs | 14 +++---- crates/horizon-client/src/game/dragon.rs | 13 +++--- crates/horizon-client/src/game/lan_actions.rs | 5 ++- crates/horizon-client/src/game/passengers.rs | 17 +++++--- crates/horizon-client/src/game/raids.rs | 18 ++++---- crates/horizon-client/src/game/sculk.rs | 40 ++++++++---------- crates/horizon-client/src/game/sniffers.rs | 30 ++++++++------ 9 files changed, 119 insertions(+), 73 deletions(-) diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index c49af48c9..d175a14fb 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -450,7 +450,10 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { "memories", Tag::compound([( "minecraft:liked_player", - Tag::compound([("value", Tag::IntArray(vec![0, 0, 0, 1]))]), + Tag::compound([( + "value", + Tag::IntArray(m.extra.owner.unwrap_or([0, 0, 0, 1]).to_vec()), + )]), )]), )]), ); @@ -994,11 +997,14 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - m.extra.ints[1] = number(first.get("count")).unwrap_or(1) as i32; } m.extra.egg_time = number(tag.get("DuplicationCooldown")).unwrap_or(0) as i32; - m.extra.tame = tag + let liked = tag .get("Brain") .and_then(|b| b.get("memories")) - .and_then(|b| b.get("minecraft:liked_player")) - .is_some(); + .and_then(|b| b.get("minecraft:liked_player")); + m.extra.tame = liked.is_some(); + m.extra.owner = liked + .and_then(|l| l.get("value")) + .and_then(|v| crate::save::uuid_from_tag(Some(v))); } if crate::mobs::camel::is_camel(m.species()) { let sitting = matches!(tag.get("LastPoseTick"), Some(Tag::Long(v)) if *v < 0); diff --git a/crates/horizon-client/src/game/allays.rs b/crates/horizon-client/src/game/allays.rs index bed835663..10ad5c315 100644 --- a/crates/horizon-client/src/game/allays.rs +++ b/crates/horizon-client/src/game/allays.rs @@ -8,7 +8,6 @@ use horizon_core::random::RandomSource; impl Game<'_> { pub(super) fn tick_allays(&mut self) { let griefing = self.ui.rules.bool("mob_griefing"); - let player = self.player.as_ref().map(|p| p.position); // The playing jukeboxes, found at the first allay: no allay starts // or stops one, and the block entities run into the thousands. let mut jukeboxes: Option> = None; @@ -58,7 +57,7 @@ impl Game<'_> { }; let Some((j, _)) = wanted else { self.mobs[i].extra.point = None; - self.allay_deliver(i, player); + self.allay_deliver(i); continue; }; let e = &self.dropped[j]; @@ -90,14 +89,46 @@ impl Game<'_> { } } } + /// `AllayAi.getLikedPlayer`: the player the allay was given an item by, + /// found by UUID among the host and the guests of the level (the window + /// is a guest on the server thread), when they are not a spectator and + /// within 64 blocks. + fn allay_liked_player(&self, i: usize) -> Option { + let m = &self.mobs[i]; + if !m.extra.tame { + return None; + } + // An allay saved before its player was kept belongs to the host. + let uuid = m + .extra + .owner + .filter(|u| *u != [0, 0, 0, 1]) + .or(self.level.player_uuid) + .unwrap_or([0, 0, 0, 1]); + let host = self + .player_view() + .filter(|p| !p.spectator && self.level.player_uuid.unwrap_or([0, 0, 0, 1]) == uuid) + .map(|p| p.position); + let found = host.or_else(|| { + self.lan_present() + .into_iter() + .find(|p| p.uuid == uuid && !p.spectator) + .map(|p| { + horizon_core::geometry::Vec3::new(p.position[0], p.position[1], p.position[2]) + }) + }); + found.filter(|p| p.distance_squared(m.position) < 64.0 * 64.0) + } /// `GoAndGiveItemsToTarget` / `AllayAi.throwItem`: within 3 blocks of /// the deposit, one item a tick, each restarting the three-second pause /// before it picks anything up again. - fn allay_deliver(&mut self, i: usize, player: Option) { + fn allay_deliver(&mut self, i: usize) { let m = &self.mobs[i]; if m.extra.ints[1] <= 0 { return; } + let player = self.allay_liked_player(i); + let m = &self.mobs[i]; let target = match m.extra.home.filter(|_| m.extra.ints[2] > 0) { Some(h) => horizon_core::geometry::Vec3::new( f64::from(h[0]) + 0.5, @@ -166,11 +197,14 @@ impl Game<'_> { self.consume_held(); return true; } + let acting = self.acting_uuid(); let holds = m.extra.main_hand.is_some(); if !holds && !item.is_empty() { let m = &mut self.mobs[index]; m.extra.main_hand = Some(item.into()); m.extra.tame = true; + // `Allay.mobInteract` remembers LIKED_PLAYER by UUID. + m.extra.owner = Some(acting); self.audio .play("minecraft:entity.allay.item_given", Some(at), 2.0, 1.0); self.consume_held(); @@ -185,6 +219,7 @@ impl Game<'_> { .unwrap_or_else(|| "minecraft:air".into()); let carried = std::mem::take(&mut m.extra.ints[1]); m.extra.tame = false; + m.extra.owner = None; self.audio .play("minecraft:entity.allay.item_taken", Some(at), 2.0, 1.0); if carried > 0 { diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 2bc40a7af..b6720b928 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -424,9 +424,10 @@ impl Game<'_> { sea_level: if self.dim == Dim::Nether { 32 } else { 63 }, teams: std::sync::Arc::new(self.mob_teams()), items: Some(self.res.items()), + // The first player in the level: a server has no player of its own. difficulty: self.local_difficulty( - self.player - .as_ref() + self.level_players() + .first() .map_or([0.0; 3], |p| [p.position.x, p.position.y, p.position.z]), ), } @@ -3754,13 +3755,10 @@ impl Game<'_> { f64::from((pos[0] >> 4) * 16 + 8), f64::from((pos[2] >> 4) * 16 + 8), ]; - let spectator = self.spectator(); - let Some(close) = self.player.as_ref().filter(|_| !spectator).map(|p| { + let close = self.level_players().iter().any(|p| { let (dx, dz) = (p.position.x - centre[0], p.position.z - centre[1]); - dx * dx + dz * dz < 16384.0 - }) else { - return; - }; + !p.spectator && dx * dx + dz * dz < 16384.0 + }); if !close { return; } diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index 8f3fdb224..2970d6e74 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -2859,14 +2859,11 @@ impl Game<'_> { let (a, b) = (room.min_position(), room.max_position()); lo.x < b.x && hi.x > a.x && lo.y < b.y && hi.y > a.y && lo.z < b.z && hi.z > a.z }; - let player = self.player.as_ref().is_some_and(|p| { - let w = f64::from(p.dimensions.width) / 2.0; - let h = f64::from(p.dimensions.height); - touches( - Vec3::new(p.position.x - w, p.position.y, p.position.z - w), - Vec3::new(p.position.x + w, p.position.y + h, p.position.z + w), - ) - }); + // Level.getEntities: the host's player and every guest in the level. + let player = self + .fight_players() + .iter() + .any(|p| !p.spectator && touches(p.bounds.min_position(), p.bounds.max_position())); player || self.crystals.iter().any(|c| { touches( diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 8c51a3f2a..6587ed6b9 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -2321,7 +2321,10 @@ impl Game<'_> { if let Some((at, velocity)) = soul { super::lan_particles::send_soul_particle(host, at, velocity); } - self.as_guest(host, conn, |game| game.tick_player_sculk_events()); + self.as_guest(host, conn, |game| { + game.sculk_step_on_player(); + game.tick_player_sculk_events(); + }); for event in events { match event { MotionEvent::HurtSound(sound) => self.player_hurt_sound(sound, at), diff --git a/crates/horizon-client/src/game/passengers.rs b/crates/horizon-client/src/game/passengers.rs index 8f205c484..c39a6d1af 100644 --- a/crates/horizon-client/src/game/passengers.rs +++ b/crates/horizon-client/src/game/passengers.rs @@ -279,11 +279,13 @@ impl Game<'_> { /// horse is a trap for fifteen minutes; a player within ten blocks springs /// it. pub(super) fn tick_skeleton_traps(&mut self) { - let player = self - .player - .as_ref() - .filter(|p| !p.health.dead_or_dying()) - .map(|p| p.position); + // `Level.hasNearbyAlivePlayer`: alive, not a spectator, in the level. + let players: Vec<_> = self + .level_players() + .into_iter() + .filter(|p| p.alive && !p.spectator) + .map(|p| p.position) + .collect(); let mut sprung = Vec::new(); for m in &mut self.mobs { if m.species() != Species::SkeletonHorse || !m.extra.trap || !m.alive() { @@ -296,7 +298,10 @@ impl Game<'_> { continue; } } - if player.is_some_and(|p| m.distance_sqr(p) < TRAP_RANGE * TRAP_RANGE) { + if players + .iter() + .any(|&p| m.distance_sqr(p) < TRAP_RANGE * TRAP_RANGE) + { sprung.push(m.id); } } diff --git a/crates/horizon-client/src/game/raids.rs b/crates/horizon-client/src/game/raids.rs index da7c79394..89fca4571 100644 --- a/crates/horizon-client/src/game/raids.rs +++ b/crates/horizon-client/src/game/raids.rs @@ -102,11 +102,12 @@ impl RaidLevel for Lvl<'_, '_> { && g.res.facts().is_air(g.world.block(x, y, z)) } fn player(&self) -> Option { - let p = self.game.player.as_ref()?; + // The first player in the level: a server has no player of its own. + let p = self.game.level_players().into_iter().next()?; Some(RaidPlayer { position: [p.position.x, p.position.y, p.position.z], block: block_of(p.position), - alive: !p.health.dead_or_dying(), + alive: p.alive, }) } } @@ -174,12 +175,13 @@ impl PatrolLevel for PatrolCx<'_, '_, '_> { fn bright_outside(&self) -> bool { self.level.game.dim == Dim::Overworld && self.level.game.sky_darken() < 4 } + /// `PatrolSpawner.tick` picks from `level.players()`: the local player + /// and every guest in the level. fn patrol_players(&self) -> Vec<([i32; 3], bool)> { - let g = self.level.game; - g.player - .as_ref() - .map(|p| (block_of(p.position), g.spectator())) - .into_iter() + self.level + .players + .iter() + .map(|p| (block_of(p.position), p.spectator)) .collect() } fn sections_to_village(&self, pos: [i32; 3]) -> i32 { @@ -633,7 +635,7 @@ impl Game<'_> { /// `Raid.playSound`: the horn, 13 blocks away from the player toward the /// wave, for players within 64 blocks or in the raid. fn raid_horn(&mut self, id: i32, pos: [i32; 3]) { - let Some(p) = self.player.as_ref().map(|p| p.position) else { + let Some(p) = self.level_players().first().map(|p| p.position) else { return; }; let in_raid = self.raiding.raids.get(id).is_some_and(|r| r.bar.player); diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 7916c46a1..d3111a75f 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -876,17 +876,26 @@ impl Game<'_> { /// `SculkSensorBlock.stepOn` and `SculkShriekerBlock.stepOn`: standing on /// them is heard (or seen) every tick. fn sculk_step_on(&mut self) { + self.sculk_step_on_player(); let mut standing: Vec<(Who, [f64; 3])> = Vec::new(); - if let Some(p) = self.player.as_ref() - && p.ground.on_ground - { - standing.push((Who::Player, [p.position.x, p.position.y, p.position.z])); - } for m in &self.mobs { if m.alive() && m.on_ground { standing.push((Who::Mob(m.id), [m.position.x, m.position.y, m.position.z])); } } + self.sculk_stand_on(standing); + } + /// `stepOn` for the player in place: the host, or a guest swapped in + /// (`guest_server_tick`), as a server has no player of its own. + pub(super) fn sculk_step_on_player(&mut self) { + if let Some(p) = self.player.as_ref() + && p.ground.on_ground + { + let at = [p.position.x, p.position.y, p.position.z]; + self.sculk_stand_on(vec![(Who::Player, at)]); + } + } + fn sculk_stand_on(&mut self, standing: Vec<(Who, [f64; 3])>) { for (who, at) in standing { // getOnPosLegacy: 0.2 below the feet. let key = ( @@ -1108,23 +1117,10 @@ impl Game<'_> { /// in the way (`isUnobstructed`), and no block collision. fn warden_fits(&self, mob: &Mob) -> bool { let bounds = mob.bounds(); - let player_blocks = !self.spectator() - && self.player.as_ref().is_some_and(|p| { - let w = f64::from(p.dimensions.width) / 2.0; - let b = horizon_core::geometry::Aabb::new( - horizon_core::geometry::Vec3::new( - p.position.x - w, - p.position.y, - p.position.z - w, - ), - horizon_core::geometry::Vec3::new( - p.position.x + w, - p.position.y + f64::from(p.dimensions.height), - p.position.z + w, - ), - ); - !p.health.dead_or_dying() && b.intersects(bounds) - }); + let player_blocks = self + .level_players() + .iter() + .any(|p| p.alive && !p.spectator && p.bounds.intersects(bounds)); if player_blocks || self .mobs diff --git a/crates/horizon-client/src/game/sniffers.rs b/crates/horizon-client/src/game/sniffers.rs index 8e851d028..5027f5c2b 100644 --- a/crates/horizon-client/src/game/sniffers.rs +++ b/crates/horizon-client/src/game/sniffers.rs @@ -5,9 +5,8 @@ use super::Game; use horizon_client::{ items::ItemStack, mobs::{Mob, Species, sniffer}, - particles::Particle, }; -use horizon_core::random::RandomSource; +use horizon_core::{geometry::Vec3, random::RandomSource}; impl Game<'_> { /// The egg item at the parent (`Sniffer.spawnChildFromBreeding`; the @@ -46,15 +45,20 @@ impl Game<'_> { /// block's hit sound between the 1.7th and the 6th second of the /// digging animation) and `playSearchingSound`. pub(super) fn tick_sniffer_effects(&mut self) { - let Some(camera) = self.player.as_ref().map(|p| p.position) else { + // Vanilla runs these on each client: a sniffer within 32 blocks of + // any player in the level (the host and the guests) shows them. + let players = self.level_players(); + if players.is_empty() { return; - }; + } for i in 0..self.mobs.len() { let m = &self.mobs[i]; if m.species() != Species::Sniffer || !m.alive() || m.silent - || m.position.distance_squared(camera) > 32.0 * 32.0 + || !players + .iter() + .any(|p| m.position.distance_squared(p.position) <= 32.0 * 32.0) { continue; } @@ -79,20 +83,20 @@ impl Game<'_> { } let head = sniffer::head_block(m); let below = self.world.block(head[0], head[1] - 1, head[2]); - let Some((sprite, tint)) = self.particle_sprite(below, [head[0], head[1] - 1, head[2]]) - else { + if self + .particle_sprite(below, [head[0], head[1] - 1, head[2]]) + .is_none() + { continue; - }; + } let dust = [ f64::from(head[0]) + 0.5, f64::from(head[1]) + 0.5 - f64::from(0.65_f32), f64::from(head[2]) + 0.5, ]; - for _ in 0..30 { - let particle = - Particle::terrain(&mut self.world.random, dust, [0.0; 3], sprite.clone(), tint); - self.particles.push(particle); - } + // Drawn in this window when it has one, and sent to the players + // within 32 blocks (the server thread's window is one of them). + self.landing_dust(None, below, Vec3::new(dust[0], dust[1], dust[2]), 30, 0.0); if self.mobs[i].age.is_multiple_of(10) { let hit = self.res.facts().get(below).sounds.hit.clone(); self.audio.play(&hit, Some(at), 0.5, 0.5);