From d32fd5457a9268ae0f69e9e67b4d04cd25290e8e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:57:35 +0200 Subject: [PATCH] Beehives: nests grown in the live level get their block entity and bees BeehiveDecorator stores 2 or 3 bees in the nest's block entity. World generation kept them, but trees placed in the live level (a sapling grown next to flowers, which picks the *_bees_005 trees, and /place feature) dropped them: LevelFeatureWorld had no store_beehive_occupants, so the nest had no block entity at all. Breaking it released nothing and bees could not enter it. LevelAccess gets load_block_entity, the game loads the occupants into the nest's block entity, and the occupant NBT is shared with world generation. Fixes #1097 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 10 ++++++ .../horizon-world/src/level/feature_world.rs | 11 +++++++ .../horizon-world/src/level/growth_tests.rs | 31 +++++++++++++++++++ crates/horizon-world/src/level/mod.rs | 4 +++ .../src/worldgen/generator_decoration.rs | 20 ++---------- crates/horizon-world/src/worldgen/tree.rs | 2 +- .../src/worldgen/tree/decorator.rs | 22 +++++++++++++ 7 files changed, 81 insertions(+), 19 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 4013bb04b..d32b25437 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -812,6 +812,16 @@ impl LevelAccess for GameLevel<'_> { fn remove_block_entity(&mut self, pos: BlockPos, old: BlockStateId, side_effects: bool) -> u8 { self.remove_entity(pos, old, side_effects) } + fn load_block_entity(&mut self, pos: BlockPos, mut nbt: horizon_world::nbt::Compound) { + use horizon_world::nbt::Tag; + for (k, v) in [("x", pos.x), ("y", pos.y), ("z", pos.z)] { + nbt.insert(k.into(), Tag::Int(v)); + } + if let Some((key, entity)) = horizon_client::save::decode_block_entity(&Tag::Compound(nbt)) + { + self.world.block_entities.insert(key, entity); + } + } fn set_command_block_powered(&mut self, pos: BlockPos, powered: bool) -> Option<(bool, bool)> { command_blocks::set_powered(self.world, self.registry, (pos.x, pos.y, pos.z), powered) } diff --git a/crates/horizon-world/src/level/feature_world.rs b/crates/horizon-world/src/level/feature_world.rs index f19d3ae8c..3b781df4a 100644 --- a/crates/horizon-world/src/level/feature_world.rs +++ b/crates/horizon-world/src/level/feature_world.rs @@ -122,6 +122,17 @@ impl PlacementContext for LevelFeatureWorld<'_, '_, L> ) -> Result { Ok(self.sim.b.has_flag(state, state_flags::COLLISION_EMPTY)) } + /// BeehiveDecorator's bees go into the nest's block entity (the tree + /// checked the nest is there). + fn store_beehive_occupants( + &mut self, + position: [i32; 3], + ticks_in_hive: &[i32], + ) -> Result<(), PlacementQueryError> { + let nbt = crate::worldgen::tree::beehive_occupants(ticks_in_hive); + self.sim.level.load_block_entity(pos(position), nbt); + Ok(()) + } /// Live levels hold no loot-table containers placed by these features. fn set_block_entity_loot_table( &mut self, diff --git a/crates/horizon-world/src/level/growth_tests.rs b/crates/horizon-world/src/level/growth_tests.rs index b72ccbcba..7148636be 100644 --- a/crates/horizon-world/src/level/growth_tests.rs +++ b/crates/horizon-world/src/level/growth_tests.rs @@ -55,6 +55,7 @@ struct Level { events: Vec, biome: &'static str, day: i64, + block_entities: Vec<(BlockPos, crate::nbt::Compound)>, } impl LevelAccess for Level { fn day_time(&self) -> i64 { @@ -81,6 +82,9 @@ impl LevelAccess for Level { fn biome_name(&self, _pos: BlockPos) -> Option<&str> { Some(self.biome) } + fn load_block_entity(&mut self, pos: BlockPos, nbt: crate::nbt::Compound) { + self.block_entities.push((pos, nbt)); + } } struct World { @@ -104,6 +108,7 @@ impl World { events: Vec::new(), biome: "minecraft:plains", day: 6000, + block_entities: Vec::new(), }, f, ticks, @@ -186,6 +191,32 @@ fn oak_sapling_grows_with_bone_meal() { grow("oak_sapling", "grass_block", &[(0, 0)], "oak_log"); } #[test] +fn a_tree_with_bees_fills_its_nest() { + let Some(f) = fixture() else { return }; + let mut w = World::new(f, 3); + w.floor("grass_block", 12); + let origin = BlockPos::new(0, 64, 0); + let placed = f.behaviours.place_feature( + &mut w.level, + &mut w.ticks, + "minecraft:fancy_oak_bees", + origin, + ); + assert_eq!(placed, Some(true)); + assert_eq!(w.count("bee_nest"), 1); + let [(at, nbt)] = &w.level.block_entities[..] else { + panic!("one block entity: {:?}", w.level.block_entities); + }; + assert_eq!(w.name(*at), "minecraft:bee_nest"); + let Some(crate::nbt::Tag::List(bees)) = nbt.get(&"bees".into()) else { + panic!("bees: {nbt:?}"); + }; + assert!( + (2..=3).contains(&bees.len()), + "BeehiveDecorator stores 2 or 3 bees" + ); +} +#[test] fn single_saplings_grow() { grow("birch_sapling", "dirt", &[(0, 0)], "birch_log"); grow("spruce_sapling", "dirt", &[(0, 0)], "spruce_log"); diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index c706ca280..de7312cb1 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -416,6 +416,10 @@ pub trait LevelAccess { ) -> u8 { 0 } + /// Load saved block-entity data into the block entity of the block just + /// placed at `pos` (`BlockEntity.loadWithComponents`): a bee nest a grown + /// tree placed gets its bees. Levels without block entities drop it. + fn load_block_entity(&mut self, _pos: BlockPos, _nbt: crate::nbt::Compound) {} /// Biome identifier at `pos` (`minecraft:plains`), for features placed by /// block behaviour (bone meal flowers). None when unknown. fn biome_name(&self, _pos: BlockPos) -> Option<&str> { diff --git a/crates/horizon-world/src/worldgen/generator_decoration.rs b/crates/horizon-world/src/worldgen/generator_decoration.rs index 83fe82d24..bc5dbda70 100644 --- a/crates/horizon-world/src/worldgen/generator_decoration.rs +++ b/crates/horizon-world/src/worldgen/generator_decoration.rs @@ -698,13 +698,12 @@ impl PlacementContext for World<'_> { self.set_block_entity(position, nbt) } /// BeehiveDecorator: `getBlockEntity(pos, BEEHIVE)` then - /// `storeBee(Occupant.create(ticks))` for each bee (min ticks in hive 600). + /// `storeBee(Occupant.create(ticks))` for each bee. fn store_beehive_occupants( &mut self, position: [i32; 3], ticks_in_hive: &[i32], ) -> Result<(), PlacementQueryError> { - use crate::nbt::Tag; let chunk = self.region.chunk(position[0] >> 4, position[2] >> 4)?; let state = chunk.block(position[0], position[1], position[2]); let registry = self.decoration.bindings.registry(); @@ -714,22 +713,7 @@ impl PlacementContext for World<'_> { ) { return Ok(()); } - let bees = ticks_in_hive - .iter() - .map(|&ticks| { - let mut entity = crate::nbt::Compound::new(); - entity.insert("id".into(), Tag::String("minecraft:bee".into())); - let mut bee = crate::nbt::Compound::new(); - bee.insert("entity_data".into(), Tag::Compound(entity)); - bee.insert("ticks_in_hive".into(), Tag::Int(ticks)); - bee.insert("min_ticks_in_hive".into(), Tag::Int(600)); - Tag::Compound(bee) - }) - .collect(); - let mut nbt = crate::nbt::Compound::new(); - nbt.insert("id".into(), Tag::String("minecraft:beehive".into())); - nbt.insert("bees".into(), Tag::List(bees)); - self.set_block_entity(position, nbt) + self.set_block_entity(position, super::tree::beehive_occupants(ticks_in_hive)) } /// SpawnerBlockEntity.setEntityId: the next spawn data and no potentials. fn configure_spawner( diff --git a/crates/horizon-world/src/worldgen/tree.rs b/crates/horizon-world/src/worldgen/tree.rs index 25526bb62..a094c6edf 100644 --- a/crates/horizon-world/src/worldgen/tree.rs +++ b/crates/horizon-world/src/worldgen/tree.rs @@ -21,8 +21,8 @@ use super::{ value_provider::IntProvider, }; use crate::registry::BlockStateId; -pub(crate) use decorator::GROUND_FEATURE_KEY; use decorator::TreeDecorator; +pub(crate) use decorator::{GROUND_FEATURE_KEY, beehive_occupants}; pub use fallen::FallenTreeFeature; use foliage::{FoliageAttachment, FoliagePlacer}; use horizon_core::{geometry::Direction, random::RandomSource}; diff --git a/crates/horizon-world/src/worldgen/tree/decorator.rs b/crates/horizon-world/src/worldgen/tree/decorator.rs index 942372afc..b0aaff4b5 100644 --- a/crates/horizon-world/src/worldgen/tree/decorator.rs +++ b/crates/horizon-world/src/worldgen/tree/decorator.rs @@ -675,3 +675,25 @@ fn mushroom_pos(log: [i32; 3], facing: Direction) -> [i32; 3] { let n = facing.normal(); tb::offset(log, n[0], 0, n[2]) } + +/// The nest's block entity after `BeehiveDecorator` stored its bees +/// (`Occupant.create(ticks)`: a bee, with 600 minimum ticks in the hive). +pub(crate) fn beehive_occupants(ticks_in_hive: &[i32]) -> crate::nbt::Compound { + use crate::nbt::{Compound, Tag}; + let bees = ticks_in_hive + .iter() + .map(|&ticks| { + let mut entity = Compound::new(); + entity.insert("id".into(), Tag::String("minecraft:bee".into())); + let mut bee = Compound::new(); + bee.insert("entity_data".into(), Tag::Compound(entity)); + bee.insert("ticks_in_hive".into(), Tag::Int(ticks)); + bee.insert("min_ticks_in_hive".into(), Tag::Int(600)); + Tag::Compound(bee) + }) + .collect(); + let mut nbt = Compound::new(); + nbt.insert("id".into(), Tag::String("minecraft:beehive".into())); + nbt.insert("bees".into(), Tag::List(bees)); + nbt +}