From b56b4c3cea18373d18dd16f8f619db5bda227b11 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:14:20 +0200 Subject: [PATCH] Bring the copper golem into the game and build it and shear it as vanilla does The copper golem code (its goals, chest transport, weathering, statues and interactions) was in the tree but not compiled; it is now a species with its model, keyframe animations, sounds and game hooks. Its antenna flower sits in the saddle slot as vanilla's EQUIPMENT_SLOT_ANTENNA, sheared by players and dispensers with the unequip event and the 1.5-up drop, and dropped with the carried stack as preserved equipment. A pumpkin on a copper block builds it after the snow and iron golem patterns: both blocks clear, the block becomes the matching copper chest (CopperChestBlock getFromCopperBlock with the least oxidized of connected chests), and the golem takes the block's weathering and plays its spawn sound. Weathering draws from the level random, the statue pose from the golem's own. The dispenser harness now builds copper golems and shears and equips them: 6000/6000 cases match. Block.popResource of tall flowers and flower beds and the composter's bone meal pop when the behaviour asks, as vanilla does. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 44 +++---- .../horizon-client/src/game/copper_golems.rs | 96 +++++++++++----- crates/horizon-client/src/game/creatures.rs | 8 ++ .../src/game/dispense_fixture.rs | 18 +-- crates/horizon-client/src/game/dispenser.rs | 21 +++- crates/horizon-client/src/game/golems.rs | 108 +++++++++++------- crates/horizon-client/src/game/spill.rs | 18 +++ crates/horizon-client/src/mobs.rs | 5 +- crates/horizon-client/src/mobs/ai.rs | 4 +- crates/horizon-client/src/mobs/anim.rs | 2 + .../horizon-client/src/mobs/copper_golem.rs | 20 +++- crates/horizon-client/src/mobs/species.rs | 16 ++- crates/horizon-world/src/level/mod.rs | 12 ++ crates/horizon-world/src/level/place.rs | 86 +++++++++++++- .../parity/VanillaDispenserOracle.java | 3 +- tools/test_dispenser_parity.py | 16 ++- 16 files changed, 352 insertions(+), 125 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index d70d153c4..8a4b619b8 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -16,6 +16,7 @@ mod chat; mod combat; mod commands; mod control; +mod copper_golems; mod creaking; mod creatures; mod crossbow; @@ -473,6 +474,18 @@ impl LevelAccess for GameLevel<'_> { match event { // Block.dropResources draws its loot and spots right away. LevelEvent::DropResources { pos, state } => self.drop_resources_now(pos, state), + // Block.popResource of the block's own item (tall flowers, flower beds). + LevelEvent::BlockEvent { + pos, + state, + kind: "pop_block_item", + } => self.pop_block_item(pos, state), + // ComposterBlock.extractProduce: bone meal above the composter. + LevelEvent::Player { + pos, + effect: PlayerEffect::DropItem(item), + .. + } => self.pop_above(pos, item), event => self.events.push(event), } } @@ -716,6 +729,8 @@ struct Game<'a> { ender_open: Option<(i32, i32, i32)>, /// Block entity data of broken blocks waiting for their dropped item. pending: spill::Pending, + /// The chest each copper golem has open (`CopperGolem.openedChestPos`). + golem_openers: HashMap, /// Item entities spawned and block entities removed while the block /// behaviour ran (spilled contents, popped resources). spill: spill::Spill, @@ -1505,15 +1520,8 @@ impl Game<'_> { self.pop_resource([pos.x, pos.y, pos.z], rest); } } - PlayerEffect::DropItem(item) => self.pop_resource( - [pos.x, pos.y + 1, pos.z], - ItemStack { - item, - count: 1, - damage: 0, - extra: None, - }, - ), + // Popped as the behaviour emits it (GameLevel::emit). + PlayerEffect::DropItem(_) => {} PlayerEffect::EatCake => { if let Some(p) = self.player.as_mut() { let food = &mut p.health.food; @@ -1571,22 +1579,6 @@ impl Game<'_> { state, kind: "spawn_ghastling", } => self.spawn_ghastling([pos.x, pos.y, pos.z], state), - LevelEvent::BlockEvent { - pos, - state, - kind: "pop_block_item", - } => { - let item = self.res.facts.get(state).item.clone(); - self.pop_resource( - [pos.x, pos.y, pos.z], - ItemStack { - item, - count: 1, - damage: 0, - extra: None, - }, - ); - } LevelEvent::GameEvent { pos, event, state } => { self.sculk_level_event(pos, event, state) } @@ -2734,6 +2726,7 @@ impl Game<'_> { self.spawn_loaded_entities(); if running { self.tick_mobs()?; + self.tick_copper_golems(); self.tick_experience(); } self.tick_lan(); @@ -8117,6 +8110,7 @@ pub fn run() -> Result<()> { ender_items: vec![None; 27], ender_open: None, pending: Default::default(), + golem_openers: HashMap::new(), spill: Default::default(), recipe_ui: Default::default(), mount_menu: Default::default(), diff --git a/crates/horizon-client/src/game/copper_golems.rs b/crates/horizon-client/src/game/copper_golems.rs index e1acaebaf..5c3798901 100644 --- a/crates/horizon-client/src/game/copper_golems.rs +++ b/crates/horizon-client/src/game/copper_golems.rs @@ -102,7 +102,7 @@ impl Game<'_> { m.extra.scratch[3] -= 1; } // The visited positions fade after 6000 ticks. - if m.extra.ints[2] > 0 && m.age >= m.extra.ints[2] { + if m.extra.ints[2] > 0 && m.age as i32 >= m.extra.ints[2] { m.extra.positions.clear(); m.extra.ints[2] = 0; } @@ -141,9 +141,11 @@ impl Game<'_> { if next == -2 { return false; } - let interval = |m: &mut Mob| 504_000 + i64::from(m.random.next_int_bound(48_001).unwrap_or(0)); + // updateWeathering draws from the level random. + let r = &mut self.world.random; + let mut interval = || 504_000 + i64::from(r.next_int_bound(48_001).unwrap_or(0)); if next == -1 { - m.extra.vector[0] = (now + interval(m)) as f64; + m.extra.vector[0] = (now + interval()) as f64; return false; } let fully = m.extra.variant >= 3; @@ -153,7 +155,7 @@ impl Game<'_> { m.extra.vector[0] = if new >= 3 { 0.0 } else { - (next + interval(m)) as f64 + (next + interval()) as f64 }; } if fully && self.golem_can_turn_to_statue(m) { @@ -177,8 +179,9 @@ impl Game<'_> { m.position.y.floor() as i32, m.position.z.floor() as i32, ]; + // The pose comes from the golem's own random (`this.random.nextInt(0, 4)`). let pose = ["standing", "sitting", "running", "star"] - [self.world.random.next_int_bound(4).unwrap_or(0) as usize]; + [m.random.next_int_bound(4).unwrap_or(0) as usize]; // Direction.fromYRot. let facing = ["south", "west", "north", "east"] [((f64::from(m.yaw) / 90.0 + 0.5).floor() as i32 & 3) as usize]; @@ -186,7 +189,7 @@ impl Game<'_> { name: "minecraft:oxidized_copper_golem_statue".into(), properties: [ ("facing".to_string(), facing.to_string()), - ("pose".to_string(), pose.to_string()), + ("copper_golem_pose".to_string(), pose.to_string()), ("waterlogged".to_string(), "false".to_string()), ] .into(), @@ -212,15 +215,18 @@ impl Game<'_> { 1.0, ); } - /// `dropPreservedEquipment`: the carried stack and the antenna. + /// `Mob.dropPreservedEquipment`: the guaranteed drops in `EquipmentSlot` + /// order, the carried stack (main hand) then the antenna flower (saddle), + /// each `spawnAtLocation` at the golem's feet. pub(super) fn golem_drop_equipment(&mut self, m: &mut Mob) { - let at = [m.position.x, m.position.y + 0.5, m.position.z]; + let at = m.position; if let Some(stack) = held(m).cloned() { set_held(m, None); - self.spawn_item(stack, at, [0.0, 0.2, 0.0], 10); + self.spawn_at_location(at, 0.0, stack); } - if let Some(antenna) = m.extra.legs.take() { - self.spawn_item(ItemStack::new(&antenna, 1), at, [0.0, 0.2, 0.0], 10); + if let Some(antenna) = m.saddle_item() { + m.set_saddle(None); + self.spawn_at_location(at, 0.0, antenna); } } // ---- containers ------------------------------------------------------- @@ -252,7 +258,8 @@ impl Game<'_> { fn golem_chest_blocked(&self, keys: &[Key]) -> bool { keys.iter().any(|k| { let above = self.world.block(k.0, k.1 + 1, k.2); - self.behaviours.facts(above).flags & horizon_world::level::data::state_flags::CONDUCTOR + self.res.behaviours.facts(above).flags + & horizon_world::level::data::state_flags::CONDUCTOR != 0 }) } @@ -273,7 +280,11 @@ impl Game<'_> { None } fn golem_max_stack(&self, stack: &ItemStack) -> u32 { - self.res.items.get(&stack.item).map_or(64, |i| i.max_stack).max(1) + self.res + .items + .get(&stack.item) + .map_or(64, |i| i.max_stack) + .max(1) } /// `pickupItemFromContainer`: up to 16 of the first non-empty slot. fn golem_pick_up(&mut self, keys: &[Key]) -> Option { @@ -362,7 +373,10 @@ impl Game<'_> { let Some(h) = m.extra.home else { return false }; let key = (h[0], h[1], h[2]); let valid = self.golem_wanted_block(m, key) - && matches!(self.world.block_entities.get(&key), Some(BlockEntity::Chest(_))) + && matches!( + self.world.block_entities.get(&key), + Some(BlockEntity::Chest(_)) + ) && !self.golem_chest_blocked(&self.golem_container(key)); if !valid { return false; @@ -389,7 +403,11 @@ impl Game<'_> { f64::from(n.y) + m.kind.height / 2.0, f64::from(n.z) + 0.5, ], - None => [m.position.x, m.position.y + m.kind.height / 2.0, m.position.z], + None => [ + m.position.x, + m.position.y + m.kind.height / 2.0, + m.position.z, + ], }; let reach = golem_within(m, self.golem_range(m), from, h); (path.is_none() && !reach) || reach @@ -446,7 +464,7 @@ impl Game<'_> { } m.extra.positions.retain(|p| p[1] >= UNREACHABLE); m.extra.positions.extend(list); - m.extra.ints[2] = m.age + MEMORY_TICKS; + m.extra.ints[2] = m.age as i32 + MEMORY_TICKS; } fn golem_mark_unreachable(&mut self, m: &mut Mob, h: [i32; 3]) { m.extra.positions.retain(|p| *p != h); @@ -454,11 +472,16 @@ impl Game<'_> { if !m.extra.positions.contains(&marked) { m.extra.positions.push(marked); } - let count = m.extra.positions.iter().filter(|p| p[1] >= UNREACHABLE).count(); + let count = m + .extra + .positions + .iter() + .filter(|p| p[1] >= UNREACHABLE) + .count(); if count > 50 { self.golem_enter_cooldown(m); } else { - m.extra.ints[2] = m.age + MEMORY_TICKS; + m.extra.ints[2] = m.age as i32 + MEMORY_TICKS; } } fn golem_stop_targeting(&mut self, m: &mut Mob) { @@ -495,7 +518,9 @@ impl Game<'_> { } fn golem_chest_sound(&mut self, key: Key, open: bool) { let name = self.golem_block_name(key); - let base = name.trim_start_matches("minecraft:").trim_start_matches("waxed_"); + let base = name + .trim_start_matches("minecraft:") + .trim_start_matches("waxed_"); let kind = if base.starts_with("weathered") { "copper_chest_weathered" } else if base.starts_with("oxidized") { @@ -508,8 +533,12 @@ impl Game<'_> { let side = if open { "open" } else { "close" }; let at = chest_center([key.0, key.1, key.2]); let pitch = self.world.random.next_float() * 0.1 + 0.9; - self.audio - .play(&format!("minecraft:block.{kind}.{side}"), Some(at), 0.5, pitch); + self.audio.play( + &format!("minecraft:block.{kind}.{side}"), + Some(at), + 0.5, + pitch, + ); } fn golem_on_queuing(&mut self, m: &mut Mob) { let Some(h) = m.extra.home else { return }; @@ -601,7 +630,8 @@ impl Game<'_> { && let Some(sound) = sound { let at = [m.position.x, m.position.y, m.position.z]; - self.audio.play(&format!("minecraft:{sound}"), Some(at), 1.0, 1.0); + self.audio + .play(&format!("minecraft:{sound}"), Some(at), 1.0, 1.0); } if ticks == 60 { self.golem_release(m); @@ -626,7 +656,9 @@ impl Game<'_> { .any(|s| held(m).is_some_and(|h| h.item == s.item)) { // putDownItem. - let stack = held(m).cloned().unwrap_or_else(|| ItemStack::new("minecraft:air", 0)); + let stack = held(m) + .cloned() + .unwrap_or_else(|| ItemStack::new("minecraft:air", 0)); let left = self.golem_put_down(&keys, stack); let emptied = left.is_none(); set_held(m, left); @@ -671,11 +703,18 @@ impl Game<'_> { let is_axe = name.ends_with("_axe"); let waxed = self.mobs[index].extra.vector[0] as i64 == -2; if name == "minecraft:shears" && self.golem_shearable(index) { - let antenna = self.mobs[index].extra.legs.take(); + // CopperGolem.shear: the sound, the antenna slot emptied (an + // unequip) and the flower dropped 1.5 up. self.audio .play("minecraft:entity.copper_golem.shear", Some(at), 1.0, 1.0); + let antenna = self.mobs[index].saddle_item(); + self.mobs[index].set_saddle(None); + let id = self.mobs[index].id; + let ctx = self.who_context(Some(horizon_client::mobs::Who::Mob(id)), None); + self.post_game_event("minecraft:unequip", at, ctx); if let Some(a) = antenna { - self.spawn_item(ItemStack::new(&a, 1), [at[0], at[1] + 1.5, at[2]], [0.0, 0.2, 0.0], 10); + let p = self.mobs[index].position; + self.spawn_at_location(p, 1.5, a); } self.damage_held_tool(); return true; @@ -705,13 +744,14 @@ impl Game<'_> { } false } - fn golem_shearable(&self, index: usize) -> bool { - self.mobs[index].extra.legs.as_deref().is_some_and(|a| { + /// `readyForShearing`: the antenna holds a flower shears take. + pub(super) fn golem_shearable(&self, index: usize) -> bool { + self.mobs[index].saddle_item().is_some_and(|a| { self.res .items .item_tags .get("minecraft:shearable_from_copper_golem") - .is_some_and(|t| t.contains(a)) + .is_some_and(|t| t.contains(&*a.item)) }) } } diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 0d7e34572..6d1326dd3 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -513,6 +513,10 @@ impl Game<'_> { self.villager_murdered(m, by_player); let pos = [m.position.x, m.position.y + 0.5, m.position.z]; self.drop_death_loot(&mut dying, by_player, consumed, pos); + if m.species() == Species::CopperGolem { + // CopperGolem.dropEquipment: the preserved equipment too. + self.golem_drop_equipment(&mut dying); + } if m.species() == Species::SulfurCube { self.split_sulfur_cube(m); } @@ -1701,6 +1705,10 @@ impl Game<'_> { self.swing_arm(); return Ok(true); } + if self.interact_copper_golem(index, &item) { + self.swing_arm(); + return Ok(true); + } // AbstractHorse.mobInteract: feed, equip or ride. if self.interact_mount(index, &item) { self.swing_arm(); diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs index 8747b4c25..5b2963df2 100644 --- a/crates/horizon-client/src/game/dispense_fixture.rs +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -499,13 +499,17 @@ impl Game<'_> { let _ = write!(line, " {name}={item}x1"); } } - let ready = match s { - Species::Sheep => Some(!x.sheared && !m.is_baby()), - Species::Mooshroom => Some(!m.is_baby()), - Species::Bogged => Some(!x.sheared), - Species::SnowGolem => Some(!x.sheared), - _ => None, - }; + let ready = + match s { + Species::Sheep => Some(!x.sheared && !m.is_baby()), + Species::Mooshroom => Some(!m.is_baby()), + Species::Bogged => Some(!x.sheared), + Species::SnowGolem => Some(!x.sheared), + Species::CopperGolem => Some(self.golem_shearable( + self.mobs.iter().position(|o| o.id == id).unwrap_or(0), + )), + _ => None, + }; if let Some(r) = ready { line.push_str(if r { " ready" } else { " unready" }); } diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index f6a7e5a58..69e040b59 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -1706,7 +1706,12 @@ impl Game<'_> { } /// `Entity.spawnAtLocation(level, stack, offset)`: an item at the entity's /// feet plus the offset, moving as `new ItemEntity` makes it. - fn spawn_at_location(&mut self, at: Vec3, y_offset: f64, stack: ItemStack) -> [f64; 3] { + pub(super) fn spawn_at_location( + &mut self, + at: Vec3, + y_offset: f64, + stack: ItemStack, + ) -> [f64; 3] { let v = new_item_motion(); self.spawn_item(stack, [at.x, at.y + y_offset, at.z], v, 10); v @@ -1842,6 +1847,20 @@ impl Game<'_> { self.spawn_at_location(at, eye, stack); } } + // CopperGolem.shear: the antenna's flower drops 1.5 up. + Species::CopperGolem if self.golem_shearable(i) => { + self.audio + .play("minecraft:entity.copper_golem.shear", sound_at, 1.0, 1.0); + let antenna = self.mobs[i].saddle_item(); + let id = self.mobs[i].id; + // setItemSlot(EQUIPMENT_SLOT_ANTENNA, EMPTY): an unequip. + self.mobs[i].set_saddle(None); + let ctx = self.who_context(Some(horizon_client::mobs::Who::Mob(id)), None); + self.post_game_event("minecraft:unequip", [at.x, at.y, at.z], ctx); + if let Some(antenna) = antenna { + self.spawn_at_location(at, 1.5, antenna); + } + } Species::Mooshroom if !m.is_baby() => { let variant = if m.extra.variant == 1 { "brown" } else { "red" }; let height = f64::from(m.height()); diff --git a/crates/horizon-client/src/game/golems.rs b/crates/horizon-client/src/game/golems.rs index a67e05af8..990ce4271 100644 --- a/crates/horizon-client/src/game/golems.rs +++ b/crates/horizon-client/src/game/golems.rs @@ -4,7 +4,6 @@ //! golem. Players placing the pumpkin and dispensers share it. use super::Game; use horizon_client::Result; -use horizon_world::chunk::BlockState; impl Game<'_> { fn golem_block(&self, p: [i32; 3]) -> &str { @@ -49,9 +48,20 @@ impl Game<'_> { } None } - /// `CarvedPumpkinBlock.canSpawnGolem`. + /// `CarvedPumpkinBlock.canSpawnGolem`: a snow or iron golem base, or a + /// copper block (`#minecraft:copper`) below. pub(super) fn can_spawn_golem(&self, head: [i32; 3]) -> bool { - self.find_golem(head).is_some() + self.find_golem(head).is_some() || self.copper_golem_base(head) + } + /// The copper golem pattern ("^", "#"): a block of `#minecraft:copper` under the head. + fn copper_golem_base(&self, head: [i32; 3]) -> bool { + let below = [head[0], head[1] - 1, head[2]]; + let name = self.golem_block(below); + self.res + .items + .block_tags + .get("minecraft:copper") + .is_some_and(|t| t.contains(name)) } /// `CarvedPumpkinBlock.trySpawnGolem`, after a pumpkin was placed. pub(super) fn check_golem_spawn(&mut self, head: [i32; 3]) -> Result<()> { @@ -71,9 +81,9 @@ impl Game<'_> { self.create_triggered(kind, at, 0.0); self.update_pattern_blocks(&blocks) } - /// Whether placing a golem head at `head` completes a snow or iron golem. + /// Whether placing a golem head at `head` completes a golem. pub(super) fn completes_golem(&self, head: [i32; 3]) -> bool { - self.find_golem(head).is_some() + self.find_golem(head).is_some() || self.copper_golem_base(head) } /// `EntityType.create(level, TRIGGERED)` and `snapTo`: a built golem or /// wither has no spawn finalization and draws nothing from the level. @@ -124,57 +134,67 @@ impl Game<'_> { } self.process_level_events() } - /// A pumpkin on a copper block: the block turns into the matching copper - /// chest (facing the pumpkin's way) and a copper golem appears where the - /// pumpkin was (`CarvedPumpkinBlock.spawnGolemInWorld` with - /// `CopperChestBlock.getFromCopperBlock`). + /// The copper golem (`CarvedPumpkinBlock.trySpawnGolem`, after the snow + /// and iron golems): both blocks clear, the golem appears where the + /// pumpkin was, the copper block becomes the matching copper chest facing + /// the pumpkin's way (`replaceCopperBlockWithChest`, flags 2) and the + /// golem takes the block's weathering (`CopperGolem.spawn`). fn try_spawn_copper_golem(&mut self, head: [i32; 3]) -> Result<()> { - let below = [head[0], head[1] - 1, head[2]]; - let registry = self.res.assets.registry; - let name = self.golem_block(below).to_string(); - let Some(base) = name.strip_prefix("minecraft:") else { - return Ok(()); - }; - let waxed = base.strip_prefix("waxed_").unwrap_or(base); - if !matches!( - waxed, - "copper_block" | "exposed_copper" | "weathered_copper" | "oxidized_copper" - ) { + if !self.copper_golem_base(head) { return Ok(()); } - let chest = format!( - "minecraft:{}_chest", - base.strip_suffix("_block").unwrap_or(base) - ); + let below = [head[0], head[1] - 1, head[2]]; + let copper = self.world.block(below[0], below[1], below[2]); let pumpkin = self.world.block(head[0], head[1], head[2]); + let registry = self.res.assets.registry; let facing = registry .state(pumpkin) .and_then(|s| s.properties.get("facing").cloned()) .unwrap_or_else(|| "north".into()); - let chest_state = BlockState { - name: chest.clone(), - properties: [ - ("facing".to_string(), facing), - ("type".to_string(), "single".to_string()), - ("waterlogged".to_string(), "false".to_string()), - ] - .into(), - }; - let Some(chest_id) = registry - .id(&chest_state) - .or_else(|| registry.default_state(&chest)) - else { - return Ok(()); - }; - self.destroy_particles(head, pumpkin); - self.level_set(head, self.world.air)?; - self.level_set(below, chest_id)?; + // getWeatherStateFromPattern: waxed blocks weather like their unwaxed form. + let base = self + .golem_block(below) + .trim_start_matches("minecraft:") + .trim_start_matches("waxed_"); + let weather = ["exposed", "weathered", "oxidized"] + .iter() + .position(|w| base.starts_with(w)) + .map_or(0, |i| i as u8 + 1); + let cells = [head, below]; + self.clear_pattern_blocks(&cells)?; let at = [ f64::from(head[0]) + 0.5, f64::from(head[1]) + 0.05, f64::from(head[2]) + 0.5, ]; - let _ = self.spawn_mob("minecraft:copper_golem", at, false); - Ok(()) + let id = self.create_triggered("minecraft:copper_golem", at, 0.0); + self.update_pattern_blocks(&cells)?; + let direction = match facing.as_str() { + "south" => horizon_core::geometry::Direction::South, + "west" => horizon_core::geometry::Direction::West, + "east" => horizon_core::geometry::Direction::East, + _ => horizon_core::geometry::Direction::North, + }; + let pos = horizon_core::position::BlockPos::new(below[0], below[1], below[2]); + let chest = self.with_level(|b, level, ticks| { + b.copper_chest_from_block(level, ticks, copper, direction, pos) + }); + if let Some(chest) = chest { + self.with_level(|b, level, ticks| b.set_block(level, ticks, pos, chest, 2)); + if let Some(entity) = + horizon_client::containers::BlockEntity::for_block(&self.res.facts.get(chest).name) + { + self.world + .block_entities + .insert((below[0], below[1], below[2]), entity); + } + } + if let Some(m) = id.and_then(|id| self.mobs.iter_mut().find(|m| m.id == id)) { + m.extra.variant = weather; + let p = [m.position.x, m.position.y, m.position.z]; + self.audio + .play("minecraft:entity.copper_golem.spawn", Some(p), 1.0, 1.0); + } + self.process_level_events() } } diff --git a/crates/horizon-client/src/game/spill.rs b/crates/horizon-client/src/game/spill.rs index 34b145420..a779629bd 100644 --- a/crates/horizon-client/src/game/spill.rs +++ b/crates/horizon-client/src/game/spill.rs @@ -200,6 +200,24 @@ impl GameLevel<'_> { delay: 10, }); } + /// `Block.popResource(level, pos, new ItemStack(block))`. + pub(super) fn pop_block_item(&mut self, pos: BlockPos, state: BlockStateId) { + let item = self.facts.get(state).item.clone(); + if &*item != "minecraft:air" { + self.pop_resource([pos.x, pos.y, pos.z], ItemStack::new(&item, 1)); + } + } + /// An item `atLowerCornerWithOffset(pos, 0.5, 1.01, 0.5).offsetRandomXZ(random, + /// 0.7)` with the default pickup delay (`ComposterBlock.extractProduce`). + pub(super) fn pop_above(&mut self, pos: BlockPos, item: std::sync::Arc) { + let at = removal::jukebox_pop_position(&mut self.world.random, pos); + self.spill.spawns.push(Spawn { + stack: ItemStack::new(&item, 1), + pos: at, + motion: None, + delay: 10, + }); + } /// `Block.dropResources(state, level, pos, blockEntity)`: the block's loot /// (from its random sequence, else the level random), with what its block /// entity copies into it, popped at once. diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index e6dd71e45..29c36ee34 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -15,6 +15,7 @@ pub mod breeze; pub mod camel; pub mod caravan; pub mod control; +pub mod copper_golem; pub mod creaking; pub mod cube; pub mod data; @@ -1315,8 +1316,10 @@ impl Mob { /// Wear (or take off) a saddle stack, keeping its components. pub fn set_saddle(&mut self, saddle: Option) { self.extra.saddled = saddle.is_some(); + // A plain saddle is implied by `saddled`; anything else (a copper + // golem's antenna flower) is kept as it is. self.extra.saddle_stack = saddle .map(|s| crate::items::ItemStack { count: 1, ..s }) - .filter(|s| s.damage != 0 || s.extra.is_some()); + .filter(|s| s.damage != 0 || s.extra.is_some() || &*s.item != "minecraft:saddle"); } } diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 4d5c1307e..d3d7d9a64 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -125,7 +125,7 @@ pub fn family(s: Species) -> Family { S::Witch => Family::Witch, S::Wolf | S::Cat => Family::Tame, S::Blaze | S::Ghast => Family::Nether, - S::IronGolem | S::SnowGolem => Family::Golem, + S::IronGolem | S::SnowGolem | S::CopperGolem => Family::Golem, S::Pillager | S::Vindicator | S::Evoker | S::Illusioner => Family::Illager, S::Vex => Family::Vex, S::Ravager => Family::Ravager, @@ -171,6 +171,7 @@ pub fn register(m: &mut Mob) { Family::Nether if m.species() == Species::Ghast => super::nether::register_ghast(m), Family::Nether => super::nether::register_blaze(m), Family::Golem if m.species() == Species::IronGolem => super::golem::register_iron(m), + Family::Golem if m.species() == Species::CopperGolem => super::copper_golem::register(m), Family::Golem => super::golem::register_snow(m), Family::Illager if m.species() == Species::Pillager => super::illager::register_pillager(m), Family::Illager if m.species() == Species::Evoker => super::illager::register_evoker(m), @@ -672,6 +673,7 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { super::shulker::ai_step(m, w, events); } Species::SnowGolem => super::golem::snow_ai_step(m, w, events), + Species::CopperGolem => super::copper_golem::ai_step(m, w, events), Species::Slime | Species::MagmaCube => super::cube::ai_step(m, w, events), Species::SulfurCube => { super::cube::ai_step(m, w, events); diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 27f441adc..bc033ede0 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -39,6 +39,7 @@ pub enum Anim { Villager, IronGolem, SnowGolem, + CopperGolem, Illager, Piglin, ZombifiedPiglin, @@ -462,6 +463,7 @@ pub fn setup( Anim::Camel => super::camel::anim(m, s, p, w_anims), Anim::Breeze => super::breeze::anim(m, s, p, w_anims), Anim::Sniffer => super::sniffer::anim(m, s, p, w_anims), + Anim::CopperGolem => super::copper_golem::anim(m, s, p, w_anims), Anim::HappyGhast => super::happy_ghast::anim(m, s, p), Anim::Nautilus => super::nautilus::anim(m, s, p, w_anims), Anim::Llama => super::llama::anim(m, s, p), diff --git a/crates/horizon-client/src/mobs/copper_golem.rs b/crates/horizon-client/src/mobs/copper_golem.rs index 80e5dd2b4..a1cf2774c 100644 --- a/crates/horizon-client/src/mobs/copper_golem.rs +++ b/crates/horizon-client/src/mobs/copper_golem.rs @@ -14,7 +14,7 @@ use std::collections::HashMap; // Copper golem state: `variant` the weathering (0 unaffected .. 3 oxidized), // `variant3` the `CopperGolemState`, `main_hand` with `ints[1]` the carried -// stack, `legs` the antenna block, `egg_time` the next weathering tick (-1 +// stack, the saddle slot the antenna (`EQUIPMENT_SLOT_ANTENNA`), `egg_time` the next weathering tick (-1 // unset, -2 waxed), `ints[0]` the idle animation start tick, `home` the chest // being served with `positions` the visited chests, `point` where it walks, // `scratch` = [transport phase, ticks since reaching the target, interaction @@ -44,7 +44,10 @@ pub fn register(m: &mut Mob) { pub fn texture(m: &Mob) -> String { match m.extra.variant { 0 => "entity/copper_golem/copper_golem".into(), - n => format!("entity/copper_golem/copper_golem_{}", WEATHER[n.min(3) as usize]), + n => format!( + "entity/copper_golem/copper_golem_{}", + WEATHER[n.min(3) as usize] + ), } } pub fn eye_texture(m: &Mob) -> String { @@ -102,7 +105,11 @@ impl Goal for Serve { match m.extra.point { Some(p) => { if m.ai.nav.is_done() || m.age % 10 == 0 { - let block = [p[0].floor() as i32, p[1].floor() as i32, p[2].floor() as i32]; + let block = [ + p[0].floor() as i32, + p[1].floor() as i32, + p[2].floor() as i32, + ]; let path = m.create_path_to(cx.w, block, 0); m.nav_move_to_path(path, 1.0); } @@ -179,12 +186,13 @@ pub fn ai_step(m: &mut Mob, _w: &MobWorld, events: &mut Vec) { match state { IDLE => { interaction(st, 0); - if m.extra.ints[0] == m.age { + let tick = m.age as i32; + if m.extra.ints[0] == tick { st[0].start(age); } else if m.extra.ints[0] == 0 { - m.extra.ints[0] = m.age + 200 + m.random.next_int_bound(40).unwrap_or(0); + m.extra.ints[0] = tick + 200 + m.random.next_int_bound(40).unwrap_or(0); } - if m.age == m.extra.ints[0] + 10 { + if tick == m.extra.ints[0] + 10 { let at = [m.position.x, m.position.y, m.position.z]; events.push(MobEvent::Sound(sound(m, "spin"), at, 1.0, 1.0)); m.extra.ints[0] = 0; diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index 899666265..1d2ad6180 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -45,6 +45,7 @@ pub enum Species { WanderingTrader, IronGolem, SnowGolem, + CopperGolem, Zombie, Husk, Drowned, @@ -642,6 +643,16 @@ pub const ALL: &[Def] = &[ None, Baby::None ), + def!( + CopperGolem, + "copper_golem", + "copper_golem#main", + "entity/copper_golem/copper_golem", + A::CopperGolem, + ["", "entity.copper_golem.hurt", "entity.copper_golem.death"], + Some("entity.copper_golem.step"), + Baby::None + ), def!( Zombie, "zombie", @@ -1481,7 +1492,7 @@ impl Def { && !aquatic && !matches!( s, - S::Villager | S::WanderingTrader | S::IronGolem | S::SnowGolem + S::Villager | S::WanderingTrader | S::IronGolem | S::SnowGolem | S::CopperGolem ), } } @@ -1632,7 +1643,8 @@ impl Def { | S::Dolphin | S::Axolotl | S::IronGolem - | S::SnowGolem => 120, + | S::SnowGolem + | S::CopperGolem => 120, _ if self.flags(false).animal => 120, _ => 80, } diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index 7a2255278..2546b51ae 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -522,6 +522,18 @@ impl Behaviours { ) -> bool { self.sim(level, ticks).set_block(pos, state, flags) } + /// `CopperChestBlock.getFromCopperBlock(copperBlock, facing, level, pos)`. + pub fn copper_chest_from_block( + &self, + level: &mut L, + ticks: &mut Ticks, + copper: BlockStateId, + facing: Direction, + pos: BlockPos, + ) -> Option { + self.sim(level, ticks) + .copper_chest_from_block(copper, facing, pos) + } /// `Level.removeBlock(pos, movedByPiston)`: replace with the fluid's block. pub fn remove_block( &self, diff --git a/crates/horizon-world/src/level/place.rs b/crates/horizon-world/src/level/place.rs index 16b2aad5e..2ec2f02ec 100644 --- a/crates/horizon-world/src/level/place.rs +++ b/crates/horizon-world/src/level/place.rs @@ -723,11 +723,9 @@ impl Sim<'_, L> { kind = "right"; } } - Some(wl(table.set_text( - table.set_dir(default, p.facing, facing), - p.kind, - kind, - ))) + let state = + wl(table.set_text(table.set_dir(default, p.facing, facing), p.kind, kind)); + Some(self.copper_chest_least_oxidized(state, pos)) } Class::ChorusPlant => Some(self.chorus_plant_with_connections(pos, default)), Class::ConcretePowder => { @@ -1156,6 +1154,84 @@ impl Sim<'_, L> { }) } + /// `CopperChestBlock.getLeastOxidizedChestOfConnectedBlocks`: a copper chest + /// joining another takes the less weathered of the two blocks (both unwaxed + /// when only one was waxed), keeping its own properties. + pub(crate) fn copper_chest_least_oxidized( + &self, + state: BlockStateId, + pos: BlockPos, + ) -> BlockStateId { + let b = self.b; + if !b.instance_of(state, Class::CopperChest) + || b.table.text(state, b.props.kind) == "single" + { + return state; + } + let connected = self.rel_state(pos, self.chest_connected_direction(state)); + if !b.instance_of(connected, Class::CopperChest) { + return state; + } + let name = |s: BlockStateId| b.table.name(s).trim_start_matches("minecraft:").to_owned(); + let weather = |n: &str| { + let n = n.trim_start_matches("waxed_"); + ["exposed_", "weathered_", "oxidized_"] + .iter() + .position(|p| n.starts_with(p)) + .map_or(0, |i| i + 1) + }; + let (own, other) = (name(state), name(connected)); + let (mut own_block, mut other_block) = (own.clone(), other.clone()); + if own.starts_with("waxed_") != other.starts_with("waxed_") { + own_block = own.trim_start_matches("waxed_").to_owned(); + other_block = other.trim_start_matches("waxed_").to_owned(); + } + let least = if weather(&own) <= weather(&other) { + own_block + } else { + other_block + }; + match b.table.block_by_name(&format!("minecraft:{least}")) { + Some(block) => b.table.with_properties_of(block, state), + None => state, + } + } + /// `CopperChestBlock.getFromCopperBlock`: the copper chest a copper block + /// becomes under a copper golem's pumpkin, facing `facing`. + pub(crate) fn copper_chest_from_block( + &self, + copper: BlockStateId, + facing: Direction, + pos: BlockPos, + ) -> Option { + let b = self.b; + let block = b.table.name(copper).trim_start_matches("minecraft:"); + let chest = match block { + "copper_block" => "copper_chest".to_owned(), + "waxed_copper_block" => "waxed_copper_chest".to_owned(), + n if n.ends_with("_copper") => format!("{n}_chest"), + _ => "copper_chest".to_owned(), + }; + let block = b.table.block_by_name(&format!("minecraft:{chest}"))?; + let default = b.default_of(block); + let kind = if self.chest_candidate_partner_facing(default, pos, clockwise(facing)) + == Some(facing) + { + "left" + } else if self.chest_candidate_partner_facing(default, pos, counter_clockwise(facing)) + == Some(facing) + { + "right" + } else { + "single" + }; + let state = b.table.set_text( + b.table.set_dir(default, b.props.facing, facing), + b.props.kind, + kind, + ); + Some(self.copper_chest_least_oxidized(state, pos)) + } fn chest_candidate_partner_facing( &self, chest: BlockStateId, diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java index c9aca25ee..690fd173c 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -326,7 +326,8 @@ public final class VanillaDispenserOracle { } } boolean listed = e instanceof net.minecraft.world.entity.animal.sheep.Sheep || e instanceof net.minecraft.world.entity.animal.cow.MushroomCow - || e instanceof net.minecraft.world.entity.monster.skeleton.Bogged || e instanceof net.minecraft.world.entity.animal.golem.SnowGolem; + || e instanceof net.minecraft.world.entity.monster.skeleton.Bogged || e instanceof net.minecraft.world.entity.animal.golem.SnowGolem + || e instanceof net.minecraft.world.entity.animal.golem.CopperGolem; if (listed && e instanceof Shearable shearable) line.append(shearable.readyForShearing() ? " ready" : " unready"); if (e instanceof net.minecraft.world.entity.animal.equine.AbstractChestedHorse chested && chested.hasChest()) line.append(" chest"); if (e instanceof Leashable leashable && leashable.isLeashed()) line.append(" leashed"); diff --git a/tools/test_dispenser_parity.py b/tools/test_dispenser_parity.py index 61f74d496..844322aeb 100644 --- a/tools/test_dispenser_parity.py +++ b/tools/test_dispenser_parity.py @@ -106,7 +106,10 @@ FRONT_FOR = { 'armor_stand': ['air', 'air', 'stone', 'water', 'short_grass'], } # Structures a behaviour completes (golems, the wither, a nether portal). -SCENES = {'carved_pumpkin': ['snow_golem', 'iron_golem'], 'wither_skeleton_skull': ['wither'], 'flint_and_steel': ['portal']} +SCENES = {'carved_pumpkin': ['snow_golem', 'iron_golem', 'copper_golem', 'copper_golem'], 'wither_skeleton_skull': ['wither'], + 'flint_and_steel': ['portal']} +COPPER_BLOCKS = ['copper_block', 'exposed_copper', 'weathered_copper', 'oxidized_copper', 'waxed_copper_block', + 'waxed_exposed_copper', 'waxed_weathered_copper', 'waxed_oxidized_copper'] # Plants that need soil (or a log) under them to exist at all. ROOTED = {'wheat', 'carrots', 'potatoes', 'beetroots', 'oak_sapling', 'birch_sapling', 'spruce_sapling', 'cherry_sapling', 'mangrove_propagule', 'sweet_berry_bush', 'cocoa', 'bamboo_sapling', 'bamboo', 'pumpkin_stem', 'melon_stem', @@ -115,7 +118,7 @@ ROOTED = {'wheat', 'carrots', 'potatoes', 'beetroots', 'oak_sapling', 'birch_sap BELOW = ['air', 'stone', 'water', 'rail', 'grass_block', 'soul_sand', 'dirt', 'farmland', 'netherrack'] TARGETS = { - 'iron_helmet': ['zombie', 'skeleton', 'armor_stand', 'pig'], + 'iron_helmet': ['zombie', 'skeleton', 'armor_stand', 'pig', 'copper_golem'], 'chainmail_helmet': ['zombie', 'armor_stand', 'villager'], 'diamond_chestplate': ['zombie', 'armor_stand'], 'leather_leggings': ['skeleton', 'armor_stand'], @@ -129,11 +132,11 @@ TARGETS = { 'white_carpet': ['llama', 'horse', 'trader_llama'], 'red_carpet': ['llama', 'trader_llama'], 'chest': ['donkey', 'mule', 'llama', 'horse', 'trader_llama'], - 'carved_pumpkin': ['zombie', 'armor_stand', 'snow_golem'], + 'carved_pumpkin': ['zombie', 'armor_stand', 'snow_golem', 'copper_golem'], 'elytra': ['armor_stand', 'zombie'], 'wolf_armor': ['wolf'], 'white_harness': ['happy_ghast'], - 'shears': ['sheep', 'sheep', 'mooshroom', 'bogged', 'snow_golem', 'sulfur_cube', 'pig', 'cow'], + 'shears': ['sheep', 'sheep', 'mooshroom', 'bogged', 'snow_golem', 'sulfur_cube', 'pig', 'cow', 'copper_golem', 'copper_golem'], 'brush': ['armadillo'], 'tnt': ['sulfur_cube'], 'flint_and_steel': ['sulfur_cube'], @@ -189,6 +192,8 @@ def build_scene(cells, states, rng, scene, front, facing): if scene == 'snow_golem': put((x, y - 1, z), 'snow_block') put((x, y - 2, z), 'snow_block') + elif scene == 'copper_golem': + put((x, y - 1, z), rng.choice(COPPER_BLOCKS)) elif scene == 'iron_golem': put((x, y - 1, z), 'iron_block') put((x, y - 2, z), 'iron_block') @@ -296,6 +301,9 @@ def cases(facts, count, seed, only): target[flag] = True if target['type'] == 'minecraft:sulfur_cube' and rng.random() < 0.5: target['equip'] = {'body': plain(rng.choice(SWALLOWED))} + if target['type'] == 'minecraft:copper_golem' and rng.random() < 0.7: + # The poppy an iron golem offered sits in the antenna (saddle) slot. + target['equip'] = {'saddle': plain(rng.choice(['poppy', 'poppy', 'dandelion']))} # Leads: tied to an earlier target, or that target holds this one's. holders = [i for i, e in enumerate(entities) if e['type'] != 'minecraft:armor_stand'] if holders and rng.random() < 0.3: