The spawn egg's offspring is a named, tested rule

The behaviour is in main (#2275): `Mob.checkAndHandleImportantInteractions`
uses a spawn egg of the mob's own kind on a mob, and
`SpawnEggItem.spawnOffspringFromSpawnEgg` spawns the offspring as a baby
of that kind at the parent's position, and one egg less in hand.

The rule now lives in the library, where it is named and tested:
`spawn_egg_is_for` (the egg is for the mob's own kind) and
`spawn_egg_offspring` (the baby, with the parent's variant and colour).

Fixes #2269

Co-Authored-By: minimax-m3.1-flash (Claude Code) <noreply@users.noreply.github.com>
This commit is contained in:
levzzz
2026-10-27 17:24:09 +05:00
parent 1d2c5db426
commit 1654cb51db
2 changed files with 51 additions and 6 deletions
+2 -6
View File
@@ -3604,7 +3604,7 @@ impl Game<'_> {
};
let parent = &self.mobs[index];
if !parent.alive()
|| stack.item.strip_suffix("_spawn_egg") != Some(parent.kind.id.as_str())
|| !horizon_client::mobs::finalize::spawn_egg_is_for(&stack.item, &parent.kind.id)
|| !horizon_client::mobs::drops::can_be_baby(parent.species())
// WanderingTrader.getBreedOffspring returns null: no offspring, the
// click carries on to trading.
@@ -3623,11 +3623,7 @@ impl Game<'_> {
return false;
};
if let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) {
horizon_client::mobs::finalize::set_baby(child, true);
child.extra.variant = variant;
if species == Species::Sheep {
child.extra.color = color;
}
horizon_client::mobs::finalize::spawn_egg_offspring(child, species, variant, color);
}
self.trigger_item_entity("minecraft:player_interacted_with_entity", &stack, &target);
self.consume_held();
@@ -817,6 +817,22 @@ fn age_boundary_reached(m: &mut Mob) {
}
}
/// `setBaby`: AgeableMob through its age, zombies, piglins and zoglins.
/// `SpawnEggItem.spawnsEntity`: the spawn egg is for the mob's own kind, so
/// it is used on it (`Mob.checkAndHandleImportantInteractions`).
pub fn spawn_egg_is_for(egg: &str, kind: &str) -> bool {
egg.strip_suffix("_spawn_egg") == Some(kind)
}
/// `SpawnEggItem.spawnOffspringFromSpawnEgg`: the offspring of a spawn egg
/// used on a mob of the same kind is a baby of that kind: it starts with
/// the age of a baby and keeps the parent's variant and colour.
pub fn spawn_egg_offspring(child: &mut Mob, species: Species, variant: u8, color: u8) {
set_baby(child, true);
child.extra.variant = variant;
if species == Species::Sheep {
child.extra.color = color;
}
}
pub fn set_baby(m: &mut Mob, baby: bool) {
let s = m.species();
if ageable(s) {
@@ -2077,6 +2093,39 @@ mod tests {
assert_eq!(panda_variant(4, 4), 4);
assert_eq!(panda_variant(6, 0), 6);
}
/// #2269: the offspring of a spawn egg used on a mob is a baby of that
/// kind (`SpawnEggItem.spawnOffspringFromSpawnEgg`), and only the mob's
/// own kind takes the egg.
#[test]
fn a_spawn_egg_offspring_is_a_baby() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client")
.join(horizon_core::VANILLA_VERSION);
if !root.join("items.json").exists() {
return;
}
let data = crate::mobs::MobTypes::load(&root).unwrap();
let kind = std::sync::Arc::new(data.types.get("minecraft:cow").unwrap().clone());
let mut child = crate::mobs::Mob::new(
2,
kind,
horizon_core::geometry::Vec3::new(0.0, 4.0, 0.0),
0.0,
);
assert!(!child.is_baby());
spawn_egg_offspring(&mut child, Species::Cow, 3, 0);
// The offspring is a baby of the mob's kind.
assert!(child.is_baby());
// It keeps the parent's variant.
assert_eq!(child.extra.variant, 3);
// The egg is for the mob's own kind.
assert!(spawn_egg_is_for("minecraft:cow_spawn_egg", "minecraft:cow"));
assert!(!spawn_egg_is_for(
"minecraft:pig_spawn_egg",
"minecraft:cow"
));
assert!(!spawn_egg_is_for("minecraft:stick", "minecraft:cow"));
}
#[test]
fn reasons_round_trip() {
for r in Reason::ALL {