From ff28fd839036a7c3f88203a428bd6c23a92963f9 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 06:13:40 +0100 Subject: [PATCH] Read a chiseled bookshelf's last slot and a shelf's back mask with a comparator --- crates/horizon-client/src/game.rs | 7 +++- crates/horizon-client/src/game/machines.rs | 39 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7699e83d7..087b15dc7 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -700,7 +700,7 @@ impl LevelAccess for GameLevel<'_> { &self, pos: BlockPos, state: BlockStateId, - _direction: Direction, + direction: Direction, ) -> i32 { if let Some(signal) = command_blocks::analog_signal(self.world, (pos.x, pos.y, pos.z)) { return signal; @@ -725,6 +725,11 @@ impl LevelAccess for GameLevel<'_> { if let Some(signal) = books::lectern_signal(self.world, pos) { return signal; } + if let Some(signal) = + machines::shelf_signal(self.world, self.registry, state, pos, direction) + { + return signal; + } machines::container_signal(self.world, self.behaviours, self.items, self.registry, pos) } fn block_entity_signal( diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index bb936744b..f398f4511 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -399,6 +399,45 @@ pub(super) fn container_signal( .map_or(0, |v| c.signal(&v)) } +/// `ChiseledBookShelfBlock.getAnalogOutputSignal` and +/// `ShelfBlock.getAnalogOutputSignal`, from the block entity Horizon keeps as +/// it was loaded; `None` for any other block. `direction` points from the +/// block to the comparator. +pub(super) fn shelf_signal( + world: &World, + registry: &'static horizon_world::registry::BlockRegistry, + state: BlockStateId, + pos: BlockPos, + direction: Direction, +) -> Option { + let info = registry.state(state)?; + let chiseled = info.name == "minecraft:chiseled_bookshelf"; + if !chiseled && !info.name.ends_with("_shelf") { + return None; + } + let tag = super::spill::saved_entity(world, [pos.x, pos.y, pos.z]); + if chiseled { + // The last slot touched, plus one (`lastInteractedSlot` starts at -1). + let last = tag + .as_ref() + .and_then(|t| horizon_client::save::number(t.get("last_interacted_slot"))); + return Some(last.map_or(0, |slot| slot as i32 + 1)); + } + // Only read from the back: a bit for each of the three slots in use. + let facing = info.properties.get("facing").and_then(|f| dir_from_name(f)); + if facing.map(Direction::opposite) != Some(direction) { + return Some(0); + } + let items = horizon_client::save::decode_items(tag.as_ref().and_then(|t| t.get("Items")), 3); + Some( + items + .iter() + .enumerate() + .map(|(slot, item)| i32::from(item.is_some()) << slot) + .sum(), + ) +} + impl Game<'_> { fn containers(&self) -> Containers<'_> { Containers {