From 401ac1322ffdd9aa72f2fffd927f135176a0ec98 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:43:03 +0100 Subject: [PATCH] Banners: one loader for saved and generated banners The generated banner reuses save.rs's BannerBlockEntity.loadAdditional (now decode_banner) instead of a copy of it, and the generated block entity clones its NBT only for the kinds that read it. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/save.rs | 23 +++++----- crates/horizon-client/src/structure_blocks.rs | 45 ++++++------------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index da5735c17..69b6a44f5 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -567,6 +567,17 @@ fn merge_block_entity(original: &Tag, loaded: &Tag, current: &Tag) -> Tag { } merged } +/// `BannerBlockEntity.loadAdditional` (and the `components` a block entity keeps). +pub fn decode_banner(tag: &Tag) -> crate::containers::Banner { + crate::containers::Banner { + layers: crate::banner::layers_from_nbt(tag.get("patterns")), + name: text(tag.get("CustomName")), + components: match tag.get("components") { + Some(Tag::Compound(c)) => c.clone(), + _ => Compound::new(), + }, + } +} pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers::BlockEntity)> { use crate::containers::BlockEntity; let int = |k: &str| number(tag.get(k)).map(|v| v as i32); @@ -600,17 +611,7 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain return Some((pos, BlockEntity::Lectern(lectern))); } if id == "minecraft:banner" { - return Some(( - pos, - BlockEntity::Banner(crate::containers::Banner { - layers: crate::banner::layers_from_nbt(tag.get("patterns")), - name: text(tag.get("CustomName")), - components: match tag.get("components") { - Some(Tag::Compound(c)) => c.clone(), - _ => Compound::new(), - }, - }), - )); + return Some((pos, BlockEntity::Banner(decode_banner(tag)))); } if id == "minecraft:beacon" { let beacon = crate::containers::Beacon { diff --git a/crates/horizon-client/src/structure_blocks.rs b/crates/horizon-client/src/structure_blocks.rs index da5b75258..ff51116bc 100644 --- a/crates/horizon-client/src/structure_blocks.rs +++ b/crates/horizon-client/src/structure_blocks.rs @@ -128,40 +128,23 @@ fn text(nbt: &Compound, key: &str) -> Option { } } -/// `BannerBlockEntity.loadAdditional`: the `patterns` layers and the custom -/// name a structure template saves with its banner (the End cities' tower -/// tops place black triangles this way). -fn banner_from_nbt(nbt: &Compound) -> crate::containers::Banner { - crate::containers::Banner { - layers: crate::banner::layers_from_nbt(nbt.get(&JavaString::from("patterns"))), - name: text(nbt, "CustomName"), - components: match nbt.get(&JavaString::from("components")) { - Some(Tag::Compound(c)) => c.clone(), - _ => Compound::new(), - }, - } -} - /// The client block entity for one of a chunk's generated block entities, -/// before a container's loot is unpacked. +/// before a container's loot is unpacked: `StructureTemplate.placeInWorld` +/// hands the template's block entity NBT to the level's block entity. fn generated_block_entity(block: &str, nbt: &Compound) -> Option { let mut entity = BlockEntity::for_block(block)?; - let tag = Tag::Compound(nbt.clone()); - // MonsterRoomFeature and the structures' setEntityId. - if let BlockEntity::Spawner(s) = &mut entity { - **s = crate::mobs::spawner::Spawner::load(&tag); - } - if let BlockEntity::TrialSpawner(s) = &mut entity { - **s = crate::mobs::trial_spawner::TrialSpawner::load(&tag); - } - if let BlockEntity::Vault(v) = &mut entity { - **v = crate::vault::Vault::load(&tag); - } - // StructureTemplate.placeInWorld hands the template's block entity NBT to - // the level, so `BannerBlockEntity.loadAdditional` keeps the banner's - // patterns (the End cities' black triangles) and its name. - if let BlockEntity::Banner(b) = &mut entity { - *b = banner_from_nbt(nbt); + match &mut entity { + // MonsterRoomFeature and the structures' setEntityId. + BlockEntity::Spawner(s) => { + **s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone())); + } + BlockEntity::TrialSpawner(s) => { + **s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone())); + } + BlockEntity::Vault(v) => **v = crate::vault::Vault::load(&Tag::Compound(nbt.clone())), + // BannerBlockEntity.loadAdditional: the End cities' black triangles. + BlockEntity::Banner(b) => *b = crate::save::decode_banner(&Tag::Compound(nbt.clone())), + _ => {} } Some(entity) }