//! Block entities and the items that carry them //! (`horizon_client::block_entity_item`): the item Pick Block gives, with the //! block entity's data on Ctrl in creative, and the data and components a //! placed item gives its block entity. use super::Game; use horizon_client::{ block_entity_item, block_entity_nbt, containers::BlockEntity, item_data, items::ItemStack, save, }; use horizon_world::nbt::{Compound, Tag}; /// The component patch of `stack`. fn components(stack: &ItemStack) -> Compound { match item_data::encode_components(stack) { Some(Tag::Compound(c)) => c, _ => Compound::new(), } } impl Game<'_> { /// The block entity at `key` as vanilla saves it, with its custom name. fn saved_block_entity(&self, key: (i32, i32, i32)) -> Option { let entity = self.world.block_entities.get(&key)?; let block = &self .res .facts() .get(self.world.block(key.0, key.1, key.2)) .name; let mut tag = save::encode_block_entity(key, entity, block); if let (Some(name), Tag::Compound(map)) = (self.world.block_names.get(&key), &mut tag) { map.insert("CustomName".into(), Tag::String(name.as_str().into())); } block_entity_nbt::canonical(&tag) } /// `BlockState.getCloneItemStack` of the block at `pos`, then, with /// `include_data`, `ServerGamePacketListenerImpl.addBlockDataToItem`. pub(super) fn picked_stack(&self, pos: [i32; 3], include_data: bool) -> Option { let key = (pos[0], pos[1], pos[2]); let state = self.world.block(pos[0], pos[1], pos[2]); let facts = self.res.facts().get(state); if &*facts.item == "minecraft:air" { return None; } let mut stack = ItemStack::new(&facts.item, 1); let mut patch = Compound::new(); // BeehiveBlock.getCloneItemStack: the honey level as the item's block state. if include_data && let Some(level) = self .pack .assets .registry .state(state) .filter(|s| matches!(s.name.as_str(), "minecraft:beehive" | "minecraft:bee_nest")) .and_then(|s| s.properties.get("honey_level")) { patch.insert( "minecraft:block_state".into(), Tag::compound([("honey_level", Tag::String(level.as_str().into()))]), ); } // AbstractBannerBlock.getCloneItemStack: the banner's own item // (BannerBlockEntity.getItem), data or not. let banner = matches!( self.world.block_entities.get(&key), Some(BlockEntity::Banner(_)) ); if (include_data || banner) && let Some(saved) = self.saved_block_entity(key) { patch.extend(block_entity_item::pick(&saved)); } if !patch.is_empty() { let mut all = components(&stack); all.extend(patch); (stack.damage, stack.extra) = item_data::decode_components(Some(&Tag::Compound(all))); } Some(stack) } /// `BlockItem.updateCustomBlockEntityTag` and `updateBlockEntityComponents` /// for the block entity just placed at `pos` from `stack`; `game_master`: /// the placer can use game master blocks (a dispenser can't). pub(super) fn apply_placed_item( &mut self, pos: [i32; 3], stack: &ItemStack, game_master: bool, ) { let key = (pos[0], pos[1], pos[2]); self.pending.banner.remove(&pos); let Some(saved) = self.saved_block_entity(key) else { return; }; let placed = block_entity_item::place(&saved, &components(stack), game_master); if placed == saved { return; } let Some((_, mut entity)) = save::decode_block_entity(&placed) else { return; }; // The new crafter's state flag (CrafterBlock.newBlockEntity), which // its saved data has no part of. if let (BlockEntity::Crafter(new), Some(BlockEntity::Crafter(old))) = (&mut entity, self.world.block_entities.get(&key)) { new.state_triggered = old.state_triggered; } // Banners and signs keep their name themselves. if !matches!(entity, BlockEntity::Banner(_) | BlockEntity::Sign(_)) { match save::text(placed.get("CustomName")) { Some(name) => self.world.block_names.insert(key, name), None => self.world.block_names.remove(&key), }; } self.world.block_entities.insert(key, entity); } }