Daylight detectors follow the sky and update when used

A daylight detector never changed its power: nothing ran its update.
It now has a block entity with the ticker vanilla has, and updates its
power every 20 game ticks from the sky light, the sky darkening and the
sun angle. Using it to flip inverted mode recomputes the power at once.
This commit is contained in:
Kacpherek
2026-10-26 13:44:16 +01:00
parent aea0df9770
commit d0af843094
7 changed files with 98 additions and 8 deletions
+13 -3
View File
@@ -85,6 +85,9 @@ pub enum BlockEntity {
CommandBlock(Box<crate::command_block::CommandBlock>),
/// `SkullBlockEntity` of every skull and head block.
Skull(Skull),
/// `DaylightDetectorBlockEntity`: no data, only the ticker
/// (`DaylightDetectorBlock.tickEntity`).
DaylightDetector,
}
/// `SkullBlockEntity`: the player profile the head shows (kept as loaded:
/// a compound, or a string when it names a player only), the note block
@@ -295,6 +298,9 @@ impl BlockEntity {
if is_skull(name) {
return Some(Self::Skull(Skull::default()));
}
if name == "minecraft:daylight_detector" {
return Some(Self::DaylightDetector);
}
if name == "minecraft:beacon" {
return Some(Self::Beacon(Box::new(Beacon {
last_check_y: i32::MIN / 2,
@@ -341,6 +347,7 @@ impl BlockEntity {
Self::Beehive(_) => "minecraft:beehive",
Self::CommandBlock(_) => "minecraft:command_block",
Self::Skull(_) => "minecraft:skull",
Self::DaylightDetector => "minecraft:daylight_detector",
}
}
pub fn contents(&self) -> Vec<ItemStack> {
@@ -357,7 +364,8 @@ impl BlockEntity {
| Self::Vault(_)
| Self::Beehive(_)
| Self::CommandBlock(_)
| Self::Skull(_) => Vec::new(),
| Self::Skull(_)
| Self::DaylightDetector => Vec::new(),
Self::Lectern(l) => l.book.iter().cloned().collect(),
Self::Sculk(_) => Vec::new(),
Self::Banner(_) => Vec::new(),
@@ -375,7 +383,8 @@ impl BlockEntity {
| Self::Lectern(_)
| Self::Sculk(_)
| Self::Banner(_)
| Self::Skull(_) => &[],
| Self::Skull(_)
| Self::DaylightDetector => &[],
Self::Crafter(c) => &c.items,
Self::CreakingHeart(_)
| Self::Spawner(_)
@@ -396,7 +405,8 @@ impl BlockEntity {
| Self::Lectern(_)
| Self::Sculk(_)
| Self::Banner(_)
| Self::Skull(_) => &mut [],
| Self::Skull(_)
| Self::DaylightDetector => &mut [],
Self::Crafter(c) => &mut c.items,
Self::CreakingHeart(_)
| Self::Spawner(_)
+16 -2
View File
@@ -532,6 +532,8 @@ struct GameLevel<'w> {
events: &'w mut Vec<LevelEvent>,
dimension: LevelDimension,
darken: i32,
/// The environment of the level's dimension (`SUN_ANGLE`).
environment: &'w Environment,
raining: bool,
thundering: bool,
player_box: Option<([f64; 3], [f64; 3])>,
@@ -759,6 +761,11 @@ impl LevelAccess for GameLevel<'_> {
fn sky_darken(&self) -> i32 {
self.darken
}
fn sun_angle(&self, _pos: BlockPos) -> f32 {
self.environment
.sample(self.world.day_time, None, None)
.sun_angle
}
fn is_raining(&self) -> bool {
self.raining
}
@@ -2469,6 +2476,11 @@ impl Game<'_> {
events: &mut self.level_events,
dimension,
darken,
environment: match dimension {
LevelDimension::Overworld => self.res.environment(),
LevelDimension::Nether => self.res.nether_environment(),
LevelDimension::End => self.res.end_environment(),
},
raining: rain > 0.2,
thundering: thunder > 0.9,
player_box,
@@ -5278,7 +5290,8 @@ impl Game<'_> {
| BlockEntity::Vault(_)
| BlockEntity::Beehive(_)
| BlockEntity::CommandBlock(_)
| BlockEntity::Skull(_),
| BlockEntity::Skull(_)
| BlockEntity::DaylightDetector,
) => {}
None => {
v.extend((0..self.craft_size * self.craft_size).map(|i| 100 + i));
@@ -5548,7 +5561,8 @@ impl Game<'_> {
| BlockEntity::Vault(_)
| BlockEntity::Beehive(_)
| BlockEntity::CommandBlock(_)
| BlockEntity::Skull(_),
| BlockEntity::Skull(_)
| BlockEntity::DaylightDetector,
) => {}
None if self.mount_active() => self.draw_mount_background(batch, ox, oy),
None => {
@@ -10,6 +10,7 @@ use horizon_client::{
block_entities::{Pos, Ticking},
containers::{BlockEntity, FurnaceKind, HOPPER_SLOTS},
};
use horizon_core::position::BlockPos;
use horizon_world::registry::BlockStateId;
impl Game<'_> {
@@ -56,6 +57,7 @@ impl Game<'_> {
Some(BlockEntity::Beehive(_)) => self.tick_beehive(pos),
Some(BlockEntity::Furnace(_)) => self.tick_furnace(pos)?,
Some(BlockEntity::Brewing(_)) => self.tick_brewing_stand(pos)?,
Some(BlockEntity::DaylightDetector) => self.tick_daylight_detector(pos),
_ => {}
}
}
@@ -92,7 +94,8 @@ impl Game<'_> {
| "minecraft:vault"
| "minecraft:beehive"
| "minecraft:bee_nest"
| "minecraft:brewing_stand" => Ticking::Yes,
| "minecraft:brewing_stand"
| "minecraft:daylight_detector" => Ticking::Yes,
"minecraft:jukebox" => {
let record = registry
.state(state)
@@ -113,6 +116,16 @@ impl Game<'_> {
let levels = self.tickets.levels();
move |x, z| levels.block_ticking_at(x, z)
}
/// `DaylightDetectorBlock.tickEntity` of the detector at `pos`.
fn tick_daylight_detector(&mut self, pos: Pos) {
// It updates every 20 game ticks (the behaviour checks this too).
if self.world.game_time % 20 != 0 {
return;
}
let at = BlockPos::new(pos.0, pos.1, pos.2);
self.with_level(|b, level, ticks| b.tick_daylight_detector(level, ticks, at));
let _ = self.process_level_events();
}
/// `AbstractFurnaceBlockEntity.serverTick` of the furnace at `pos`, and
/// its block's `lit` following it.
fn tick_furnace(&mut self, pos: Pos) -> Result<()> {
+3 -1
View File
@@ -569,6 +569,7 @@ pub fn encode_block_entity(
BlockEntity::Vault(v) => entries.extend(v.save()),
BlockEntity::Beehive(h) => entries.extend(h.to_nbt()),
BlockEntity::CommandBlock(c) => entries.extend(c.save()),
BlockEntity::DaylightDetector => {}
}
Tag::compound(entries)
}
@@ -724,7 +725,8 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
| BlockEntity::Lectern(_)
| BlockEntity::Banner(_)
| BlockEntity::CreakingHeart(_)
| BlockEntity::Skull(_) => {}
| BlockEntity::Skull(_)
| BlockEntity::DaylightDetector => {}
BlockEntity::Spawner(s) => **s = crate::mobs::spawner::Spawner::load(tag),
BlockEntity::TrialSpawner(s) => **s = crate::mobs::trial_spawner::TrialSpawner::load(tag),
BlockEntity::Vault(v) => **v = crate::vault::Vault::load(tag),
@@ -195,6 +195,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
let next = table.cycle(state, p.inverted);
self.set_block(pos, next, 2);
self.game_event(pos, "minecraft:block_change");
self.daylight_detector_update_signal(pos, next);
Success
}
Class::Cake => {
+22
View File
@@ -319,6 +319,10 @@ pub trait LevelAccess {
fn sky_darken(&self) -> i32 {
0
}
/// `EnvironmentAttributes.SUN_ANGLE` at `pos`, in degrees (0 at noon).
fn sun_angle(&self, _pos: BlockPos) -> f32 {
0.0
}
fn is_raining(&self) -> bool {
false
}
@@ -749,6 +753,24 @@ impl Behaviours {
) {
self.sim(level, ticks).entity_inside(pos);
}
/// `DaylightDetectorBlock.tickEntity` of the daylight detector at `pos`:
/// its signal follows the sky every 20 game ticks. The detector's ticker
/// exists only where the dimension has sky light.
pub fn tick_daylight_detector<L: LevelAccess + ?Sized>(
&self,
level: &mut L,
ticks: &mut Ticks,
pos: BlockPos,
) {
if level.dimension() == Dimension::Nether || level.game_time() % 20 != 0 {
return;
}
let mut sim = self.sim(level, ticks);
let state = sim.state(pos);
if sim.b.data(state).is(classes::Class::DaylightDetector) {
sim.daylight_detector_update_signal(pos, state);
}
}
/// `BigDripleafBlock.entityInside` for an entity whose box is inside
/// `pos`: whether it is on the ground and its feet's y (`canEntityTilt`).
pub fn entity_on_big_dripleaf<L: LevelAccess + ?Sized>(
+29 -1
View File
@@ -11,7 +11,10 @@ use super::{
};
use crate::registry::BlockStateId;
use horizon_core::{
geometry::Direction, position::BlockPos, random::RandomSource, ticks::TickPriority,
geometry::{Direction, trig},
position::BlockPos,
random::RandomSource,
ticks::TickPriority,
};
const SIGNAL_ORDER: [Direction; 6] = [
@@ -892,6 +895,31 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
}
self.game_event(pos, "minecraft:block_deactivate");
}
/// `DaylightDetectorBlock.updateSignalStrength`: the effective sky light
/// at the detector, shaped by the sun's angle (or inverted), as its
/// power.
pub(crate) fn daylight_detector_update_signal(&mut self, pos: BlockPos, state: BlockStateId) {
let b = self.b;
let mut target = (self.level.sky_light(pos) - self.level.sky_darken()).max(0);
if b.table.bool(state, b.props.inverted) {
target = 15 - target;
} else if target > 0 {
let mut sun_angle = self.level.sun_angle(pos) * trig::DEG_TO_RAD;
let offset = if sun_angle < std::f32::consts::PI {
0.0
} else {
std::f32::consts::TAU
};
sun_angle += (offset - sun_angle) * 0.2;
// Math.round(float).
let scaled = target as f32 * trig::cos(f64::from(sun_angle));
target = (f64::from(scaled) + 0.5).floor() as i32;
}
let target = target.clamp(0, 15);
if b.table.int(state, b.props.power) != target {
self.set_block_and_update(pos, b.table.set_int(state, b.props.power, target));
}
}
/// `LeverBlock.pull`.
pub(crate) fn lever_pull(&mut self, pos: BlockPos, state: BlockStateId) {
let b = self.b;