From 875773a08fc568fe637b0255799c3fc5f87a35d8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 23:10:31 +0200 Subject: [PATCH] Villagers, traders and golems never despawn for distance Village villagers are placed from the jigsaw templates without PersistenceRequired (the village's iron golem has it), so Mob.checkDespawn discarded every villager of a village generated more than 128 blocks from the player, and the rest after a while beyond 32 blocks. Vanilla's Villager, WanderingTrader and AbstractGolem (iron, snow and copper golems, shulkers) override removeWhenFarAway to false. Fixes #917. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/mobs/ai.rs | 51 ++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 475bd7611..709a57619 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -1026,14 +1026,11 @@ fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool { || super::nautilus::is_nautilus(m.species()) && m.extra.tame || m.species() == Species::SulfurCube && (super::sulfur_cube::has_body(m) || m.extra.scratch[3] != 0); - // removeWhenFarAway: animals, wardens, allays, patrols and raiders, - // zombie villagers being cured and ridden mobs stay; the wither's - // checkDespawn never removes it for distance. + // removeWhenFarAway: animals, villagers, golems, patrols and raiders, + // zombie villagers being cured and ridden mobs stay. let creature = m.kind.category == "creature"; - let removable = !(matches!( - m.species(), - Species::Warden | Species::Allay | Species::Wither - ) || creature && m.kind.flags.animal && !m.extra.jockey + let removable = !(never_removed_when_far(m.species()) + || creature && m.kind.flags.animal && !m.extra.jockey || super::raider::keeps_alive(m, d) || super::zombie_villager::keeps_alive(m) || super::riders::keeps_alive(m)); @@ -1059,6 +1056,24 @@ fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool { } true } +/// The types whose `removeWhenFarAway` is always false (`Villager`, +/// `WanderingTrader`, `AbstractGolem`: iron, snow and copper golems and +/// shulkers; `Allay`, `Warden`), and the wither, whose `checkDespawn` never +/// removes it for distance. +fn never_removed_when_far(s: Species) -> bool { + matches!( + s, + Species::Villager + | Species::WanderingTrader + | Species::IronGolem + | Species::SnowGolem + | Species::CopperGolem + | Species::Shulker + | Species::Allay + | Species::Warden + | Species::Wither + ) +} /// Movement speed bonus multiplier (baby zombies are 50% faster, etc.). pub fn speed_bonus(m: &Mob) -> f64 { match m.species() { @@ -1579,3 +1594,25 @@ mod bucket_axolotl_tests { assert_eq!(family(Species::Axolotl), Family::Aquatic); } } +#[cfg(test)] +mod despawn_tests { + use super::*; + /// Village villagers are placed without PersistenceRequired (unlike the + /// village's iron golem), so they must never despawn for distance. + #[test] + fn villagers_traders_and_golems_never_despawn_for_distance() { + for s in [ + Species::Villager, + Species::WanderingTrader, + Species::IronGolem, + Species::SnowGolem, + Species::CopperGolem, + Species::Shulker, + ] { + assert!(never_removed_when_far(s), "{s:?}"); + } + for s in [Species::Zombie, Species::ZombieVillager, Species::Pillager] { + assert!(!never_removed_when_far(s), "{s:?}"); + } + } +}