Let a comparator read an item frame through a block

A comparator behind a solid block now reads the item frame on its far side (rotation + 1, or 0 when empty), and updates when the frame's item or rotation changes.
This commit is contained in:
Kacpherek
2026-10-26 19:33:24 +01:00
parent 68db8847e7
commit 8c0ed0e591
4 changed files with 45 additions and 2 deletions
+17
View File
@@ -545,6 +545,8 @@ struct GameLevel<'w> {
player_collision: Option<PlayerCollision>,
/// Minecart boxes (detector rails).
cart_boxes: Vec<([f64; 3], [f64; 3])>,
/// The item frames a comparator reads through a conductor.
item_frames: &'w [horizon_client::entities::ItemFrame],
/// The other entities that keep blocks from being placed into them.
blockers: Blockers<'w>,
biomes: &'w Biomes,
@@ -706,6 +708,20 @@ impl LevelAccess for GameLevel<'_> {
}
machines::container_signal(self.world, self.behaviours, self.items, self.registry, pos)
}
fn item_frame_analog_output(&self, pos: BlockPos, facing: Direction) -> Option<i32> {
let mut frames = self
.item_frames
.iter()
.filter(|f| f.pos == [pos.x, pos.y, pos.z] && f.facing == facing as u8);
let frame = frames.next().filter(|_| frames.next().is_none())?;
// ItemFrame.getAnalogOutput.
Some(
frame
.item
.as_ref()
.map_or(0, |_| i32::from(frame.rotation % 8) + 1),
)
}
fn block_entity_signal(
&self,
pos: BlockPos,
@@ -2541,6 +2557,7 @@ impl Game<'_> {
};
let mut level = GameLevel {
cart_boxes,
item_frames: &self.item_frames,
blockers,
world: &mut self.world,
facts: self.res.facts(),
+14
View File
@@ -7,6 +7,7 @@ use horizon_assets::{bake::bake_model, model::CuboidModel, resolve::ModelLibrary
use horizon_client::{entities::ItemFrame, items::ItemStack, mesher::Vertex};
use horizon_core::{
geometry::{Aabb, Vec3},
position::BlockPos,
random::RandomSource,
};
use std::sync::Arc;
@@ -223,6 +224,7 @@ impl Game<'_> {
}
let p = self.item_frames[i].pos;
self.world.modified.insert((p[0] >> 4, p[2] >> 4));
self.frame_output_changed(p);
self.swing_arm();
true
}
@@ -255,8 +257,17 @@ impl Game<'_> {
}
self.drop_frame_items(&f, with_frame, true);
self.world.modified.insert((f.pos[0] >> 4, f.pos[2] >> 4));
if f.item.is_some() {
self.frame_output_changed(f.pos);
}
true
}
/// `ItemFrame.setItem` / `setRotation`: `updateNeighbourForOutputSignal`, so
/// the comparators reading the frame take its new signal.
fn frame_output_changed(&mut self, pos: [i32; 3]) {
let at = BlockPos::new(pos[0], pos[1], pos[2]);
self.with_level(|b, level, ticks| b.update_neighbour_for_output_signal(level, ticks, at));
}
/// `ItemFrame.dropItem`: the frame (when breaking) and the held item.
fn drop_frame_items(&mut self, f: &ItemFrame, with_frame: bool, by_player: bool) {
if !self.ui.rules.bool("entity_drops") || (by_player && self.creative) {
@@ -300,6 +311,9 @@ impl Game<'_> {
self.audio
.play(&sound(&f, "break"), Some(center(&f)), 1.0, 1.0);
self.drop_frame_items(&f, true, false);
if f.item.is_some() {
self.frame_output_changed(f.pos);
}
continue;
}
}
+6
View File
@@ -372,6 +372,12 @@ pub trait LevelAccess {
) -> i32 {
0
}
/// `ItemFrame.getAnalogOutput` of the one item frame in the block at
/// `pos` that faces `facing` (`ComparatorBlock.getItemFrame`), or None when
/// there is not exactly one.
fn item_frame_analog_output(&self, _pos: BlockPos, _facing: Direction) -> Option<i32> {
None
}
/// `ContainerOpenersCounter.recheckOpenCount` for the chest or ender chest
/// block entity at `pos`: recounts its openers and returns the previous
/// and new counts (the caller plays open/close effects when they change),
+8 -2
View File
@@ -490,8 +490,14 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
} else if result < 15 && self.is_redstone_conductor(target_state, target) {
let further = rel(target, facing);
let further_state = self.state(further);
if b.has_flag(further_state, state_flags::ANALOG_OUTPUT) {
result = self.analog_output(further_state, further, facing.opposite());
let block = b
.has_flag(further_state, state_flags::ANALOG_OUTPUT)
.then(|| self.analog_output(further_state, further, facing.opposite()));
// ComparatorBlock.getInputSignal: an item frame behind the
// conductor counts too, the larger signal winning.
let frame = self.level.item_frame_analog_output(further, facing);
if let Some(signal) = frame.into_iter().chain(block).max() {
result = signal;
}
}
}