From 28d218b3a2c17ff1b5b373b52b24b8d3000bfd2e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 16:48:13 +0200 Subject: [PATCH 01/67] Guests: a bed puts the guest to sleep, not the host The bed's sleeping state, timer and respawn point were the host's fields, so a guest clicking a bed put the host to sleep and moved the host's spawn. as_guest now swaps them with the guest's own; the guest gets its sleeping pose and bed (entity data 14), wakes on STOP_SLEEPING with the WAKE_UP animation, and the night skips only once enough players sleep (players_sleeping_percentage). Guests respawn at their own bed, else the world spawn. Server clients lie in the bed from the synced position. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 52 +++++---- crates/horizon-client/src/game/agent.rs | 2 + crates/horizon-client/src/game/lan.rs | 1 + crates/horizon-client/src/game/lan_actions.rs | 106 +++++++++++++++++- crates/horizon-client/src/game/remote.rs | 34 +++++- crates/horizon-client/src/lan/guest.rs | 12 ++ crates/horizon-client/src/lan/hub.rs | 37 +++++- crates/horizon-client/src/lan/playerdata.rs | 2 + tools/lan_e2e.py | 54 ++++++++- 9 files changed, 273 insertions(+), 27 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 238f68e55..5ce509705 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -8057,40 +8057,48 @@ impl Game<'_> { self.stats.reset("custom", "minecraft:time_since_rest"); } fn wake_up(&mut self) { + if self.remote.is_some() { + // InBedChatScreen.sendWakeUp: the server wakes the player. + self.remote_stop_sleeping(); + return; + } self.sleeping = None; self.sleep_timer = 100; } fn tick_sleep(&mut self) { self.sleep_timer = tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); - let Some(head) = self.sleeping else { - return; - }; - if self.keys.contains(&KeyCode::ShiftLeft) - || self - .player - .as_ref() - .is_none_or(|p| p.health.dead_or_dying()) - { - self.wake_up(); - return; + if let Some(head) = self.sleeping { + if self.keys.contains(&KeyCode::ShiftLeft) + || self + .player + .as_ref() + .is_none_or(|p| p.health.dead_or_dying()) + { + self.wake_up(); + return; + } + if let Some(p) = self.player.as_mut() { + p.position = Vec3::new( + f64::from(head[0]) + 0.5, + f64::from(head[1]) + 0.5625, + f64::from(head[2]) + 0.5, + ); + p.velocity = Vec3::ZERO; + } } - if let Some(p) = self.player.as_mut() { - p.position = Vec3::new( - f64::from(head[0]) + 0.5, - f64::from(head[1]) + 0.5625, - f64::from(head[2]) + 0.5, - ); - p.velocity = Vec3::ZERO; - } - // ServerLevel: once everyone has slept 100 ticks, skip to morning and clear weather. - if self.sleep_timer >= 100 { + // ServerLevel.tick: once enough of the players have slept 100 ticks, + // skip to morning, clear the weather and wake everyone. + if self.enough_slept() { let t = self.world.day_time; self.world.day_time = t + 24000 - t.rem_euclid(24000); self.world.weather.raining = false; self.world.weather.thundering = false; self.world.weather.rain_time = 0; self.world.weather.thunder_time = 0; - self.wake_up(); + if self.sleeping.is_some() { + self.wake_up(); + } + self.wake_guests(); } } fn creative_origin(batch: &HudBatch) -> (f32, f32) { diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index 48b24bbb3..11e4cef07 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -466,6 +466,8 @@ impl Game<'_> { }), "dimension": self.dim.id(), "gamemode": format!("{:?}", self.mode).to_lowercase(), + // The bed (its head) the player sleeps in. + "sleeping": self.sleeping, "time": { "day_time": self.world.day_time, "day": self.world.day_time.div_euclid(24000), "time_of_day": self.world.day_time.rem_euclid(24000) }, "weather": { "raining": self.world.weather.raining, "thundering": self.world.weather.thundering }, "paused": self.pause, diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index d574b8928..1c149ba00 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -370,6 +370,7 @@ impl Game<'_> { "loaded": g.loaded, "game_mode": g.game_mode, "sneaking": g.sneaking, + "sleeping": g.bed, "health": g.health, // FoodData: food level, saturation and exhaustion. "food": [ diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 7cd4b0474..380fa09ec 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -163,6 +163,16 @@ impl Game<'_> { self.right = play.using.is_some() || self.using.is_some(); let riding = self.riding.take(); let host_station = self.station.take(); + // The guest's bed, sleep counter and respawn point; the host's + // respawn point, anchor and action bar stay theirs. + let sleeping = std::mem::replace(&mut self.sleeping, g.bed); + let sleep_timer = std::mem::replace(&mut self.sleep_timer, g.sleep_timer); + let spawn = std::mem::replace( + &mut self.spawn, + g.spawn.map(|s| Vec3::new(s[0], s[1], s[2])), + ); + let respawn = (self.respawn_config.take(), self.level.spawn); + let message = self.message.take(); // The guest's own ender inventory; one the host has open goes back // into the host's while the guest acts. let host_ender = self.ender_open; @@ -194,6 +204,12 @@ impl Game<'_> { let opened_table = std::mem::replace(&mut self.craft_size, craft_size) == 3; self.craft_grid = craft_grid; let opened_station = std::mem::replace(&mut self.station, host_station); + let bed = std::mem::replace(&mut self.sleeping, sleeping); + let guest_sleep_timer = std::mem::replace(&mut self.sleep_timer, sleep_timer); + let guest_spawn = std::mem::replace(&mut self.spawn, spawn).map(|s| [s.x, s.y, s.z]); + (self.respawn_config, self.level.spawn) = respawn; + // ServerPlayer.displayClientMessage(_, true): the guest's action bar. + let guest_message = std::mem::replace(&mut self.message, message); self.riding = riding; self.left = left; self.right = right; @@ -211,6 +227,15 @@ impl Game<'_> { self.keys.remove(&KeyCode::ShiftLeft); } let g = &mut host.hub.guests[i]; + let fell_asleep = bed.is_some() && g.bed.is_none(); + (g.bed, g.sleep_timer, g.spawn) = (bed, guest_sleep_timer, guest_spawn); + g.sleeping = g.bed.is_some(); + if let Some((text, _)) = guest_message { + g.send(&C::SystemChat(play::SystemChat { + content: literal(&text), + overlay: true, + })); + } std::mem::swap(&mut self.ender_items, &mut g.ender_items); std::mem::swap(&mut self.adv.progress, &mut g.advancements); std::mem::swap(&mut self.adv.changed, &mut g.advancements_changed); @@ -260,6 +285,19 @@ impl Game<'_> { } } let g = &mut host.hub.guests[i]; + if let Some(head) = g.bed.filter(|_| fell_asleep) { + // LivingEntity.startSleeping: in the bed, still. + let at = [ + f64::from(head[0]) + 0.5, + f64::from(head[1]) + 0.5625, + f64::from(head[2]) + 0.5, + ]; + g.position = at; + if let Some(m) = g.motion.as_mut() { + m.position = Vec3::new(at[0], at[1], at[2]); + m.velocity = Vec3::ZERO; + } + } if new_mode != g.game_mode { let conn = g.conn; host.hub.set_game_mode(conn, new_mode); @@ -903,10 +941,19 @@ impl Game<'_> { } fn guest_respawn(&mut self, host: &mut LanHost, conn: u32) { - let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]); let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { return; }; + // The guest's own bed, else the world spawn: never the host's bed. + let world = self + .level + .world_spawn + .map(|[x, y, z]| [f64::from(x) + 0.5, f64::from(y), f64::from(z) + 0.5]); + let spawn = host.hub.guests[i] + .spawn + .or(world) + .or(self.spawn.map(|s| [s.x, s.y, s.z])) + .unwrap_or([0.5, 80.0, 0.5]); if !host.hub.guests[i].dead { return; } @@ -964,6 +1011,7 @@ impl Game<'_> { let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { continue; }; + self.guest_sleep_tick(host, gi); if host.hub.guests[gi].dead || self.on_server_trip(conn) { continue; } @@ -1029,6 +1077,62 @@ impl Game<'_> { } } + /// `Player.tick`'s sleep counter, and `LivingEntity.tick`'s wake-up of a + /// sleeper who died or whose bed is gone. + fn guest_sleep_tick(&mut self, host: &mut LanHost, gi: usize) { + let g = &mut host.hub.guests[gi]; + g.sleep_timer = super::tick_sleep_timer(g.sleep_timer, g.bed.is_some()); + if let Some(head) = g.bed { + let block = self.world.block(head[0], head[1], head[2]); + let bed = self.res.facts.get(block).name.ends_with("_bed"); + if g.dead || g.health <= 0.0 || !bed { + host.hub.wake(gi); + } + } + } + + /// `SleepStatus.areEnoughSleeping` and `areEnoughDeepSleeping` over the + /// host's player and the guests: of the players not in spectator mode, + /// players_sleeping_percentage of them (at least one) are in bed, and as + /// many have slept 100 ticks. + pub(super) fn enough_slept(&self) -> bool { + let (mut active, mut sleeping, mut deep) = (0i64, 0i64, 0i64); + let mut count = |in_bed: bool, timer: u32| { + active += 1; + if in_bed { + sleeping += 1; + if timer >= 100 { + deep += 1; + } + } + }; + if self.player.is_some() && !self.spectator() { + count(self.sleeping.is_some(), self.sleep_timer); + } + if let Some(host) = self.lan.as_ref() { + for g in host + .hub + .guests + .iter() + .filter(|g| g.placed && !g.spectator()) + { + count(g.bed.is_some(), g.sleep_timer); + } + } + let percent = i64::from(self.ui.rules.int("players_sleeping_percentage").max(0)); + let needed = ((active * percent + 99) / 100).max(1); + sleeping >= needed && deep >= needed + } + + /// `ServerLevel.wakeUpAllPlayers` for the guests. + pub(super) fn wake_guests(&mut self) { + if let Some(host) = self.lan.as_mut() { + for i in 0..host.hub.guests.len() { + host.hub.wake(i); + } + } + } + /// `PlayerList.broadcastSystemMessage` of the advancements guests made /// (their own actions, commands and ticks). fn announce_guest_advancements(&mut self, host: &mut LanHost) { diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 05e1f81c3..0a2c987d1 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -760,6 +760,7 @@ impl Game<'_> { 0 => { if me { self.sleeping = None; + self.sleep_timer = 100; } return; } @@ -1271,6 +1272,18 @@ impl Game<'_> { } } } + // LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): this + // player lies down in the bed, or gets up. + if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) { + for (index, _, value) in &p.values { + if let (14, Value::OptPos(bed)) = (index, value) { + if bed.is_some() && self.sleeping.is_none() { + self.sleep_timer = 0; + } + self.sleeping = *bed; + } + } + } for (index, _, value) in &p.values { if *index == ITEM_ENTITY_ITEM && let Value::Item(stack) = value @@ -2124,6 +2137,8 @@ impl Game<'_> { self.tick_hand(); self.tick_particles(); self.tick_client_effects(); + // Player.tick's sleep counter (the darkening and the wake-up fade). + self.sleep_timer = super::tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); let ghast_collisions = self.happy_ghast_player_collisions(); let Some(player) = self.player.as_mut() else { return Ok(()); @@ -2176,7 +2191,15 @@ impl Game<'_> { ); player.yaw = self.yaw; let dead = self.screen == Screen::Dead; - if !dead { + // LivingEntity.isImmobile: a sleeper lies still in the bed. + if let Some(head) = self.sleeping { + player.position = Vec3::new( + f64::from(head[0]) + 0.5, + f64::from(head[1]) + 0.5625, + f64::from(head[2]) + 0.5, + ); + player.velocity = Vec3::ZERO; + } else if !dead { MotionWorld { entities: &ghast_collisions, player, @@ -3134,6 +3157,15 @@ impl Game<'_> { .ok()?; Some((rows, r.menu_title.clone().unwrap_or_default())) } + /// ServerboundPlayerCommandPacket STOP_SLEEPING. + pub(super) fn remote_stop_sleeping(&mut self) { + let id = self.remote.as_ref().map_or(0, |r| r.entity_id); + self.send(S::PlayerCommand(play::PlayerCommand { + id, + action: 0, + data: 0, + })); + } pub(super) fn remote_respawn(&mut self) { self.send(S::ClientCommand(play::ClientCommand { action: 0 })); } diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index b0ecf772f..a397e8bc3 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -69,6 +69,14 @@ pub struct Guest { pub fall_flying: bool, pub flying: bool, pub sleeping: bool, + /// The bed (its head) the player sleeps in (`LivingEntity.getSleepingPos`), + /// `Player.sleepCounter`, and the bed last told to the player itself. + pub bed: Option<[i32; 3]>, + pub sleep_timer: u32, + pub sent_bed: Option<[i32; 3]>, + /// Where the player respawns (a bed's spot; `ServerPlayer.respawnConfig`), + /// None for the world spawn. + pub spawn: Option<[f64; 3]>, /// Pending ClientboundPlayerPositionPacket id; movement is ignored until /// the client accepts it (ServerGamePacketListenerImpl.awaitingPositionFromClient). pub awaiting_teleport: Option<(i32, [f64; 3])>, @@ -201,6 +209,10 @@ impl Guest { fall_flying: false, flying: false, sleeping: false, + bed: None, + sleep_timer: 0, + sent_bed: None, + spawn: None, awaiting_teleport: None, teleport_counter: 0, teleport_tick: 0, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 598bbc088..cac03821b 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1439,10 +1439,44 @@ impl Hub { } } + /// ServerPlayer.stopSleepInBed: the wake-up animation goes to the player + /// and those around, the pose is standing again and the sleep counter + /// starts the wake-up fade (100). + pub fn wake(&mut self, i: usize) { + let g = &mut self.guests[i]; + if g.bed.take().is_none() && !g.sleeping { + return; + } + g.sleeping = false; + g.sleep_timer = 100; + let (id, at) = (g.entity_id, g.position); + // ClientboundAnimatePacket.WAKE_UP. + self.broadcast_near( + at, + 128.0, + &C::Animate(play::Animate { id, action: 0 }), + None, + ); + } + // ---- per-player state ------------------------------------------------ /// ServerPlayer.doTick: health, food, experience and the inventory menu. fn sync_player_state(&mut self, i: usize) { + // LivingEntity.startSleeping / stopSleeping: the pose and the bed go + // to the player too (its client lies down in the bed from them). + let g = &mut self.guests[i]; + if g.bed != g.sent_bed { + g.sent_bed = g.bed; + let pose = if g.bed.is_some() { 2 } else { 0 }; + g.send(&C::SetEntityData(play::SetEntityData { + id: g.entity_id, + values: vec![ + (6, serializer::POSE, Value::Id(pose)), + (14, serializer::OPTIONAL_BLOCK_POS, Value::OptPos(g.bed)), + ], + })); + } // ServerEntity.sendDirtyEntityData on the player's first tick: the // health its creation set is still dirty, and goes to the player too. if self.guests[i].sent_health.is_none() { @@ -1691,7 +1725,8 @@ impl Hub { 1 => self.guests[i].sprinting = true, 2 => self.guests[i].sprinting = false, 6 => self.guests[i].fall_flying = true, - 0 => self.guests[i].sleeping = false, + // STOP_SLEEPING. + 0 => self.wake(i), _ => {} }, S::Chat(c) => { diff --git a/crates/horizon-client/src/lan/playerdata.rs b/crates/horizon-client/src/lan/playerdata.rs index 555463685..770120879 100644 --- a/crates/horizon-client/src/lan/playerdata.rs +++ b/crates/horizon-client/src/lan/playerdata.rs @@ -69,6 +69,7 @@ pub fn apply(guest: &mut Guest, level: &Level) { guest.effects = crate::effects::PlayerEffects::from_saved(&level.active_effects); guest.game_mode = level.mode(); guest.selected = (level.selected_slot.max(0) as usize).min(8); + guest.spawn = level.spawn.map(|s| [s[0] + 0.5, s[1], s[2] + 0.5]); guest.saved = level.player_extra.clone(); guest.recipe_book = level.recipe_book.clone(); if level.ender_items.len() == 27 { @@ -125,6 +126,7 @@ pub fn store(guest: &Guest, level: &mut Level) { level.game_mode = guest.game_mode; level.creative = guest.game_mode == 1; level.selected_slot = guest.selected as i32; + level.spawn = guest.spawn.map(|s| [s[0] - 0.5, s[1], s[2] - 0.5]); level.on_ground = guest.on_ground; level.player_extra = guest.saved.clone(); level.recipe_book = guest.recipe_book.clone(); diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 667fdda2d..c4d5969e4 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -20,6 +20,8 @@ address. The script then checks, from both sides, what a guest's game used to ge - containers: the guest's ender chest holds the guest's items, not the host's, and a double chest opens as 9x6 with the right half first; - feedback: the guest's command feedback names the guest and shows the numbers. +- sleep: at night the guest lies down in its own bed (not the host); alone it stays night, it + gets up with Escape, and once the host sleeps too the night ends and both wake. Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -48,7 +50,7 @@ from agent_control_e2e import Cli, Failed, game_env # noqa: E402 ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials', 'sleep'] class Game: @@ -622,11 +624,59 @@ def check_trials(host, guest, out): return {'spawner': trial, 'vault': active, 'rewarded': uuid(rewarded), 'host': uuid(host_uuid)} +def bed_at(game, foot): + """A bed whose foot is at `foot`, its head one block east.""" + x, y, z = foot + game('command', command=f'setblock {x} {y} {z} minecraft:red_bed[facing=east,part=foot]') + game('command', command=f'setblock {x + 1} {y} {z} minecraft:red_bed[facing=east,part=head]') + return [x + 1, y, z] + + +def check_sleep(host, guest, out): + for g in (host, guest): + g('command', command='gamemode survival') + host('command', command='time set midnight') + gx, gy, gz = (int(v // 1) for v in pos_of(guest)) + hx, hy, hz = (int(v // 1) for v in pos_of(host)) + guest_bed = bed_at(guest, (gx, gy, gz + 2)) + host_bed = bed_at(host, (hx, hy, hz + 2)) + guest.wait(10) + guest.raw('interact', *guest_bed) + guest.wait(10) + asleep = {'guest': guest('state').get('sleeping'), 'host': host('state').get('sleeping'), + 'server': host.raw('lan', 'status')['guests'][0].get('sleeping')} + guest.shot(out, 'sleep-guest') + # Alone in bed: still night 120 ticks later (everyone must sleep). + guest.wait(120) + night = host('state')['time']['time_of_day'] + # Escape gets the guest up (InBedChatScreen), then back to bed. + guest.raw('key', 'esc', 'tap') + guest.wait(10) + up = guest('state').get('sleeping') + guest.raw('interact', *guest_bed) + host.raw('interact', *host_bed) + host.wait(130) + after = {'guest': guest('state').get('sleeping'), 'host': host('state').get('sleeping'), + 'time': host('state')['time']['time_of_day']} + guest.shot(out, 'sleep-morning') + for at in (guest_bed, host_bed): + guest('command', command=f'fill {at[0] - 1} {at[1]} {at[2]} {at[0]} {at[1]} {at[2]} minecraft:air') + host('command', command='time set day') + detail = {'asleep': asleep, 'night': night, 'up': up, 'after': after} + assert asleep['guest'] == guest_bed and asleep['server'] == guest_bed, f'the guest is not in its bed: {detail}' + assert asleep['host'] is None, f'the guest\'s bed put the host to sleep: {detail}' + assert 13000 < night < 23000, f'one of two players asleep ended the night: {detail}' + assert up is None, f'Escape did not get the guest up: {detail}' + assert after['guest'] is None and after['host'] is None and after['time'] < 1000, f'no morning: {detail}' + return detail + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, 'containers': check_containers, 'feedback': check_feedback, 'trials': check_trials, - 'walls': check_walls} + 'walls': check_walls, + 'sleep': check_sleep} def run(ctl, out, resources, checks): From ce62f9c57f70ea4a50fd6a0d45604f017d9cde38 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 17:34:55 +0200 Subject: [PATCH 02/67] Server clients: eating plays on the player's own screen A server client sent UseItem and never started the use itself, so food showed no eating animation or crumbs, and holding right click sent a new UseItem every 4 ticks. Like MultiPlayerGameMode.useItem, the client now starts eating (Consumable.startConsuming), counts the use down for the animation and its particles, and starts no other use until it ends; the server still eats the item. Looking at another player no longer stops the item from being used (Player.interactOn passes). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/agent.rs | 2 + crates/horizon-client/src/game/remote.rs | 56 ++++++++++++++++++++++-- tools/lan_e2e.py | 40 ++++++++++++++++- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index 48b24bbb3..1659ab225 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -466,6 +466,8 @@ impl Game<'_> { }), "dimension": self.dim.id(), "gamemode": format!("{:?}", self.mode).to_lowercase(), + // The item in use: its slot and for how many ticks. + "using": self.using.map(|(slot, ticks)| json!({ "slot": slot, "ticks": ticks })), "time": { "day_time": self.world.day_time, "day": self.world.day_time.div_euclid(24000), "time_of_day": self.world.day_time.rem_euclid(24000) }, "weather": { "raining": self.world.weather.raining, "thundering": self.world.weather.thundering }, "paused": self.pause, diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 05e1f81c3..a473eeb57 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2220,10 +2220,12 @@ impl Game<'_> { self.tick_player_model(); if !dead { self.remote_mining(); - if self.right && self.use_delay == 0 && playing { + // Minecraft.handleKeybinds: no new use while an item is in use. + if self.right && self.use_delay == 0 && playing && self.using.is_none() { self.remote_use(); self.use_delay = 4; } + self.remote_tick_using(); } self.remote_send_state(input); self.send(S::ClientTickEnd(Empty)); @@ -2602,8 +2604,16 @@ impl Game<'_> { location: [0.0, 1.0, 0.0], secondary: sneaking, })); - self.swing_arm(); - return; + // Player.interactOn: another player passes, and the held item is + // used instead (Minecraft.startUseItem). + let player = self + .remote + .as_ref() + .is_some_and(|r| r.level_players.contains_key(&id)); + if !player { + self.swing_arm(); + return; + } } if let Some(hit) = self.pick() { let seq = self.next_sequence(); @@ -2636,7 +2646,7 @@ impl Game<'_> { return; } } - if self.held().is_some() { + if let Some(held) = self.held().cloned() { let seq = self.next_sequence(); self.send(S::UseItem(play::UseItem { hand: 0, @@ -2644,6 +2654,44 @@ impl Game<'_> { yaw: self.yaw, pitch: self.pitch, })); + // MultiPlayerGameMode.useItem runs the use here too: food starts + // being eaten (Consumable.startConsuming). + if let Some(info) = self.res.items.get(&held.item).cloned() { + self.start_eating(&info); + } + } + } + /// `LivingEntity.updatingUsingItem` for food on a server client: the + /// eating animation and its crumbs. The server eats it and sends the + /// stack, the hunger and the sounds. + fn remote_tick_using(&mut self) { + let Some((slot, ticks)) = self.using else { + return; + }; + let stack = self.stack_in(slot).cloned(); + let food = stack + .as_ref() + .and_then(|s| self.res.items.get(&s.item)) + .is_some_and(|i| i.consumable && i.food.is_some()); + let (Some(stack), true) = (stack, food) else { + self.using = None; + return; + }; + if !self.right || slot != self.selected || self.screen != Screen::Playing { + self.using = None; + return; + } + // Consumable.shouldEmitParticlesAndSounds, as in tick_used_item. + let duration = 32; + let remaining = duration - ticks.min(duration); + if duration - remaining > (duration as f32 * 0.21875) as u32 && remaining % 4 == 0 { + self.eat_particles(&stack, 5); + } + if ticks + 1 >= duration { + self.using = None; + self.eat_particles(&stack, 16); + } else { + self.using = Some((slot, ticks + 1)); } } /// Left click: attack the targeted entity (MultiPlayerGameMode.attack). diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 667fdda2d..fa8e0ffb9 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -20,6 +20,8 @@ address. The script then checks, from both sides, what a guest's game used to ge - containers: the guest's ender chest holds the guest's items, not the host's, and a double chest opens as 9x6 with the right half first; - feedback: the guest's command feedback names the guest and shows the numbers. +- eat: holding right click with bread, the guest's own screen plays the eating (the item in use + counts up) and the server eats one loaf and feeds the guest. Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -39,6 +41,7 @@ import shutil import subprocess import sys import tempfile +import threading import time import zlib from pathlib import Path @@ -48,7 +51,7 @@ from agent_control_e2e import Cli, Failed, game_env # noqa: E402 ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials', 'eat'] class Game: @@ -622,11 +625,44 @@ def check_trials(host, guest, out): return {'spawner': trial, 'vault': active, 'rewarded': uuid(rewarded), 'host': uuid(host_uuid)} +def check_eat(host, guest, out): + guest('command', command='gamemode survival') + guest('command', command='clear @s') + # Hungry: the eating needs food below 20 (Player.canEat). + guest('command', command='effect give @s minecraft:hunger 3 255') + guest.wait(70) + guest('command', command='effect clear @s') + guest('command', command='give @s minecraft:bread 3') + guest.wait(5) + guest.raw('select', 'minecraft:bread') + hungry = guest('state')['player']['food'] + # use-item --hold returns once released: watch the guest's screen meanwhile. + hold = threading.Thread(target=guest.raw, args=('use-item', '--hold', 2.2)) + hold.start() + using = [] + for _ in range(4): + time.sleep(0.2) + using.append(guest('state').get('using')) + guest.shot(out, 'eat') + hold.join() + guest.wait(10) + fed = guest('state')['player']['food'] + left = guest('inventory')['counts'].get('minecraft:bread', 0) + detail = {'hungry': hungry, 'fed': fed, 'bread': left, 'using': using, 'after': guest('state').get('using')} + assert hungry < 20, f'the guest is not hungry: {detail}' + ticks = [u['ticks'] for u in using if u] + assert len(ticks) >= 3 and ticks == sorted(ticks) and ticks[-1] > ticks[0], f'no eating on the guest\'s screen: {detail}' + assert left == 2 and fed > hungry, f'the server did not eat one loaf: {detail}' + assert detail['after'] is None, f'still eating after the release: {detail}' + return detail + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, 'containers': check_containers, 'feedback': check_feedback, 'trials': check_trials, - 'walls': check_walls} + 'walls': check_walls, + 'eat': check_eat} def run(ctl, out, resources, checks): From 123e8286ac55894a0e9f61c78b3844fd46241e2a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 16:13:14 +0200 Subject: [PATCH 03/67] Guests: container clicks reach the server (chests, furnaces, crafting drags) On a LAN world or a server, a guest's clicks that went through the menu engine (placing a carried stack, quick craft drags, double clicks, number keys, Q) ran only on the guest's own copy and were never sent: the item showed in the chest or furnace, the server still had it in the inventory, and the next menu brought it back. menu_ops now sends every click of the server's open menu as vanilla's MultiPlayerGameMode.handleContainerInput does: the engine predicts it on the client's copy, then a ContainerClick goes out with the slots it changed and the carried stack after it (a drag is its three kinds of clicks). The plain inventory and crafting table are predicted too, the Shift double click sends each QUICK_MOVE, and a prediction no longer drops, crafts or makes maps on the client (the server does). tools/lan_e2e.py clicks: real mouse presses put a stack into a chest and a furnace, drag planks over the crafting grid and craft; the server has each. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 31 +++-- crates/horizon-client/src/game/remote.rs | 142 ++++++++++++++++++++++- crates/horizon-client/src/game/tweaks.rs | 10 +- crates/horizon-client/src/menu.rs | 13 +++ tools/lan_e2e.py | 92 ++++++++++++++- 5 files changed, 263 insertions(+), 25 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 238f68e55..83b4a665a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4524,6 +4524,10 @@ impl Game<'_> { /// Several clicks on one menu instance (a quick craft drag is three). fn menu_ops(&mut self, ops: &[(Option, i32, horizon_client::menu::Input)]) -> bool { use horizon_client::menu::Input; + if self.remote.is_some() && self.screen == Screen::Inventory { + // On a server the clicks are the server's: predicted and sent. + return self.remote_menu_ops(ops); + } if let [(Some(slot), button, input)] = ops { self.crafter_slot_clicked(*slot, *button, *input); } @@ -4698,7 +4702,7 @@ impl Game<'_> { plain: false, }; (kind, map) - } else if self.station.is_none() && !self.mount_active() && self.remote.is_none() { + } else if self.station.is_none() && !self.mount_active() { let table = self.craft_size == 3; let map = Map { container: 0, @@ -4811,7 +4815,12 @@ impl Game<'_> { Some(false) => return true, Some(true) => {} } - self.register_exploration_maps(made); + // On a server the clicks only predict: the server drops, crafts and + // makes maps itself. + let predicting = self.remote.is_some(); + if !predicting { + self.register_exploration_maps(made); + } if let Some(s) = menu.merchant.take() { self.merchant_sync_out(s); } @@ -4829,10 +4838,12 @@ impl Game<'_> { if !offhand_mapped { self.offhand = menu.player_item(40).cloned(); } - for stack in dropped { - self.player_drop(stack); + if !predicting { + for stack in dropped { + self.player_drop(stack); + } + self.award_crafted(&crafted); } - self.award_crafted(&crafted); if kind == Kind::Loom { if let Some(s) = self.station.as_mut() { s.loom.selected = usize::try_from(menu.data[0]).ok(); @@ -4964,9 +4975,13 @@ impl Game<'_> { return; } if let Some(moved) = self.last_quick_moved.clone() { - self.menu_session(|menu, env, to_menu| { - Some(menu.quick_move_all(env, to_menu(slot)?, &moved).is_ok()) - }); + if self.remote.is_some() { + self.remote_quick_move_all(slot, &moved); + } else { + self.menu_session(|menu, env, to_menu| { + Some(menu.quick_move_all(env, to_menu(slot)?, &moved).is_ok()) + }); + } } self.last_inv_click = None; self.inv_clicks.skip_release(); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 05e1f81c3..37c561476 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -14,6 +14,7 @@ use horizon_client::{ entities::{Experience, ItemEntity}, hud::{HudBatch, rgba}, items::ItemStack, + menu::Input, mobs::{Mob, MobType}, motion::{MotionWorld, PlayerMotion}, net::{Event, Session}, @@ -2880,17 +2881,108 @@ impl Game<'_> { let Some(internal) = self.slot_at(&batch, self.cursor.0, self.cursor.1) else { return; }; - let input = if self.has_shift_down() { 1 } else { 0 }; - self.remote_slot_click(internal, i8::from(right), input); + let input = if self.has_shift_down() { + Input::QuickMove + } else { + Input::Pickup + }; + self.remote_menu_ops(&[(Some(internal), i32::from(right), input)]); + } + /// The server's open menu: its window id and menu type (0, "inventory" + /// when none is open). + fn remote_menu(&self) -> (i32, String) { + match self.remote.as_ref().and_then(|r| r.menu.clone()) { + Some((id, name)) => (id, name), + None => (0, "inventory".to_owned()), + } + } + /// `MultiPlayerGameMode.handleContainerInput` for each click of `ops` on + /// the server's open menu (a quick craft drag is several): the verified + /// menu engine predicts them on this client's copy, then each goes out as + /// a `ContainerClick` with the slots it changed and the carried stack + /// after it; the server corrects anything it sees differently. A menu the + /// engine doesn't model sends the clicks unpredicted. False when a click's + /// slot isn't one of the menu's. + pub(super) fn remote_menu_ops(&mut self, ops: &[(Option, i32, Input)]) -> bool { + let (container, menu) = self.remote_menu(); + let mut clicks = Vec::with_capacity(ops.len()); + for &(slot, button, input) in ops { + let slot = match slot { + None => -999, + Some(internal) => { + match (0..64).find(|&s| Self::menu_slot(&menu, s) == Some(internal)) { + Some(s) => s as i16, + None => return false, + } + } + }; + clicks.push((slot, button, input)); + } + // Unpredicted: nothing changed, the carried stack as it is. + let mut sent: Vec = clicks + .iter() + .map(|&(slot, button, input)| (slot, button, input, Vec::new(), self.carried.clone())) + .collect(); + // The engine's slot indices are the menu's own, as on the network. + self.menu_session(|m, env, _| { + let mut predicted = Vec::with_capacity(clicks.len()); + for &(slot, button, input) in &clicks { + let before = menu_items(m); + if m.click(env, i32::from(slot), button, input).is_err() { + // The server throws on it too: nothing is predicted. + return Some(false); + } + let changed = changed_slots(&before, m); + predicted.push((slot, button, input, changed, m.carried.clone())); + } + sent = predicted; + Some(true) + }); + self.send_clicks(container, sent); + true + } + /// A Shift double click on a server (`AbstractContainerScreen.mouseReleased`): + /// a predicted QUICK_MOVE for every slot the engine moves, each sent. + pub(super) fn remote_quick_move_all(&mut self, slot: usize, moved: &ItemStack) { + let (container, _) = self.remote_menu(); + let mut sent: Vec = Vec::new(); + self.menu_session(|m, env, to_menu| { + let clicked = to_menu(slot)?; + let mut before = menu_items(m); + let mut each = |m: &horizon_client::menu::Menu, t: usize| { + let changed = changed_slots(&before, m); + sent.push((t as i16, 0, Input::QuickMove, changed, m.carried.clone())); + before = menu_items(m); + }; + Some( + m.quick_move_all_each(env, clicked, moved, &mut each) + .is_ok(), + ) + }); + self.send_clicks(container, sent); + } + fn send_clicks(&mut self, container: i32, clicks: Vec) { + let state = self.remote.as_ref().map_or(0, |r| r.state_id); + let hashed = |g: &Self, s: &Option| g.to_network(s).map(|s| Hashed::of(&s)); + for (slot, button, input, changed, carried) in clicks { + let changed = changed.iter().map(|(i, s)| (*i, hashed(self, s))).collect(); + let carried = hashed(self, &carried); + self.send(S::ContainerClick(play::ContainerClick { + container, + state, + slot, + button: button as i8, + input: input_ordinal(input), + changed, + carried, + })); + } } /// A `ContainerClick` on game slot `internal` of the server's open menu /// (`input` is the ContainerInput ordinal). False when the menu has no /// such slot. pub(super) fn remote_slot_click(&mut self, internal: usize, button: i8, input: i32) -> bool { - let (container, menu) = match self.remote.as_ref().and_then(|r| r.menu.clone()) { - Some((id, name)) => (id, name), - None => (0, "inventory".to_owned()), - }; + let (container, menu) = self.remote_menu(); let slot = (0..64).find(|&s| Self::menu_slot(&menu, s) == Some(internal)); let Some(slot) = slot else { return false }; let carried = self.to_network(&self.carried).map(|s| Hashed::of(&s)); @@ -3288,6 +3380,44 @@ impl Game<'_> { } } +/// A click sent to a server: its slot, button and input, the slots it +/// changed and the carried stack after it. +type Predicted = ( + i16, + i32, + Input, + Vec<(i16, Option)>, + Option, +); + +fn menu_items(m: &horizon_client::menu::Menu) -> Vec> { + (0..m.slot_count()).map(|i| m.slot(i).cloned()).collect() +} + +/// The slots whose stack is no longer `before`'s (`ItemStack.matches`). +fn changed_slots( + before: &[Option], + m: &horizon_client::menu::Menu, +) -> Vec<(i16, Option)> { + (0..m.slot_count()) + .filter(|&i| m.slot(i) != before.get(i).and_then(Option::as_ref)) + .map(|i| (i as i16, m.slot(i).cloned())) + .collect() +} + +/// ServerboundContainerClickPacket's `ContainerInput` ordinal. +pub(super) fn input_ordinal(input: Input) -> i32 { + match input { + Input::Pickup => 0, + Input::QuickMove => 1, + Input::Swap => 2, + Input::Clone => 3, + Input::Throw => 4, + Input::QuickCraft => 5, + Input::PickupAll => 6, + } +} + #[cfg(test)] mod tests { use super::Game; diff --git a/crates/horizon-client/src/game/tweaks.rs b/crates/horizon-client/src/game/tweaks.rs index 018b6640b..f15aa9ea7 100644 --- a/crates/horizon-client/src/game/tweaks.rs +++ b/crates/horizon-client/src/game/tweaks.rs @@ -70,15 +70,7 @@ impl Game<'_> { /// One ordinary click on a game slot. False when nothing took it. pub(super) fn tweak_click(&mut self, slot: usize, button: i32, input: Input) -> bool { if self.remote.is_some() { - let ordinal = match input { - Input::Pickup => 0, - Input::QuickMove => 1, - Input::Swap => 2, - Input::Clone => 3, - Input::Throw => 4, - Input::QuickCraft => 5, - Input::PickupAll => 6, - }; + let ordinal = super::remote::input_ordinal(input); return self.remote_slot_click(slot, button as i8, ordinal); } if !self.menu_click(Some(slot), button, input) { diff --git a/crates/horizon-client/src/menu.rs b/crates/horizon-client/src/menu.rs index 5e57edcfc..64286b1fe 100644 --- a/crates/horizon-client/src/menu.rs +++ b/crates/horizon-client/src/menu.rs @@ -1668,6 +1668,18 @@ impl Menu { env: &Env, clicked: usize, moved: &ItemStack, + ) -> Result<(), &'static str> { + self.quick_move_all_each(env, clicked, moved, &mut |_, _| {}) + } + + /// `quick_move_all`, calling `each` with the menu and the slot after each + /// QUICK_MOVE (a server's client sends every one of them). + pub fn quick_move_all_each( + &mut self, + env: &Env, + clicked: usize, + moved: &ItemStack, + each: &mut dyn FnMut(&Self, usize), ) -> Result<(), &'static str> { if clicked >= self.slots.len() || !self.can_take_for_pick_all(clicked) { return Ok(()); @@ -1680,6 +1692,7 @@ impl Menu { && self.quick_replace(t, moved, env) { self.click(env, t as i32, 0, Input::QuickMove)?; + each(self, t); } } Ok(()) diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 667fdda2d..0ced0915d 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -20,6 +20,9 @@ address. The script then checks, from both sides, what a guest's game used to ge - containers: the guest's ender chest holds the guest's items, not the host's, and a double chest opens as 9x6 with the right half first; - feedback: the guest's command feedback names the guest and shows the numbers. +- clicks: with real mouse presses (not click-slot, which skips the screen's press and drag + handling), the guest puts a stack into a chest and a furnace, drags planks across + its crafting grid and gathers a stack with a double click; the server has each. Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -48,7 +51,7 @@ from agent_control_e2e import Cli, Failed, game_env # noqa: E402 ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials', 'clicks'] class Game: @@ -456,6 +459,91 @@ def check_containers(host, guest, out): return {'ender': ender, 'double': double, 'taken': taken} +def slot_point(game, slot): + """The GUI point in the middle of slot `slot` of the open screen (`ui`'s slot-N).""" + for w in game.raw('ui')['widgets']: + if w.get('id') == f'slot-{slot}': + x, y, width, height = w['bounds'] + return x + width / 2, y + height / 2 + raise Failed(f'no slot {slot} on the screen') + + +def mouse(game, *slots, right=False): + """Press over the first slot, move over the others and release: one click, or a drag.""" + points = [f'{v:.1f}' for s in slots for v in slot_point(game, s)] + game.raw('drag-at', *(['right'] if right else []), *points) + game.wait(4) + + +def hotbar_slot(game, item): + return next(s['slot'] for s in game('inventory')['hotbar'] if s and s.get('item') == item) + + +def check_clicks(host, guest, out): + guest('command', command='gamemode survival') + guest('command', command='clear @s') + guest('command', command='give @s minecraft:cobblestone 10') + guest('command', command='give @s minecraft:coal 3') + guest('command', command='give @s minecraft:raw_iron 2') + guest('command', command='give @s minecraft:oak_planks 2') + guest.wait(10) + x, y, z = (int(v // 1) for v in pos_of(guest)) + chest, furnace = (x, y, z + 2), (x, y, z - 2) + guest('command', command=f'setblock {chest[0]} {chest[1]} {chest[2]} minecraft:chest') + guest('command', command=f'setblock {furnace[0]} {furnace[1]} {furnace[2]} minecraft:furnace') + guest.wait(10) + # Pick the cobblestone up and put it down in the chest's first slot. + guest.raw('interact', *chest) + guest.raw('wait-for', 'screen', 'container', '--timeout', 10) + guest.wait(10) + mouse(guest, hotbar_slot(guest, 'minecraft:cobblestone')) + mouse(guest, 300) + guest.wait(10) + guest.shot(out, 'clicks-chest') + guest.raw('key', 'esc', 'tap') + guest.wait(10) + in_chest, _ = open_container(guest, chest, out, 'clicks-chest-after') + # Coal into the fuel slot, raw iron into the input. + guest.raw('interact', *furnace) + guest.raw('wait-for', 'screen', 'container', '--timeout', 10) + guest.wait(10) + mouse(guest, hotbar_slot(guest, 'minecraft:coal')) + mouse(guest, 301) + mouse(guest, hotbar_slot(guest, 'minecraft:raw_iron')) + mouse(guest, 300) + guest.wait(10) + guest.shot(out, 'clicks-furnace') + guest.raw('key', 'esc', 'tap') + guest.wait(10) + # Opening the furnace again resends the whole menu: the server's own view. + in_furnace, _ = open_container(guest, furnace, out, 'clicks-furnace-after') + # Two planks dragged over the 2x2 grid's left column: one each, four sticks. + guest.raw('key', 'e', 'tap') + guest.raw('wait-for', 'screen', 'inventory') + guest.wait(5) + mouse(guest, hotbar_slot(guest, 'minecraft:oak_planks')) + mouse(guest, 100, 102) + guest.wait(10) + grid = {s['slot']: (s.get('item'), s.get('count')) for s in guest('screen').get('slots', []) + if s.get('slot') in (100, 102, 200) and s.get('item')} + guest.shot(out, 'clicks-drag') + guest.raw('click-slot', 200, 'shift') + guest.wait(10) + guest.raw('key', 'esc', 'tap') + guest.wait(10) + # The chest's menu brings the whole inventory as the server has it. + open_container(guest, chest, out, 'clicks-inventory') + counts = guest('inventory')['counts'] + for at in (chest, furnace): + guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:air') + assert in_chest == {0: 'minecraft:cobblestone'}, f'the chest holds {in_chest} after the guest put cobblestone in' + assert in_furnace == {0: 'minecraft:raw_iron', 1: 'minecraft:coal'}, f'the furnace holds {in_furnace}' + assert grid.get(100) == ('minecraft:oak_planks', 1) and grid.get(102) == ('minecraft:oak_planks', 1), \ + f'the drag left the grid as {grid}' + assert counts.get('minecraft:stick') == 4 and 'minecraft:cobblestone' not in counts, f'inventory after: {counts}' + return {'chest': in_chest, 'furnace': in_furnace, 'grid': grid, 'inventory': counts} + + def check_feedback(host, guest, out): x, y, z = (round(v, 1) for v in pos_of(guest)) guest('command', command='item replace entity @s weapon.mainhand with minecraft:stick') @@ -626,7 +714,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'chest': check_chest, 'crafting': check_crafting, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, 'containers': check_containers, 'feedback': check_feedback, 'trials': check_trials, - 'walls': check_walls} + 'walls': check_walls, 'clicks': check_clicks} def run(ctl, out, resources, checks): From 69dd4feb316616eef6255a4f41dc3b680a6a43e6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 16:14:18 +0200 Subject: [PATCH 04/67] Server clients: mobs' and players' heads turn smoothly, not flicker On a LAN world or a server, other entities' heads flickered between two directions. The client wrote ClientboundRotateHeadPacket's head yaw into the entity's yRot and the move packets' yRot into the body yaw, never moved the head's previous yaw and pitch on, and never ran the body rotation, so each frame interpolated the head from stale values. As vanilla: move packets interpolate the position, yRot and xRot over three ticks (LinearInterpolationHandler), RotateHead the head yaw over three (lerpHeadTo), each tick keeps the previous head rotation for the frames, and the body follows (a mob's BodyRotationControl, a player's walk direction within 50 degrees of the look). AddEntity sets yRot and xRot and puts the body and head at the packet's head yaw (recreateFromPacket). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 106 ++++++++++++++++++++--- 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 05e1f81c3..079f00d59 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -61,8 +61,11 @@ struct Tracked { } /// Smooth an entity toward a new position over three ticks (InterpolationHandler). #[derive(Clone, Copy)] +/// `LinearInterpolationHandler`'s target: a position and rotation (yRot, +/// xRot) reached in `steps` ticks. struct Lerp { target: [f64; 3], + rotation: Option<(f32, f32)>, steps: u32, } @@ -102,6 +105,8 @@ pub(super) struct Remote { use_target: Option<[i32; 3]>, entities: HashMap, lerps: HashMap, + /// `LivingEntity.lerpHeadTo`: the head yaw and the ticks left to reach it. + head_lerps: HashMap, /// Item entities waiting for their item data. pending_items: HashMap, health: (f32, i32, f32), @@ -288,6 +293,7 @@ impl Game<'_> { use_target: None, entities: HashMap::new(), lerps: HashMap::new(), + head_lerps: HashMap::new(), pending_items: HashMap::new(), health: (20.0, 20, 5.0), game_mode: 0, @@ -587,6 +593,7 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.entities.clear(); r.lerps.clear(); + r.head_lerps.clear(); r.pending_items.clear(); // A new ClientLevel: a new chunk storage, centred on 0, 0. r.chunk_center = (0, 0); @@ -868,7 +875,10 @@ impl Game<'_> { } } } - fn spawn_entity(&mut self, id: i32, kind: &str, position: [f64; 3], yaw: f32, head: f32) { + /// `recreateFromPacket`: yRot and xRot from the packet, the body and head + /// both at its head yaw (LivingEntity). + fn spawn_entity(&mut self, id: i32, kind: &str, position: [f64; 3], rotation: [f32; 3]) { + let [yaw, pitch, head] = rotation; let at = Vec3::new(position[0], position[1], position[2]); let mob_type = if kind == "minecraft:player" { self.remote.as_ref().and_then(|r| r.player_type.clone()) @@ -877,8 +887,8 @@ impl Game<'_> { }; if let Some(t) = mob_type { let mut mob = Mob::new(id as u32, t, at, head); - mob.body_yaw = yaw; - mob.previous_body_yaw = yaw; + (mob.yaw, mob.previous_yaw) = (yaw, yaw); + (mob.head_pitch, mob.previous_head_pitch) = (pitch, pitch); mob.persistent = true; self.mobs.retain(|m| m.id != id as u32); self.mobs.push(mob); @@ -903,14 +913,21 @@ impl Game<'_> { rotation: Option<(f32, f32)>, on_ground: bool, ) { + let mob = self.mobs.iter_mut().find(|m| m.id == id as u32); if let Some(r) = self.remote.as_mut() { if let Some(t) = r.entities.get_mut(&id) { t.position = position; } + // AbstractInterpolationHandler.interpolateTo: without a rotation, + // the one it was heading for (or has). + let rotation = rotation + .or_else(|| r.lerps.get(&id).and_then(|l| l.rotation)) + .or_else(|| mob.as_ref().map(|m| (m.yaw, m.head_pitch))); r.lerps.insert( id, Lerp { target: position, + rotation, steps: 3, }, ); @@ -918,12 +935,8 @@ impl Game<'_> { *p = position; } } - if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id as u32) { + if let Some(m) = mob { m.on_ground = on_ground; - if let Some((yaw, pitch)) = rotation { - m.body_yaw = yaw; - m.head_pitch = pitch; - } } } fn remove_entity(&mut self, id: i32) { @@ -932,6 +945,7 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.entities.remove(&id); r.lerps.remove(&id); + r.head_lerps.remove(&id); r.pending_items.remove(&id); } } @@ -1181,8 +1195,8 @@ impl Game<'_> { .map(str::to_owned) }) .unwrap_or_default(); - let yaw = horizon_protocol::buf::unpack_degrees(p.yaw); - let head = horizon_protocol::buf::unpack_degrees(p.head_yaw); + let rotation = + [p.yaw, p.pitch, p.head_yaw].map(horizon_protocol::buf::unpack_degrees); if let Some(r) = self.remote.as_mut() { if kind == "minecraft:player" { r.level_players.insert(p.id, p.uuid); @@ -1190,7 +1204,7 @@ impl Game<'_> { r.level_players.remove(&p.id); } } - self.spawn_entity(p.id, &kind, p.position, yaw, head); + self.spawn_entity(p.id, &kind, p.position, rotation); } C::RemoveEntities(p) => { for id in p.ids { @@ -1241,9 +1255,11 @@ impl Game<'_> { ]; self.move_entity(p.id, position, Some((p.yaw, p.pitch)), p.on_ground); } + // ClientPacketListener.handleRotateMob: lerpHeadTo(yHeadRot, 3). C::RotateHead(p) => { - if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { - m.yaw = horizon_protocol::buf::unpack_degrees(p.head_yaw); + if let Some(r) = self.remote.as_mut() { + let yaw = horizon_protocol::buf::unpack_degrees(p.head_yaw); + r.head_lerps.insert(p.id, (yaw, 3)); } } C::SetEntityData(p) => { @@ -2236,14 +2252,18 @@ impl Game<'_> { return; }; let mut lerps = std::mem::take(&mut r.lerps); + let head_lerps = &mut r.head_lerps; let players = &r.level_players; for m in &mut self.mobs { - if !running && !players.contains_key(&(m.id as i32)) { + let player = players.contains_key(&(m.id as i32)); + if !running && !player { continue; } m.previous = m.position; m.previous_yaw = m.yaw; m.previous_body_yaw = m.body_yaw; + m.previous_head_yaw = m.head_yaw; + m.previous_head_pitch = m.head_pitch; m.previous_limb_speed = m.limb_speed; m.age += 1; if m.species() == horizon_client::mobs::Species::Rabbit { @@ -2258,6 +2278,7 @@ impl Game<'_> { if !m.alive() { m.death_time += 1; } + // Entity.commonTick: LinearInterpolationHandler.interpolate. if let Some(l) = lerps.get_mut(&(m.id as i32)) && l.steps > 0 { @@ -2267,8 +2288,27 @@ impl Game<'_> { m.position.y + (l.target[1] - m.position.y) / n, m.position.z + (l.target[2] - m.position.z) / n, ); + if let Some((yaw, pitch)) = l.rotation { + let alpha = 1.0 / l.steps as f32; + m.yaw += wrap_degrees(yaw - m.yaw) * alpha; + m.head_pitch += (pitch - m.head_pitch) * alpha; + } l.steps -= 1; } + // LivingEntity.aiStep: the head's own interpolation. + if let Some((yaw, steps)) = head_lerps.get_mut(&(m.id as i32)) + && *steps > 0 + { + m.head_yaw += wrap_degrees(*yaw - m.head_yaw) / *steps as f32; + *steps -= 1; + } + // LivingEntity.tick's tickHeadTurn: a mob's BodyRotationControl, + // a player's body following its walk and look. + if player { + remote_player_head_turn(m); + } else if m.alive() { + m.body_rotation_tick(); + } // WalkAnimationState.update from the horizontal distance moved. let (dx, dz) = (m.position.x - m.previous.x, m.position.z - m.previous.z); let target = (((dx * dx + dz * dz).sqrt() * 4.0) as f32).min(1.0); @@ -2294,6 +2334,7 @@ impl Game<'_> { } } lerps.retain(|_, l| l.steps > 0); + head_lerps.retain(|_, (_, steps)| *steps > 0); if let Some(r) = self.remote.as_mut() { for (id, l) in lerps { r.lerps.entry(id).or_insert(l); @@ -3288,6 +3329,43 @@ impl Game<'_> { } } +/// `Mth.wrapDegrees`. +fn wrap_degrees(a: f32) -> f32 { + let a = a % 360.0; + if a >= 180.0 { + a - 360.0 + } else if a < -180.0 { + a + 360.0 + } else { + a + } +} + +/// `LivingEntity.tick`'s body target and `tickHeadTurn` for another player: +/// the body turns toward where it walks (backwards when walking back), or to +/// the look while the arm swings, and stays within 50 degrees of the look. +fn remote_player_head_turn(m: &mut Mob) { + let (dx, dz) = (m.position.x - m.previous.x, m.position.z - m.previous.z); + let mut target = m.body_yaw; + if (dx * dx + dz * dz) as f32 > 0.002_500_000_2 { + let walk = (dz.atan2(dx) as f32).to_degrees() - 90.0; + let facing = (wrap_degrees(m.yaw) - walk).abs(); + target = if 95.0 < facing && facing < 265.0 { + walk - 180.0 + } else { + walk + }; + } + if m.anim.attack_anim > 0.0 { + target = m.yaw; + } + m.body_yaw += wrap_degrees(target - m.body_yaw) * 0.3; + let head = wrap_degrees(m.yaw - m.body_yaw); + if head.abs() > 50.0 { + m.body_yaw += head - head.signum() * 50.0; + } +} + #[cfg(test)] mod tests { use super::Game; From 22dc3e595404dfc0c0143da4ab72edbb82ebe31b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 17:08:13 +0200 Subject: [PATCH 05/67] Guests: touching mobs push them on their own client, as in vanilla A mob's push of a guest went to the guest as SetEntityMotion carrying the server copy's velocity, which nothing damps: every tick of contact added to it and replaced what the guest's client was doing. Vanilla pushes players on their own client only (EntitySelector.pushableBy): the server no longer sends those pushes, and a server client now pushes its player away from every living entity it touches (Entity.push, team rules, no spectators, bats or passengers of the same vehicle). Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/examples/mob_ai_probe.rs | 1 + crates/horizon-client/src/game/creatures.rs | 1 + crates/horizon-client/src/game/lan_mobs.rs | 2 + crates/horizon-client/src/game/remote.rs | 69 ++++++++++++++++++- .../horizon-client/src/game/respawn_anchor.rs | 2 +- crates/horizon-client/src/mobs.rs | 3 + crates/horizon-client/src/mobs/ai.rs | 12 ++-- tools/lan_e2e.py | 27 +++++++- 8 files changed, 109 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 087fd3edf..0b3adec31 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -999,6 +999,7 @@ fn main() -> horizon_client::Result<()> { hero: false, uuid: PLAYER_UUID, allies: Default::default(), + remote: false, } }); let mut scratch = LegacyRandom::new(0); diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 076c2fbef..acac3b596 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -196,6 +196,7 @@ impl Game<'_> { hero: self.potions.effects.get("hero_of_the_village").is_some(), uuid: self.level.player_uuid.unwrap_or_default(), allies: std::sync::Arc::new(self.player_allies()), + remote: false, }) } /// Biome facts at a position for finalizeSpawn and variant rules. diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 7cef7cda6..e12446b0c 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -107,6 +107,7 @@ impl Game<'_> { v.velocity = Vec3::new(moved[0], moved[1], moved[2]); v.uuid = horizon_client::lan::playerdata::uuid_ints(uuid); v.allies = Default::default(); + v.remote = true; views.push((conn, v)); } } @@ -271,6 +272,7 @@ mod tests { hero: false, uuid: [0; 4], allies: Default::default(), + remote: false, } } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 05e1f81c3..1200da1ca 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -14,12 +14,13 @@ use horizon_client::{ entities::{Experience, ItemEntity}, hud::{HudBatch, rgba}, items::ItemStack, - mobs::{Mob, MobType}, + mobs::{Mob, MobType, Who}, motion::{MotionWorld, PlayerMotion}, net::{Event, Session}, save::SaveDir, sprint::Keys, streaming::{ChunkGenerator, Proto, Streaming}, + tab_list::SPECTATOR, world::World, }; use horizon_core::{geometry::Vec3, paletted::PalettedContainer, random::RandomSource}; @@ -2125,6 +2126,7 @@ impl Game<'_> { self.tick_particles(); self.tick_client_effects(); let ghast_collisions = self.happy_ghast_player_collisions(); + let pushed = self.remote_pushes(); let Some(player) = self.player.as_mut() else { return Ok(()); }; @@ -2177,6 +2179,7 @@ impl Game<'_> { player.yaw = self.yaw; let dead = self.screen == Screen::Dead; if !dead { + player.velocity = player.velocity.add_vector(pushed); MotionWorld { entities: &ghast_collisions, player, @@ -2229,6 +2232,70 @@ impl Game<'_> { self.send(S::ClientTickEnd(Empty)); Ok(()) } + /// The local player's share of every touching entity's + /// `LivingEntity.pushEntities`: on a client `EntitySelector.pushableBy` + /// lets entities push only the local player, so the server never sends + /// these pushes. `doPush(player)` is `player.push(entity)`. + fn remote_pushes(&self) -> Vec3 { + let (Some(p), Some(r)) = (self.player.as_ref(), self.remote.as_ref()) else { + return Vec3::ZERO; + }; + // LivingEntity.isPushable: alive, no spectator, off ladders. + let feet = [ + p.position.x.floor() as i32, + p.position.y.floor() as i32, + p.position.z.floor() as i32, + ]; + if self.screen == Screen::Dead + || p.spectator + || self.block_in_tag("minecraft:climbable", &self.block_name(feet)) + { + return Vec3::ZERO; + } + let bounds = p.bounds(); + let teams = self.mob_teams(); + let mut push = Vec3::ZERO; + for m in &self.mobs { + let spectator = r + .level_players + .get(&(m.id as i32)) + .and_then(|uuid| r.tab_players.get(uuid)) + .is_some_and(|t| t.game_mode == SPECTATOR); + // Bats never push; a spectator has no physics; passengers of the + // same vehicle never push each other. + if !m.alive() + || spectator + || m.species() == horizon_client::mobs::Species::Bat + || self + .riding + .is_some_and(|v| v == m.id || m.extra.mount == Some(v)) + || !m.bounds().intersects(bounds) + || !horizon_client::scoreboard::pushable_by( + teams.of(Who::Mob(m.id)), + teams.of(Who::Player), + true, + ) + { + continue; + } + // Entity.push(Entity), `this` the player. + let (mut xa, mut za) = (m.position.x - p.position.x, m.position.z - p.position.z); + let mut dd = xa.abs().max(za.abs()); + if dd < f64::from(0.01_f32) { + continue; + } + dd = dd.sqrt(); + xa /= dd; + za /= dd; + let pow = (1.0 / dd).min(1.0); + xa *= pow; + za *= pow; + xa *= f64::from(0.05_f32); + za *= f64::from(0.05_f32); + push = Vec3::new(push.x - xa, push.y, push.z - za); + } + push + } /// ClientLevel.tickEntities; a frozen level only ticks the players /// (`TickRateManager.isEntityFrozen`). fn tick_remote_entities(&mut self, running: bool) { diff --git a/crates/horizon-client/src/game/respawn_anchor.rs b/crates/horizon-client/src/game/respawn_anchor.rs index 5a31dfd5c..aa94d5b17 100644 --- a/crates/horizon-client/src/game/respawn_anchor.rs +++ b/crates/horizon-client/src/game/respawn_anchor.rs @@ -324,7 +324,7 @@ impl Game<'_> { ) .unwrap_or_else(|_| VoxelShape::empty()) } - fn block_in_tag(&self, tag: &str, block: &str) -> bool { + pub(super) fn block_in_tag(&self, tag: &str, block: &str) -> bool { self.res .items .block_tags diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 2ab4c0557..53143d2df 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1398,6 +1398,9 @@ pub struct PlayerView { /// The mobs on the player's team (`isAlliedTo`): combat targeting /// leaves the player alone. pub allies: std::sync::Arc>, + /// A LAN guest: its own client moves it, and touching mobs push it there + /// (`EntitySelector.pushableBy` on the client). + pub remote: bool, } pub struct MobWorld<'a> { pub world: &'a World, diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 9ab29d0a5..f74a3c7d1 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -822,10 +822,14 @@ fn push_entities(m: &mut Mob, w: &MobWorld, events: &mut Vec) { za *= pow; xa *= f64::from(0.05_f32); za *= f64::from(0.05_f32); - events.push(MobEvent::Push { - target: who, - velocity: [-xa, 0.0, -za], - }); + // A guest's server-side push never reaches its client (ServerPlayer + // motion is synced only when hurt): its client pushes it itself. + if who != Who::Player || w.player.as_ref().is_none_or(|p| !p.remote) { + events.push(MobEvent::Push { + target: who, + velocity: [-xa, 0.0, -za], + }); + } if self_pushable { m.velocity = Vec3::new(m.velocity.x + xa, m.velocity.y, m.velocity.z + za); } diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 667fdda2d..85b2bb12a 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -20,6 +20,8 @@ address. The script then checks, from both sides, what a guest's game used to ge - containers: the guest's ender chest holds the guest's items, not the host's, and a double chest opens as 9x6 with the right half first; - feedback: the guest's command feedback names the guest and shows the numbers. +- push: a cow standing in the guest pushes it just clear of the cow (the guest's own client + pushes it, as in vanilla), not ever faster across the ground. Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -48,7 +50,7 @@ from agent_control_e2e import Cli, Failed, game_env # noqa: E402 ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials', 'push'] class Game: @@ -622,11 +624,32 @@ def check_trials(host, guest, out): return {'spawner': trial, 'vault': active, 'rewarded': uuid(rewarded), 'host': uuid(host_uuid)} +def check_push(host, guest, out): + guest('command', command='gamemode survival') + x, y, z = (int(v // 1) + 0.5 for v in pos_of(guest)) + guest('command', command=f'tp @s {x} {y - 0.5} {z}') + guest.wait(20) + x, y, z = pos_of(guest) + # A cow that doesn't walk, 0.3 east of the guest: the two boxes overlap until their centres are + # 0.75 apart, so the push stops a fraction of a block away. + guest('command', command=f'summon minecraft:cow {x + 0.3:.2f} {y:.2f} {z:.2f} {{NoAI:1b}}') + guest.wait(60) + nx, _, nz = pos_of(guest) + guest.shot(out, 'push') + guest('command', command='kill @e[type=minecraft:cow]') + moved = ((nx - x) ** 2 + (nz - z) ** 2) ** 0.5 + detail = {'from': [round(x, 3), round(z, 3)], 'to': [round(nx, 3), round(nz, 3)], 'moved': round(moved, 3)} + assert nx < x, f'the cow did not push the guest west: {detail}' + assert moved < 1.5, f'the cow pushed the guest too far: {detail}' + return detail + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, 'containers': check_containers, 'feedback': check_feedback, 'trials': check_trials, - 'walls': check_walls} + 'walls': check_walls, + 'push': check_push} def run(ctl, out, resources, checks): From d1d36f4121bd8eb1a4d3d95b7072420cf3ef6d65 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 16:26:37 +0200 Subject: [PATCH 06/67] Guests: experience orbs fly to them and give them their points On a LAN world or a server, guests never got experience: orbs followed and were taken only by the host's own player (none on a dedicated server), and guests weren't sent orbs at all. - Each orb follows the nearest living, non-spectator player within 8 blocks, the host or a guest (ExperienceOrb.followNearbyPlayer). - A guest takes an orb as Player.aiStep does: one of those in its box grown by 1, 0.5, 1, at random, at most every 2 ticks (takeXpDelay), repairing with Mending first; the rest goes to the guest's experience (sent with SetExperience as before), the level-up sound every fifth level. TakeItemEntity goes to the players around, the pickup sound plays at the orb. - Orbs have entity ids and are tracked to guests (AddEntity, DATA_VALUE). The client shows a server's orbs, moves them with the server's packets and flies them to their collector. tools/lan_e2e.py xp: three 7-point orbs near the guest: it sees three and ends at level 2 (21 points); the host gets none. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entities.rs | 2 + crates/horizon-client/src/entity_nbt.rs | 2 + crates/horizon-client/src/game.rs | 78 ++++++++++++++----- crates/horizon-client/src/game/commands.rs | 2 + crates/horizon-client/src/game/lan.rs | 19 ++++- crates/horizon-client/src/game/lan_actions.rs | 78 ++++++++++++++++++- crates/horizon-client/src/game/persist.rs | 8 +- crates/horizon-client/src/game/pickups.rs | 1 + crates/horizon-client/src/game/remote.rs | 70 +++++++++++++---- tools/lan_e2e.py | 34 +++++++- 10 files changed, 254 insertions(+), 40 deletions(-) diff --git a/crates/horizon-client/src/entities.rs b/crates/horizon-client/src/entities.rs index 5e6a3336a..c0ee36bcd 100644 --- a/crates/horizon-client/src/entities.rs +++ b/crates/horizon-client/src/entities.rs @@ -178,6 +178,8 @@ impl ItemEntity { /// ExperienceOrb: gravity 0.03, homing towards the player within 8 blocks. #[derive(Clone, Debug)] pub struct ExperienceOrb { + /// The entity id servers track it by. + pub id: u32, pub position: Vec3, pub previous: Vec3, pub velocity: Vec3, diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index adeb1da25..90e1efc03 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -937,6 +937,7 @@ pub fn orbs_from_nbt(tag: &Tag) -> Vec { }; let count = number(tag.get("Count")).unwrap_or(1).clamp(1, 64) as usize; let orb = ExperienceOrb { + id: 0, position, previous: position, velocity: motion(tag), @@ -1575,6 +1576,7 @@ mod tests { #[test] fn orbs_expand_their_count() { let o = ExperienceOrb { + id: 0, position: Vec3::new(1.0, 2.0, 3.0), previous: Vec3::ZERO, velocity: Vec3::ZERO, diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 238f68e55..4604ce855 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -7352,7 +7352,9 @@ impl Game<'_> { let mut r = dispenser::entity_random(); let _yaw = r.next_float(); let p = Vec3::new(at[0], at[1], at[2]); + self.next_entity += 1; self.orbs.push(ExperienceOrb { + id: self.next_entity, position: p, previous: p, velocity: Vec3::new( @@ -7416,13 +7418,29 @@ impl Game<'_> { .player .as_ref() .filter(|p| !p.health.dead_or_dying() && !spectator); - let target = player.map(|p| { - [ - p.position.x, - p.position.y + f64::from(p.dimensions.eye_height) / 2.0, - p.position.z, - ] - }); + // ExperienceOrb.followNearbyPlayer: each orb goes for the nearest + // living player within 8 blocks, the host's own or a guest. + let mut followed: Vec<([f64; 3], [f64; 3])> = player + .map(|p| { + let feet = [p.position.x, p.position.y, p.position.z]; + let eye = f64::from(p.dimensions.eye_height); + (feet, [feet[0], feet[1] + eye / 2.0, feet[2]]) + }) + .into_iter() + .collect(); + if let Some(host) = self.lan.as_ref() { + followed.extend( + host.hub + .guests + .iter() + .filter(|g| g.placed && g.loaded && !g.dead && g.health > 0.0) + .filter(|g| !g.spectator()) + .map(|g| { + let p = g.position; + (p, [p[0], p[1] + g.eye_height() / 2.0, p[2]]) + }), + ); + } let bounds = player.map(|p| { let w = f64::from(p.dimensions.width) / 2.0; ( @@ -7435,7 +7453,19 @@ impl Game<'_> { ) }); let mut orbs = std::mem::take(&mut self.orbs); - orbs.retain_mut(|o| o.tick(&self.world, &self.res.facts, self.collision, target)); + orbs.retain_mut(|o| { + let distance = |feet: &[f64; 3]| -> f64 { + (feet[0] - o.position.x).powi(2) + + (feet[1] - o.position.y).powi(2) + + (feet[2] - o.position.z).powi(2) + }; + let target = followed + .iter() + .filter(|(feet, _)| distance(feet) < 64.0) + .min_by(|a, b| distance(&a.0).total_cmp(&distance(&b.0))) + .map(|(_, target)| *target); + o.tick(&self.world, &self.res.facts, self.collision, target) + }); let mut gained = 0; let mut taken = Vec::new(); if let Some((min, max)) = bounds { @@ -7469,19 +7499,25 @@ impl Game<'_> { (self.world.random.next_float() - self.world.random.next_float()) * 0.35 + 0.9; self.audio .play("minecraft:entity.experience_orb.pickup", at, 0.1, pitch); - if self.experience.give(gained as i32) - && self.experience.level % 5 == 0 - && self.ticks > self.last_level_up + 100 - { - self.last_level_up = self.ticks; - let volume = if self.experience.level > 30 { - 1.0 - } else { - self.experience.level as f32 / 30.0 - }; - self.audio - .play("minecraft:entity.player.levelup", at, volume * 0.75, 1.0); - } + self.give_orb_points(gained); + } + } + /// `Player.giveExperiencePoints`, and `giveExperienceLevels`' level-up + /// sound every fifth level (at most every 100 ticks). + pub(super) fn give_orb_points(&mut self, gained: u32) { + let at = self.eye_position(); + if self.experience.give(gained as i32) + && self.experience.level % 5 == 0 + && self.ticks > self.last_level_up + 100 + { + self.last_level_up = self.ticks; + let volume = if self.experience.level > 30 { + 1.0 + } else { + self.experience.level as f32 / 30.0 + }; + self.audio + .play("minecraft:entity.player.levelup", at, volume * 0.75, 1.0); } } /// BowItem.releaseUsing: power from charge time, arrows at power * 3. diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 6c4817f63..4f53f1add 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -1418,7 +1418,9 @@ impl Game<'_> { return Err(err("commands.summon.failed", &[])); } "experience_orb" => { + self.next_entity += 1; self.orbs.push(horizon_client::entities::ExperienceOrb { + id: self.next_entity, position: Vec3::new(pos[0], pos[1], pos[2]), previous: Vec3::new(pos[0], pos[1], pos[2]), velocity: Vec3::new(0.0, 0.0, 0.0), diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index d574b8928..1c7b53f2a 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -714,7 +714,7 @@ impl Game<'_> { /// Mobs and items as the entities guests track. fn lan_entities(&self, hub: &mut Hub) -> Vec { - let mut out = Vec::with_capacity(self.mobs.len() + self.dropped.len()); + let mut out = Vec::with_capacity(self.mobs.len() + self.dropped.len() + self.orbs.len()); for m in self.mobs.iter().filter(|m| !m.removed) { let kind = m.kind.id.as_str(); let meta = mob_meta(m, hub); @@ -752,6 +752,23 @@ impl Game<'_> { equipment: Vec::new(), }); } + for o in &self.orbs { + out.push(EntityState { + id: o.id as i32, + uuid: entity_uuid(o.id), + kind: "minecraft:experience_orb".to_owned(), + position: [o.position.x, o.position.y, o.position.z], + yaw: 0.0, + pitch: 0.0, + head_yaw: 0.0, + on_ground: o.on_ground, + velocity: [o.velocity.x, o.velocity.y, o.velocity.z], + data: 0, + // ExperienceOrb.DATA_VALUE. + meta: vec![(8, serializer::INT, Value::Int(o.value as i32))], + equipment: Vec::new(), + }); + } out } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 7cd4b0474..240d5fbaa 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -18,7 +18,7 @@ use horizon_client::{ }, motion::{MotionEvent, MotionWorld, PlayerMotion}, }; -use horizon_core::geometry::Vec3; +use horizon_core::{geometry::Vec3, random::RandomSource}; use horizon_protocol::{ packets::play::{self, Clientbound as C, Serverbound as S}, text::literal, @@ -54,6 +54,8 @@ pub(super) struct GuestPlay { /// `ServerPlayer.getStats` and the inventory `inventory_changed` last saw. pub(super) stats: super::stats::Stats, last_inventory: u64, + /// `Player.takeXpDelay`: ticks until the next orb can be taken. + xp_delay: u32, } struct ItemRules<'a>(&'a horizon_client::items::Items); @@ -1006,6 +1008,7 @@ impl Game<'_> { host.hub.flush_progress(conn); self.guest_dig_feedback(host, conn); self.guest_pickups(host, gi); + self.guest_orbs(host, gi); let g = &mut host.hub.guests[gi]; if g.health <= 0.0 && !g.dead { g.dead = true; @@ -1307,6 +1310,79 @@ impl Game<'_> { } } + /// `Player.tick`'s takeXpDelay and `Player.aiStep`'s touch of the + /// experience orbs in the guest's box grown by 1, 0.5, 1: one of them at + /// random, at most every 2 ticks (`ExperienceOrb.playerTouch`), repairs + /// with Mending and gives the rest as points. Everyone near hears it. + fn guest_orbs(&mut self, host: &mut LanHost, gi: usize) { + let g = &host.hub.guests[gi]; + // Player's standing and crouching boxes: 1.8 and 1.5 high. + let height = if g.sneaking { 1.5 } else { 1.8 }; + let (conn, id, at) = (g.conn, g.entity_id, g.position); + let unable = g.spectator() || g.dead || g.health <= 0.0; + let play = host.play.entry(conn).or_default(); + if play.xp_delay > 0 { + play.xp_delay -= 1; + } + if unable || play.xp_delay > 0 { + return; + } + let (min, max) = ( + [at[0] - 1.3, at[1] - 0.5, at[2] - 1.3], + [at[0] + 1.3, at[1] + height + 0.5, at[2] + 1.3], + ); + let touching: Vec = self + .orbs + .iter() + .enumerate() + .filter(|(_, o)| { + o.position.x + 0.25 > min[0] + && o.position.x - 0.25 < max[0] + && o.position.y + 0.5 > min[1] + && o.position.y < max[1] + && o.position.z + 0.25 > min[2] + && o.position.z - 0.25 < max[2] + }) + .map(|(i, _)| i) + .collect(); + if touching.is_empty() { + return; + } + // Util.getRandom(orbs, random). + let pick = self + .world + .random + .next_int_bound(touching.len() as i32) + .unwrap_or(0) as usize; + let orb = self.orbs.remove(touching[pick]); + play.xp_delay = 2; + host.hub.take_item(orb.id as i32, id, 1, at); + // ClientPacketListener.handleTakeItemEntity's sound, at the orb. + let pitch = (self.world.random.next_float() - self.world.random.next_float()) * 0.35 + 0.9; + let p = orb.position; + self.audio.play( + "minecraft:entity.experience_orb.pickup", + Some([p.x, p.y, p.z]), + 0.1, + pitch, + ); + let value = orb.value; + if let Some(target) = super::pickups::guest_target(host, id as u32) { + use super::pickups::{Collector, Taken}; + self.add_pickup_toward( + Taken::Orb(Box::new(orb)), + Collector::Guest(id as u32), + target, + ); + } + self.as_guest(host, conn, |game| { + let rest = game.mending(value); + if rest > 0 { + game.give_orb_points(rest); + } + }); + } + /// The host's own `ItemPickupParticle` toward the guest's model. fn guest_pickup( &mut self, diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 96d879f13..27fe36694 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -176,7 +176,13 @@ impl Game<'_> { self.dropped.push(e); } } - "minecraft:experience_orb" => self.orbs.extend(entity_nbt::orbs_from_nbt(&tag)), + "minecraft:experience_orb" => { + for mut orb in entity_nbt::orbs_from_nbt(&tag) { + self.next_entity += 1; + orb.id = self.next_entity; + self.orbs.push(orb); + } + } "minecraft:arrow" => { if let Some((mut p, in_block)) = entity_nbt::arrow_from_nbt( &tag, diff --git a/crates/horizon-client/src/game/pickups.rs b/crates/horizon-client/src/game/pickups.rs index 2729dc98f..1e19bcfc3 100644 --- a/crates/horizon-client/src/game/pickups.rs +++ b/crates/horizon-client/src/game/pickups.rs @@ -148,6 +148,7 @@ mod tests { fn orb_at(x: f64, y: f64, z: f64) -> Taken { let at = horizon_core::geometry::Vec3::new(x, y, z); Taken::Orb(Box::new(ExperienceOrb { + id: 0, position: at, previous: at, velocity: horizon_core::geometry::Vec3::new(0.0, 0.0, 0.0), diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 05e1f81c3..def1060f7 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -11,7 +11,7 @@ use horizon_client::{ Result, chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section}, containers::{BlockEntity, Furnace, FurnaceKind}, - entities::{Experience, ItemEntity}, + entities::{Experience, ExperienceOrb, ItemEntity}, hud::{HudBatch, rgba}, items::ItemStack, mobs::{Mob, MobType}, @@ -875,6 +875,19 @@ impl Game<'_> { } else { self.res.mob_types.get(kind).cloned() }; + if kind == "minecraft:experience_orb" { + // Its value (ExperienceOrb.DATA_VALUE) follows in SetEntityData. + self.orbs.retain(|o| o.id != id as u32); + self.orbs.push(ExperienceOrb { + id: id as u32, + position: at, + previous: at, + velocity: Vec3::ZERO, + value: 1, + age: 0, + on_ground: false, + }); + } if let Some(t) = mob_type { let mut mob = Mob::new(id as u32, t, at, head); mob.body_yaw = yaw; @@ -928,6 +941,7 @@ impl Game<'_> { } fn remove_entity(&mut self, id: i32) { self.mobs.retain(|m| m.id != id as u32); + self.orbs.retain(|o| o.id != id as u32); self.dropped.retain(|d| d.id != id as u32); if let Some(r) = self.remote.as_mut() { r.entities.remove(&id); @@ -1277,6 +1291,13 @@ impl Game<'_> { { self.remote_item_data(p.id, stack); } + // ExperienceOrb.DATA_VALUE (the icon's size). + if *index == 8 + && let Value::Int(v) = value + && let Some(o) = self.orbs.iter_mut().find(|o| o.id == p.id as u32) + { + o.value = (*v).max(0) as u32; + } } } // ClientPacketListener.handleSetEquipment: what a mob or another @@ -1303,10 +1324,13 @@ impl Game<'_> { } } C::TakeItemEntity(p) => { - if self - .remote - .as_ref() - .is_some_and(|r| r.entity_id == p.player) + // An orb's sound comes from the server, at the orb. + let orb = self.orbs.iter().any(|o| o.id == p.item as u32); + if !orb + && self + .remote + .as_ref() + .is_some_and(|r| r.entity_id == p.player) && let Some(eye) = self.eye_position() { let pitch = ((self.world.random.next_float() - self.world.random.next_float()) @@ -1317,17 +1341,20 @@ impl Game<'_> { .play("minecraft:entity.item.pickup", Some(eye), 0.2, pitch); } // ItemPickupParticle toward the collector (this player, another one or a mob). + let collector = if self + .remote + .as_ref() + .is_some_and(|r| r.entity_id == p.player) + { + super::pickups::Collector::Player + } else { + super::pickups::Collector::Mob(p.player as u32) + }; if let Some(item) = self.dropped.iter().find(|d| d.id == p.item as u32).cloned() { - let collector = if self - .remote - .as_ref() - .is_some_and(|r| r.entity_id == p.player) - { - super::pickups::Collector::Player - } else { - super::pickups::Collector::Mob(p.player as u32) - }; self.add_pickup(super::pickups::Taken::Item(Box::new(item)), collector); + } else if let Some(orb) = self.orbs.iter().find(|o| o.id == p.item as u32).cloned() + { + self.add_pickup(super::pickups::Taken::Orb(Box::new(orb)), collector); } self.remove_entity(p.item); } @@ -2278,6 +2305,21 @@ impl Game<'_> { horizon_client::mobs::rabbit::post_tick(m); } } + for o in self.orbs.iter_mut().filter(|_| running) { + o.previous = o.position; + o.age += 1; + if let Some(l) = lerps.get_mut(&(o.id as i32)) + && l.steps > 0 + { + let n = f64::from(l.steps); + o.position = Vec3::new( + o.position.x + (l.target[0] - o.position.x) / n, + o.position.y + (l.target[1] - o.position.y) / n, + o.position.z + (l.target[2] - o.position.z) / n, + ); + l.steps -= 1; + } + } for d in self.dropped.iter_mut().filter(|_| running) { d.previous = d.position; d.age += 1; diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 667fdda2d..7997d001e 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -20,6 +20,8 @@ address. The script then checks, from both sides, what a guest's game used to ge - containers: the guest's ender chest holds the guest's items, not the host's, and a double chest opens as 9x6 with the right half first; - feedback: the guest's command feedback names the guest and shows the numbers. +- xp: experience orbs 4 blocks away fly to the guest, show on the guest's screen and give the + guest their points (not the host). Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -48,7 +50,7 @@ from agent_control_e2e import Cli, Failed, game_env # noqa: E402 ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials', 'xp'] class Game: @@ -622,11 +624,39 @@ def check_trials(host, guest, out): return {'spawner': trial, 'vault': active, 'rewarded': uuid(rewarded), 'host': uuid(host_uuid)} +def check_xp(host, guest, out): + for g in (host, guest): + g('command', command='gamemode survival') + g('command', command='xp set @s 0 levels') + g('command', command='xp set @s 0 points') + guest.wait(10) + x, y, z = pos_of(guest) + # Three orbs of 7 points 4 blocks east of the guest (out of reach, within the 8 blocks orbs + # follow from): they fly to the guest. + for _ in range(3): + guest('command', command=f'summon minecraft:experience_orb {x + 4:.2f} {y + 0.5:.2f} {z:.2f} {{Value:7s}}') + guest.wait(2) + seen = remote(guest).get('entities', {}).get('minecraft:experience_orb', 0) + guest.shot(out, 'xp-orbs') + guest.wait(60) + xp = guest('state')['player']['xp'] + host_xp = host('state')['player']['xp'] + left = remote(guest).get('entities', {}).get('minecraft:experience_orb', 0) + detail = {'seen': seen, 'guest': xp, 'host': host_xp, 'left': left} + # ExperienceOrb.scanForMerges: orbs of one value side by side can merge into one. + assert 1 <= seen <= 3, f'the guest saw {seen} orbs: {detail}' + assert xp['total'] == 21 and xp['level'] == 2, f'the guest\'s experience after three 7-point orbs: {detail}' + assert host_xp['total'] == 0, f'the host took the orbs: {detail}' + assert left == 0, f'orbs left on the guest\'s screen: {detail}' + return {'seen': seen, 'guest': xp, 'host': host_xp} + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, 'containers': check_containers, 'feedback': check_feedback, 'trials': check_trials, - 'walls': check_walls} + 'walls': check_walls, + 'xp': check_xp} def run(ctl, out, resources, checks): From 2e531f35e52638330e838036d5b74de600a65216 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:25:11 +0200 Subject: [PATCH 07/67] Per-state tables: check state identities while reading, never as a tree occlusion.nbt, cached-collision.nbt, cached-support.nbt, block-properties.nbt and movement-properties.nbt each carry the identity of every block state, and every loader parsed them into an NBT tree first: 240 MB per table for ATM10's 153,511 states, about 1.4 GB allocated and freed while the game starts. nbt::read_named_streaming hands a named list to a visitor element by element. registry::BlockTableFile reads a table that way, checking each state against the registry as it comes, and loaders build from a checked registry::BlockTable. from_nbt stays for trees built in memory (tests, fixtures). Co-Authored-By: Claude Opus 5.5 --- .../examples/resource_memory.rs | 14 -- crates/horizon-client/src/collisions.rs | 14 +- crates/horizon-client/src/light.rs | 14 +- crates/horizon-client/src/models.rs | 8 +- crates/horizon-client/src/motion.rs | 12 +- crates/horizon-world/src/block_properties.rs | 35 ++-- crates/horizon-world/src/block_support.rs | 16 +- crates/horizon-world/src/collision.rs | 19 +- crates/horizon-world/src/level/data.rs | 13 +- .../horizon-world/src/movement_properties.rs | 18 +- crates/horizon-world/src/nbt.rs | 167 +++++++++++++++++- crates/horizon-world/src/registry.rs | 117 ++++++++++++ .../src/worldgen/generator_blocks.rs | 26 ++- crates/horizon-world/tests/collision_data.rs | 72 ++++++++ 14 files changed, 421 insertions(+), 124 deletions(-) diff --git a/crates/horizon-client/examples/resource_memory.rs b/crates/horizon-client/examples/resource_memory.rs index 6deb5a9d4..c292ff2f4 100644 --- a/crates/horizon-client/examples/resource_memory.rs +++ b/crates/horizon-client/examples/resource_memory.rs @@ -122,20 +122,6 @@ fn main() -> Result<()> { step("collision"); let motion = MotionResources::load(&root, &assets)?; step("motion"); - for name in [ - "worldgen/blocks/block-properties.nbt", - "worldgen/blocks/cached-collision.nbt", - "worldgen/blocks/cached-support.nbt", - "cached-collision.nbt", - "occlusion.nbt", - "movement-properties.nbt", - ] { - let bytes = std::fs::read(root.join(name))?; - let tree = horizon_world::nbt::read_named(&bytes, horizon_world::nbt::Limits::tables())?; - step(&format!(" tree of {name}")); - drop((tree, bytes)); - step(" dropped"); - } let terrain = VanillaTerrain::new(&root.join("worldgen"), assets.registry, &biomes, 1234)?; step("worldgen"); println!( diff --git a/crates/horizon-client/src/collisions.rs b/crates/horizon-client/src/collisions.rs index d3e00daca..5d5f4c1bb 100644 --- a/crates/horizon-client/src/collisions.rs +++ b/crates/horizon-client/src/collisions.rs @@ -15,24 +15,18 @@ use horizon_world::{ entity_obstruction::EntityIdentity, fluid::{FluidKind, FluidState}, liquid_collision::LiquidCollisionWorld, - nbt::{self, Limits}, powder_snow_collision::{PowderSnowEntity, PowderSnowWorld}, - registry::BlockRegistry, + registry::{BlockRegistry, BlockTableFile}, world_movement::{ CollisionCheckRequest, EntityMovementWorld, MovementRequest, no_world_collision, resolve_world_movement, }, }; -use std::{fs, io, path::Path, sync::Arc}; +use std::{io, path::Path, sync::Arc}; pub fn load<'r>(root: &Path, registry: &'r BlockRegistry) -> Result> { - let data = nbt::read_named( - &fs::read(root.join("cached-collision.nbt"))?, - Limits::tables(), - )? - .0 - .tag; - let cached = CachedCollisionRegistry::from_nbt(&data, registry)?; + let file = BlockTableFile::open(&root.join("cached-collision.nbt"), registry)?; + let cached = CachedCollisionRegistry::from_table(file.table("collision")?)?; if !cached.has_canonical_identities() { return Err("scene collisions require canonical shape identities".into()); } diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index 595efd9b2..dcf4633a9 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -65,17 +65,9 @@ impl LightTables { } /// Full tables with the vanilla occlusion shapes from `occlusion.nbt`. pub fn load(root: &Path, registry: &BlockRegistry, facts: &BlockFacts) -> crate::Result { - use horizon_world::{ - collision::CachedCollisionRegistry, - nbt::{self, Limits}, - }; - let data = nbt::read_named( - &std::fs::read(root.join("occlusion.nbt"))?, - Limits::tables(), - )? - .0 - .tag; - let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?; + use horizon_world::{collision::CachedCollisionRegistry, registry::BlockTableFile}; + let file = BlockTableFile::open(&root.join("occlusion.nbt"), registry)?; + let occlusion = CachedCollisionRegistry::from_table(file.table("occlusion")?)?; let mut tables = Self::new(facts); let block = VoxelShape::block(); let mut shapes: Vec = vec![VoxelShape::empty(), block.clone()]; diff --git a/crates/horizon-client/src/models.rs b/crates/horizon-client/src/models.rs index 110166f29..5979672e9 100644 --- a/crates/horizon-client/src/models.rs +++ b/crates/horizon-client/src/models.rs @@ -117,13 +117,11 @@ pub fn load_visibility( use horizon_world::{ block_render::{FaceVisibility, RenderDefinition, SkipRendering}, collision::CachedCollisionRegistry, - nbt::{self, Limits}, + registry::BlockTableFile, }; use std::{collections::BTreeMap, fs}; - let data = nbt::read_named(&fs::read(root.join("occlusion.nbt"))?, Limits::tables())? - .0 - .tag; - let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?; + let file = BlockTableFile::open(&root.join("occlusion.nbt"), registry)?; + let occlusion = CachedCollisionRegistry::from_table(file.table("occlusion")?)?; let raw: BTreeMap = serde_json::from_slice(&fs::read(root.join("render-definitions.json"))?)?; let definitions = raw diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 0eb79d65c..4c026ac45 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -31,8 +31,7 @@ use horizon_world::{ movement_restitution::{self, RestitutionInput, RestitutionWorld}, movement_statistics::{self, MovementStat, MovementStatisticsWorld}, movement_support::{self, MovementLocation}, - nbt::{self, Limits}, - registry::BlockStateId, + registry::{BlockStateId, BlockTableFile}, world_movement::{self, MovementRequest}, worldgen::tags::TagDefinitions, }; @@ -61,13 +60,8 @@ impl MotionResources { let attributes = Arc::new(Catalog::from_json(&serde_json::from_slice(&fs::read( root.join("attributes.json"), )?)?)?); - let data = nbt::read_named( - &fs::read(root.join("movement-properties.nbt"))?, - Limits::tables(), - )? - .0 - .tag; - let properties = MovementProperties::from_nbt(&data, assets.registry)?; + let file = BlockTableFile::open(&root.join("movement-properties.nbt"), assets.registry)?; + let properties = MovementProperties::from_table(file.table("movement")?)?; let bundle: BTreeMap = serde_json::from_slice(&fs::read(root.join("block-tags.json"))?)?; let mut definitions = TagDefinitions::default(); diff --git a/crates/horizon-world/src/block_properties.rs b/crates/horizon-world/src/block_properties.rs index 2a463b9e3..7c0e17f61 100644 --- a/crates/horizon-world/src/block_properties.rs +++ b/crates/horizon-world/src/block_properties.rs @@ -2,11 +2,10 @@ //! collision shapes. Replacement with an item/fluid context is separate behavior. use crate::{ DATA_VERSION, - chunk::BlockState, collision::CachedCollisionRegistry, compression::invalid, nbt::Tag, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTable}, }; use horizon_core::geometry::{Direction, ShapeError, ShapeOp, VoxelShape}; use std::{ @@ -112,21 +111,25 @@ pub struct BlockPropertiesRegistry<'r> { } impl<'r> BlockPropertiesRegistry<'r> { pub fn from_nbt(tag: &Tag, collisions: &CachedCollisionRegistry<'r>) -> io::Result { + Self::from_table( + BlockTable::check(tag, collisions.registry(), "block property")?, + collisions, + ) + } + pub fn from_table( + table: BlockTable<'_, '_>, + collisions: &CachedCollisionRegistry<'r>, + ) -> io::Result { let registry = collisions.registry(); + if !std::ptr::eq(table.registry(), registry) { + return Err(invalid( + "block properties and collisions use other registries", + )); + } + let tag = table.tag(); if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) { return Err(invalid("block properties DataVersion mismatch")); } - let Some(Tag::List(states)) = tag.get("states") else { - return Err(invalid("missing block property state identities")); - }; - if states.len() != registry.len() { - return Err(invalid("block property registry size mismatch")); - } - for (id, state) in states.iter().enumerate() { - if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) { - return Err(invalid("block property state identities differ")); - } - } let Some(Tag::Compound(blocks)) = tag.get("blocks") else { return Err(invalid("missing block definition properties")); }; @@ -150,10 +153,8 @@ impl<'r> BlockPropertiesRegistry<'r> { }, ); } - let occlusion = CachedCollisionRegistry::from_nbt( - tag.get("occlusion") - .ok_or_else(|| invalid("missing block occlusion shapes"))?, - registry, + let occlusion = CachedCollisionRegistry::from_table( + table.nested("occlusion", "block occlusion shapes")?, )?; let mut result = Vec::with_capacity(registry.len()); let mut faces = Vec::with_capacity(registry.len()); diff --git a/crates/horizon-world/src/block_support.rs b/crates/horizon-world/src/block_support.rs index 2c7a9aecc..295999d18 100644 --- a/crates/horizon-world/src/block_support.rs +++ b/crates/horizon-world/src/block_support.rs @@ -61,7 +61,7 @@ use crate::{ collision::CachedCollisionRegistry, compression::invalid, nbt::Tag, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTable}, }; use std::{collections::HashMap, io}; /// Cached branch of BlockState.isFaceSturdy. Dynamic support-shape selection @@ -72,10 +72,14 @@ pub struct CachedBlockSupportRegistry<'r> { } impl<'r> CachedBlockSupportRegistry<'r> { pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result { - if tag.get("Kind") != Some(&Tag::String("block_support".into())) { + Self::from_table(BlockTable::check(tag, registry, "support")?) + } + pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result { + if table.get("Kind") != Some(&Tag::String("block_support".into())) { return Err(invalid("wrong cached support data kind")); } - let shapes = CachedCollisionRegistry::from_nbt(tag, registry)?; + let registry = table.registry(); + let shapes = CachedCollisionRegistry::from_table(table)?; let mut cache = HashMap::new(); let mut states = Vec::with_capacity(registry.len()); for id in 0..registry.len() { @@ -163,7 +167,11 @@ impl std::fmt::Display for SupportQueryError { impl std::error::Error for SupportQueryError {} impl<'r> BlockSupportRegistry<'r> { pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result { - let cached = CachedBlockSupportRegistry::from_nbt(tag, registry)?; + Self::from_table(BlockTable::check(tag, registry, "support")?) + } + pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result { + let registry = table.registry(); + let cached = CachedBlockSupportRegistry::from_table(table)?; let mut dynamic = HashMap::new(); for id in 0..registry.len() { let id = BlockStateId(id as u32); diff --git a/crates/horizon-world/src/collision.rs b/crates/horizon-world/src/collision.rs index 694264a5e..3d3311646 100644 --- a/crates/horizon-world/src/collision.rs +++ b/crates/horizon-world/src/collision.rs @@ -5,10 +5,10 @@ //! This module covers the cached branch only; uncached states remain explicit. use crate::{ DATA_VERSION, - chunk::{BlockSection, BlockState}, + chunk::BlockSection, compression::invalid, nbt::Tag, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTable}, }; use horizon_core::geometry::{Axis, VoxelShape}; use std::{collections::HashMap, io}; @@ -35,20 +35,13 @@ impl<'r> CachedCollisionRegistry<'r> { self.registry } pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result { + Self::from_table(BlockTable::check(tag, registry, "collision")?) + } + pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result { + let (tag, registry) = (table.tag(), table.registry()); if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) { return Err(invalid("collision DataVersion mismatch")); } - let Some(Tag::List(states)) = tag.get("states") else { - return Err(invalid("missing collision state identities")); - }; - if states.len() != registry.len() { - return Err(invalid("collision registry size mismatch")); - } - for (id, state) in states.iter().enumerate() { - if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) { - return Err(invalid("collision state identities differ")); - } - } let Some(Tag::List(entries)) = tag.get("shapes") else { return Err(invalid("missing collision shapes")); }; diff --git a/crates/horizon-world/src/level/data.rs b/crates/horizon-world/src/level/data.rs index 280f21582..0862aa5b8 100644 --- a/crates/horizon-world/src/level/data.rs +++ b/crates/horizon-world/src/level/data.rs @@ -8,8 +8,7 @@ use crate::{ collision::CachedCollisionRegistry, compression::invalid, fluid::{FluidBlockBindings, FluidKind, FluidState}, - nbt::{self, Limits}, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTableFile}, worldgen::tags::TagDefinitions, }; use horizon_core::geometry::{Aabb, Direction, ShapeOp, Vec3, VoxelShape}; @@ -787,14 +786,8 @@ fn build_with_gameplay( }; // Collision shapes. - let collision_tag = nbt::read_named( - &fs::read(root.join("cached-collision.nbt"))?, - Limits::tables(), - ) - .map_err(|e| invalid(e.to_string()))? - .0 - .tag; - let cached = CachedCollisionRegistry::from_nbt(&collision_tag, registry)?; + let collision_file = BlockTableFile::open(&root.join("cached-collision.nbt"), registry)?; + let cached = CachedCollisionRegistry::from_table(collision_file.table("collision")?)?; let mut shapes: Vec = Vec::new(); let mut shape_ids = Vec::with_capacity(table.state_count()); for id in 0..table.state_count() { diff --git a/crates/horizon-world/src/movement_properties.rs b/crates/horizon-world/src/movement_properties.rs index 84cafae1e..9e1dc6236 100644 --- a/crates/horizon-world/src/movement_properties.rs +++ b/crates/horizon-world/src/movement_properties.rs @@ -1,11 +1,10 @@ //! Registry-bound block movement coefficients, identities and reloadable tags. use crate::{ DATA_VERSION, - chunk::BlockState, compression::invalid, movement_support::MovementBlockWorld, nbt::Tag, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTable}, }; use horizon_core::position::BlockPos; use std::{ @@ -55,20 +54,13 @@ impl<'r> MovementProperties<'r> { } pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result { + Self::from_table(BlockTable::check(tag, registry, "movement")?) + } + pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result { + let (tag, registry) = (table.tag(), table.registry()); if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) { return Err(invalid("movement properties DataVersion mismatch")); } - let Some(Tag::List(states)) = tag.get("states") else { - return Err(invalid("missing movement state identities")); - }; - if states.len() != registry.len() { - return Err(invalid("movement registry size mismatch")); - } - for (id, state) in states.iter().enumerate() { - if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) { - return Err(invalid("movement state identities differ")); - } - } let Some(Tag::Compound(blocks)) = tag.get("blocks") else { return Err(invalid("missing movement definitions")); }; diff --git a/crates/horizon-world/src/nbt.rs b/crates/horizon-world/src/nbt.rs index 414767795..2a8b11591 100644 --- a/crates/horizon-world/src/nbt.rs +++ b/crates/horizon-world/src/nbt.rs @@ -124,13 +124,20 @@ impl std::fmt::Display for Error { } impl std::error::Error for Error {} -struct Reader<'a> { +/// Hands a streamed list's elements to the caller one at a time (see +/// [`read_named_streaming`]). +type Visit<'s> = &'s mut dyn FnMut(&mut ListElements<'_, '_, '_>) -> Result<(), Error>; + +struct Reader<'a, 's> { bytes: &'a [u8], offset: usize, limits: Limits, nodes: usize, + /// Lists with this key in the root compound or in a compound directly + /// under it are streamed to the visitor instead of kept. + stream: Option<(&'s str, Visit<'s>)>, } -impl<'a> Reader<'a> { +impl<'a> Reader<'a, '_> { fn error(&self, reason: &'static str) -> Error { Error { offset: self.offset, @@ -266,7 +273,11 @@ impl<'a> Reader<'a> { break; } let name = self.string()?; - let value = self.payload(id, depth + 1)?; + let value = if id == 9 && depth <= 1 && self.streams(&name) { + self.stream_list(depth + 1)? + } else { + self.payload(id, depth + 1)? + }; map.insert(name, value); // Java maps retain the last duplicate key. } Tag::Compound(map) @@ -290,6 +301,82 @@ impl<'a> Reader<'a> { _ => return Err(self.error("unknown tag type")), }) } + fn streams(&self, name: &JavaString) -> bool { + self.stream + .as_ref() + .is_some_and(|(key, _)| name.0.iter().copied().eq(key.encode_utf16())) + } + /// A list's payload handed to the visitor element by element; the tree + /// keeps it empty. Elements the visitor leaves are read and dropped. + fn stream_list(&mut self, depth: usize) -> Result { + if self.nodes >= self.limits.max_nodes { + return Err(self.error("node limit exceeded")); + } + self.nodes += 1; + if depth >= self.limits.max_depth { + return Err(self.error("depth limit exceeded")); + } + let element_id = self.byte()?; + let len = self.count(0)?; + if len > 0 && (element_id == 0 || element_id > 12) { + return Err(self.error("invalid list element type")); + } + if len > self.bytes.len().saturating_sub(self.offset) { + return Err(self.error("truncated list")); + } + let (key, visit) = self.stream.take().expect("streams() checked the visitor"); + let mut elements = ListElements { + reader: self, + element_id, + depth: depth + 1, + len, + left: len, + }; + let rest = visit(&mut elements).and_then(|()| { + while elements.next_element()?.is_some() {} + Ok(()) + }); + self.stream = Some((key, visit)); + rest.map(|()| Tag::List(Vec::new())) + } +} + +/// The elements of a list [`read_named_streaming`] streams, read one at a +/// time. +pub struct ListElements<'r, 'a, 's> { + reader: &'r mut Reader<'a, 's>, + element_id: u8, + depth: usize, + len: usize, + left: usize, +} +impl ListElements<'_, '_, '_> { + /// The list's length, as its header gives it. + pub fn len(&self) -> usize { + self.len + } + pub fn is_empty(&self) -> bool { + self.len == 0 + } + /// The next element, unwrapped as [`read_named`] unwraps mixed-list + /// elements; `None` past the last. + pub fn next_element(&mut self) -> Result, Error> { + if self.left == 0 { + return Ok(None); + } + self.left -= 1; + let mut value = self.reader.payload(self.element_id, self.depth)?; + if let Tag::Compound(map) = &mut value { + if map.len() == 1 && map.contains_key(&JavaString::default()) { + value = map.remove(&JavaString::default()).unwrap(); + } + } + Ok(Some(value)) + } + /// An error at the current position in the input. + pub fn error(&self, reason: &'static str) -> Error { + self.reader.error(reason) + } } /// Read a file root (type, modified-UTF name, payload), returning bytes consumed. @@ -299,6 +386,35 @@ pub fn read_named(bytes: &[u8], limits: Limits) -> Result<(NamedTag, usize), Err offset: 0, limits, nodes: 0, + stream: None, + }; + let id = reader.byte()?; + let name = if id == 0 { + JavaString::default() + } else { + reader.string()? + }; + let tag = reader.payload(id, 0)?; + Ok((NamedTag { name, tag }, reader.offset)) +} + +/// [`read_named`], except that lists keyed `key` in the root compound or in +/// a compound directly under it are not kept: `visit` reads each one's +/// elements as they come, and the tree holds it as an empty list. Per-state +/// tables check hundreds of thousands of state identities this way without +/// holding them as a tree (crate::registry::BlockTableFile). +pub fn read_named_streaming( + bytes: &[u8], + limits: Limits, + key: &str, + visit: &mut dyn FnMut(&mut ListElements<'_, '_, '_>) -> Result<(), Error>, +) -> Result<(NamedTag, usize), Error> { + let mut reader = Reader { + bytes, + offset: 0, + limits, + nodes: 0, + stream: Some((key, visit)), }; let id = reader.byte()?; let name = if id == 0 { @@ -317,6 +433,7 @@ pub fn read_network(bytes: &[u8], limits: Limits) -> Result<(Tag, usize), Error> offset: 0, limits, nodes: 0, + stream: None, }; let id = reader.byte()?; let tag = reader.payload(id, 0)?; @@ -659,6 +776,50 @@ impl Snbt<'_> { mod tests { use super::*; #[test] + fn streamed_lists_reach_the_visitor_and_stay_empty_in_the_tree() { + let list = |n: i32| Tag::List((0..n).map(Tag::Int).collect()); + let root = NamedTag { + name: "".into(), + tag: Tag::compound([ + ("states", list(3)), + ("inner", Tag::compound([("states", list(2))])), + ( + "deep", + Tag::compound([("inner", Tag::compound([("states", list(1))]))]), + ), + ("other", list(4)), + ]), + }; + let bytes = write_named(&root, Limits::default()).unwrap(); + let mut seen = Vec::new(); + let (read, used) = read_named_streaming(&bytes, Limits::default(), "states", &mut |l| { + let mut values = vec![l.len() as i32]; + // Elements left unread are skipped. + if let Some(Tag::Int(v)) = l.next_element()? { + values.push(v); + } + seen.push(values); + Ok(()) + }) + .unwrap(); + assert_eq!(used, bytes.len()); + // Compounds are written in key order: `inner` before `states`. + assert_eq!(seen, [vec![2, 0], vec![3, 0]]); + assert_eq!(read.tag.get("states"), Some(&Tag::List(Vec::new()))); + assert_eq!( + read.tag.get("inner").unwrap().get("states"), + Some(&Tag::List(Vec::new())) + ); + // Only the root and its direct children stream. + assert_eq!(read.tag.get("deep"), root.tag.get("deep")); + assert_eq!(read.tag.get("other"), root.tag.get("other")); + // A visitor's error stops the read. + let failed = read_named_streaming(&bytes, Limits::default(), "states", &mut |l| { + Err(l.error("refused")) + }); + assert_eq!(failed.unwrap_err().reason, "refused"); + } + #[test] fn wire_node_budget_includes_mixed_list_wrappers() { let tag = Tag::List(vec![Tag::Int(1), Tag::Byte(2)]); let limits = Limits { diff --git a/crates/horizon-world/src/registry.rs b/crates/horizon-world/src/registry.rs index 5ca2e994a..30cd5e83c 100644 --- a/crates/horizon-world/src/registry.rs +++ b/crates/horizon-world/src/registry.rs @@ -132,3 +132,120 @@ impl BlockRegistry { .ok_or_else(|| invalid("invalid block property value")) } } + +/// A per-state table (`occlusion.nbt`, `cached-collision.nbt`, ...) whose +/// `states` list, the identity of every state it describes in id order, has +/// been checked against `registry`. +#[derive(Clone, Copy)] +pub struct BlockTable<'t, 'r> { + tag: &'t Tag, + registry: &'r BlockRegistry, + /// How many levels, from this one down, [`BlockTableFile::read`] checked + /// while reading: their `states` lists are kept empty in the tree. + read_checked: u8, +} +impl<'t, 'r> BlockTable<'t, 'r> { + /// Checks that `tag`'s `states` names the registry's states in id order. + pub fn check(tag: &'t Tag, registry: &'r BlockRegistry, what: &str) -> io::Result { + Self { + tag, + registry, + read_checked: 0, + } + .checked(what) + } + fn checked(self, what: &str) -> io::Result { + let Some(Tag::List(states)) = self.tag.get("states") else { + return Err(invalid(format!("missing {what} state identities"))); + }; + if self.read_checked == 0 { + if states.len() != self.registry.len() { + return Err(invalid(format!("{what} registry size mismatch"))); + } + for (id, state) in states.iter().enumerate() { + if self.registry.state(BlockStateId(id as u32)) + != Some(&BlockState::from_nbt(state)?) + { + return Err(invalid(format!("{what} state identities differ"))); + } + } + } + Ok(self) + } + /// The table nested under `key` (`block-properties.nbt`'s `occlusion`), + /// with its own `states`, checked the same way. + pub fn nested(&self, key: &str, what: &str) -> io::Result { + Self { + tag: self + .tag + .get(key) + .ok_or_else(|| invalid(format!("missing {what}")))?, + registry: self.registry, + read_checked: self.read_checked.saturating_sub(1), + } + .checked(what) + } + pub fn tag(&self) -> &'t Tag { + self.tag + } + pub fn get(&self, key: &str) -> Option<&'t Tag> { + self.tag.get(key) + } + pub fn registry(&self) -> &'r BlockRegistry { + self.registry + } +} + +/// A per-state table read from its file with its state identities checked +/// while reading, so they never exist as a tree: for 190,000 states (a big +/// modpack) that tree took 300-600 MB per table. +pub struct BlockTableFile<'r> { + tag: Tag, + registry: &'r BlockRegistry, +} +impl<'r> BlockTableFile<'r> { + /// [`Self::read`] of the file at `path`. + pub fn open(path: &std::path::Path, registry: &'r BlockRegistry) -> io::Result { + let bytes = std::fs::read(path)?; + Self::read(&bytes, registry).map_err(|e| invalid(format!("{}: {e}", path.display()))) + } + /// Reads `bytes`, checking every `states` list of the root compound and + /// of the compounds directly under it against `registry`. + pub fn read(bytes: &[u8], registry: &'r BlockRegistry) -> io::Result { + let mut visit = |states: &mut crate::nbt::ListElements<'_, '_, '_>| { + if states.len() != registry.len() { + return Err(states.error("table registry size mismatch")); + } + let mut id = 0u32; + while let Some(tag) = states.next_element()? { + let state = + BlockState::from_nbt(&tag).map_err(|_| states.error("invalid table state"))?; + if registry.state(BlockStateId(id)) != Some(&state) { + return Err(states.error("table state identities differ")); + } + id += 1; + } + Ok(()) + }; + let (named, _) = crate::nbt::read_named_streaming( + bytes, + crate::nbt::Limits::tables(), + "states", + &mut visit, + ) + .map_err(|e| invalid(e.to_string()))?; + Ok(Self { + tag: named.tag, + registry, + }) + } + /// The table, checked (`what` names it in errors). + pub fn table(&self, what: &str) -> io::Result> { + BlockTable { + tag: &self.tag, + registry: self.registry, + read_checked: 2, + } + .checked(what) + } +} diff --git a/crates/horizon-world/src/worldgen/generator_blocks.rs b/crates/horizon-world/src/worldgen/generator_blocks.rs index dc7db7d20..ed1182f36 100644 --- a/crates/horizon-world/src/worldgen/generator_blocks.rs +++ b/crates/horizon-world/src/worldgen/generator_blocks.rs @@ -8,8 +8,7 @@ use crate::{ block_support::BlockSupportRegistry, block_survival::{BlockSurvivalRegistry, SurvivalDefinitions, SurvivalRule}, collision::CachedCollisionRegistry, - nbt::{self, Limits, Tag}, - registry::BlockRegistry, + registry::{BlockRegistry, BlockTableFile}, }; use serde_json::Value; use std::{ @@ -102,24 +101,21 @@ impl BlockBehavior { if !dir.join("block-properties.nbt").exists() { return Ok(None); } - let read = |name: &str| -> Result { - Ok( - nbt::read_named(&fs::read(dir.join(name))?, Limits::tables()) - .map_err(|e| resource(format!("{name}: {e:?}")))? - .0 - .tag, - ) + let read = |name: &str| -> Result, GeneratorError> { + BlockTableFile::open(&dir.join(name), registry) + .map_err(|e| resource(format!("{name}: {e}"))) }; let json = |name: &str| -> Result { Ok(serde_json::from_str(&fs::read_to_string(dir.join(name))?)?) }; let collisions = - CachedCollisionRegistry::from_nbt(&read("cached-collision.nbt")?, registry)?; - let properties = - BlockPropertiesRegistry::from_nbt(&read("block-properties.nbt")?, &collisions)?; - let support = Arc::new(BlockSupportRegistry::from_nbt( - &read("cached-support.nbt")?, - registry, + CachedCollisionRegistry::from_table(read("cached-collision.nbt")?.table("collision")?)?; + let properties = BlockPropertiesRegistry::from_table( + read("block-properties.nbt")?.table("block property")?, + &collisions, + )?; + let support = Arc::new(BlockSupportRegistry::from_table( + read("cached-support.nbt")?.table("support")?, )?); let collision = Arc::new(BlockCollisionRegistry::new(Arc::new(collisions))?); let rules = json("survival-rules.json")? diff --git a/crates/horizon-world/tests/collision_data.rs b/crates/horizon-world/tests/collision_data.rs index 3738edf1a..85b41d98e 100644 --- a/crates/horizon-world/tests/collision_data.rs +++ b/crates/horizon-world/tests/collision_data.rs @@ -267,3 +267,75 @@ fn registry_scan_binding_requires_complete_canonical_metadata() { ); } } + +/// A table read from its bytes checks its state identities while reading and +/// keeps none of them, yet loads the same shapes as the tree; a nested table +/// (`block-properties.nbt`'s `occlusion`) is checked the same way. +#[test] +fn streamed_tables_check_states_while_reading() { + use horizon_world::{ + nbt::{self, Limits, NamedTag}, + registry::{BlockTable, BlockTableFile}, + }; + let (registry_tag, collision_tag) = fixture(); + let registry = BlockRegistry::from_nbt(®istry_tag).unwrap(); + let bytes = |tag: &Tag| { + let root = NamedTag { + name: "".into(), + tag: tag.clone(), + }; + nbt::write_named(&root, Limits::tables()).unwrap() + }; + let file = BlockTableFile::read(&bytes(&collision_tag), ®istry).unwrap(); + let table = file.table("collision").unwrap(); + assert_eq!(table.get("states"), Some(&Tag::List(Vec::new()))); + let streamed = CachedCollisionRegistry::from_table(table).unwrap(); + let tree = CachedCollisionRegistry::from_nbt(&collision_tag, ®istry).unwrap(); + for id in 0..3 { + let id = BlockStateId(id); + assert_eq!( + streamed.cached_shape(id).unwrap().map(|s| s.boxes()), + tree.cached_shape(id).unwrap().map(|s| s.boxes()) + ); + } + + let mut outer = Tag::compound([("occlusion", collision_tag.clone())]); + let Tag::Compound(map) = &mut outer else { + unreachable!() + }; + map.insert( + "states".into(), + collision_tag.get("states").unwrap().clone(), + ); + let file = BlockTableFile::read(&bytes(&outer), ®istry).unwrap(); + let nested = file + .table("outer") + .unwrap() + .nested("occlusion", "occlusion") + .unwrap(); + assert_eq!(nested.get("states"), Some(&Tag::List(Vec::new()))); + CachedCollisionRegistry::from_table(nested).unwrap(); + // Checked only as a tree, an emptied list is refused. + assert!(BlockTable::check(nested.tag(), ®istry, "occlusion").is_err()); + + // Other states, a different count or no states at all are refused. + let with_states = |edit: &dyn Fn(&mut Vec)| { + let mut tag = collision_tag.clone(); + let Some(Tag::List(states)) = tag.get_mut("states") else { + unreachable!() + }; + edit(states); + tag + }; + let swapped = with_states(&|s| s.swap(1, 2)); + assert!(BlockTableFile::read(&bytes(&swapped), ®istry).is_err()); + let short = with_states(&|s| drop(s.pop())); + assert!(BlockTableFile::read(&bytes(&short), ®istry).is_err()); + let mut swapped = swapped; + let Tag::Compound(map) = &mut swapped else { + unreachable!() + }; + map.remove(&"states".into()); + let file = BlockTableFile::read(&bytes(&swapped), ®istry).unwrap(); + assert!(file.table("collision").is_err()); +} From 4ae51c1d5ee5e8a06dda0c6aae67cddbde148909 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:39:48 +0200 Subject: [PATCH 08/67] perf_bench --warm: measure a later start, after a first one built the caches A Horizon game with content mods builds its content root and block behaviour snapshot on its first start, in its game folder, and reuses them afterwards. perf_bench starts every game from an empty folder, so it only measured first starts. --warm starts the game once to the title screen first, unmeasured. Co-Authored-By: Claude Opus 5.5 --- tools/perf_bench.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tools/perf_bench.py b/tools/perf_bench.py index bbae4934f..6d0c5571d 100644 --- a/tools/perf_bench.py +++ b/tools/perf_bench.py @@ -364,6 +364,20 @@ def run_horizon(args, distance): for carry in dev_mods(args): command += ["--dev-mod", str(carry)] env = dict(os.environ, HORIZON_NO_AUDIO="1") + if args.warm: + # A first start to the title screen builds what later starts reuse (the content-mod + # root, the block behaviour snapshot). It is not measured: players see it once. + with (WORK / f"horizon-rd{distance}-warm.log").open("w") as log: + proc = subprocess.Popen(command, cwd=game_dir, env=env, stdout=log, stderr=subprocess.STDOUT) + try: + poll(Control(sock), lambda s: s.get("screen") == "title", 600) + try: + Control(sock)("quit", timeout=60) + except (OSError, ValueError, RuntimeError): + pass + finally: + stop(proc) + sock.unlink(missing_ok=True) log = (WORK / f"horizon-rd{distance}.log").open("w") started = time.time() proc = subprocess.Popen(command, cwd=game_dir, env=env, stdout=log, stderr=subprocess.STDOUT) @@ -774,6 +788,8 @@ def main(): p.add_argument("--build", default=str(ROOT / "target/release")) p.add_argument("--repeats", type=int, default=3, help="games per render distance; the report gives means and 95%% CIs") p.add_argument("--port-work", help="horizon: also load the mods carried over in this horizon-port-pack work folder") + p.add_argument("--warm", action="store_true", + help="horizon: measure a later start, after one start to the title screen built the caches") p.add_argument("--mod-jars", help="neoforge: the mod jars to load (the user's own; never committed)") p.add_argument("--deps", help="neoforge: their required libraries") p.add_argument("--table", nargs="+", metavar="LABEL", help="print markdown tables of saved runs, and exit") From 580c2b1dd790d19e7146159d91bd9f8fecb3f4f7 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:46:38 +0200 Subject: [PATCH 09/67] Motion: reuse the game's block facts for the fluid tables FluidResources decoded gameplay-facts.json a second time for three per-state columns the game's BlockFacts already has. With ATM10's carried mods that file is 211 MB: the second decode took a few seconds and peaked about 500 MB above what the game keeps. MotionResources::load_with_facts takes the game's facts; load still decodes them itself for tests and tools. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/examples/resource_memory.rs | 2 +- crates/horizon-client/src/game.rs | 2 +- crates/horizon-client/src/motion.rs | 17 ++++++++++++++++- crates/horizon-client/src/motion/fluids.rs | 3 +-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/examples/resource_memory.rs b/crates/horizon-client/examples/resource_memory.rs index c292ff2f4..1eb6c45b7 100644 --- a/crates/horizon-client/examples/resource_memory.rs +++ b/crates/horizon-client/examples/resource_memory.rs @@ -120,7 +120,7 @@ fn main() -> Result<()> { step("mesh tables"); let collision = collisions::load(&root, assets.registry)?; step("collision"); - let motion = MotionResources::load(&root, &assets)?; + let motion = MotionResources::load_with_facts(&root, &assets, &facts)?; step("motion"); let terrain = VanillaTerrain::new(&root.join("worldgen"), assets.registry, &biomes, 1234)?; step("worldgen"); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 6de683c67..37b8b1919 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10379,7 +10379,7 @@ pub fn run() -> Result<()> { log::info!("load atlas: {:.2}s", started.elapsed().as_secs_f32()); let registry_owner = assets.clone(); let collision = collisions::load(&root, registry_owner.registry)?; - let motion = MotionResources::load(&root, ®istry_owner)?; + let motion = MotionResources::load_with_facts(&root, ®istry_owner, &facts)?; log::info!( "load collision+motion: {:.2}s", started.elapsed().as_secs_f32() diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 4c026ac45..321af2458 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -56,7 +56,22 @@ pub struct MotionResources { pub fluids: FluidResources, } impl MotionResources { + /// [`Self::load_with_facts`] with the block facts decoded here. pub fn load(root: &Path, assets: &Assets) -> Result { + Self::load_with_facts( + root, + assets, + &crate::blocks::BlockFacts::load(root, assets.registry)?, + ) + } + /// The motion tables, with the block facts the game already decoded + /// (decoding `gameplay-facts.json` again costs a second or more and, with + /// a big modpack, hundreds of megabytes while it runs). + pub fn load_with_facts( + root: &Path, + assets: &Assets, + facts: &crate::blocks::BlockFacts, + ) -> Result { let attributes = Arc::new(Catalog::from_json(&serde_json::from_slice(&fs::read( root.join("attributes.json"), )?)?)?); @@ -85,7 +100,7 @@ impl MotionResources { Ok(Self { landing: crate::landing::load(root, &properties)?, health: crate::health::HealthResources::load(root)?, - fluids: FluidResources::load(root, assets)?, + fluids: FluidResources::new(assets, facts)?, attributes, blocks: properties.bind(&MovementTags { fences: tag("minecraft:fences")?, diff --git a/crates/horizon-client/src/motion/fluids.rs b/crates/horizon-client/src/motion/fluids.rs index f85f755a9..89affeb64 100644 --- a/crates/horizon-client/src/motion/fluids.rs +++ b/crates/horizon-client/src/motion/fluids.rs @@ -33,8 +33,7 @@ pub struct FluidResources { pub shapes: Mutex, } impl FluidResources { - pub fn load(root: &Path, assets: &Assets) -> Result { - let facts = crate::blocks::BlockFacts::load(root, assets.registry)?; + pub fn new(assets: &Assets, facts: &crate::blocks::BlockFacts) -> Result { Ok(Self { states: FluidStateTable::new(assets.registry)?, sturdy: facts.states.iter().map(|s| s.sturdy_full).collect(), From 24fd51b186b850b30e12797a5e13767189eb75c7 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:51:33 +0200 Subject: [PATCH 10/67] Content mods: keep the built content root for later starts resource_root built the content root again on every start. With ATM10's 40 carried data-tier mods that is about 25 s on a quiet machine (50 s on a busy one) and the biggest memory peak of the start, above 3 GB of footprint. The root now records a stamp of what it was built from (this build of the game, the base's manifest and rewritten tables, every mod file by size and time) and later starts with the same stamp use it as it is. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/mod_content.rs | 60 +++++++++++++++++++ .../horizon-client/src/mod_content_tests.rs | 35 +++++++++++ crates/horizon-mods/src/content.rs | 7 +++ 3 files changed, 102 insertions(+) diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index 25df87352..8176ddfae 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -943,15 +943,75 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { /// The root to load: `base` itself with no content, else the mods' overlay /// over it, built in `cache/mod-content`. +/// +/// The root is built once and kept: later starts with the same build, base +/// and mod files reuse it. With ATM10's carried mods a build takes about +/// 25 s and peaks above 3 GB, more than everything the game keeps. pub fn resource_root(base: &Path, mods: &[ContentMod], cache: &Path) -> Result { if mods.iter().all(|m| m.content.is_empty()) { return Ok(base.to_owned()); } let out = cache.join("mod-content"); + let stamp = stamp(base, mods)?; + if fs::read(out.join(STAMP)).is_ok_and(|s| s == stamp.as_bytes()) { + return Ok(out); + } build(base, mods, &out)?; + fs::write(out.join(STAMP), stamp)?; Ok(out) } +/// In a root built by [`resource_root`]: what it was built from. +const STAMP: &str = "horizon-mod-content.stamp"; + +/// What a content root is built from: this build of the game, the base's +/// manifest and the tables [`build`] rewrites, and every file of every mod +/// in order. Files count by size and modification time, as build tools do. +fn stamp(base: &Path, mods: &[ContentMod]) -> Result { + use sha2::{Digest, Sha256}; + let meta = |path: &Path| -> Result { + let m = fs::metadata(path).map_err(|e| format!("{}: {e}", path.display()))?; + let modified = m + .modified()? + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default(); + Ok(format!( + "{} {} {}", + path.display(), + m.len(), + modified.as_nanos() + )) + }; + let mut parts = vec!["horizon mod content 1".to_owned()]; + parts.push(meta(&std::env::current_exe()?)?); + let base = std::path::absolute(base)?; + parts.push( + String::from_utf8_lossy(&fs::read(base.join("horizon-resources.json"))?).into_owned(), + ); + for name in JSON_TABLES.iter().chain(&NBT_TABLES).chain(&TAG_BUNDLES) { + if base.join(name).exists() { + parts.push(meta(&base.join(name))?); + } + } + for m in mods { + parts.push(m.id.clone()); + let source = m.content.source(); + if source.is_dir() { + for file in &m.content.files { + parts.push(meta(&source.join(file))?); + } + } else { + parts.push(meta(source)?); + } + } + let mut hasher = Sha256::new(); + for part in &parts { + hasher.update((part.len() as u64).to_le_bytes()); + hasher.update(part.as_bytes()); + } + Ok(format!("{:x}", hasher.finalize())) +} + #[cfg(test)] #[path = "mod_content_tests.rs"] mod tests; diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs index 2a040b0fc..c086f8c0c 100644 --- a/crates/horizon-client/src/mod_content_tests.rs +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -502,3 +502,38 @@ fn independent_reloads_read_changed_behaviour_json_and_report_invalid_files() { ); fs::remove_dir_all(&scratch).unwrap(); } + +/// A built root is kept for later starts, and built again when a mod's +/// files change. +#[test] +fn content_root_is_reused_until_a_mod_changes() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-reuse-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let m = ruby_mod(&scratch.join("ruby")); + let cache = scratch.join("cache"); + let root = resource_root(&base, std::slice::from_ref(&m), &cache).unwrap(); + // A rebuild starts from an empty folder: this file tells them apart. + let sentinel = root.join("sentinel"); + fs::write(&sentinel, b"").unwrap(); + assert_eq!( + resource_root(&base, std::slice::from_ref(&m), &cache).unwrap(), + root + ); + assert!(sentinel.exists(), "an unchanged mod rebuilt the root"); + let lang = scratch.join("ruby/assets/ruby/lang/en_us.json"); + fs::write(&lang, r#"{"block.ruby.ruby_block": "Ruby Block"}"#).unwrap(); + let file = fs::File::options().write(true).open(&lang).unwrap(); + file.set_modified(std::time::SystemTime::now() + std::time::Duration::from_secs(5)) + .unwrap(); + resource_root(&base, std::slice::from_ref(&m), &cache).unwrap(); + assert!(!sentinel.exists(), "a changed mod kept the old root"); + assert!( + fs::read_to_string(root.join("assets/ruby/lang/en_us.json")) + .unwrap() + .contains("Ruby Block") + ); + let _ = fs::remove_dir_all(&scratch); +} diff --git a/crates/horizon-mods/src/content.rs b/crates/horizon-mods/src/content.rs index 40b641759..9afe95d0a 100644 --- a/crates/horizon-mods/src/content.rs +++ b/crates/horizon-mods/src/content.rs @@ -192,6 +192,13 @@ impl Content { Ok(content) } + /// Where the files are read from: the mod's folder or its `.hmod`. + pub fn source(&self) -> &Path { + match &self.source { + Source::Dir(path) | Source::Zip(path) => path, + } + } + /// No files: a mod with code only. pub fn is_empty(&self) -> bool { self.files.is_empty() From a2f8952d05484387833d693de8b78a61d7f0697a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 23:13:16 +0200 Subject: [PATCH 11/67] Compression: flate2's zlib-rs backend instead of miniz_oxide Halves the CPU of deflating a chunk column and makes small packets about 20 % cheaper. The compressed bytes change; what they inflate to doesn't. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/Cargo.toml | 2 +- crates/horizon-protocol/Cargo.toml | 2 +- crates/horizon-world/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 29a02553f..ffccb88e8 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -37,7 +37,7 @@ miniz_oxide = "0.8" ab_glyph = "0.2.32" log = "0.4" chrono = { version = "0.4.42", default-features = false, features = ["clock", "std"] } -flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } horizon-theme = { path = "../horizon-theme" } # Opt-in data collection: the game keeps, the launcher sends (docs/telemetry.md). horizon-telemetry = { path = "../horizon-telemetry", default-features = false } diff --git a/crates/horizon-protocol/Cargo.toml b/crates/horizon-protocol/Cargo.toml index fad282080..aa1f60f89 100644 --- a/crates/horizon-protocol/Cargo.toml +++ b/crates/horizon-protocol/Cargo.toml @@ -8,7 +8,7 @@ description = "Horizon's Minecraft Java Edition 26.3 network protocol (version 7 [dependencies] horizon-world = { path = "../horizon-world" } -flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } serde_json = "1.0.151" # The shared secret and RSA padding (OS randomness) and the server hash. getrandom = "0.3" diff --git a/crates/horizon-world/Cargo.toml b/crates/horizon-world/Cargo.toml index 737edd289..3a8621243 100644 --- a/crates/horizon-world/Cargo.toml +++ b/crates/horizon-world/Cargo.toml @@ -13,7 +13,7 @@ serde = { version = "1", features = ["derive"] } sha2 = "0.10" postcard = { version = "=1.1.3", default-features = false, features = ["alloc"] } horizon-core = { path = "../horizon-core" } -flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } lz4_flex = { version = "0.14", default-features = false, features = ["std", "safe-encode", "safe-decode", "checked-decode"] } xxhash-rust = { version = "0.8.19", features = ["xxh32"] } From c4cc7954cecbee7639f11f7a0b21d383311d948d Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:29:59 +0100 Subject: [PATCH 12/67] Portal scans: keep per-chunk section masks across ticks The nether-portal section precheck from #661 answered "does this section's palette hold a portal?" per section, per tick. Keep the answer per resident chunk in `portal_sections`, keyed by a weak handle: an edit or a chunk replacement rebuilds it, and it is dropped when a chunk unloads and cleared when the block facts change, so a portal block always forces the sweep. The entity sweep now finds portal blocks without the per-tick map. This is a scan microbenchmark, not a whole-game claim: steady improves 2.4x to 17x over the fixtures and the run parity-checks the cached and per-tick answers. Rescanning every chunk every tick (cold, reload, edit_all) is slower because a miss reads all 24 sections of a chunk; see docs/portal-scan-benchmark.md. --- .../examples/portal_scan_bench.rs | 263 ++++++++++++++++++ crates/horizon-client/src/game.rs | 1 + .../horizon-client/src/game/nether_portal.rs | 38 +-- crates/horizon-client/src/game/packs.rs | 1 + crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/portal_sections.rs | 123 ++++++++ docs/portal-scan-benchmark.md | 81 ++++++ 7 files changed, 490 insertions(+), 18 deletions(-) create mode 100644 crates/horizon-client/examples/portal_scan_bench.rs create mode 100644 crates/horizon-client/src/portal_sections.rs create mode 100644 docs/portal-scan-benchmark.md diff --git a/crates/horizon-client/examples/portal_scan_bench.rs b/crates/horizon-client/examples/portal_scan_bench.rs new file mode 100644 index 000000000..158b6a440 --- /dev/null +++ b/crates/horizon-client/examples/portal_scan_bench.rs @@ -0,0 +1,263 @@ +//! The `entity_portals` section scan only: `may_hold_portal` over synthetic +//! entity reaches, with the per-tick palette map against the resident cache. +//! Run with prepared client resources: +//! cargo run --profile fast -p horizon-client --example portal_scan_bench -- RESOURCES +use horizon_client::{ + blocks::BlockFacts, + chunk::{Chunk, MIN_Y, SECTIONS, Section}, + fast_hash::FxHashMap, + portal_sections::PortalSections, +}; +use horizon_core::{ + geometry::{Aabb, Vec3}, + position::BlockPos, +}; +use horizon_world::{level::Behaviours, registry::BlockStateId}; +use std::{hint::black_box, path::Path, sync::Arc, time::Instant}; + +/// `Game::swept_portal`'s reach (`Entity.checkInsideBlocks`'s swept box). +fn reach(from: Vec3, to: Vec3, width: f32, height: f32) -> Aabb { + let half = f64::from(width) / 2.0; + let bounds = Aabb::new( + Vec3::new(to.x - half, to.y, to.z - half), + Vec3::new(to.x + half, to.y + f64::from(height), to.z + half), + ) + .deflate(Vec3::new(1.0e-5, 1.0e-5, 1.0e-5)); + bounds + .union_bounds(bounds.move_by(from.subtract(to))) + .inflate(Vec3::new(1.0, 1.0, 1.0)) +} + +fn sections(reach: Aabb) -> (i64, i32, i32, i32, i32, i32, i32) { + let (min, max) = (reach.min_position(), reach.max_position()); + let (min, max) = (BlockPos::containing(min), BlockPos::containing(max)); + let top = SECTIONS as i32 - 1; + let (x0, x1) = (min.x >> 4, max.x >> 4); + let (z0, z1) = (min.z >> 4, max.z >> 4); + let (y0, y1) = ( + ((min.y - MIN_Y) >> 4).clamp(0, top), + ((max.y - MIN_Y) >> 4).clamp(0, top), + ); + let count = (i64::from(x1) - i64::from(x0) + 1) + * (i64::from(y1) - i64::from(y0) + 1) + * (i64::from(z1) - i64::from(z0) + 1); + (count, x0, x1, y0, y1, z0, z1) +} + +/// The original `Game::may_hold_portal`: per-tick `seen`, palette per section. +fn may_hold_portal_baseline( + reach: Aabb, + world: &FxHashMap<(i32, i32), Arc>, + seen: &mut FxHashMap<(i32, i32, i32), bool>, + behaviours: &Behaviours, +) -> bool { + let (count, x0, x1, y0, y1, z0, z1) = sections(reach); + if count > 64 { + return true; + } + (x0..=x1).any(|cx| { + (z0..=z1).any(|cz| { + (y0..=y1).any(|sy| { + *seen.entry((cx, sy, cz)).or_insert_with(|| { + world + .get(&(cx, cz)) + .and_then(|c| c.sections.get(sy as usize)) + .is_some_and(|s| { + s.blocks + .maybe_has(|v| behaviours.portal_axis(BlockStateId(v)).is_some()) + }) + }) + }) + }) + }) +} + +/// The cached `Game::may_hold_portal`: one mask per resident chunk. +fn may_hold_portal_cached( + reach: Aabb, + world: &FxHashMap<(i32, i32), Arc>, + cache: &mut PortalSections, + behaviours: &Behaviours, +) -> bool { + let (count, x0, x1, y0, y1, z0, z1) = sections(reach); + if count > 64 { + return true; + } + let mut y_mask = 0u32; + for sy in y0..=y1 { + y_mask |= 1 << sy; + } + (x0..=x1).any(|cx| { + (z0..=z1).any(|cz| { + world.get(&(cx, cz)).is_some_and(|chunk| { + cache.get((cx, cz), chunk, |v| { + behaviours.portal_axis(BlockStateId(v)).is_some() + }) & y_mask + != 0 + }) + }) + }) +} + +fn main() -> horizon_client::Result<()> { + let root = std::env::args() + .nth(1) + .ok_or("expected prepared client resource directory")?; + let start = Instant::now(); + let registry = horizon_world::level::data::load_registry(Path::new(&root))?; + let (facts, behaviours) = BlockFacts::load_pair(Path::new(&root), ®istry)?; + println!( + "resource_load_ms={:.3}", + start.elapsed().as_secs_f64() * 1000.0 + ); + let len = registry.len() as u32; + let portal: Vec = (0..len) + .filter(|&id| behaviours.portal_axis(BlockStateId(id)).is_some()) + .collect(); + let inactive_states: Vec = (0..len) + .filter(|&id| behaviours.portal_axis(BlockStateId(id)).is_none()) + .collect(); + let _ = &facts; + let portal_state = *portal + .first() + .ok_or("no nether portal block state in the registry")?; + println!("portal_states={}", portal.len()); + + // 500 entities spread over a 17x17 chunk world. Their reaches touch 1-4 + // sections, as an item or a mob standing in or stepping into a portal does. + let grid = 17; + let base = -(grid / 2) * 16; + let entities: Vec<(Vec3, Vec3, f32, f32)> = (0..500) + .map(|i| { + let x = base + (i % grid) * 16 + (i % 5); + let z = base + (i / grid % grid) * 16 + (i % 7); + let y = 64 + (i % 3) * 16; + let from = Vec3::new(f64::from(x), f64::from(y), f64::from(z)); + let to = from.add_vector(Vec3::new(0.1, 0.0, 0.07)); + (from, to, 0.25, 0.25) + }) + .collect(); + let reaches: Vec = entities + .iter() + .map(|&(from, to, w, h)| reach(from, to, w, h)) + .collect(); + + for (name, palette, portals_every) in [ + ("sparse", 1usize, 0usize), + ("dense", 128, 0), + ("global", 300, 0), + ("dense_portal", 128, 5), + ] { + let start = Instant::now(); + let mut world: FxHashMap<(i32, i32), Arc> = FxHashMap::default(); + for czi in 0..grid { + for cxi in 0..grid { + let (cx, cz) = (-(grid / 2) + cxi, -(grid / 2) + czi); + let mut chunk = Chunk::empty(cx, cz); + for (i, section) in chunk.sections.iter_mut().enumerate() { + let values: [u32; 4096] = std::array::from_fn(|j| { + if portals_every > 0 && i % portals_every == 0 && j == 0 { + portal_state + } else { + inactive_states[(j * 7 + i) % palette.min(inactive_states.len())] + } + }); + Arc::make_mut(section).blocks = Section::from_blocks(&values); + } + world.insert((cx, cz), Arc::new(chunk)); + } + } + println!( + "{name} fixture_ms={:.3}", + start.elapsed().as_secs_f64() * 1000.0 + ); + let keys: Vec<_> = world.keys().copied().collect(); + let portal_chunks: Vec<(i32, i32)> = keys + .iter() + .copied() + .filter(|&(cx, cz)| { + world[&(cx, cz)] + .sections + .iter() + .any(|s| s.blocks.maybe_has(|v| v == portal_state)) + }) + .collect(); + println!("{name} portal_chunks={}", portal_chunks.len()); + // The global palette's maybe_has walks the whole ID range, so rescanning + // every chunk each tick (cold/reload/edit_all) is intractable; those rows + // are only meaningful for the palettes whose scans are bounded. + let workloads: &[(&str, usize)] = if name == "global" { + &[("steady", 0), ("edit_1pct", 3)] + } else { + &[ + ("steady", 0), + ("cold", 0), + ("reload", 0), + ("edit_1pct", 3), + ("edit_all", 289), + ] + }; + for &(workload, changed) in workloads { + let mut baseline = Vec::new(); + let mut candidate = Vec::new(); + let mut cache = PortalSections::default(); + let (mut base_hits, mut cand_hits) = (0u64, 0u64); + for sample in 0..8 { + for cached in if sample % 2 == 0 { + [false, true] + } else { + [true, false] + } { + let start = Instant::now(); + let mut result = 0u64; + for tick in 0..300 { + if cached && workload == "cold" { + cache = PortalSections::default(); + } + if cached && workload == "reload" { + cache.clear(); + } + for key in keys.iter().take(changed) { + Arc::make_mut(world.get_mut(key).unwrap()).set( + 0, + MIN_Y, + 0, + inactive_states[tick % inactive_states.len()], + ); + } + let mut seen = FxHashMap::default(); + for reach in &reaches { + let hit = if cached { + may_hold_portal_cached(*reach, &world, &mut cache, &behaviours) + } else { + may_hold_portal_baseline(*reach, &world, &mut seen, &behaviours) + }; + result += hit as u64; + } + } + if cached { + cache = PortalSections::default(); + cand_hits += black_box(result); + } else { + base_hits += black_box(result); + } + if sample > 0 { + let us = start.elapsed().as_secs_f64() * 1e6 / 300.0; + if cached { + candidate.push(us); + } else { + baseline.push(us); + } + } + } + } + assert_eq!( + base_hits, cand_hits, + "{name} {workload}: cached may_hold_portal diverged" + ); + println!("{name} {workload} baseline_us={baseline:.3?} candidate_us={candidate:.3?}"); + } + } + println!("chunk_wrapper_bytes={}", std::mem::size_of::()); + Ok(()) +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f13be4c84..26eaf4470 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1815,6 +1815,7 @@ impl Game<'_> { } for &key in self.tick_chunks.removed() { self.random_tick_sections.remove(&key); + self.portals.sections.remove(&key); self.block_ticks .remove_chunk(horizon_core::position::ChunkPos::new(key.0, key.1)); } diff --git a/crates/horizon-client/src/game/nether_portal.rs b/crates/horizon-client/src/game/nether_portal.rs index 9ff9e8421..abfff5b53 100644 --- a/crates/horizon-client/src/game/nether_portal.rs +++ b/crates/horizon-client/src/game/nether_portal.rs @@ -65,6 +65,8 @@ pub(super) struct Portals { arrivals: Vec, loaded: bool, dirty: bool, + /// Per-chunk portal palette masks, kept across ticks (portal_sections). + pub(super) sections: horizon_client::portal_sections::PortalSections, } fn rotate_y(v: Vec3, degrees: f32) -> Vec3 { @@ -486,13 +488,14 @@ impl Game<'_> { .filter(|m| self.mob_can_use_portal(m)) .map(|m| (m.id, m.position, m.width(), m.height())) .collect(); - let mut portal_sections = horizon_client::fast_hash::FxHashMap::default(); + let mut portal_sections = std::mem::take(&mut self.portals.sections); for (id, to, width, height) in items.into_iter().chain(mobs) { let from = self.portals.before.get(&id).copied().unwrap_or(to); if let Some(pos) = self.swept_portal(from, to, width, height, &mut portal_sections) { inside.push((id, pos)); } } + self.portals.sections = portal_sections; for (id, pos) in inside { self.portals .entities @@ -509,7 +512,7 @@ impl Game<'_> { to: Vec3, width: f32, height: f32, - portal_sections: &mut horizon_client::fast_hash::FxHashMap<(i32, i32, i32), bool>, + portal_sections: &mut horizon_client::portal_sections::PortalSections, ) -> Option { let half = f64::from(width) / 2.0; let bounds = horizon_core::geometry::Aabb::new( @@ -550,12 +553,13 @@ impl Game<'_> { found } /// Whether a section `reach` touches has a nether portal state in its - /// palette: without one, no block there is a portal. Answers are kept - /// per section for the tick; a box over many sections is not checked. + /// palette: without one, no block there is a portal. Palette answers are + /// kept per chunk across ticks (`portal_sections`); a box over many + /// sections is not checked. fn may_hold_portal( &self, reach: horizon_core::geometry::Aabb, - seen: &mut horizon_client::fast_hash::FxHashMap<(i32, i32, i32), bool>, + sections: &mut horizon_client::portal_sections::PortalSections, ) -> bool { use horizon_client::chunk::{MIN_Y, SECTIONS}; let (min, max) = (reach.min_position(), reach.max_position()); @@ -576,23 +580,21 @@ impl Game<'_> { if count > 64 { return true; } + let mut y_mask = 0u32; + for sy in y0..=y1 { + y_mask |= 1 << sy; + } let behaviours = &self.res.behaviours; let chunks = &self.world.chunks; (x0..=x1).any(|cx| { (z0..=z1).any(|cz| { - (y0..=y1).any(|sy| { - *seen.entry((cx, sy, cz)).or_insert_with(|| { - chunks - .get(&(cx, cz)) - .and_then(|c| c.sections.get(sy as usize)) - .is_some_and(|s| { - s.blocks.maybe_has(|v| { - behaviours - .portal_axis(horizon_world::registry::BlockStateId(v)) - .is_some() - }) - }) - }) + chunks.get(&(cx, cz)).is_some_and(|chunk| { + sections.get((cx, cz), chunk, |v| { + behaviours + .portal_axis(horizon_world::registry::BlockStateId(v)) + .is_some() + }) & y_mask + != 0 }) }) }) diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 0b95dd2ad..febafc932 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -154,6 +154,7 @@ impl Game<'_> { }; // Everything loaded: switch over. self.random_tick_sections.clear(); + self.portals.sections.clear(); self.options.resources = root.to_path_buf(); let context = pack.context.clone(); self.pack = pack; diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 45e09a6a3..e652c8598 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -133,6 +133,7 @@ pub mod patrol; pub mod perf_overlay; pub mod pick_item; pub mod poi_sections; +pub mod portal_sections; pub mod pose; pub mod post; pub mod projectiles; diff --git a/crates/horizon-client/src/portal_sections.rs b/crates/horizon-client/src/portal_sections.rs new file mode 100644 index 000000000..f22cd2b83 --- /dev/null +++ b/crates/horizon-client/src/portal_sections.rs @@ -0,0 +1,123 @@ +//! Memoized nether-portal palette eligibility per resident chunk. +//! +//! `Game::find_entity_portals` asks the same sections every tick whether their +//! palette may hold a portal state. The answer only changes when a chunk's +//! blocks change, so it is kept per chunk until the weak handle stops matching. +use crate::{chunk::Chunk, fast_hash::FxHashMap}; +use std::sync::{Arc, Weak}; + +struct Cached { + chunk: Weak, + /// A bit per section, set when that section's palette may hold a portal. + sections: u32, +} + +/// Resident chunks are immutable behind `Arc`; edits use `Arc::make_mut`. +/// A weak reference detects both edits and replacement, without retaining block +/// data or forcing a deep copy on the next edit. It also prevents address reuse. +/// Clear this cache whenever the gameplay facts change (including resource reload). +#[derive(Default)] +pub struct PortalSections { + chunks: FxHashMap<(i32, i32), Cached>, +} + +impl PortalSections { + pub fn clear(&mut self) { + self.chunks.clear(); + } + pub fn remove(&mut self, key: &(i32, i32)) { + self.chunks.remove(key); + } + /// The sections of `chunk` whose palette may hold a portal, as a bitmask in + /// ascending section order (`MIN_Y` at bit 0). `portal` is only called while + /// (re)building an entry, so an unchanged chunk scans its palette once. + pub fn get( + &mut self, + key: (i32, i32), + chunk: &Arc, + mut portal: impl FnMut(u32) -> bool, + ) -> u32 { + const { + assert!(crate::chunk::SECTIONS <= u32::BITS as usize); + } + let cached = self.chunks.entry(key).or_insert_with(|| Cached { + chunk: Weak::new(), + sections: 0, + }); + if cached.chunk.as_ptr() != Arc::as_ptr(chunk) { + let mut mask = 0; + for (i, section) in chunk.sections.iter().enumerate() { + // Preserve maybe_has, including unused entries and global palettes. + if section.blocks.maybe_has(&mut portal) { + mask |= 1 << i; + } + } + cached.sections = mask; + cached.chunk = Arc::downgrade(chunk); + } + cached.sections + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::chunk::{MIN_Y, SECTIONS, Section}; + + #[test] + fn edits_replacements_compaction_and_reload() { + let mut cache = PortalSections::default(); + let mut chunk = Arc::new(Chunk::empty(0, 0)); + assert_eq!(cache.get((0, 0), &chunk, |id| id == 7), 0); + cache.get((0, 0), &chunk, |_| panic!("unchanged chunk rescanned")); + let worker = chunk.clone(); + Arc::make_mut(&mut chunk).set(0, MIN_Y, 0, 7); + assert_eq!(cache.get((0, 0), &chunk, |id| id == 7), 1); + assert_eq!(worker.get(0, MIN_Y, 0), 0); + // Even after the last portal block is removed, its unused palette entry counts. + Arc::make_mut(&mut chunk).set(0, MIN_Y, 0, 0); + assert_eq!(cache.get((0, 0), &chunk, |id| id == 7), 1); + Arc::make_mut(&mut Arc::make_mut(&mut chunk).sections[0]).compact(); + assert_eq!(cache.get((0, 0), &chunk, |id| id == 7), 0); + Arc::make_mut(&mut chunk).sections[SECTIONS - 1] = Arc::new(Section::default()); + cache.clear(); // resource reload, with changed facts for the same state IDs + assert_eq!( + cache.get((0, 0), &chunk, |id| id == 0), + (1 << SECTIONS) - 1, + "a fresh palette is rescanned for every section" + ); + cache.remove(&(0, 0)); + assert!(cache.chunks.is_empty()); + } + + #[test] + fn replacement_at_same_coordinates_and_early_snapshot() { + let mut cache = PortalSections::default(); + let mut chunk = Arc::new(Chunk::empty(-2, 3)); + let before = cache.get((-2, 3), &chunk, |id| id == 1); + Arc::make_mut(&mut chunk).set(0, MIN_Y + 16, 0, 1); + assert_eq!(before, 0); + assert_eq!(cache.get((-2, 3), &chunk, |id| id == 1), 1 << 1); + let weak = Arc::downgrade(&chunk); + drop(chunk); + assert!(weak.upgrade().is_none()); // cache does not retain unloaded block data + let replacement = Arc::new(Chunk::empty(-2, 3)); + assert_eq!(cache.get((-2, 3), &replacement, |id| id == 1), 0); + } + + #[test] + fn global_palette_covers_the_whole_id_range() { + let mut chunk = Chunk::empty(0, 0); + for i in [0, 9, SECTIONS - 1] { + Arc::make_mut(&mut chunk.sections[i]).blocks = + Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u32)); + } + let chunk = Arc::new(chunk); + let mut cache = PortalSections::default(); + // Global maybe_has considers the entire ID range, including absent states. + assert_eq!( + cache.get((0, 0), &chunk, |id| id == 40000), + (1 << 0) | (1 << 9) | (1 << (SECTIONS - 1)) + ); + } +} diff --git a/docs/portal-scan-benchmark.md b/docs/portal-scan-benchmark.md new file mode 100644 index 000000000..b8104ef26 --- /dev/null +++ b/docs/portal-scan-benchmark.md @@ -0,0 +1,81 @@ +# Nether-portal section-scan benchmark + +Run with the pinned prepared client resources: + +```sh +CARGO_BUILD_JOBS=6 cargo run --profile fast -p horizon-client --example portal_scan_bench -- \ + .local/horizon-client/26.3 +``` + +The example keeps the original `Game::may_hold_portal` alongside the resident +cache (`portal_sections`). Both run in the same optimized binary, alternating +measurement order. Each workload has one discarded warmup pair and seven pairs +of 300 ticks. It reports resource loading and fixture construction separately. +This measures the section scan only, not the whole `entity_portals` span, the +sweep that follows a positive answer, or the rest of the tick. + +The fixture is a 17x17 chunk world (289 chunks), each with 24 sections, and 500 +entity reaches spread over it (an item or mob box 0.25 wide and the small move +that produces the swept box). A reach touches 1-4 sections. Four palettes: +sparse (one non-portal state), dense (128 distinct), global (300 distinct, a +global palette whose `maybe_has` walks the whole state range) and dense_portal +(the dense palette with a portal in every fifth section). These are controlled +fixtures, not a claim about typical generated terrain. + +Steady includes the initial cache fill amortized over 300 ticks. Cold recreates +the cache each tick, including allocation and destruction; reload clears it each +tick while retaining capacity. Edit workloads alternate a block value in three +or all 289 chunks before every scan. Timing includes the `Arc::make_mut` move of +the 176-byte chunk wrapper caused by weak cache handles; the baseline holds no +such handles. Both paths keep the original outer reach list. + +The scan is parity-checked in the same run: the cached and per-tick answers must +agree for every reach over every sample, or the example panics. + +Measured on an AMD Ryzen 5 5600X (12 logical CPUs), Linux x86-64, rustc 1.99.0 +(b940084d7), using `fast` (release optimization, LTO off, 16 codegen units). +Resource loading took 705 ms; fixture construction took 66 ms sparse, 360 ms +dense, 392 ms global and 385 ms dense_portal, shared by both implementations. + +Median microseconds per tick (seven paired samples): + +| Palette | Workload | Original | Cached | Speedup | +| --- | --- | ---: | ---: | ---: | +| sparse | steady | 40.058 | 16.836 | 2.38x | +| sparse | cold | 40.203 | 48.619 | 0.83x | +| sparse | reload | 40.828 | 44.552 | 0.92x | +| sparse | edit_1pct | 41.282 | 52.200 | 0.79x | +| sparse | edit_all | 50.119 | 4192.396 | 0.01x | +| dense | steady | 244.610 | 21.869 | 11.19x | +| dense | cold | 292.533 | 1505.691 | 0.19x | +| dense | reload | 250.397 | 1301.608 | 0.19x | +| dense | edit_1pct | 256.522 | 69.132 | 3.71x | +| dense | edit_all | 263.829 | 5904.581 | 0.04x | +| global | steady | 6179.070 | 360.082 | 17.16x | +| global | edit_1pct | 6251.530 | 1565.249 | 3.99x | +| dense_portal | steady | 256.854 | 21.078 | 12.19x | +| dense_portal | cold | 240.498 | 1526.251 | 0.16x | +| dense_portal | reload | 347.879 | 2037.123 | 0.17x | +| dense_portal | edit_1pct | 368.684 | 118.108 | 3.12x | +| dense_portal | edit_all | 282.876 | 7194.112 | 0.04x | + +The global palette's `maybe_has` walks the whole ID range, so rescanning every +chunk each tick (cold, reload, edit_all) is intractable; those rows are not +measured for it. + +Keep: the steady scan improves for every palette (2.4x to 17x), which is the +case that repeats each tick in real play. Under constant edits the cache also +improves the dense and global palettes (edit_1pct 3-4x) because the original +rescans through the per-tick map. Rescanning every chunk every tick (cold, +reload, edit_all) is slower than the original because a miss scans all 24 +sections of a chunk, where the original only touched the sections a reach +overlaps. No whole-game speedup is inferred from this microbenchmark; real +terrain's palette sizes and block-write frequency determine the gain. Lighting +and other chunk changes conservatively invalidate a chunk's mask. + +The cache adds a weak handle and mask per visited resident chunk, retains no +unloaded block data, and is pruned when a chunk unloads (`sync_tick_chunks`) and +cleared on resource reload, like `random_tick_sections`. Editing a cached chunk +can move its 176-byte wrapper (plus Arc bookkeeping) on the first edit after it +is cached; it does not force a deep copy of the block data. The original outer +reach-list allocation remains. From d3673b1509f56cf852e35268843773fc82dd1dc8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:06:18 +0200 Subject: [PATCH 13/67] Carry: write the self-drop loot table a mod registers in code Chipped registers its blocks' loot in code: its jar ships 7 loot tables for about 7,000 blocks, so carried over they dropped nothing, while in the NeoForge oracle every one drops itself. When a declared block has no loot table in the jar and the oracle shows vanilla's dropSelf (one of itself with every tool, nothing only where the state needs a tool it did not get), carry writes that table in 26.3's shape. When only the states with one property value drop it and the rest drop nothing (a door's lower half), the entry gets vanilla's match_block condition on that value, as createDoorTable writes. Chipped: 6,843 tables added. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/carry.rs | 38 ++++++++ crates/horizon-port-pack/src/oracle.rs | 119 +++++++++++++++++++++++ crates/horizon-port-pack/src/pipeline.rs | 3 +- 3 files changed, 159 insertions(+), 1 deletion(-) diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index dfe4d3dbc..75c2d8408 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -25,6 +25,9 @@ pub struct Report { pub unreadable: BTreeMap, pub recipes_upgraded: usize, pub loot_tables_upgraded: usize, + /// Blocks whose jar has no loot table but drop themselves in the oracle: + /// the mod registers that table in code, and carry-over writes it. + pub loot_tables_added: usize, /// Worldgen and structure files (`data//worldgen/`, `structure/`). pub worldgen: usize, /// Recipes left as they were: custom types, NeoForge conditions. @@ -351,6 +354,7 @@ pub fn carry( let mut blockstates: BTreeMap = BTreeMap::new(); let mut item_models = BTreeSet::new(); let mut convention = BTreeSet::new(); + let mut block_loot = BTreeSet::new(); for i in 0..zip.len() { let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; let name = entry.name().to_owned(); @@ -401,6 +405,12 @@ pub fn carry( } } } else if rel.starts_with("loot_table/") && name.starts_with("data/") { + if let Some(b) = rel + .strip_prefix("loot_table/blocks/") + .and_then(|b| b.strip_suffix(".json")) + { + block_loot.insert(b.to_owned()); + } if let Ok(mut table) = serde_json::from_slice::(&bytes) { let before = table.clone(); upgrade_loot(&mut table); @@ -470,6 +480,34 @@ pub fn carry( .push(format!("its item is {}", block.item)); } else { declared.insert(full.clone()); + let drop = (!block_loot.contains(b)) + .then(|| block.drops_itself(full)) + .flatten(); + if let Some(drop) = drop { + let mut entry = json!({"type": "minecraft:item", "name": full}); + if let crate::oracle::SelfDrop::When(property, value) = drop { + entry["condition"] = json!({ + "type": "minecraft:match_block", + "blocks": full, + "state": {property: value} + }); + } + let table = json!({ + "type": "minecraft:block", + "pools": [{ + "condition": {"type": "minecraft:survives_explosion"}, + "entries": [entry], + "rolls": 1 + }], + "random_sequence": format!("{id}:blocks/{b}") + }); + let file = inside.join(format!("data/{id}/loot_table/blocks/{b}.json")); + write( + &file, + &serde_json::to_vec_pretty(&table).unwrap_or_default(), + )?; + report.loot_tables_added += 1; + } } write_decl("block", b, &v)?; report.blocks.insert(b.to_owned(), choice.template); diff --git a/crates/horizon-port-pack/src/oracle.rs b/crates/horizon-port-pack/src/oracle.rs index 1fa9a717c..7e04aa9ef 100644 --- a/crates/horizon-port-pack/src/oracle.rs +++ b/crates/horizon-port-pack/src/oracle.rs @@ -60,6 +60,10 @@ pub struct State { pub sound: Value, pub outline: Value, pub collision: Value, + /// What breaking the state drops, per tool (`hand`, `diamond_pickaxe`, + /// `shears`, `silk_touch_pickaxe`...): item id → count. + #[serde(default)] + pub drops: BTreeMap>, } #[derive(Debug, Deserialize)] @@ -76,6 +80,23 @@ pub struct Item { const VANILLA_PACKAGE: &str = "net.minecraft."; /// A state key with its properties sorted, so keys compare across blocks. +/// Which states of a block drop the block itself. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SelfDrop { + /// Every state: `dropSelf`. + Always, + /// Only the states where this property has this value. + When(String, String), +} + +/// A state key's `property=value` pairs. +fn pairs(key: &str) -> BTreeSet<(String, String)> { + key.split(',') + .filter_map(|p| p.split_once('=')) + .map(|(k, v)| (k.to_owned(), v.to_owned())) + .collect() +} + pub fn state_key(key: &str) -> String { let mut parts: Vec<&str> = key.split(',').filter(|p| !p.is_empty()).collect(); parts.sort_unstable(); @@ -96,6 +117,45 @@ impl Block { .first() .is_some_and(|c| !c.starts_with(VANILLA_PACKAGE)) } + /// Every state drops one `item` (the block's own) with every tool, except + /// where the state needs a tool and gets none: vanilla's `dropSelf`. Or + /// only the states where one property has one value do, and the others + /// drop nothing with any tool: a door's lower half (`createDoorTable`). + /// Mods often register these tables in code, so their jar has no file. + pub fn drops_itself(&self, item: &str) -> Option { + let one = BTreeMap::from([(item.to_owned(), 1)]); + let (mut dropping, mut nothing) = (Vec::new(), Vec::new()); + for (key, state) in &self.states { + if state.drops.is_empty() { + continue; + } + let mut drops = false; + let mut short = false; + for got in state.drops.values() { + if *got == one { + drops = true; + } else if got.is_empty() { + short = true; + } else { + return None; + } + } + match (drops, short) { + (true, true) if !state.requires_tool => return None, + (true, _) => dropping.push(pairs(key)), + (false, _) => nothing.push(pairs(key)), + } + } + let (first, rest) = dropping.split_first()?; + if nothing.is_empty() { + return Some(SelfDrop::Always); + } + first + .iter() + .filter(|pair| rest.iter().all(|s| s.contains(*pair))) + .find(|pair| nothing.iter().all(|s| !s.contains(*pair))) + .map(|(p, v)| SelfDrop::When(p.clone(), v.clone())) + } fn default_state(&self) -> Option<&State> { self.states .get(&self.default) @@ -373,6 +433,65 @@ mod tests { assert_eq!(tile.differences[0], "sounds: no vanilla block has them"); } + #[test] + fn drops_itself_is_vanillas_drop_self() { + let cube = json!([[0, 0, 0, 1, 1, 1]]); + let with = |requires_tool: bool, drops: Value| { + let mut s = state(1.5, "stone", cube.clone()); + s["requires_tool"] = json!(requires_tool); + s["drops"] = drops; + let b: Block = serde_json::from_value(block(&[], json!({}), json!({"": s}))).unwrap(); + b.drops_itself("m:tile") + }; + let me = json!({"m:tile": 1}); + let always = Some(SelfDrop::Always); + assert_eq!(with(false, json!({"hand": me, "shears": me})), always); + // A state that needs a tool drops nothing by hand, as dropSelf does. + assert_eq!( + with(true, json!({"hand": {}, "diamond_pickaxe": me})), + always + ); + assert_eq!( + with(false, json!({"hand": {}, "diamond_pickaxe": me})), + None + ); + // Two of itself (a double slab), or something else, is not dropSelf. + assert_eq!(with(false, json!({"hand": {"m:tile": 2}})), None); + assert_eq!(with(false, json!({"hand": {"m:other": 1}})), None); + assert_eq!(with(false, json!({})), None); + } + + #[test] + fn a_door_drops_from_its_lower_half() { + let cube = json!([[0, 0, 0, 1, 1, 1]]); + let me = json!({"hand": {"m:door": 1}, "shears": {"m:door": 1}}); + let none = json!({"hand": {}, "shears": {}}); + let door = |upper: &Value| { + let mut states = serde_json::Map::new(); + for (key, drops) in [ + ("half=lower,open=false", &me), + ("half=lower,open=true", &me), + ("half=upper,open=false", upper), + ("half=upper,open=true", upper), + ] { + let mut s = state(3.0, "wood", cube.clone()); + s["drops"] = drops.clone(); + states.insert(key.to_owned(), s); + } + let props = json!({"half": ["upper", "lower"], "open": ["true", "false"]}); + let b: Block = + serde_json::from_value(block(&[], props, Value::Object(states))).unwrap(); + b.drops_itself("m:door") + }; + assert_eq!( + door(&none), + Some(SelfDrop::When("half".to_owned(), "lower".to_owned())) + ); + assert_eq!(door(&me), Some(SelfDrop::Always)); + // Something else from the upper half: not a door table. + assert_eq!(door(&json!({"hand": {"m:other": 1}})), None); + } + #[test] fn state_keys_sort() { assert_eq!( diff --git a/crates/horizon-port-pack/src/pipeline.rs b/crates/horizon-port-pack/src/pipeline.rs index 80a88ee18..0fe87d181 100644 --- a/crates/horizon-port-pack/src/pipeline.rs +++ b/crates/horizon-port-pack/src/pipeline.rs @@ -158,12 +158,13 @@ pub fn carry( Ok(report) => { done += 1; log(format!( - "{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded, {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside", + "{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded ({} added), {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside", r.key, report.copied, report.recipes_upgraded, report.recipes_kept.len(), report.loot_tables_upgraded, + report.loot_tables_added, report.item_definitions_added, report.blocks.len(), report.blocks_unknown.len(), From 9afb6d76de42be1cb68525490d62599ce7910c89 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:27:29 +0200 Subject: [PATCH 14/67] Carry: structure mods' worldgen in vanilla's types, with the loader's biome tags Structure mods build on libraries whose structure, placement and pool element types are vanilla's with extra rules: Lithostitched's, Moog's Structure Lib's and Repurposed Structures' jigsaw structures, their advanced random spreads, versioned and ocean-bottom single pool elements. Carry rewrites them as vanilla's types; the extra rules (no start in liquid, a minimum distance from the origin) are lost, so a structure may start where the mod would not. Types with no vanilla counterpart (YUNG's, Formations', the nether variants) are left as they are and counted in the carry report. The structures' biomes name the loader's convention tags (#c:is_plains). fetch now also gets the pack's NeoForge jar from NeoForge's Maven repository, checked against its SHA-1, and carry copies the convention biome tags each mod's structures use, with those they include, into the mod. ATM10's 42 carried mods: 1,061 files rewritten, 103 convention biome tags. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/carry.rs | 39 +++- crates/horizon-port-pack/src/fetch.rs | 22 ++ crates/horizon-port-pack/src/lib.rs | 1 + crates/horizon-port-pack/src/pipeline.rs | 2 + crates/horizon-port-pack/src/worldgen.rs | 262 +++++++++++++++++++++++ 5 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-port-pack/src/worldgen.rs diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index 75c2d8408..7b9bc8fee 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -47,6 +47,11 @@ pub struct Report { pub items_unknown: BTreeMap, /// Convention (`c:`) tags the recipes use, filled in from the oracle. pub convention_tags: usize, + /// Structure, placement and pool element types rewritten as vanilla's, + /// and those left as they are. + pub worldgen_types: crate::worldgen::Upgrade, + /// The loader's convention biome tags the structures name, carried. + pub convention_biome_tags: usize, } /// Vanilla blocks by their sorted property names. @@ -333,6 +338,7 @@ pub fn carry( out: &Path, facts: Option<&Facts>, allowed: &dyn Fn(&str) -> bool, + conventions: &BTreeMap, ) -> Result { let id = &info.mod_id; if id.is_empty() @@ -355,6 +361,7 @@ pub fn carry( let mut item_models = BTreeSet::new(); let mut convention = BTreeSet::new(); let mut block_loot = BTreeSet::new(); + let mut biome_conventions = BTreeSet::new(); for i in 0..zip.len() { let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; let name = entry.name().to_owned(); @@ -404,6 +411,24 @@ pub fn carry( report.recipes_kept.push(file.to_owned()); } } + } else if let Some(kind) = rel + .strip_prefix("worldgen/") + .and_then(|r| r.split_once('/')) + .map(|(kind, _)| kind) + .filter(|_| name.starts_with("data/") && name.ends_with(".json")) + { + if let Ok(mut v) = serde_json::from_slice::(&bytes) { + if kind == "structure" { + crate::worldgen::convention_tags(&v["biomes"], &mut biome_conventions); + } + if crate::worldgen::upgrade(kind, &mut v, &mut report.worldgen_types) { + bytes = serde_json::to_vec_pretty(&v).unwrap_or(bytes); + } + } + } else if rel.starts_with("tags/worldgen/biome/") && name.starts_with("data/") { + if let Ok(v) = serde_json::from_slice::(&bytes) { + crate::worldgen::convention_tags(&v, &mut biome_conventions); + } } else if rel.starts_with("loot_table/") && name.starts_with("data/") { if let Some(b) = rel .strip_prefix("loot_table/blocks/") @@ -445,6 +470,18 @@ pub fn carry( report.item_definitions_added += 1; } } + // The loader's convention biome tags the structures and their tags name + // (NeoForge's `c:is_plains`...): the game has no loader, so they come along. + for tag in crate::worldgen::with_included(&biome_conventions, conventions) { + let file = inside.join(format!("data/c/tags/worldgen/biome/{tag}.json")); + if !file.exists() { + write( + &file, + &serde_json::to_vec_pretty(&conventions[&tag]).unwrap_or_default(), + )?; + report.convention_biome_tags += 1; + } + } let decl = inside.join(format!("data/{id}/horizon")); let write_decl = |kind: &str, name: &str, v: &Value| { let text = serde_json::to_vec_pretty(v).unwrap_or_default(); @@ -819,7 +856,7 @@ mod tests { ); assert_eq!(info.census.classes, 1); let out = dir.join("work/mods/gem"); - let report = carry(&jar, &info, &out, None, &|_| true).unwrap(); + let report = carry(&jar, &info, &out, None, &|_| true, &BTreeMap::new()).unwrap(); assert_eq!(report.copied, 13); let unreadable: Vec<&str> = report.unreadable.keys().map(String::as_str).collect(); assert_eq!( diff --git a/crates/horizon-port-pack/src/fetch.rs b/crates/horizon-port-pack/src/fetch.rs index 011e99e1f..f8f7b2dc7 100644 --- a/crates/horizon-port-pack/src/fetch.rs +++ b/crates/horizon-port-pack/src/fetch.rs @@ -139,6 +139,28 @@ pub fn fetch( } fetched.files.push(o); } + // The loader's own data (its convention tags), from its own repository. + if let Some((dest, url)) = crate::worldgen::loader_jar(work, &pack.loader, &pack.loader_version) + .filter(|(dest, _)| !dest.exists()) + { + let sha1 = ureq::get(&format!("{url}.sha1")) + .set("User-Agent", modrinth::AGENT) + .call() + .ok() + .and_then(|r| r.into_string().ok()) + .map(|s| s.trim().to_owned()); + let made = dest.parent().map(fs::create_dir_all); + match made + .unwrap_or(Ok(())) + .map_err(|e| e.to_string()) + .and_then(|()| download(&url, sha1.as_deref(), &dest)) + { + Ok(()) => log(format!("loader: {}", dest.display())), + Err(e) => log(format!( + "loader: {e} (structures keep only vanilla biome tags)" + )), + } + } write_json(&work.join("fetch.json"), &fetched)?; log(format!( "jars: {} downloaded, {} copied, {} already here, {} need a CurseForge key, {} not found, {} failed", diff --git a/crates/horizon-port-pack/src/lib.rs b/crates/horizon-port-pack/src/lib.rs index ecd61956c..0e678fb9a 100644 --- a/crates/horizon-port-pack/src/lib.rs +++ b/crates/horizon-port-pack/src/lib.rs @@ -15,3 +15,4 @@ pub mod modrinth; pub mod oracle; pub mod pipeline; pub mod report; +pub mod worldgen; diff --git a/crates/horizon-port-pack/src/pipeline.rs b/crates/horizon-port-pack/src/pipeline.rs index 0fe87d181..3afe5b3a3 100644 --- a/crates/horizon-port-pack/src/pipeline.rs +++ b/crates/horizon-port-pack/src/pipeline.rs @@ -119,6 +119,7 @@ pub fn carry( .ok() }); let allowed = |b: &str| vanilla.as_ref().is_none_or(|v| v.contains(b)); + let conventions = crate::worldgen::loader_biome_tags(work); let mut done = 0; for r in &records { let chosen = if only.is_empty() { @@ -154,6 +155,7 @@ pub fn carry( &work.join("mods").join(&r.key), facts.as_ref().filter(seen), &allowed, + &conventions, ) { Ok(report) => { done += 1; diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs new file mode 100644 index 000000000..c44cc1108 --- /dev/null +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -0,0 +1,262 @@ +//! Structure mods' worldgen, carried over: the structure, placement and pool +//! element types of the common structure libraries rewritten as vanilla's, +//! and the loader's convention biome tags (`c:`) the structures name. +//! +//! The libraries' types add rules on top of vanilla's (a structure that may +//! not start in liquid, a minimum distance from the world origin, a count +//! cap per piece). Mapped to vanilla, those rules are lost: the structure +//! keeps its pieces and its spacing but can start where the mod would not. +//! Each rewrite is counted in the carry report, and every type left as it is +//! is listed, so the coverage report can say what generates and how closely. +use serde_json::Value; +use std::{ + collections::{BTreeMap, BTreeSet}, + io::Read, + path::{Path, PathBuf}, +}; + +/// Structure types that are vanilla's jigsaw with extra start rules. +const JIGSAWS: [&str; 3] = [ + "lithostitched:jigsaw", + "moogs_structures:moogs_structures_generic_jigsaw_structure", + "repurposed_structures:generic_jigsaw_structure", +]; +/// Placements that are vanilla's random spread with extra rules. +const SPREADS: [&str; 2] = [ + "moogs_structures:advanced_random_spread", + "repurposed_structures:advanced_random_spread", +]; +/// Pool elements that are vanilla's with extra fields: (mod type, vanilla type). +const ELEMENTS: [(&str, &str); 3] = [ + ( + "moogs_structures:versioned_single_pool_element", + "minecraft:single_pool_element", + ), + ( + "repurposed_structures:legacy_ocean_bottom_single_pool_element", + "minecraft:legacy_single_pool_element", + ), + ("empty_pool_element", "minecraft:empty_pool_element"), +]; + +/// What carry-over did to a mod's worldgen files. +#[derive(Debug, Default, serde::Serialize)] +pub struct Upgrade { + /// Files rewritten to vanilla types. + pub mapped: usize, + /// Types left as they are (Horizon skips what it cannot run), by type. + pub unsupported: BTreeMap, +} + +/// Rewrites one `data//worldgen//...` file in place; whether it +/// changed. +pub fn upgrade(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool { + let mut unsupported = |t: &str| *report.unsupported.entry(t.to_owned()).or_default() += 1; + let changed = match kind { + "structure" => match v["type"].as_str() { + Some(t) if JIGSAWS.contains(&t) => { + v["type"] = "minecraft:jigsaw".into(); + true + } + Some(t) if !t.starts_with("minecraft:") => { + unsupported(t); + false + } + _ => false, + }, + "structure_set" => match v["placement"]["type"].as_str() { + Some(t) if SPREADS.contains(&t) => { + v["placement"]["type"] = "minecraft:random_spread".into(); + true + } + Some(t) if !t.starts_with("minecraft:") => { + unsupported(t); + false + } + _ => false, + }, + "template_pool" => { + let mut changed = false; + let mut elements: Vec<&mut Value> = Vec::new(); + for e in v["elements"].as_array_mut().into_iter().flatten() { + let element = &mut e["element"]; + // A list element holds elements of its own. + if element.get("elements").is_some_and(Value::is_array) { + elements.extend(element["elements"].as_array_mut().into_iter().flatten()); + } else { + elements.push(element); + } + } + for element in elements { + let Some(t) = element["element_type"].as_str() else { + continue; + }; + if let Some((_, to)) = ELEMENTS.iter().find(|(from, _)| *from == t) { + element["element_type"] = (*to).into(); + changed = true; + } else if !t.starts_with("minecraft:") { + unsupported(t); + } + } + changed + } + _ => false, + }; + report.mapped += usize::from(changed); + changed +} + +/// The `#c:` biome tags `v` names. +pub fn convention_tags(v: &Value, out: &mut BTreeSet) { + match v { + Value::String(s) => { + if let Some(tag) = s.strip_prefix("#c:") { + out.insert(tag.to_owned()); + } + } + Value::Array(a) => a.iter().for_each(|x| convention_tags(x, out)), + Value::Object(o) => o.values().for_each(|x| convention_tags(x, out)), + _ => {} + } +} + +/// The loader's own convention biome tags (`data/c/tags/worldgen/biome/`), +/// by path, read from the loader jar `fetch` keeps in `/loader/`. +pub fn loader_biome_tags(work: &Path) -> BTreeMap { + let mut out = BTreeMap::new(); + let Ok(dir) = std::fs::read_dir(work.join("loader")) else { + return out; + }; + for jar in dir.flatten().map(|e| e.path()) { + let Some(mut zip) = std::fs::File::open(&jar) + .ok() + .and_then(|f| zip::ZipArchive::new(f).ok()) + else { + continue; + }; + for i in 0..zip.len() { + let Ok(mut entry) = zip.by_index(i) else { + continue; + }; + let Some(path) = entry + .name() + .strip_prefix("data/c/tags/worldgen/biome/") + .and_then(|p| p.strip_suffix(".json")) + .map(str::to_owned) + else { + continue; + }; + let mut text = String::new(); + if entry.read_to_string(&mut text).is_ok() { + if let Ok(v) = serde_json::from_str(&text) { + out.insert(path, v); + } + } + } + } + out +} + +/// `tags` and the convention tags they include, in `conventions`. +pub fn with_included( + tags: &BTreeSet, + conventions: &BTreeMap, +) -> BTreeSet { + let mut all = BTreeSet::new(); + let mut todo: Vec = tags.iter().cloned().collect(); + while let Some(tag) = todo.pop() { + if !conventions.contains_key(&tag) || !all.insert(tag.clone()) { + continue; + } + let mut more = BTreeSet::new(); + convention_tags(&conventions[&tag], &mut more); + todo.extend(more); + } + all +} + +/// Where `fetch` keeps the pack's loader jar, and where it comes from: +/// NeoForge's own Maven repository. Only NeoForge packs need it today. +pub fn loader_jar(work: &Path, loader: &str, version: &str) -> Option<(PathBuf, String)> { + (loader == "neoforge" && !version.is_empty()).then(|| { + ( + work.join("loader").join(format!("neoforge-{version}-universal.jar")), + format!( + "https://maven.neoforged.net/releases/net/neoforged/neoforge/{version}/neoforge-{version}-universal.jar" + ), + ) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn maps_library_types_to_vanillas() { + let mut report = Upgrade::default(); + let mut s = json!({"type": "lithostitched:jigsaw", "start_pool": "a:b"}); + assert!(upgrade("structure", &mut s, &mut report)); + assert_eq!(s["type"], "minecraft:jigsaw"); + let mut nether = + json!({"type": "moogs_structures:moogs_structures_generic_nether_jigsaw_structure"}); + assert!(!upgrade("structure", &mut nether, &mut report)); + let mut set = json!({"placement": {"type": "moogs_structures:advanced_random_spread", + "spacing": 10, "separation": 5, "min_distance_from_world_origin": 250}}); + assert!(upgrade("structure_set", &mut set, &mut report)); + assert_eq!(set["placement"]["type"], "minecraft:random_spread"); + assert_eq!(set["placement"]["spacing"], 10); + let mut pool = json!({"elements": [ + {"weight": 1, "element": {"element_type": "moogs_structures:versioned_single_pool_element", "location": "m:x"}}, + {"weight": 1, "element": {"element_type": "minecraft:list_pool_element", "elements": [ + {"element_type": "empty_pool_element"}]}}, + {"weight": 1, "element": {"element_type": "yungsapi:yung_single_element"}} + ]}); + assert!(upgrade("template_pool", &mut pool, &mut report)); + assert_eq!( + pool["elements"][0]["element"]["element_type"], + "minecraft:single_pool_element" + ); + assert_eq!( + pool["elements"][1]["element"]["elements"][0]["element_type"], + "minecraft:empty_pool_element" + ); + assert_eq!(report.mapped, 3); + assert_eq!( + report.unsupported, + BTreeMap::from([ + ( + "moogs_structures:moogs_structures_generic_nether_jigsaw_structure".into(), + 1 + ), + ("yungsapi:yung_single_element".into(), 1), + ]) + ); + } + + #[test] + fn convention_tags_include_theirs() { + let conventions = BTreeMap::from([ + ( + "is_hot".to_owned(), + json!({"values": ["#c:is_desert", "#minecraft:is_badlands"]}), + ), + ( + "is_desert".to_owned(), + json!({"values": ["minecraft:desert"]}), + ), + ("is_cold".to_owned(), json!({"values": []})), + ]); + let mut used = BTreeSet::new(); + convention_tags( + &json!({"biomes": ["#c:is_hot", "#c:unknown", "#minecraft:x"]}), + &mut used, + ); + assert_eq!(used, BTreeSet::from(["is_hot".into(), "unknown".into()])); + assert_eq!( + with_included(&used, &conventions), + BTreeSet::from(["is_hot".into(), "is_desert".into()]) + ); + } +} From 56a251eb130f76f6f9638f6b9dd09f1fe19ada80 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:38:35 +0100 Subject: [PATCH 15/67] Paletted container access: one palette probe per write, dispatched unpack BlockSection::set resolved the old cell through get and then probed the section palette three times per non-trivial write: keying the old state by id, testing the incoming state and finding it. The slot at the written index already is the old state's id, so read it straight from storage and resolve only the incoming state, with a single map probe. Palette reuse, free slots and growth are unchanged, so palette, counts, free list and NBT are identical. PalettedContainer::unpack and unpack_u16 matched the palette variant inside the per-cell loop; dispatch Single/List/Global once before it. Measured against copies of the pre-optimization code (ignored tests): BlockSection::set 12.73 -> 11.37 ns/write (1.12x), unpack list 1.956 -> 1.860 us/call (1.05x), global 2.255 -> 2.172 (1.04x). See docs/performance/paletted-container-access-results.md. set_matches_baseline_under_random_edits drives both implementations over the same 50k-edit streams for 1/2/16/17/24/64 distinct states, with periodic whole-section collapses, and asserts equal results, bits, palette, counts, free list and NBT. --- crates/horizon-core/src/paletted.rs | 112 ++++++- crates/horizon-world/src/chunk.rs | 301 ++++++++++++++++-- .../paletted-container-access-results.md | 62 ++++ 3 files changed, 433 insertions(+), 42 deletions(-) create mode 100644 docs/performance/paletted-container-access-results.md diff --git a/crates/horizon-core/src/paletted.rs b/crates/horizon-core/src/paletted.rs index f162404f5..5c0ec374c 100644 --- a/crates/horizon-core/src/paletted.rs +++ b/crates/horizon-core/src/paletted.rs @@ -391,17 +391,24 @@ impl PalettedContainer { assert_eq!(out.len(), self.strategy.entries()); match &self.palette { Palette::Single(value) => out.fill(*value), - palette => { + Palette::List(list) => { let bits = usize::from(self.bits); let per = 64 / bits; let m = mask(self.bits); for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { for (slot, cell) in chunk.iter_mut().enumerate() { - let id = ((word >> (slot * bits)) & m) as u32; - *cell = match palette { - Palette::List(list) => list[id as usize], - _ => id, - }; + let id = ((word >> (slot * bits)) & m) as usize; + *cell = list[id]; + } + } + } + Palette::Global => { + let bits = usize::from(self.bits); + let per = 64 / bits; + let m = mask(self.bits); + for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { + for (slot, cell) in chunk.iter_mut().enumerate() { + *cell = ((word >> (slot * bits)) & m) as u32; } } } @@ -412,17 +419,24 @@ impl PalettedContainer { debug_assert_eq!(self.strategy.entries(), 4096); match &self.palette { Palette::Single(value) => out.fill(*value as u16), - palette => { + Palette::List(list) => { let bits = usize::from(self.bits); let per = 64 / bits; let m = mask(self.bits); for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { for (slot, cell) in chunk.iter_mut().enumerate() { let id = ((word >> (slot * bits)) & m) as usize; - *cell = match palette { - Palette::List(list) => list[id] as u16, - _ => id as u16, - }; + *cell = list[id] as u16; + } + } + } + Palette::Global => { + let bits = usize::from(self.bits); + let per = 64 / bits; + let m = mask(self.bits); + for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { + for (slot, cell) in chunk.iter_mut().enumerate() { + *cell = ((word >> (slot * bits)) & m) as u16; } } } @@ -738,4 +752,80 @@ mod tests { } } } + + /// The pre-optimization `unpack`, kept to compare against in the benchmark. + fn baseline_unpack(c: &PalettedContainer, out: &mut [u32]) { + match &c.palette { + Palette::Single(value) => out.fill(*value), + palette => { + let bits = usize::from(c.bits); + let per = 64 / bits; + let m = mask(c.bits); + for (chunk, &word) in out.chunks_mut(per).zip(&c.words) { + for (slot, cell) in chunk.iter_mut().enumerate() { + let id = ((word >> (slot * bits)) & m) as u32; + *cell = match palette { + Palette::List(list) => list[id as usize], + _ => id, + }; + } + } + } + } + } + + /// `unpack` against the pre-optimization implementation, for a list palette + /// and the global palette. The result is checked before timing; 25 measured + /// rounds alternate order and report the median and best microsecond-per-call. + #[test] + #[ignore = "microbenchmark; run with --ignored --nocapture"] + fn benchmark_unpack() { + use std::{hint::black_box, time::Instant}; + let values: Vec = (0..4096).map(|i| (i % 12) as u32).collect(); + let list = PalettedContainer::from_values(BLOCKS, &values); + let global = PalettedContainer::from_values( + BLOCKS, + &(0..4096).map(|i| (i * 3) as u32).collect::>(), + ); + for (name, container) in [("list", &list), ("global", &global)] { + let mut expected = vec![0u32; 4096]; + baseline_unpack(container, &mut expected); + let mut out = vec![0u32; 4096]; + container.unpack(&mut out); + assert_eq!(expected, out, "unpack {name} differs"); + let mut times = [Vec::new(), Vec::new()]; + for round in 0..25 { + for optimized in [round % 2 == 0, round % 2 != 0] { + let started = Instant::now(); + for _ in 0..2000 { + if optimized { + container.unpack(black_box(&mut out)); + } else { + baseline_unpack(black_box(container), black_box(&mut out)); + } + } + if round > 0 { + times[usize::from(optimized)] + .push(started.elapsed().as_secs_f64() * 1e6 / 2000.0); + } + } + } + for values in &mut times { + values.sort_by(f64::total_cmp); + } + let median = |v: &[f64]| v[v.len() / 2]; + println!( + "unpack {name} median: baseline {:.3} us optimized {:.3} us speedup {:.3}x", + median(×[0]), + median(×[1]), + median(×[0]) / median(×[1]) + ); + println!( + "unpack {name} best: baseline {:.3} us optimized {:.3} us speedup {:.3}x", + times[0][0], + times[1][0], + times[0][0] / times[1][0] + ); + } + } } diff --git a/crates/horizon-world/src/chunk.rs b/crates/horizon-world/src/chunk.rs index f4341c92f..b7650a3d3 100644 --- a/crates/horizon-world/src/chunk.rs +++ b/crates/horizon-world/src/chunk.rs @@ -446,44 +446,52 @@ impl<'r> BlockSection<'r> { Ok(self.palette[id as usize]) } pub fn set(&mut self, index: usize, state: BlockStateId) -> io::Result { - let old = self.get(index)?; + if index >= 4096 { + return Err(invalid("section index out of bounds")); + } if state.0 as usize >= self.registry.len() { return Err(invalid("state ID outside registry")); } + // The slot at `index` is the old state's id, so no palette lookup is + // needed for it; only the new state has to be resolved. + let old_id = self.storage.as_ref().map_or(0, |s| s.get(index).unwrap()); + let old = self.palette[old_id as usize]; if old == state { return Ok(old); } - let old_id = self.local_ids[&old]; - if self.counts[old_id as usize] == 1 && !self.local_ids.contains_key(&state) { - // Replacing the last occurrence can reuse its slot without repacking. - self.palette[old_id as usize] = state; - self.local_ids.remove(&old); - self.local_ids.insert(state, old_id); - return Ok(old); - } - let id = if let Some(id) = self.local_ids.get(&state) { - *id - } else if let Some(id) = self.free.pop() { - self.palette[id as usize] = state; - self.local_ids.insert(state, id); - id - } else { - let id = self.palette.len() as u32; - let next_bits = bits_for_palette(self.palette.len() + 1, 4); - if next_bits != self.bits() { - let mut storage = - PackedStorage::new(next_bits, 4096).map_err(|e| invalid(e.to_string()))?; - if let Some(old) = &self.storage { - for i in 0..4096 { - storage.set(i, old.get(i).unwrap()).unwrap(); - } - } - self.storage = Some(storage); + let id = match self.local_ids.get(&state) { + Some(&id) => id, + None if self.counts[old_id as usize] == 1 => { + // Replacing the last occurrence can reuse its slot without repacking. + self.palette[old_id as usize] = state; + self.local_ids.remove(&old); + self.local_ids.insert(state, old_id); + return Ok(old); + } + None => { + if let Some(id) = self.free.pop() { + self.palette[id as usize] = state; + self.local_ids.insert(state, id); + id + } else { + let id = self.palette.len() as u32; + let next_bits = bits_for_palette(self.palette.len() + 1, 4); + if next_bits != self.bits() { + let mut storage = PackedStorage::new(next_bits, 4096) + .map_err(|e| invalid(e.to_string()))?; + if let Some(old) = &self.storage { + for i in 0..4096 { + storage.set(i, old.get(i).unwrap()).unwrap(); + } + } + self.storage = Some(storage); + } + self.palette.push(state); + self.counts.push(0); + self.local_ids.insert(state, id); + id + } } - self.palette.push(state); - self.counts.push(0); - self.local_ids.insert(state, id); - id }; self.storage .as_mut() @@ -585,4 +593,235 @@ mod tests { map.insert(key("data"), Tag::LongArray(vec![-1; 256])); assert!(PalettedContainer::from_nbt(&tag, 4096, 4).is_err()); } + + /// The pre-optimization `get`, kept to compare against in the benchmark. + fn baseline_get(section: &BlockSection<'_>, index: usize) -> io::Result { + if index >= 4096 { + return Err(invalid("section index out of bounds")); + } + let id = section + .storage + .as_ref() + .map_or(0, |s| s.get(index).unwrap()); + Ok(section.palette[id as usize]) + } + /// The pre-optimization `set`, kept to compare against in the benchmark. + fn baseline_set( + section: &mut BlockSection<'_>, + index: usize, + state: BlockStateId, + ) -> io::Result { + let old = baseline_get(section, index)?; + if state.0 as usize >= section.registry.len() { + return Err(invalid("state ID outside registry")); + } + if old == state { + return Ok(old); + } + let old_id = section.local_ids[&old]; + if section.counts[old_id as usize] == 1 && !section.local_ids.contains_key(&state) { + section.palette[old_id as usize] = state; + section.local_ids.remove(&old); + section.local_ids.insert(state, old_id); + return Ok(old); + } + let id = if let Some(id) = section.local_ids.get(&state) { + *id + } else if let Some(id) = section.free.pop() { + section.palette[id as usize] = state; + section.local_ids.insert(state, id); + id + } else { + let id = section.palette.len() as u32; + let next_bits = bits_for_palette(section.palette.len() + 1, 4); + if next_bits != section.bits() { + let mut storage = + PackedStorage::new(next_bits, 4096).map_err(|e| invalid(e.to_string()))?; + if let Some(old) = §ion.storage { + for i in 0..4096 { + storage.set(i, old.get(i).unwrap()).unwrap(); + } + } + section.storage = Some(storage); + } + section.palette.push(state); + section.counts.push(0); + section.local_ids.insert(state, id); + id + }; + section + .storage + .as_mut() + .unwrap() + .set(index, id) + .map_err(|e| invalid(e.to_string()))?; + section.counts[old_id as usize] -= 1; + section.counts[id as usize] += 1; + if section.counts[old_id as usize] == 0 { + section.local_ids.remove(&old); + section.free.push(old_id); + } + if section.counts[id as usize] == 4096 { + section.palette = vec![state]; + section.local_ids = local_ids(state); + section.counts = vec![4096]; + section.free.clear(); + section.storage = None; + } + Ok(old) + } + + fn benchmark_registry(states: usize) -> BlockRegistry { + let list: Vec = (0..states) + .map(|i| { + Tag::compound([("id", Tag::String(format!("minecraft:b{i}").as_str().into()))]) + }) + .collect(); + let defaults: BTreeMap<_, _> = (0..states) + .map(|i| { + ( + JavaString::from(format!("minecraft:b{i}").as_str()), + Tag::Int(i as i32), + ) + }) + .collect(); + let tag = Tag::compound([ + ("DataVersion", Tag::Int(crate::DATA_VERSION)), + ("states", Tag::List(list)), + ("defaults", Tag::Compound(defaults)), + ]); + BlockRegistry::from_nbt(&tag).unwrap() + } + + /// The optimized `set`/`get` must agree with the pre-optimization ones on + /// every field, across growth, collapse and reclaimed palette slots. + #[test] + fn set_matches_baseline_under_random_edits() { + let registry = benchmark_registry(64); + for &(distinct, seed) in &[(1u64, 1u64), (2, 2), (16, 3), (17, 4), (24, 5), (64, 6)] { + let mut base = BlockSection::uniform(BlockStateId(0), ®istry).unwrap(); + let mut fast = BlockSection::uniform(BlockStateId(0), ®istry).unwrap(); + let mut rng = seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1; + let mut next = || { + rng ^= rng << 13; + rng ^= rng >> 7; + rng ^= rng << 17; + rng + }; + for step in 0..50_000u32 { + if step % 5_000 == 4_999 { + let value = (next() % distinct) as u32; + for index in 0..4096 { + assert_eq!( + baseline_set(&mut base, index, BlockStateId(value)).unwrap(), + fast.set(index, BlockStateId(value)).unwrap() + ); + } + } else { + let index = (next() % 4096) as usize; + let value = (next() % distinct) as u32; + assert_eq!( + baseline_set(&mut base, index, BlockStateId(value)).unwrap(), + fast.set(index, BlockStateId(value)).unwrap() + ); + } + assert_eq!(base.bits(), fast.bits()); + } + assert_eq!(base.palette, fast.palette); + assert_eq!(base.counts, fast.counts); + assert_eq!(base.free, fast.free); + assert_eq!(base.local_ids, fast.local_ids); + for index in 0..4096 { + assert_eq!( + baseline_get(&base, index).unwrap(), + fast.get(index).unwrap(), + "distinct {distinct} index {index}" + ); + } + assert_eq!(base.to_nbt().unwrap(), fast.to_nbt().unwrap()); + } + } + + /// `set` and `get` against the pre-optimization implementations, on the same + /// edit stream. The result is checked before timing; 51 measured rounds + /// alternate order and report the median and best nanosecond-per-operation. + #[test] + #[ignore = "microbenchmark; run with --ignored --nocapture"] + fn benchmark_section_get_set() { + use std::{hint::black_box, time::Instant}; + const OPS: usize = 50_000; + const ROUNDS: u32 = 51; + let registry = benchmark_registry(32); + let mut rng = 0x1234_5678_9abc_def1u64; + let mut next = || { + rng ^= rng << 13; + rng ^= rng >> 7; + rng ^= rng << 17; + rng + }; + let ops: Vec<(usize, u32)> = (0..OPS) + .map(|_| ((next() % 4096) as usize, (next() % 24) as u32)) + .collect(); + let make = || BlockSection::uniform(BlockStateId(0), ®istry).unwrap(); + + let mut base = make(); + let mut fast = make(); + for &(index, value) in &ops { + assert_eq!( + baseline_set(&mut base, index, BlockStateId(value)).unwrap(), + fast.set(index, BlockStateId(value)).unwrap() + ); + } + assert_eq!(base.bits(), fast.bits()); + assert_eq!(base.palette, fast.palette); + assert_eq!(base.counts, fast.counts); + assert_eq!(base.free, fast.free); + assert_eq!(base.to_nbt().unwrap(), fast.to_nbt().unwrap()); + for i in 0..4096 { + assert_eq!(baseline_get(&base, i).unwrap(), fast.get(i).unwrap()); + } + + let mut times = [Vec::new(), Vec::new()]; + for round in 0..ROUNDS { + for optimized in [round % 2 == 0, round % 2 != 0] { + let mut section = make(); + let started = Instant::now(); + for &(index, value) in &ops { + if optimized { + section + .set(black_box(index), black_box(BlockStateId(value))) + .unwrap(); + } else { + baseline_set( + &mut section, + black_box(index), + black_box(BlockStateId(value)), + ) + .unwrap(); + } + } + black_box(§ion); + if round > 0 { + times[usize::from(optimized)] + .push(started.elapsed().as_secs_f64() * 1e9 / OPS as f64); + } + } + } + for values in &mut times { + values.sort_by(f64::total_cmp); + } + let median = |v: &[f64]| v[v.len() / 2]; + println!( + "section set median: baseline {:.3} ns/op optimized {:.3} ns/op speedup {:.3}x", + median(×[0]), + median(×[1]), + median(×[0]) / median(×[1]) + ); + println!( + "section set best: baseline {:.3} ns/op optimized {:.3} ns/op speedup {:.3}x", + times[0][0], + times[1][0], + times[0][0] / times[1][0] + ); + } } diff --git a/docs/performance/paletted-container-access-results.md b/docs/performance/paletted-container-access-results.md new file mode 100644 index 000000000..233e320ab --- /dev/null +++ b/docs/performance/paletted-container-access-results.md @@ -0,0 +1,62 @@ +# Paletted container access + +The `unpack`/`get` cell reader and `BlockSection::get`/`set` are hot in the +chunk workers: light and mesh workers unpack a section's 4,096 cells before +propagating or meshing, and world-generation workers write one block at a time +through `BlockSection::set`. + +Two redundant lookups were removed from the write path, and the palette dispatch +was hoisted in the bulk readers. + +## Changes + +`BlockSection::set` (`crates/horizon-world/src/chunk.rs`) previously resolved the +old cell through `get` and then probed the section's palette map three times for +one non-trivial write: once to key the old state by id (`local_ids[&old]`), once +to test the incoming state (`contains_key`), and once to find it (`get`). The +slot at the written index already *is* the old state's id, so the old state is +now read straight from storage, and the incoming state is resolved with a single +map probe. The palette-reuse, free-slot and growth paths are unchanged, so the +encoded palette, counts, free list and NBT output stay identical. + +`PalettedContainer::unpack` and `unpack_u16` +(`crates/horizon-core/src/paletted.rs`) matched on the palette variant inside the +per-cell loop. `Single`, `List` and `Global` are now dispatched once before the +loop, leaving each arm a branch-free indexed loop. + +No allocation, layout or wire format changes. + +## Reproduce + +```sh +cargo test -p horizon-world --lib --profile fast benchmark_section_get_set -- --ignored --nocapture +cargo test -p horizon-core --lib --profile fast benchmark_unpack -- --ignored --nocapture +``` + +The ignored tests compare the new code against copies of the pre-optimization +implementations, kept in the test modules (`baseline_get`/`baseline_set`, +`baseline_unpack`). They check every resulting palette, count, free list, cell +and NBT before timing. Rounds alternate baseline/optimized order with `black_box` +and report the median and best nanosecond- or microsecond-per-operation; the best +round is the estimate of uninterrupted CPU time on a contended machine. + +`benchmark_section_get_set` uses a 32-state registry and 50,000 random writes per +round over a section whose palette grows through the 4- and 5-bit widths (24 +distinct states); 51 rounds. `benchmark_unpack` fills a 4,096-entry list palette +(12 states) and the global palette, 2,000 calls per round, 25 rounds. + +AMD Ryzen 5 5600X, Linux x86_64, rustc 1.99.0, optimized `fast` profile, +nonincremental, no LTO: + +| Operation | Original main | New | Speedup | +| --- | ---: | ---: | ---: | +| `BlockSection::set` | 12.73 ns/write | 11.37 ns/write | 1.12x | +| `unpack`, list palette | 1.956 us/call | 1.860 us/call | 1.05x | +| `unpack`, global palette | 2.255 us/call | 2.172 us/call | 1.04x | + +The distinct-state differential test +`chunk::tests::set_matches_baseline_under_random_edits` drives the optimized +`set` and `get` and the originals over the same 50,000-edit streams for 1, 2, 16, +17, 24 and 64 distinct states, including periodic whole-section collapses that +clear the storage and reclaim palette slots, and asserts equal results, bits, +palette, counts, free list, `local_ids` and NBT throughout. From 90053d15aaceb6eec419a11fb199fac2824bf9d4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 23:45:23 +0200 Subject: [PATCH 16/67] docs/modpacks.md: memory, NeoForge comparison, worldgen and Chipped status The NBT tables stream, the fluid tables reuse the game's facts and the content root is kept between starts (#753, #754, #756). Horizon with ATM10's 40 data-tier mods against the same mods on NeoForge 21.1.259. Worldgen carry-over and what it does not cover yet (#763, #764). Chipped's harness row with its self-drop tables (#762). The overlay cache line said it was cached; it is now. Co-Authored-By: Claude Opus 5.5 --- docs/modpacks.md | 108 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 94 insertions(+), 14 deletions(-) diff --git a/docs/modpacks.md b/docs/modpacks.md index 23b560633..adf9a5318 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -236,7 +236,15 @@ How the game loads them, without touching vanilla's path: so the same mod set gives the same ids. Saves store block names, so ids never reach the disk. - Loaders that read only `data/minecraft/` (recipes, loot, tags) read every namespace, as vanilla does. With no mods there is only `minecraft`, so nothing changes. -- The overlay is cached like the resource-pack overlay and rebuilt when a mod file changes. +- The overlay is built on the first start with a set of mods and kept for later starts. A stamp + (`horizon-mod-content.stamp`) hashes the game executable, the base tables and every mod file's + size and mtime. Adding, removing or editing a mod, or a new build of Horizon, rebuilds it. For + ATM10's 40 data-tier mods the build takes about a minute of the first start on a busy machine. +- **Worldgen.** Mods' structures, structure sets, template pools, processor lists, configured and + placed features join the `worldgen/*.json` bundles. Their structure templates go under + `worldgen/structure//`. Their biome tags are merged into `worldgen/biome_tags.json`, minus + the biomes and tags Horizon does not have. Mod biomes, noise and density functions are not + carried yet. - **Limits.** Chunk sections size their global palette to the registry: 16 bits for vanilla's 35,723 states (the same chunks, saves and packets as before), one more bit each time content mods double it, up to 2^20 states (1,048,576). Past that the content root is refused with the @@ -398,6 +406,40 @@ Mechanical, the same for every mod, and no agent: carries those over. 5. Write `.hmod`. For a data-tier mod, it is now ported. +Two cases the oracle fills in: + +- **Loot registered in code.** Chipped ships 7 loot tables for about 7,000 blocks; the rest + drop themselves through Java code. When a declared block has no table in the jar and the + oracle shows vanilla's drop-self, carry writes that table in 26.3's shape. When only the + states with one property value drop it (a door's lower half), the entry gets vanilla's + `match_block` condition. Chipped: 6,843 tables added. +- **Worldgen carry-over.** Structure libraries give vanilla's types other names. Carry maps + these to vanilla's: + - `lithostitched:jigsaw`, MoogsStructureLib's and Repurposed Structures' generic jigsaws + become `minecraft:jigsaw`; + - their `advanced_random_spread` becomes `minecraft:random_spread`; + - their versioned, legacy and empty pool elements become vanilla's. + + The rules these libraries add on top of vanilla's (no start in liquid, a minimum distance, + caps per piece) are lost, so a mapped structure can start where the mod would not. NeoForge's + convention biome tags (`#c:is_forest`, ...) come from the loader jar + (`/loader/neoforge--universal.jar`, fetched dev-only and SHA-1 checked) and + are written into the carry. Each mod's `carry-report.json` lists what was mapped and the + types left as they are. The game skips the structures it cannot decode. + + On ATM10's 40 data-tier mods: + - 1,061 files were mapped (MVS 413, MNS 210, MSS 157, MES 110, Repurposed Structures 104, + ChoiceTheorem's Overhauled Village 66), and 103 convention biome tags written. + - When Dungeons Arise, Structory and Towns and Towers use vanilla's types already. + - Not generated yet, because their types are Java code: YUNG's API (491 elements and + jigsaws: every "YUNG's Better" mod), Formations (179), the nether jigsaw variants of Moog's + and Repurposed (69), MO Structures (38), and Repurposed's mineshafts, mansions, monuments + and cities. + + In a world with the 40 mods, seed 1234, `/locate structure mvs:azelea_house` finds one 27 + blocks from spawn and it stands there as the template builds it; When Dungeons Arise's + bandit towers and Structory's chapel locate too. + ### 5. Code: one agent per mod For tiers logic, machines and deep, in dependency order, as many agents at once as the AI @@ -473,12 +515,15 @@ user's jars are, and its facts and results stay in the work folder. The first re | Glassential-renewed 3.4.7 | 105/114 | 5177/5177 | 5177/5177 | 4727/5177 | 6447/6447 | 210/210 | 105/105 | 143/143 | 106/115 | | Factory Blocks 1.4.0 | 42/49 | 42/42 | 0/42 | 42/42 | 294/294 | 84/84 | 42/42 | 1/1 | 42/49 | | Cloud Glass 1.1.0 | 16/16 | 16/16 | 16/16 | 0/16 | 112/112 | 32/32 | – | 31/31 | 16/16 | +| Chipped 4.0.2 | 6857/6981 | 46212/46443 | 46443/46443 | 46396/46443 | 149093/149093 | 13548/13714 | 11984/12378 | 6/13 | 6849/6973 | What fails is what API 0.2 cannot express yet: shapes of their own (Japanese doors, hedges, paths a sixteenth high, glass without collision), sound events of the mod's own (`factory_blocks:metal_sound`), and blocks whose vanilla class has no vanilla block with the same states (garage doors, double gates, engraved and facing paths, fans). Every drop, recipe -and tag of the declared blocks matches. +and tag of the declared blocks matches. Chipped's 124 missing blocks are its own classes (workbenches and +`DirectionBlock`), and its workbench recipes need those blocks. Its drops match since carry writes +the self-drop tables it registers in code (step 4). ### 7. Coverage report @@ -520,9 +565,9 @@ files; `inventory`; `carry` and the harness on the eight mods we downloaded; `re By tier, the 367 not yet: data 57 (carry-over and the harness, no agent: their jars were not downloaded), logic 66 (API 0.3), machines 97 (0.4), deep 92 (0.5), client 35, unclassified 20. Ported: Macaw's Trapdoors. Partial: Macaw's Doors, Fences and Walls, Paths and Pavings, -Glassential, Factory Blocks, Cloud Glass (see the harness table) and MVS, whose 784 worldgen -files are carried but not yet read by the game. Of the 8, 7 are MIT and one LGPL; nothing is -published. +Glassential, Factory Blocks, Cloud Glass, Chipped (see the harness table) and MVS, whose +structures now generate (see step 4). Of the 9, 7 are MIT, one LGPL and Chipped is under the +Terrarium License; nothing is published. ## Measuring "10x" @@ -582,8 +627,9 @@ vanilla's 35,723; the per-state tables are the next thing to measure), while the inside its 2 GB heap. Caveats, so none of this is a "10x" claim yet: other sessions and CI runners kept the machine's load average between 30 and 56 on 14 threads, so the confidence intervals are as wide as the means (`perf_bench.py --table`); Horizon's frames are paced near -the display's 144 Hz at 8 chunks; Horizon's title time includes building the content overlay on -a first start (about 5 s, cached afterwards); and the pack runs 1.21.1 against Horizon's 26.3. +the display's 144 Hz at 8 chunks; Horizon's title time includes building the content overlay +(about 5 s for six mods; it was rebuilt on every start then, and is kept for later starts now); +and the pack runs 1.21.1 against Horizon's 26.3. A quiet rerun with more games is the next measurement. **Where the RAM goes (2026-10-17).** RSS overstates it on macOS. `ps` counts pages the allocator @@ -620,8 +666,44 @@ indexes ids by a hash checked against the states: -22 MB live for vanilla, -124 ATM10's, -30 MB and -140 MB of footprint. The startup peaks are what is left. Each step parses its per-state tables whole: as JSON trees, or as NBT trees of 300-600 MB each for ATM10 (`block-properties.nbt`, the collision, support, occlusion and movement tables). The peak -reaches 3.9 GB during worldgen loading, and the footprint stays near the peak. Typed, streaming -readers for those tables are the next cut. +reaches 3.9 GB during worldgen loading, and the footprint stays near the peak. + +The NBT tables now stream: the readers check each state's identity as it is read, and no +`states` tree is built (#753). That tree was about 240 MB for each table. With the carried +ATM10 mods (153,511 states), the peak of each step falls by about 210 MB: + +| step | before MB | after MB | +|---|---:|---:| +| light | 911 | 699 | +| collision | 945 | 733 | +| motion | 1,427 | 1,215 | +| worldgen, uncached | 3,000 | 2,788 | + +The fluid tables no longer decode the 211 MB `gameplay-facts.json` a second time (#754). The +content root is kept between starts (#756). What is left at startup: + +- `facts.json` goes through a JSON tree, then an NBT tree, in both the registry and the assets + step; +- on a first start, the content-root build and `Behaviours`' decode of `gameplay-facts.json` + (cached afterwards in the behaviour snapshot). + +**Against NeoForge, same 40 mods (2026-10-17).** The 40 data-tier mods carried from ATM10 (the +42 without Regions Unexplored and MO Structures, whose libraries are only on CurseForge), +against their jars on NeoForge 21.1.259 with `-Xmx2G`. `perf_bench.py --port-work`, RD 8, one +game each, load average 80-125 on 14 threads; `--warm` measures a later start: + +| game | title / world s | RSS steady / peak MB | footprint steady / peak MB | +|---|---|---|---| +| Horizon before #756 (every start) | 81.1 / 102.8 | 4,008 / 4,164 | 2,368 / 2,554 | +| Horizon, first start | 105.2 / 128.8 | 3,795 / 3,993 | 2,306 / 2,560 | +| **Horizon, later starts** | **21.6 / 33.7** | **2,629 / 2,935** | **2,445 / 2,731** | +| NeoForge + 40 mods | 53.2 / 119.9 | 3,099 / 3,203 | 3,304 / 3,381 | + +On later starts, Horizon uses 15 % less RSS and 26 % less footprint than NeoForge with the +same mods. It reaches the title 2.5x sooner and the world 3.6x sooner, and its flights ran at +93-233 FPS against NeoForge's 27-62. The first start is slower than NeoForge's to the title, +because it builds the content root once. One game per row on a loaded machine is not a +benchmark; a quiet rerun with repeats is still to do. ## The working version for 2026-10-23 @@ -648,11 +730,9 @@ In scope (each is a PR or a small stack): 6. **Measured:** load time, RAM and FPS of that pack on NeoForge 1.21.1 against Horizon, with the full ATM10 on NeoForge as the reference row. -Out of scope for that date, and next in order: worldgen carry-over (the 30 or so structure and biome mods; -the first one looked at, Moog's Voyager Structures, declares all 130 structures and 116 structure -sets with MoogsStructureLib's own `generic_jigsaw_structure` and `advanced_random_spread` types and -NeoForge's `c:` biome tags, so its carry needs a mapping to vanilla's jigsaw and random spread -types plus the oracle's biome tags, not only bundle merging), +Out of scope for that date, and next in order: the rest of worldgen carry-over (mods' +structures, sets, pools and features generate now, see step 4; mod biomes, noise and density +functions, and the structure types written in Java such as YUNG's and Formations', do not yet), behaviour callbacks and server events (0.3), block entities, menus, capabilities and net (0.4), the first agent-ported logic-tier mod as the pilot of step 5, then machines. Porting all of ATM10 is a campaign. Each step publishes its coverage number; we make no claim beyond what From ff9449099220d4039f93312652ba55d0d79a27c2 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:08:44 +0200 Subject: [PATCH 17/67] Block entities: tick only those with a ticker, in vanilla's order World::block_entities keeps vanilla's Level.blockEntityTickers beside the map: a slot when a block entity whose state has a ticker is added, kept when it is replaced, freed when it is removed, unloaded or its state loses the ticker; a loaded chunk's in its block entity map's order. The tick visits that list once, in block-ticking chunks within the world border, instead of scanning the whole map once per kind. New harness block_entity_tickers: real LevelChunks of a server level against BlockEntities. Co-Authored-By: Claude Opus 5.5 --- .../examples/block_entity_tickers_fixture.rs | 99 +++++ crates/horizon-client/src/block_entities.rs | 374 ++++++++++++++++++ crates/horizon-client/src/game.rs | 37 +- crates/horizon-client/src/game/beacon.rs | 25 +- crates/horizon-client/src/game/beehives.rs | 79 ++-- .../src/game/block_entity_ticks.rs | 140 +++++++ .../horizon-client/src/game/command_blocks.rs | 4 +- crates/horizon-client/src/game/creaking.rs | 73 ++-- .../src/game/dispense_fixture.rs | 12 +- crates/horizon-client/src/game/jukebox.rs | 94 ++--- crates/horizon-client/src/game/machines.rs | 61 ++- .../src/game/machines/crafter.rs | 59 ++- .../horizon-client/src/game/render_control.rs | 2 +- crates/horizon-client/src/game/spawner.rs | 48 +-- .../horizon-client/src/game/trial_spawner.rs | 41 +- .../horizon-client/src/game/trial_visuals.rs | 2 +- crates/horizon-client/src/game/vault.rs | 31 +- crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/streaming.rs | 2 +- crates/horizon-client/src/world.rs | 17 +- .../VanillaBlockEntityTickerOracle.java | 217 ++++++++++ tools/test_block_entity_tickers_parity.py | 132 +++++++ 22 files changed, 1209 insertions(+), 341 deletions(-) create mode 100644 crates/horizon-client/examples/block_entity_tickers_fixture.rs create mode 100644 crates/horizon-client/src/block_entities.rs create mode 100644 crates/horizon-client/src/game/block_entity_ticks.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockEntityTickerOracle.java create mode 100644 tools/test_block_entity_tickers_parity.py diff --git a/crates/horizon-client/examples/block_entity_tickers_fixture.rs b/crates/horizon-client/examples/block_entity_tickers_fixture.rs new file mode 100644 index 000000000..f08c333f6 --- /dev/null +++ b/crates/horizon-client/examples/block_entity_tickers_fixture.rs @@ -0,0 +1,99 @@ +//! The Rust side of tools/test_block_entity_tickers_parity.py: the ops of +//! `VanillaBlockEntityTickerOracle` on `BlockEntities`, as `World` drives it +//! (a state write calls `state_changed`), printing the tick order. +use horizon_client::{ + block_entities::{BlockEntities, Pos, Ticking}, + containers::BlockEntity, +}; +use std::collections::HashMap; + +/// State ids of the fixture: 0 air, 1 a block whose block entity ticks, 2 a +/// jukebox without a record, 3 a block entity that never ticks (a chest). +fn state(name: &str) -> u32 { + match name { + "chest" | "barrel" => 3, + "jukebox" => 2, + _ => 1, + } +} + +fn entity(name: &str) -> BlockEntity { + if name.starts_with("jukebox") { + // The game makes a jukebox's block entity when a record goes in. + return BlockEntity::Jukebox(Default::default()); + } + BlockEntity::for_block(&format!("minecraft:{name}")) + .unwrap_or_else(|| panic!("no block entity for {name}")) +} + +fn main() { + let args: Vec = std::env::args().collect(); + let input = std::fs::read_to_string(&args[1]).expect("input"); + let mut out = String::new(); + let mut entities = BlockEntities::default(); + let mut states: HashMap = HashMap::new(); + let mut kinds: HashMap = HashMap::new(); + let table = vec![Ticking::No, Ticking::Yes, Ticking::Idle, Ticking::No]; + for line in input.lines() { + let w: Vec<&str> = line.split_whitespace().collect(); + let pos = |i: usize| -> Pos { + let n = |j: usize| w[i + j].parse::().expect("coordinate"); + (n(0), n(1), n(2)) + }; + match w[0] { + "reset" => { + entities = BlockEntities::default(); + entities.set_states(table.clone()); + states.clear(); + kinds.clear(); + } + "load" => { + let mut chunk = Vec::new(); + for i in (3..w.len()).step_by(4) { + let p = pos(i + 1); + states.insert(p, state(w[i])); + kinds.insert(p, w[i].to_owned()); + chunk.push((p, entity(w[i]))); + } + entities.load_chunk(chunk); + } + "place" => { + let p = pos(2); + // World::set, then the game adds the block entity. + let old = states.insert(p, state(w[1])).unwrap_or(0); + kinds.insert(p, w[1].to_owned()); + entities.state_changed(p, old, state(w[1])); + entities.insert(p, entity(w[1])); + } + "replace" => { + let p = pos(1); + entities.insert(p, entity(&kinds[&p])); + } + "remove" => { + let p = pos(1); + entities.remove(&p); + let old = states.insert(p, 0).unwrap_or(0); + entities.state_changed(p, old, 0); + } + "record" => { + let p = pos(1); + let s = if w[4] == "1" { 1 } else { 2 }; + let old = states.insert(p, s).unwrap_or(0); + entities.state_changed(p, old, s); + } + "unload" => { + let c = (w[1].parse::().unwrap(), w[2].parse::().unwrap()); + entities.retain(|p, _| (p.0 >> 4, p.2 >> 4) != c); + } + "tick" => { + out.push_str("tick"); + for (p, _) in entities.tickers(|p| states.get(&p).copied().unwrap_or(0)) { + out.push_str(&format!(" {},{},{}", p.0, p.1, p.2)); + } + out.push('\n'); + } + other => panic!("unknown op {other}"), + } + } + std::fs::write(&args[2], out).expect("output"); +} diff --git a/crates/horizon-client/src/block_entities.rs b/crates/horizon-client/src/block_entities.rs new file mode 100644 index 000000000..f4b0c411d --- /dev/null +++ b/crates/horizon-client/src/block_entities.rs @@ -0,0 +1,374 @@ +//! The world's block entities and the order they tick in: vanilla's +//! `Level.blockEntityTickers`, kept beside the block entity map so a tick +//! visits only the block entities that tick, in vanilla's order, instead of +//! scanning the whole map once per kind. +//! +//! The list follows `LevelChunk.updateBlockEntityTicker` and +//! `Level.tickBlockEntities`: +//! - a block entity whose block state has a ticker gets a slot at the end of +//! the list (through `pendingBlockEntityTickers`, so a slot made during a +//! tick first ticks on the next one); +//! - replacing the block entity at a position (`setBlockEntity`), or changing +//! its block state to another state with a ticker, keeps its slot +//! (`RebindableTickingBlockEntityWrapper.rebind`); +//! - removing it (`removeBlockEntity`), unloading its chunk +//! (`clearAllBlockEntities`) or changing its state to one without a ticker +//! (a jukebox losing its record) frees the slot; a later ticker there is +//! appended again; +//! - a loaded chunk's block entities are added in the iteration order of its +//! `Object2ObjectOpenHashMap` +//! (`registerAllBlockEntitiesAfterLevelLoad`), filled in the order the chunk +//! listed them. +//! +//! Which states tick is a table by state id, built by the game from the block +//! registry; whether a slot is made is decided when the slot is promoted at +//! the start of the next tick, from the block state then. +use crate::containers::BlockEntity; +use crate::fast_hash::FxHashMap; +use std::collections::HashMap; + +pub type Pos = (i32, i32, i32); + +/// What a block state's block entity does each tick (`BlockState.getTicker`). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Ticking { + /// No ticker, and the block has no block entity that could have one. + #[default] + No, + /// A ticker. + Yes, + /// The block's block entity, but this state has no ticker (a jukebox + /// without a record): a state change to it frees the slot. + Idle, +} + +#[derive(Default)] +pub struct BlockEntities { + map: HashMap, + /// `Level.blockEntityTickers`: positions and the slot each was given. + tickers: Vec<(Pos, u32)>, + /// `LevelChunk.tickersInLevel`: the live slot of each ticking position. + live: FxHashMap, + /// Positions that may need a slot, in the order vanilla would have made + /// it (`pendingBlockEntityTickers`), each with a token; decided at the + /// next promotion from the block state then. + added: Vec<(Pos, u32)>, + /// The token of each position's pending entry: an entry whose token is + /// not here any more was cancelled. + pending: FxHashMap, + next_slot: u32, + /// `Ticking` by block state id; empty until the game builds it. + states: Vec, +} + +impl std::ops::Deref for BlockEntities { + type Target = HashMap; + fn deref(&self) -> &Self::Target { + &self.map + } +} + +impl<'a> IntoIterator for &'a BlockEntities { + type Item = (&'a Pos, &'a BlockEntity); + type IntoIter = std::collections::hash_map::Iter<'a, Pos, BlockEntity>; + fn into_iter(self) -> Self::IntoIter { + self.map.iter() + } +} + +/// `Vec3i.hashCode`. +fn java_hash((x, y, z): Pos) -> i32 { + y.wrapping_add(z.wrapping_mul(31)) + .wrapping_mul(31) + .wrapping_add(x) +} +/// `HashCommon.mix`. +fn mix(x: i32) -> usize { + let h = (x as u32).wrapping_mul(0x9E37_79B9); + (h ^ (h >> 16)) as usize +} + +/// The order `values().forEach` visits a `new Object2ObjectOpenHashMap<>()` +/// keyed by these positions, put in this order (repeats keep their first +/// slot): open addressing with linear probing, a rehash to twice the size +/// past three quarters full, and iteration from the last slot down. +pub fn chunk_map_order(positions: impl IntoIterator) -> Vec { + // HashCommon.arraySize(16, .75f), and maxFill of each size. + let mut slots: Vec> = vec![None; 32]; + let max_fill = |n: usize| (n * 3).div_ceil(4).min(n - 1); + let mut size = 0; + for p in positions { + let mask = slots.len() - 1; + let mut at = mix(java_hash(p)) & mask; + let mut known = false; + while let Some(k) = slots[at] { + if k == p { + known = true; + break; + } + at = (at + 1) & mask; + } + if known { + continue; + } + slots[at] = Some(p); + // `if (size++ >= maxFill) rehash(arraySize(size + 1, f))`, which + // puts the keys back from the last slot down. + size += 1; + if size > max_fill(slots.len()) { + let n = ((size + 1) * 4).div_ceil(3).next_power_of_two(); + let old = std::mem::replace(&mut slots, vec![None; n]); + for k in old.into_iter().rev().flatten() { + let mut at = mix(java_hash(k)) & (n - 1); + while slots[at].is_some() { + at = (at + 1) & (n - 1); + } + slots[at] = Some(k); + } + } + } + slots.into_iter().rev().flatten().collect() +} + +impl BlockEntities { + /// Whether the ticking table has been built. + pub fn has_states(&self) -> bool { + !self.states.is_empty() + } + pub fn set_states(&mut self, states: Vec) { + self.states = states; + } + pub fn take_states(&mut self) -> Vec { + std::mem::take(&mut self.states) + } + pub fn ticking(&self, state: u32) -> Ticking { + self.states.get(state as usize).copied().unwrap_or_default() + } + + fn next(&mut self) -> u32 { + let n = self.next_slot; + self.next_slot = n.wrapping_add(1); + n + } + /// A ticker may be made for `pos` now (`updateBlockEntityTicker`). + fn add(&mut self, pos: Pos) { + let token = self.next(); + self.pending.insert(pos, token); + self.added.push((pos, token)); + } + /// `removeBlockEntityTicker`: no slot, made or pending. + fn cancel(&mut self, pos: &Pos) { + self.live.remove(pos); + self.pending.remove(pos); + } + + pub fn get_mut(&mut self, pos: &Pos) -> Option<&mut BlockEntity> { + self.map.get_mut(pos) + } + pub fn iter_mut(&mut self) -> impl Iterator { + self.map.iter_mut() + } + pub fn values_mut(&mut self) -> impl Iterator { + self.map.values_mut() + } + /// `LevelChunk.setBlockEntity` (+ `addAndRegisterBlockEntity`): a new + /// block entity may get a slot; one replacing another keeps its slot. + pub fn insert(&mut self, pos: Pos, entity: BlockEntity) -> Option { + let old = self.map.insert(pos, entity); + if old.is_none() { + self.add(pos); + } + old + } + /// The block entity at `pos`, made by `make` when there is none. + pub fn get_or_insert_with( + &mut self, + pos: Pos, + make: impl FnOnce() -> BlockEntity, + ) -> &mut BlockEntity { + if !self.map.contains_key(&pos) { + self.add(pos); + } + self.map.entry(pos).or_insert_with(make) + } + /// `LevelChunk.removeBlockEntity`: the block entity and its slot. + pub fn remove(&mut self, pos: &Pos) -> Option { + self.cancel(pos); + self.map.remove(pos) + } + /// The block entity at `pos`, out of the map for a while: its slot stays + /// for `put` to bring it back (a tick working on it). + pub fn take(&mut self, pos: &Pos) -> Option { + self.map.remove(pos) + } + /// Brings back what `take` took, in its slot. + pub fn put(&mut self, pos: Pos, entity: BlockEntity) { + if self.map.insert(pos, entity).is_none() + && !self.live.contains_key(&pos) + && !self.pending.contains_key(&pos) + { + self.add(pos); + } + } + /// Removes the block entities `keep` refuses, and their slots + /// (`clearAllBlockEntities` of unloaded chunks). + pub fn retain(&mut self, mut keep: impl FnMut(&Pos, &mut BlockEntity) -> bool) { + let (live, pending) = (&mut self.live, &mut self.pending); + self.map.retain(|p, e| { + let kept = keep(p, e); + if !kept { + live.remove(p); + pending.remove(p); + } + kept + }); + } + pub fn clear(&mut self) { + self.map.clear(); + self.tickers.clear(); + self.live.clear(); + self.added.clear(); + self.pending.clear(); + } + /// A loaded or generated chunk's block entities, in the order the chunk + /// lists them; they tick in its block entity map's order + /// (`registerAllBlockEntitiesAfterLevelLoad`). + pub fn load_chunk(&mut self, entities: Vec<(Pos, BlockEntity)>) { + let order = chunk_map_order(entities.iter().map(|(p, _)| *p)); + for (p, e) in entities { + self.map.insert(p, e); + } + for p in order { + self.add(p); + } + } + /// `LevelChunk.setBlockState` on a block entity's position, from state + /// `old` to `new`: a ticker made, kept or dropped. + pub fn state_changed(&mut self, pos: Pos, old: u32, new: u32) { + if self.states.is_empty() || !self.map.contains_key(&pos) { + return; + } + match self.ticking(new) { + // A new ticker, unless one was made or is pending (rebound). + Ticking::Yes => { + if self.ticking(old) != Ticking::Yes && !self.live.contains_key(&pos) { + self.add(pos); + } + } + Ticking::Idle => self.cancel(&pos), + // Another block: the game removes or replaces the block entity. + Ticking::No => {} + } + } + /// The start of `Level.tickBlockEntities`: pending slots join the list + /// and freed ones leave it. `state` reads the block state at a position. + /// Returns the slots to tick, in order; check each with `is_live` before + /// ticking it, as the ticks before it can free it. + pub fn tickers(&mut self, state: impl Fn(Pos) -> u32) -> Vec<(Pos, u32)> { + for (pos, token) in std::mem::take(&mut self.added) { + if self.pending.get(&pos) != Some(&token) { + continue; + } + self.pending.remove(&pos); + if self.live.contains_key(&pos) + || !self.map.contains_key(&pos) + || self.ticking(state(pos)) != Ticking::Yes + { + continue; + } + let slot = self.next(); + self.live.insert(pos, slot); + self.tickers.push((pos, slot)); + } + let (live, map) = (&mut self.live, &self.map); + self.tickers.retain(|(p, slot)| { + let mine = live.get(p) == Some(slot); + if mine && !map.contains_key(p) { + live.remove(p); + } + mine && map.contains_key(p) + }); + self.tickers.clone() + } + /// Whether a slot from `tickers` still ticks. + pub fn is_live(&self, (pos, slot): (Pos, u32)) -> bool { + self.live.get(&pos) == Some(&slot) && self.map.contains_key(&pos) + } + /// Frees the slot at `pos` (the block entity stays). + pub fn forget(&mut self, pos: &Pos) { + self.cancel(pos); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn hopper() -> BlockEntity { + BlockEntity::Chest(vec![None; 5]) + } + /// States: 0 air, 1 ticks, 2 idle. + fn level() -> BlockEntities { + let mut b = BlockEntities::default(); + b.set_states(vec![Ticking::No, Ticking::Yes, Ticking::Idle]); + b + } + fn order(b: &mut BlockEntities, states: &HashMap) -> Vec { + b.tickers(|p| states.get(&p).copied().unwrap_or(0)) + .into_iter() + .map(|(p, _)| p) + .collect() + } + + #[test] + fn slots_follow_vanilla_lifecycle() { + let mut b = level(); + let mut s = HashMap::new(); + for p in [(0, 0, 0), (1, 0, 0), (2, 0, 0)] { + s.insert(p, 1); + b.insert(p, hopper()); + } + assert_eq!(order(&mut b, &s), [(0, 0, 0), (1, 0, 0), (2, 0, 0)]); + // Replacing keeps the slot; removing and placing again appends. + b.insert((0, 0, 0), hopper()); + b.remove(&(1, 0, 0)); + b.insert((1, 0, 0), hopper()); + assert_eq!(order(&mut b, &s), [(0, 0, 0), (2, 0, 0), (1, 0, 0)]); + // Take and put keep it; an idle state frees it, ticking again appends. + let e = b.take(&(0, 0, 0)).unwrap(); + b.put((0, 0, 0), e); + assert_eq!(order(&mut b, &s), [(0, 0, 0), (2, 0, 0), (1, 0, 0)]); + s.insert((2, 0, 0), 2); + b.state_changed((2, 0, 0), 1, 2); + assert_eq!(order(&mut b, &s), [(0, 0, 0), (1, 0, 0)]); + s.insert((2, 0, 0), 1); + b.state_changed((2, 0, 0), 2, 1); + assert_eq!(order(&mut b, &s), [(0, 0, 0), (1, 0, 0), (2, 0, 0)]); + // A slot freed during a tick is not live any more. + let slots = b.tickers(|p| s.get(&p).copied().unwrap_or(0)); + b.retain(|p, _| *p != (1, 0, 0)); + assert!(b.is_live(slots[0]) && !b.is_live(slots[1])); + } + + #[test] + fn chunk_loads_in_open_hash_map_order() { + // A few keys: their mixed hashes over 32 slots, read from the top. + let positions = [ + (0, 64, 0), + (1, 64, 0), + (0, 64, 1), + (15, -64, 15), + (3, 70, 9), + ]; + let slot = |p: &Pos| mix(java_hash(*p)) & 31; + let mut expected = positions.to_vec(); + expected.sort_by_key(|p| std::cmp::Reverse(slot(p))); + let distinct: std::collections::HashSet = positions.iter().map(slot).collect(); + assert_eq!(distinct.len(), positions.len()); + assert_eq!(chunk_map_order(positions), expected); + // Past 24 keys the table doubles; every key stays, once. + let many: Vec = (0..100).map(|i| (i % 16, i / 16, 3)).collect(); + let order = chunk_map_order(many.iter().copied().chain(many.iter().copied())); + assert_eq!(order.len(), 100); + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 117f7a194..ae63ec36f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10,6 +10,7 @@ mod banners; mod beacon; mod beehives; mod block_entity_anims; +mod block_entity_ticks; mod block_items; mod blockctl; mod boats; @@ -2196,13 +2197,13 @@ impl Game<'_> { self.audio .play("minecraft:block.ender_chest.open", Some(at), 0.5, pitch); } else if &*name == "minecraft:beacon" && self.open_station(pos, &name) { - self.world.block_entities.entry(key).or_insert_with(|| { + self.world.block_entities.get_or_insert_with(key, || { BlockEntity::for_block(&name).unwrap_or(BlockEntity::Chest(Vec::new())) }); } else if storage::is_shulker_box(&name) && !self.shulker_can_open(pos) { // ShulkerBoxBlock.canOpen: the lid has no room; nothing opens. } else if let Some(entity) = BlockEntity::for_block(&name) { - self.world.block_entities.entry(key).or_insert(entity); + self.world.block_entities.get_or_insert_with(key, || entity); let centre = [ f64::from(key.0) + 0.5, f64::from(key.1) + 0.5, @@ -6087,38 +6088,6 @@ impl Game<'_> { false, ) } - fn tick_block_entities(&mut self) -> Result<()> { - self.tick_hoppers()?; - self.tick_crafters(); - self.tick_mount_menu(); - self.tick_jukeboxes(); - self.tick_beacons(); - self.tick_creaking_hearts()?; - self.tick_spawners()?; - self.tick_trial_spawners()?; - self.tick_vaults()?; - self.tick_beehives(); - let mut lit_changes = Vec::new(); - for (pos, entity) in self.world.block_entities.iter_mut() { - if let BlockEntity::Furnace(f) = entity - && let Some(lit) = f.tick(&self.res.recipes, &self.res.items, &self.res.providers) - { - lit_changes.push((*pos, lit)); - } - } - for ((x, y, z), lit) in lit_changes { - let state = self.world.block(x, y, z); - if let Some(mut s) = self.pack.assets.registry.state(state).cloned() - && s.properties.contains_key("lit") - { - s.properties.insert("lit".into(), lit.to_string()); - if let Ok(id) = self.pack.assets.registry.resolve(&s) { - self.world.set(x, y, z, id)?; - } - } - } - Ok(()) - } /// Copies of a save batch carrying its live block entities and save data. fn with_entities<'a>( &self, diff --git a/crates/horizon-client/src/game/beacon.rs b/crates/horizon-client/src/game/beacon.rs index c8c437965..73cdd15a4 100644 --- a/crates/horizon-client/src/game/beacon.rs +++ b/crates/horizon-client/src/game/beacon.rs @@ -105,24 +105,13 @@ impl Game<'_> { .find(|&y| !self.res.facts.is_air(self.world.block(x, y, z))) .map_or(min, |y| y + 1) } - /// `BeaconBlockEntity.tick` for every beacon. - pub(super) fn tick_beacons(&mut self) { - let keys: Vec<(i32, i32, i32)> = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Beacon(_))) - .map(|(k, _)| *k) - .collect(); - for key in keys { - let Some(BlockEntity::Beacon(mut b)) = self.world.block_entities.remove(&key) else { - continue; - }; - self.tick_beacon(key, &mut b); - self.world - .block_entities - .insert(key, BlockEntity::Beacon(b)); - } + /// `BeaconBlockEntity.tick` of the beacon at `key`. + pub(super) fn tick_beacon_at(&mut self, key: (i32, i32, i32)) { + let Some(BlockEntity::Beacon(mut b)) = self.world.block_entities.take(&key) else { + return; + }; + self.tick_beacon(key, &mut b); + self.world.block_entities.put(key, BlockEntity::Beacon(b)); } fn tick_beacon(&mut self, (x, y, z): (i32, i32, i32), b: &mut Beacon) { let mut check = if b.last_check_y < y { diff --git a/crates/horizon-client/src/game/beehives.rs b/crates/horizon-client/src/game/beehives.rs index 0520e4367..52fed3f3b 100644 --- a/crates/horizon-client/src/game/beehives.rs +++ b/crates/horizon-client/src/game/beehives.rs @@ -87,48 +87,37 @@ impl Game<'_> { let facing = beehive::facing_step(props.get("facing").map(String::as_str)); Some((honey, facing)) } - /// `BeehiveBlockEntity.serverTick` for every hive, after an emergency - /// exit for those with fire beside them (`BeehiveBlock.updateShape`). - pub(super) fn tick_beehives(&mut self) { - let mut keys: Vec<(i32, i32, i32)> = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Beehive(_))) - .map(|(k, _)| *k) - .collect(); - keys.sort_unstable(); - for key in keys { - let at = [key.0, key.1, key.2]; - let Some((honey, facing)) = self.hive_state(at) else { - continue; - }; - let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.remove(&key) - else { - continue; - }; - let fire = [ - [1, 0, 0], - [-1, 0, 0], - [0, 1, 0], - [0, -1, 0], - [0, 0, 1], - [0, 0, -1], - ] - .iter() - .any(|d| { - let state = self.world.block(at[0] + d[0], at[1] + d[1], at[2] + d[2]); - &*self.res.facts.get(state).name == "minecraft:fire" - }); - let mut host = GameHive { game: self }; - if fire && !hive.bees.is_empty() { - hive.release_all(at, facing, honey, Release::Emergency, &mut host); - } - hive.server_tick(at, facing, honey, &mut host); - self.world - .block_entities - .insert(key, BlockEntity::Beehive(hive)); + /// `BeehiveBlockEntity.serverTick` of the hive at `key`, after an + /// emergency exit if fire is beside it (`BeehiveBlock.updateShape`). + pub(super) fn tick_beehive(&mut self, key: (i32, i32, i32)) { + let at = [key.0, key.1, key.2]; + let Some((honey, facing)) = self.hive_state(at) else { + return; + }; + let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.take(&key) else { + return; + }; + let fire = [ + [1, 0, 0], + [-1, 0, 0], + [0, 1, 0], + [0, -1, 0], + [0, 0, 1], + [0, 0, -1], + ] + .iter() + .any(|d| { + let state = self.world.block(at[0] + d[0], at[1] + d[1], at[2] + d[2]); + &*self.res.facts.get(state).name == "minecraft:fire" + }); + let mut host = GameHive { game: self }; + if fire && !hive.bees.is_empty() { + hive.release_all(at, facing, honey, Release::Emergency, &mut host); } + hive.server_tick(at, facing, honey, &mut host); + self.world + .block_entities + .put(key, BlockEntity::Beehive(hive)); } /// `BeehiveBlockEntity.addOccupant` for a bee its `BeeEnterHiveGoal` /// sent in: off its lead, its tag stored, the bee gone. @@ -152,13 +141,13 @@ impl Game<'_> { self.mobs[i].extra.mount = None; let tag = self.mob_tree_nbt(i); let flower = bee::get(&self.mobs[i]).and_then(|b| b.flower); - let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.remove(&key) else { + let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.take(&key) else { return; }; let stored = hive.add_occupant(at, tag, flower, &mut GameHive { game: self }); self.world .block_entities - .insert(key, BlockEntity::Beehive(hive)); + .put(key, BlockEntity::Beehive(hive)); if stored && let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob) { m.removed = true; } @@ -166,13 +155,13 @@ impl Game<'_> { /// `BeehiveBlockEntity.emptyAllLivingFromHive` with no player about. pub(super) fn empty_hive(&mut self, at: [i32; 3], facing: [i32; 2], status: Release) { let key = (at[0], at[1], at[2]); - let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.remove(&key) else { + let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.take(&key) else { return; }; hive.release_all(at, facing, None, status, &mut GameHive { game: self }); self.world .block_entities - .insert(key, BlockEntity::Beehive(hive)); + .put(key, BlockEntity::Beehive(hive)); } /// `BeehiveBlock.playerDestroy` without silk touch: the bees come out /// angry at the player unless smoke calms them, then diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs new file mode 100644 index 000000000..7de960716 --- /dev/null +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -0,0 +1,140 @@ +//! `Level.tickBlockEntities` for the block entities ticked together here +//! (hoppers, crafters, jukeboxes, beacons, creaking hearts, spawners, trial +//! spawners, vaults, beehives and furnaces): only those whose state has a +//! ticker, in vanilla's list order (`horizon_client::block_entities`), in +//! chunks that tick blocks. Sculk, brewing stands and pistons tick in their +//! own passes. +use super::Game; +use horizon_client::{ + Result, + block_entities::{Pos, Ticking}, + containers::{BlockEntity, FurnaceKind, HOPPER_SLOTS}, +}; +use horizon_world::registry::BlockStateId; + +impl Game<'_> { + pub(super) fn tick_block_entities(&mut self) -> Result<()> { + self.tick_mount_menu(); + self.load_entity_types(); + if !self.world.block_entities.has_states() { + let states = self.ticking_states(); + self.world.block_entities.set_states(states); + } + self.forget_gone_hoppers(); + let ticking = self.block_ticking_area(); + let mut spawners = Vec::new(); + let mut trial_spawners = Vec::new(); + for slot in self.world.block_entity_tickers() { + let pos = slot.0; + // Ticks before this one can remove it (`TickingBlockEntity.isRemoved`). + if !self.world.block_entities.is_live(slot) + || !ticking(pos.0, pos.2) + || !self.border.contains(f64::from(pos.0), f64::from(pos.2)) + { + continue; + } + match self.world.block_entities.get(&pos) { + Some(BlockEntity::Chest(items)) if items.len() == HOPPER_SLOTS => { + self.tick_hopper(pos) + } + Some(BlockEntity::Crafter(_)) => self.tick_crafter(pos), + Some(BlockEntity::Jukebox(_)) => self.tick_jukebox(pos), + Some(BlockEntity::Beacon(_)) => self.tick_beacon_at(pos), + Some(BlockEntity::CreakingHeart(_)) => self.tick_creaking_heart(pos)?, + Some(BlockEntity::Spawner(_)) => { + self.tick_spawner(pos)?; + spawners.push(pos); + } + Some(BlockEntity::TrialSpawner(_)) => { + self.tick_trial_spawner(pos)?; + trial_spawners.push(pos); + } + Some(BlockEntity::Vault(_)) => self.tick_vault(pos)?, + Some(BlockEntity::Beehive(_)) => self.tick_beehive(pos), + Some(BlockEntity::Furnace(_)) => self.tick_furnace(pos)?, + _ => {} + } + } + self.update_song_volumes(); + self.scan_for_hearts(); + self.client_tick_spawners(&spawners); + for key in trial_spawners { + self.client_tick_trial_spawner(key); + } + Ok(()) + } + /// `BlockState.getTicker` of every state, for the block entities ticked + /// here. A creaking heart keeps its slot while uprooted (see + /// `tick_creaking_heart`). + fn ticking_states(&self) -> Vec { + let registry = self.pack.assets.registry; + (0..registry.len()) + .map(|i| { + let state = BlockStateId(i as u32); + let name: &str = &self.res.facts.get(state).name; + match name { + "minecraft:hopper" + | "minecraft:crafter" + | "minecraft:beacon" + | "minecraft:creaking_heart" + | "minecraft:spawner" + | "minecraft:trial_spawner" + | "minecraft:vault" + | "minecraft:beehive" + | "minecraft:bee_nest" => Ticking::Yes, + "minecraft:jukebox" => { + let record = registry + .state(state) + .and_then(|s| s.properties.get("has_record")) + .is_some_and(|v| v == "true"); + if record { Ticking::Yes } else { Ticking::Idle } + } + n if FurnaceKind::from_block(n).is_some() => Ticking::Yes, + _ => Ticking::No, + } + }) + .collect() + } + /// `ServerLevel.shouldTickBlocksAt`: chunks within the simulation + /// distance plus one (`DistanceManager.inBlockTickingRange`, Chebyshev + /// in chunks) of the player or a guest. With nobody there, a dedicated + /// server ticks none; the game alone (no player yet, headless fixtures) + /// ticks every chunk. + fn block_ticking_area(&self) -> impl Fn(i32, i32) -> bool + use<> { + let d = self.settings.simulation_distance.max(2) + 1; + let mut centres: Vec<(i32, i32)> = self.streaming.extra.iter().map(|&(c, _)| c).collect(); + if let Some(p) = &self.player { + centres.push(( + (p.position.x.floor() as i32) >> 4, + (p.position.z.floor() as i32) >> 4, + )); + } + let everywhere = centres.is_empty() && self.options.dedicated.is_none(); + move |x, z| { + everywhere + || centres + .iter() + .any(|&(cx, cz)| ((x >> 4) - cx).abs().max(((z >> 4) - cz).abs()) <= d) + } + } + /// `AbstractFurnaceBlockEntity.serverTick` of the furnace at `pos`, and + /// its block's `lit` following it. + fn tick_furnace(&mut self, pos: Pos) -> Result<()> { + let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) else { + return Ok(()); + }; + let Some(lit) = f.tick(&self.res.recipes, &self.res.items, &self.res.providers) else { + return Ok(()); + }; + let state = self.world.block(pos.0, pos.1, pos.2); + if let Some(mut s) = self.pack.assets.registry.state(state).cloned() + && s.properties.contains_key("lit") + { + s.properties.insert("lit".into(), lit.to_string()); + if let Ok(id) = self.pack.assets.registry.resolve(&s) { + self.world.set(pos.0, pos.1, pos.2, id)?; + } + } + Ok(()) + } +} diff --git a/crates/horizon-client/src/game/command_blocks.rs b/crates/horizon-client/src/game/command_blocks.rs index 5a5fa11c8..92e7d9979 100644 --- a/crates/horizon-client/src/game/command_blocks.rs +++ b/crates/horizon-client/src/game/command_blocks.rs @@ -598,13 +598,13 @@ impl Game<'_> { if let Ok(id) = registry.resolve(&wanted) && wanted != *current { - let entity = self.world.block_entities.remove(&pos); + let entity = self.world.block_entities.take(&pos); let at = BlockPos::new(pos.0, pos.1, pos.2); self.with_level(|b, level, ticks| { b.set_block(level, ticks, at, id, flags::UPDATE_CLIENTS) }); if let Some(entity) = entity { - self.world.block_entities.insert(pos, entity); + self.world.block_entities.put(pos, entity); } let _ = self.process_level_events(); } diff --git a/crates/horizon-client/src/game/creaking.rs b/crates/horizon-client/src/game/creaking.rs index 099f3d7d8..e9d04b6fd 100644 --- a/crates/horizon-client/src/game/creaking.rs +++ b/crates/horizon-client/src/game/creaking.rs @@ -164,7 +164,7 @@ impl Game<'_> { return; } let key = (home[0], home[1], home[2]); - let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.remove(&key) + let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.take(&key) else { return; }; @@ -173,7 +173,7 @@ impl Game<'_> { }); self.world .block_entities - .insert(key, BlockEntity::CreakingHeart(heart)); + .put(key, BlockEntity::CreakingHeart(heart)); } /// `Creaking.tearDown`: the crumble particles. pub(super) fn creaking_tear_down(&mut self, center: [f64; 3], spread: [f64; 3]) { @@ -220,7 +220,7 @@ impl Game<'_> { /// death and a natural heart drops experience. pub(super) fn creaking_heart_broken(&mut self, pos: [i32; 3]) { let key = (pos[0], pos[1], pos[2]); - let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.remove(&key) + let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.take(&key) else { return; }; @@ -248,43 +248,35 @@ impl Game<'_> { } self.world .block_entities - .insert(key, BlockEntity::CreakingHeart(heart)); + .put(key, BlockEntity::CreakingHeart(heart)); } - /// `CreakingHeartBlockEntity.serverTick` for every heart. - pub(super) fn tick_creaking_hearts(&mut self) -> Result<()> { - let keys: Vec = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::CreakingHeart(_))) - .map(|(k, _)| *k) - .collect(); - for key in keys { - let Some(BlockEntity::CreakingHeart(mut heart)) = - self.world.block_entities.remove(&key) - else { - continue; - }; - let state = self.world.block(key.0, key.1, key.2); - if &*self.res.facts.get(state).name != "minecraft:creaking_heart" { - // The block is gone: preRemoveSideEffects. - self.with_heart(key, &mut heart, |h, _, _, host, out| { - creaking_heart::remove_protector(h, host, false, out); - })?; - continue; - } - // getTicker: none once uprooted. - if self.heart_state_name(state) != "uprooted" { - let pos = [key.0, key.1, key.2]; - self.with_heart(key, &mut heart, |h, w, lr, host, out| { - creaking_heart::server_tick(h, pos, w, lr, host, out); - })?; - } - self.world - .block_entities - .insert(key, BlockEntity::CreakingHeart(heart)); + /// `CreakingHeartBlockEntity.serverTick` of the heart at `key`. Its slot + /// stays while uprooted, so that a heart whose block went away is still + /// found here and lets go of its protector. + pub(super) fn tick_creaking_heart(&mut self, key: Pos) -> Result<()> { + let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.take(&key) + else { + return Ok(()); + }; + let state = self.world.block(key.0, key.1, key.2); + if &*self.res.facts.get(state).name != "minecraft:creaking_heart" { + // The block is gone: preRemoveSideEffects. + self.world.block_entities.forget(&key); + self.with_heart(key, &mut heart, |h, _, _, host, out| { + creaking_heart::remove_protector(h, host, false, out); + })?; + return Ok(()); } - self.scan_for_hearts(); + // getTicker: none once uprooted. + if self.heart_state_name(state) != "uprooted" { + let pos = [key.0, key.1, key.2]; + self.with_heart(key, &mut heart, |h, w, lr, host, out| { + creaking_heart::server_tick(h, pos, w, lr, host, out); + })?; + } + self.world + .block_entities + .put(key, BlockEntity::CreakingHeart(heart)); Ok(()) } /// `ServerLevel.isSpawningMonsters`. @@ -295,7 +287,7 @@ impl Game<'_> { } /// Hearts that came with generated or loaded chunks get their block /// entity (`newBlockEntity`) the first time their chunk is seen. - fn scan_for_hearts(&mut self) { + pub(super) fn scan_for_hearts(&mut self) { let Some(heart) = self .pack .assets @@ -344,8 +336,7 @@ impl Game<'_> { ); self.world .block_entities - .entry(pos) - .or_insert_with(|| BlockEntity::CreakingHeart(Box::default())); + .get_or_insert_with(pos, || BlockEntity::CreakingHeart(Box::default())); } } } diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs index ee8170bc5..fbad3cfc7 100644 --- a/crates/horizon-client/src/game/dispense_fixture.rs +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -318,8 +318,18 @@ impl Game<'_> { } else { self.dispense_from(origin, state)?; // CrafterBlockEntity.serverTick, the crafting animation running down. + let mut crafters: Vec<(i32, i32, i32)> = self + .world + .block_entities + .iter() + .filter(|(_, e)| matches!(e, BlockEntity::Crafter(_))) + .map(|(p, _)| *p) + .collect(); + crafters.sort_unstable(); for _ in 0..case["ticks"].as_u64().unwrap_or(0) { - self.tick_crafters(); + for &p in &crafters { + self.tick_crafter(p); + } } } diff --git a/crates/horizon-client/src/game/jukebox.rs b/crates/horizon-client/src/game/jukebox.rs index 235ad7c24..ba7d2b786 100644 --- a/crates/horizon-client/src/game/jukebox.rs +++ b/crates/horizon-client/src/game/jukebox.rs @@ -140,63 +140,53 @@ impl Game<'_> { pub(super) fn jukebox_removed(&mut self, pos: [i32; 3]) { self.stop_song(pos); } - /// `JukeboxSongPlayer.tick` for every playing jukebox. - pub(super) fn tick_jukeboxes(&mut self) { - let playing: Vec<((i32, i32, i32), String, i64)> = self - .world - .block_entities - .iter() - .filter_map(|(k, e)| match e { - BlockEntity::Jukebox(j) if j.playing => { - let song = self - .res - .items - .get(&j.record.as_ref()?.item)? - .jukebox_song - .clone()?; - Some((*k, song, j.ticks)) - } - _ => None, - }) - .collect(); - for (key, song, ticks) in playing { - let pos = [key.0, key.1, key.2]; - let Some(s) = self.res.items.songs.get(&song).cloned() else { - continue; - }; - if s.finished(ticks) { - if let Some(BlockEntity::Jukebox(j)) = self.world.block_entities.get_mut(&key) { - j.playing = false; - j.ticks = 0; - } - self.stop_song(pos); - self.jukebox_changed(pos); - continue; - } - if !self.jukebox_sounds.contains_key(&key) && ticks == 0 { - self.start_song(pos, &song); - } - if ticks % 20 == 0 { - let color = f64::from(self.world.random.next_int_bound(4).unwrap_or(0)) / 24.0; - let at = [ - f64::from(pos[0]) + 0.5, - f64::from(pos[1]) + 1.2, - f64::from(pos[2]) + 0.5, - ]; - self.particles.add( - &ParticleOptions::simple(ParticleType::Note), - at, - [color, 0.0, 0.0], - ); - } + /// `JukeboxSongPlayer.tick` of the jukebox at `key`, while it plays. + pub(super) fn tick_jukebox(&mut self, key: (i32, i32, i32)) { + let playing = match self.world.block_entities.get(&key) { + Some(BlockEntity::Jukebox(j)) if j.playing => j.record.as_ref().and_then(|r| { + let song = self.res.items.get(&r.item)?.jukebox_song.clone()?; + Some((song, j.ticks)) + }), + _ => None, + }; + let Some((song, ticks)) = playing else { + return; + }; + let pos = [key.0, key.1, key.2]; + let Some(s) = self.res.items.songs.get(&song).cloned() else { + return; + }; + if s.finished(ticks) { if let Some(BlockEntity::Jukebox(j)) = self.world.block_entities.get_mut(&key) { - j.ticks += 1; + j.playing = false; + j.ticks = 0; } + self.stop_song(pos); + self.jukebox_changed(pos); + return; + } + if !self.jukebox_sounds.contains_key(&key) && ticks == 0 { + self.start_song(pos, &song); + } + if ticks % 20 == 0 { + let color = f64::from(self.world.random.next_int_bound(4).unwrap_or(0)) / 24.0; + let at = [ + f64::from(pos[0]) + 0.5, + f64::from(pos[1]) + 1.2, + f64::from(pos[2]) + 0.5, + ]; + self.particles.add( + &ParticleOptions::simple(ParticleType::Note), + at, + [color, 0.0, 0.0], + ); + } + if let Some(BlockEntity::Jukebox(j)) = self.world.block_entities.get_mut(&key) { + j.ticks += 1; } - self.update_song_volumes(); } /// Let go of songs that ended or whose jukebox stopped. - fn update_song_volumes(&mut self) { + pub(super) fn update_song_volumes(&mut self) { let sounds: Vec<_> = self .jukebox_sounds .iter() diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index a736be2db..d46928060 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -497,42 +497,35 @@ impl Game<'_> { } } } - /// Every hopper's `pushItemsTick`, in a stable position order. - pub(super) fn tick_hoppers(&mut self) -> Result<()> { + /// `HopperBlockEntity.pushItemsTick` of the hopper at `pos`, if its block + /// is still a hopper (`BlockEntityType.isValid`). + pub(super) fn tick_hopper(&mut self, pos: Pos) { let hopper = self - .pack - .assets - .registry - .default_state("minecraft:hopper") - .map(|s| self.res.facts.get(s).name.clone()); - let Some(hopper) = hopper else { return Ok(()) }; - // A hopper's block entity is the plain container with five slots - // (`BlockEntity::for_block`; `BlockEntity::id` names it the hopper): - // only those need their block looked up (the ticker runs for a - // HopperBlockEntity whose block is still a hopper, - // `BlockEntityType.isValid`). - let mut positions: Vec = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Chest(items) if items.len() == HOPPER_SLOTS)) - .map(|(p, _)| *p) - .filter(|p| self.res.facts.get(self.world.block(p.0, p.1, p.2)).name == hopper) - .collect(); - positions.sort_unstable(); - self.hoppers - .retain(|p, _| positions.binary_search(p).is_ok()); - let time = self.world.game_time; - for pos in positions { - let state = self.hoppers.entry(pos).or_default(); - state.cooldown -= 1; - state.ticked = time; - if state.cooldown <= 0 { - state.cooldown = 0; - self.hopper_try_move_items(pos); - } + .res + .facts + .get(self.world.block(pos.0, pos.1, pos.2)) + .name + .as_ref() + == "minecraft:hopper"; + if !hopper { + self.hoppers.remove(&pos); + return; } - Ok(()) + let time = self.world.game_time; + let state = self.hoppers.entry(pos).or_default(); + state.cooldown -= 1; + state.ticked = time; + if state.cooldown <= 0 { + state.cooldown = 0; + self.hopper_try_move_items(pos); + } + } + /// Drops the cooldowns of hoppers whose block entity is gone. + pub(super) fn forget_gone_hoppers(&mut self) { + let entities = &self.world.block_entities; + self.hoppers.retain(|p, _| { + matches!(entities.get(p), Some(BlockEntity::Chest(items)) if items.len() == HOPPER_SLOTS) + }); } /// `HopperBlockEntity.tryMoveItems` with `suckInItems`. pub(super) fn hopper_try_move_items(&mut self, pos: Pos) { diff --git a/crates/horizon-client/src/game/machines/crafter.rs b/crates/horizon-client/src/game/machines/crafter.rs index 11ca70d5d..5642e5c2a 100644 --- a/crates/horizon-client/src/game/machines/crafter.rs +++ b/crates/horizon-client/src/game/machines/crafter.rs @@ -56,44 +56,33 @@ impl Game<'_> { _ => None, } } - /// `CrafterBlockEntity.serverTick` for every crafter, and the powered - /// flag following the block state. - pub(in crate::game) fn tick_crafters(&mut self) { - let positions: Vec = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Crafter(_))) - .map(|(p, _)| *p) - .collect(); - let mut positions = positions; - positions.sort_unstable(); - for pos in positions { - let state = self.world.block(pos.0, pos.1, pos.2); - let triggered = self.containers().prop(state, "triggered") == Some("true"); - let mut finished = false; - if let Some(BlockEntity::Crafter(c)) = self.world.block_entities.get_mut(&pos) { - // CrafterBlock.neighborChanged sets the flag with the state; a - // loaded crafter keeps its saved flag until then. - if c.state_triggered.is_some_and(|t| t != triggered) { - c.triggered = triggered; - } - c.state_triggered = Some(triggered); - let remaining = c.crafting_ticks - 1; - if remaining >= 0 { - c.crafting_ticks = remaining; - finished = remaining == 0; - } + /// `CrafterBlockEntity.serverTick` of the crafter at `pos`, and the + /// powered flag following the block state. + pub(in crate::game) fn tick_crafter(&mut self, pos: Pos) { + let state = self.world.block(pos.0, pos.1, pos.2); + let triggered = self.containers().prop(state, "triggered") == Some("true"); + let mut finished = false; + if let Some(BlockEntity::Crafter(c)) = self.world.block_entities.get_mut(&pos) { + // CrafterBlock.neighborChanged sets the flag with the state; a + // loaded crafter keeps its saved flag until then. + if c.state_triggered.is_some_and(|t| t != triggered) { + c.triggered = triggered; } - if finished { - self.set_bool_prop( - pos, - "crafting", - false, - horizon_world::level::flags::UPDATE_ALL, - ); + c.state_triggered = Some(triggered); + let remaining = c.crafting_ticks - 1; + if remaining >= 0 { + c.crafting_ticks = remaining; + finished = remaining == 0; } } + if finished { + self.set_bool_prop( + pos, + "crafting", + false, + horizon_world::level::flags::UPDATE_ALL, + ); + } } /// `CrafterBlock.dispenseFrom` (the scheduled tick after a redstone pulse). pub(super) fn crafter_dispense(&mut self, pos: BlockPos, state: BlockStateId) -> Result<()> { diff --git a/crates/horizon-client/src/game/render_control.rs b/crates/horizon-client/src/game/render_control.rs index e9a08c6a2..21a5aee46 100644 --- a/crates/horizon-client/src/game/render_control.rs +++ b/crates/horizon-client/src/game/render_control.rs @@ -127,7 +127,7 @@ impl Game<'_> { let entity = horizon_client::containers::BlockEntity::for_block(&name) .filter(|e| matches!(e, horizon_client::containers::BlockEntity::Sign(_))) .ok_or("no sign there")?; - self.world.block_entities.entry(key).or_insert(entity); + self.world.block_entities.get_or_insert_with(key, || entity); self.open_sign_editor(key, words.get(4) != Some(&"back")); Ok(Some(json!({ "ok": true }))) } diff --git a/crates/horizon-client/src/game/spawner.rs b/crates/horizon-client/src/game/spawner.rs index c42299c86..a2131f9b0 100644 --- a/crates/horizon-client/src/game/spawner.rs +++ b/crates/horizon-client/src/game/spawner.rs @@ -45,7 +45,7 @@ impl Game<'_> { .as_ref() .is_some_and(|t| t.contains(id)) } - fn load_entity_types(&mut self) { + pub(super) fn load_entity_types(&mut self) { if self.spawners.entity_types.is_some() { return; } @@ -63,32 +63,27 @@ impl Game<'_> { .unwrap_or_default(); self.spawners.entity_types = Some(types); } - /// Every spawner's server tick, then its client tick. - pub(super) fn tick_spawners(&mut self) -> Result<()> { - self.load_entity_types(); - let keys: Vec = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Spawner(_))) - .map(|(k, _)| *k) - .collect(); - for key in &keys { - let state = self.world.block(key.0, key.1, key.2); - if &*self.res.facts.get(state).name != "minecraft:spawner" { - continue; - } - let Some(BlockEntity::Spawner(mut spawner)) = self.world.block_entities.remove(key) - else { - continue; - }; - let result = self.server_tick_spawner(*key, &mut spawner); - self.world - .block_entities - .insert(*key, BlockEntity::Spawner(spawner)); - result?; + /// The server tick of the spawner at `key`, if its block is still a + /// spawner. Its client tick comes after every block entity's + /// (`client_tick_spawners`). + pub(super) fn tick_spawner(&mut self, key: Pos) -> Result<()> { + let state = self.world.block(key.0, key.1, key.2); + if &*self.res.facts.get(state).name != "minecraft:spawner" { + return Ok(()); } - for key in keys { + let Some(BlockEntity::Spawner(mut spawner)) = self.world.block_entities.take(&key) else { + return Ok(()); + }; + let result = self.server_tick_spawner(key, &mut spawner); + self.world + .block_entities + .put(key, BlockEntity::Spawner(spawner)); + result + } + /// The client ticks of these spawners, then the shown mobs of spawners + /// that are gone go. + pub(super) fn client_tick_spawners(&mut self, keys: &[Pos]) { + for &key in keys { self.client_tick_spawner(key); } let entities = &self.world.block_entities; @@ -98,7 +93,6 @@ impl Game<'_> { Some(BlockEntity::Spawner(_) | BlockEntity::TrialSpawner(_)) ) }); - Ok(()) } fn server_tick_spawner(&mut self, key: Pos, spawner: &mut Spawner) -> Result<()> { let pos = [key.0, key.1, key.2]; diff --git a/crates/horizon-client/src/game/trial_spawner.rs b/crates/horizon-client/src/game/trial_spawner.rs index 4107e0859..42a26c6e4 100644 --- a/crates/horizon-client/src/game/trial_spawner.rs +++ b/crates/horizon-client/src/game/trial_spawner.rs @@ -106,34 +106,21 @@ impl TrialSpawnerLevel for GameLevel<'_, '_> { } impl Game<'_> { - /// Every trial spawner's server tick. - pub(super) fn tick_trial_spawners(&mut self) -> Result<()> { - let keys: Vec = self - .world + /// The server tick of the trial spawner at `key`. Its client tick comes + /// after every block entity's. + pub(super) fn tick_trial_spawner(&mut self, key: Pos) -> Result<()> { + let Some((current, ominous)) = self.trial_spawner_block(key) else { + return Ok(()); + }; + let Some(BlockEntity::TrialSpawner(mut spawner)) = self.world.block_entities.take(&key) + else { + return Ok(()); + }; + let result = self.server_tick_trial_spawner(key, &mut spawner, current, ominous); + self.world .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::TrialSpawner(_))) - .map(|(k, _)| *k) - .collect(); - for &key in &keys { - let Some((current, ominous)) = self.trial_spawner_block(key) else { - continue; - }; - let Some(BlockEntity::TrialSpawner(mut spawner)) = - self.world.block_entities.remove(&key) - else { - continue; - }; - let result = self.server_tick_trial_spawner(key, &mut spawner, current, ominous); - self.world - .block_entities - .insert(key, BlockEntity::TrialSpawner(spawner)); - result?; - } - for key in keys { - self.client_tick_trial_spawner(key); - } - Ok(()) + .put(key, BlockEntity::TrialSpawner(spawner)); + result } /// The trial spawner block at `key`: its state and ominous flag. pub(super) fn trial_spawner_block(&self, key: Pos) -> Option<(TrialState, bool)> { diff --git a/crates/horizon-client/src/game/trial_visuals.rs b/crates/horizon-client/src/game/trial_visuals.rs index d32957fe1..cac28b5cc 100644 --- a/crates/horizon-client/src/game/trial_visuals.rs +++ b/crates/horizon-client/src/game/trial_visuals.rs @@ -237,7 +237,7 @@ impl Game<'_> { let shows = vault.display_item.is_some(); self.world .block_entities - .insert(key, BlockEntity::Vault(vault)); + .put(key, BlockEntity::Vault(vault)); let pos = [key.0, key.1, key.2]; if self.world.game_time % 20 == 0 { self.vault_connection_particles(pos); diff --git a/crates/horizon-client/src/game/vault.rs b/crates/horizon-client/src/game/vault.rs index 31d7d3ea3..8cf67804d 100644 --- a/crates/horizon-client/src/game/vault.rs +++ b/crates/horizon-client/src/game/vault.rs @@ -61,7 +61,7 @@ impl Game<'_> { .and_then(|s| VaultState::from_name(s)) .unwrap_or_default(); let ominous = block.properties.get("ominous").is_some_and(|v| v == "true"); - let Some(BlockEntity::Vault(vault)) = self.world.block_entities.remove(&key) else { + let Some(BlockEntity::Vault(vault)) = self.world.block_entities.take(&key) else { return None; }; Some((vault, current, ominous)) @@ -118,28 +118,19 @@ impl Game<'_> { self.rand_sequences = sequences; self.world .block_entities - .insert(key, BlockEntity::Vault(vault)); + .put(key, BlockEntity::Vault(vault)); Some(self.vault_events(key, events).map(|()| out)) } - /// Every vault's server tick, then its client tick. - pub(super) fn tick_vaults(&mut self) -> Result<()> { - let keys: Vec = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Vault(_))) - .map(|(k, _)| *k) - .collect(); - for key in keys { - let pos = [key.0, key.1, key.2]; - let ticked = self.with_vault(key, |v, l, random, state, ominous, roll, events| { - v.server_tick(l, random, pos, state, ominous, roll, events); - }); - if let Some(result) = ticked { - result?; - } - self.client_tick_vault(key); + /// The server tick of the vault at `key`, then its client tick. + pub(super) fn tick_vault(&mut self, key: Pos) -> Result<()> { + let pos = [key.0, key.1, key.2]; + let ticked = self.with_vault(key, |v, l, random, state, ominous, roll, events| { + v.server_tick(l, random, pos, state, ominous, roll, events); + }); + if let Some(result) = ticked { + result?; } + self.client_tick_vault(key); Ok(()) } /// `VaultBlock.useItemOn` with `held` in the hand that acts: true when the diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 45e09a6a3..c51aa09ce 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -16,6 +16,7 @@ pub mod banner; pub mod beehive; pub mod benchmark; pub mod biome; +pub mod block_entities; pub mod block_entity_anim; pub mod block_entity_item; pub mod block_entity_nbt; diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 38fa33c7d..7b3f3dd50 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -589,7 +589,7 @@ impl Streaming { } world .block_entities - .extend(std::mem::take(&mut chunk.block_entities)); + .load_chunk(std::mem::take(&mut chunk.block_entities)); world .block_names .extend(std::mem::take(&mut chunk.block_names)); diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index 05b89234c..f6fbcd04d 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -26,7 +26,8 @@ pub struct World { pub loaded_entities: Vec<((i32, i32), Vec)>, /// Chunks changed since the last save. pub modified: HashSet<(i32, i32)>, - pub block_entities: HashMap<(i32, i32, i32), crate::containers::BlockEntity>, + /// Block entities, and the order they tick in. + pub block_entities: crate::block_entities::BlockEntities, /// Each chest's `ContainerOpenersCounter`: opener count and largest /// interaction range. pub chest_openers: HashMap<(i32, i32, i32), (i32, f64)>, @@ -122,7 +123,7 @@ impl World { light_edits: Vec::new(), loaded_entities: Vec::new(), modified: HashSet::new(), - block_entities: HashMap::new(), + block_entities: Default::default(), chest_openers: HashMap::new(), block_names: HashMap::new(), watched: Vec::new(), @@ -200,6 +201,8 @@ impl World { } let previous = Arc::make_mut(chunk).set(x & 15, y, z & 15, state.0); if previous != state.0 { + self.block_entities + .state_changed((x, y, z), previous, state.0); let watches = |id: u32| self.watched.get(id as usize).copied().unwrap_or(false); if watches(previous) || watches(state.0) { self.watched_writes.push(((x, y, z), previous, state.0)); @@ -240,6 +243,16 @@ impl World { .get(&(x >> 4, z >> 4)) .map_or(self.air, |chunk| chunk.block(x & 15, y, z & 15)) } + /// The block entities to tick this tick, in order (`BlockEntities::tickers`). + pub fn block_entity_tickers(&mut self) -> Vec<(crate::block_entities::Pos, u32)> { + let (chunks, air) = (&self.chunks, self.air); + self.block_entities.tickers(|(x, y, z)| { + chunks + .get(&(x >> 4, z >> 4)) + .map_or(air, |chunk| chunk.block(x & 15, y, z & 15)) + .0 + }) + } pub fn light(&self, x: i32, y: i32, z: i32) -> u8 { self.chunks .get(&(x >> 4, z >> 4)) diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockEntityTickerOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockEntityTickerOracle.java new file mode 100644 index 000000000..75330c4fe --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockEntityTickerOracle.java @@ -0,0 +1,217 @@ +package dev.horizonlab.parity; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.Identifier; +import net.minecraft.util.debug.LevelDebugSynchronizers; +import net.minecraft.world.TickRateManager; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.EntityBlock; +import net.minecraft.world.level.block.JukeboxBlock; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.TickingBlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.ChunkAccess; +import net.minecraft.world.level.chunk.LevelChunk; + +/** + * The order of vanilla's `Level.blockEntityTickers`: real `LevelChunk`s of a + * server level register, rebind and drop the tickers of real block entities + * (`addAndRegisterBlockEntity`, `setBlockEntity`, `removeBlockEntity`, + * `updateBlockEntityTicker` after a state change, `registerAllBlockEntitiesAfterLevelLoad` + * after a chunk load, `clearAllBlockEntities` when it unloads), and + * `Level.tickBlockEntities` runs with the tick rate frozen, so it only + * promotes pending tickers and drops removed ones. After each `tick` the + * positions of the live tickers are printed in list order. + * + * Input lines: `reset`, `load CX CZ BLOCK X Y Z ...` (a chunk's block entities in + * its save order), `place BLOCK X Y Z`, `replace X Y Z`, `remove X Y Z`, + * `record X Y Z 0|1` (a jukebox's has_record), `unload CX CZ`, `tick`. + */ +public final class VanillaBlockEntityTickerOracle { + static Object unsafe; + static Method allocate; + + static T alloc(Class c) throws Exception { + return c.cast(allocate.invoke(unsafe, c)); + } + + static void set(Class owner, Object target, String name, Object value) throws Exception { + Field f = owner.getDeclaredField(name); + f.setAccessible(true); + f.set(target, value); + } + + static Object get(Class owner, Object target, String name) throws Exception { + Field f = owner.getDeclaredField(name); + f.setAccessible(true); + return f.get(target); + } + + static Map cells = new HashMap<>(); + static Map chunks = new HashMap<>(); + static World world; + + static final class World extends net.minecraft.server.level.ServerLevel { + TickRateManager rate; + LevelDebugSynchronizers debug; + World() { + super(null, null, null, null, null, null, false, 0L, List.of(), false); + } + @Override public boolean isClientSide() { return false; } + @Override public TickRateManager tickRateManager() { return rate; } + @Override public LevelDebugSynchronizers debugSynchronizers() { return debug; } + @Override public void onBlockEntityAdded(BlockEntity blockEntity) { } + @Override public void levelEvent(net.minecraft.world.entity.Entity source, int type, BlockPos at, int data) { } + @Override public void gameEvent(net.minecraft.core.Holder event, + net.minecraft.world.phys.Vec3 at, net.minecraft.world.level.gameevent.GameEvent.Context context) { } + @Override public BlockState getBlockState(BlockPos p) { return cells.getOrDefault(p, Blocks.AIR.defaultBlockState()); } + @Override public void playSeededSound(net.minecraft.world.entity.Entity except, double x, double y, double z, + net.minecraft.core.Holder sound, net.minecraft.sounds.SoundSource source, + float volume, float pitch, long seed) { } + @Override public int getMinY() { return -64; } + @Override public int getHeight() { return 384; } + } + + static final class Chunk extends LevelChunk { + Chunk() { + super(null, null); + } + @Override public BlockState getBlockState(BlockPos p) { return cells.getOrDefault(p, Blocks.AIR.defaultBlockState()); } + @Override public void markUnsaved() { } + } + + static void reset() throws Exception { + cells = new HashMap<>(); + chunks = new HashMap<>(); + world = alloc(World.class); + set(Level.class, world, "blockEntityTickers", new ArrayList()); + set(Level.class, world, "pendingBlockEntityTickers", new ArrayList()); + set(Level.class, world, "soundSeedGenerator", net.minecraft.util.RandomSource.create(0)); + TickRateManager rate = new TickRateManager(); + set(TickRateManager.class, rate, "runGameElements", false); + world.rate = rate; + LevelDebugSynchronizers debug = alloc(LevelDebugSynchronizers.class); + set(LevelDebugSynchronizers.class, debug, "sleeping", true); + world.debug = debug; + } + + static Chunk chunk(int cx, int cz) throws Exception { + Chunk c = chunks.get(ChunkPos.pack(cx, cz)); + if (c != null) return c; + c = alloc(Chunk.class); + set(ChunkAccess.class, c, "blockEntities", new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap()); + set(ChunkAccess.class, c, "pendingBlockEntities", new HashMap()); + set(ChunkAccess.class, c, "chunkPos", new ChunkPos(cx, cz)); + set(LevelChunk.class, c, "level", world); + set(LevelChunk.class, c, "tickersInLevel", new HashMap<>()); + chunks.put(ChunkPos.pack(cx, cz), c); + return c; + } + + /** The chunk at `p`, in the level (a chunk first seen here was already loaded). */ + static Chunk at(BlockPos p) throws Exception { + Chunk c = chunk(p.getX() >> 4, p.getZ() >> 4); + c.setLoaded(true); + return c; + } + + /** The block's state: a jukebox's has_record from the name (`jukebox_record`). */ + static BlockState state(String name) { + boolean record = name.equals("jukebox_record"); + Block block = BuiltInRegistries.BLOCK.getValue(Identifier.withDefaultNamespace(record ? "jukebox" : name)); + BlockState s = block.defaultBlockState(); + return block instanceof JukeboxBlock ? s.setValue(JukeboxBlock.HAS_RECORD, record) : s; + } + + static BlockEntity make(BlockPos p) { + BlockState s = cells.get(p); + return ((EntityBlock) s.getBlock()).newBlockEntity(p, s); + } + + static BlockPos pos(String[] w, int i) { + return new BlockPos(Integer.parseInt(w[i]), Integer.parseInt(w[i + 1]), Integer.parseInt(w[i + 2])); + } + + public static void main(String[] args) throws Exception { + // Registries with item components bound (vaults hold item stacks). + ParityData.init(); + var allocator = Class.forName("sun.misc.Unsafe"); + var field = allocator.getDeclaredField("theUnsafe"); + field.setAccessible(true); + unsafe = field.get(null); + allocate = allocator.getMethod("allocateInstance", Class.class); + Method update = LevelChunk.class.getDeclaredMethod("updateBlockEntityTicker", BlockEntity.class); + update.setAccessible(true); + StringBuilder out = new StringBuilder(); + for (String line : Files.readAllLines(Path.of(args[0]))) { + String[] w = line.trim().split(" "); + switch (w[0]) { + case "reset" -> reset(); + case "load" -> { + // SerializableChunkData.postLoadChunk, then ChunkStatusTasks.full. + Chunk c = chunk(Integer.parseInt(w[1]), Integer.parseInt(w[2])); + for (int i = 3; i + 3 < w.length; i += 4) { + BlockPos p = pos(w, i + 1); + cells.put(p, state(w[i])); + c.setBlockEntity(make(p)); + } + c.setLoaded(true); + c.registerAllBlockEntitiesAfterLevelLoad(); + } + case "place" -> { + // LevelChunk.setBlockState: a new block entity block. + BlockPos p = pos(w, 2); + cells.put(p, state(w[1])); + at(p).addAndRegisterBlockEntity(make(p)); + } + case "replace" -> { + BlockPos p = pos(w, 1); + at(p).addAndRegisterBlockEntity(make(p)); + } + case "remove" -> { + BlockPos p = pos(w, 1); + at(p).removeBlockEntity(p); + cells.remove(p); + } + case "record" -> { + // LevelChunk.setBlockState: the same block, another state. + BlockPos p = pos(w, 1); + BlockState s = state(w[4].equals("1") ? "jukebox_record" : "jukebox"); + cells.put(p, s); + BlockEntity be = at(p).getBlockEntity(p); + be.setBlockState(s); + update.invoke(at(p), be); + } + case "unload" -> { + Chunk c = chunks.remove(ChunkPos.pack(Integer.parseInt(w[1]), Integer.parseInt(w[2]))); + if (c != null) c.clearAllBlockEntities(); + } + case "tick" -> { + world.tickBlockEntities(); + @SuppressWarnings("unchecked") + List list = (List) get(Level.class, world, "blockEntityTickers"); + out.append("tick"); + for (TickingBlockEntity t : list) { + BlockPos p = t.getPos(); + out.append(' ').append(p.getX()).append(',').append(p.getY()).append(',').append(p.getZ()); + } + out.append('\n'); + } + default -> throw new IllegalArgumentException(line); + } + } + Files.writeString(Path.of(args[1]), out); + } +} diff --git a/tools/test_block_entity_tickers_parity.py b/tools/test_block_entity_tickers_parity.py new file mode 100644 index 000000000..58604b269 --- /dev/null +++ b/tools/test_block_entity_tickers_parity.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 +"""Compare the order block entities tick in with vanilla's `Level.blockEntityTickers`. + +Random scenarios load chunks with block entities in a save order (enough in +one chunk to make its block entity map grow), place, replace and remove block +entities, give jukeboxes a record and take it, and unload chunks, with ticks +in between. `VanillaBlockEntityTickerOracle` runs them on real `LevelChunk`s +of a server level and prints, after each tick, the positions of the live +tickers in list order; `examples/block_entity_tickers_fixture` runs the same +on `horizon_client::block_entities::BlockEntities`. The block entities are +the kinds the game ticks in that list (hoppers, furnaces, beacons, spawners, +trial spawners, vaults, hives, crafters, jukebox), with chests and barrels, +which have no ticker, among them. +""" +import argparse +import os +import random +import subprocess +import sys +try: + from . import lab, test_rust_parity as base +except ImportError: + import lab + import test_rust_parity as base + +PARITY = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity' +TICKING = ['hopper', 'furnace', 'blast_furnace', 'smoker', 'beacon', 'spawner', 'trial_spawner', 'vault', + 'beehive', 'bee_nest', 'crafter', 'jukebox_record'] +KINDS = TICKING + ['jukebox', 'chest', 'barrel'] + + +def scenario(rng): + """One scenario's lines: a few chunks near the origin and the ops on them.""" + lines = ['reset'] + loaded, blocks = set(), {} + + def free(cx, cz): + while True: + p = (cx * 16 + rng.randrange(16), rng.randrange(-64, 320), cz * 16 + rng.randrange(16)) + if p not in blocks: + return p + + def load(): + options = [(x, z) for x in range(-2, 3) for z in range(-2, 3) if (x, z) not in loaded] + if not options: + return + cx, cz = rng.choice(options) + loaded.add((cx, cz)) + words = ['load', str(cx), str(cz)] + for _ in range(rng.choice([0, 1, 3, 8, 20, 30, 60])): + p, kind = free(cx, cz), rng.choice(KINDS) + blocks[p] = kind + words += [kind, *map(str, p)] + lines.append(' '.join(words)) + for _ in range(rng.randrange(1, 4)): + load() + lines.append('tick') + for _ in range(rng.randrange(10, 60)): + op = rng.random() + mine = sorted(blocks) + if op < 0.25 and loaded: + cx, cz = rng.choice(sorted(loaded)) + p, kind = free(cx, cz), rng.choice(KINDS) + blocks[p] = kind + lines.append(f'place {kind} {p[0]} {p[1]} {p[2]}') + elif op < 0.4 and mine: + p = rng.choice(mine) + lines.append(f'replace {p[0]} {p[1]} {p[2]}') + elif op < 0.6 and mine: + p = rng.choice(mine) + del blocks[p] + lines.append(f'remove {p[0]} {p[1]} {p[2]}') + elif op < 0.72: + jukeboxes = [p for p in mine if blocks[p].startswith('jukebox')] + if jukeboxes: + p = rng.choice(jukeboxes) + record = blocks[p] == 'jukebox' + blocks[p] = 'jukebox_record' if record else 'jukebox' + lines.append(f'record {p[0]} {p[1]} {p[2]} {int(record)}') + elif op < 0.78 and loaded: + cx, cz = rng.choice(sorted(loaded)) + loaded.discard((cx, cz)) + for p in [p for p in blocks if (p[0] >> 4, p[2] >> 4) == (cx, cz)]: + del blocks[p] + lines.append(f'unload {cx} {cz}') + elif op < 0.85: + load() + else: + lines.append('tick') + lines.append('tick') + return lines + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--scenarios', type=int, default=3000) + parser.add_argument('--seed', type=int, default=0x7469636b) + args = parser.parse_args() + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / 'rust-block-entity-tickers-parity' + build.mkdir(parents=True, exist_ok=True) + rng = random.Random(args.seed) + lines = [line for _ in range(args.scenarios) for line in scenario(rng)] + inputs = build / 'inputs.txt' + inputs.write_text('\n'.join(lines) + '\n') + classes = build / 'classes' + classes.mkdir(exist_ok=True) + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + + def run(command, name): + with (build / (name + '.log')).open('w') as log: + result = subprocess.run(list(map(str, command)), cwd=lab.ROOT, timeout=3600, stdout=log, + stderr=subprocess.STDOUT) + if result.returncode: + print((build / (name + '.log')).read_text()[-4000:]) + sys.exit(f'{name} failed') + run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-cp', dependencies, + '-d', classes, *(PARITY / f'{n}.java' for n in ('ParityData', 'VanillaBlockEntityTickerOracle'))], + 'compile-java') + run(['java', '-Xmx1G', '-cp', str(classes) + os.pathsep + dependencies, + 'dev.horizonlab.parity.VanillaBlockEntityTickerOracle', inputs, build / 'vanilla.txt'], 'vanilla') + run(['cargo', 'run', '--quiet', '--profile', 'fast', '-p', 'horizon-client', '--example', + 'block_entity_tickers_fixture', '--', inputs, build / 'rust.txt'], 'rust') + matched = base.compare(build / 'vanilla.txt', build / 'rust.txt') + ticks = sum(1 for line in lines if line == 'tick') + listed = sum(len(line.split()) - 1 for line in (build / 'vanilla.txt').read_text().splitlines()) + print(f'block entity tickers: {args.scenarios} scenarios, {matched} of {ticks} ticks match vanilla ' + f'({listed} tickers listed)') + + +if __name__ == '__main__': + main() From e92f74f01dd53a53938e01b778a5b5868d90c538 Mon Sep 17 00:00:00 2001 From: lazy-importer Date: Sun, 18 Oct 2026 00:12:39 +0200 Subject: [PATCH 18/67] fix(minecarts): use per-kind display offset in cart geometry (#741) --- crates/horizon-client/src/game/minecarts.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index 21ba79b46..de440433d 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -1324,7 +1324,11 @@ impl Game<'_> { if let Some(state) = self.display_state(c) { let display = base * glam::Mat4::from_scale(glam::Vec3::splat(0.75)) - * glam::Mat4::from_translation(glam::Vec3::new(-0.5, (6.0 - 8.0) / 16.0, 0.5)) + * glam::Mat4::from_translation(glam::Vec3::new( + -0.5, + (display_offset(c.kind) as f32 - 8.0) / 16.0, + 0.5, + )) * glam::Mat4::from_rotation_y(90f32.to_radians()); if c.kind == CartKind::Chest { self.cart_chest(state, display, l, out); @@ -1460,3 +1464,18 @@ impl Minecart { Some(c) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn display_offset_matches_vanilla_for_each_cart_kind() { + assert_eq!(display_offset(CartKind::Hopper), 1.0); + assert_eq!(display_offset(CartKind::Chest), 6.0); + assert_eq!(display_offset(CartKind::Furnace), 6.0); + assert_eq!(display_offset(CartKind::Tnt), 6.0); + assert_eq!(display_offset(CartKind::CommandBlock), 6.0); + assert_eq!(display_offset(CartKind::Rideable), 6.0); + } +} From 5c55e7d855dc999d19ebd9f1e85e3c06c8a59606 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sat, 17 Oct 2026 23:23:15 +0100 Subject: [PATCH 19/67] Paletted container access: skip the storage revalidation a section already did BlockSection::get and set check the section index and the state id once, then read and write through new PackedStorage::read/write, which skip the storage-level bounds and value checks and the Result that get/set keep for external callers. A section index is below 4,096 and a palette id always fits the width bits_for_palette chose; both methods debug_assert that contract. Used for the hot read, the hot write and the growth re-encode. Benchmark (ignored tests, best of alternating-order rounds, fast profile): BlockSection::set 13.15 -> 9.12 ns/write (1.44x), get 2.33 -> 1.45 ns/read (1.61x). See docs/performance/paletted-container-access-results.md. --- crates/horizon-core/src/storage.rs | 22 +++++++++ crates/horizon-world/src/chunk.rs | 48 +++++++++++++++---- .../paletted-container-access-results.md | 29 +++++++---- 3 files changed, 82 insertions(+), 17 deletions(-) diff --git a/crates/horizon-core/src/storage.rs b/crates/horizon-core/src/storage.rs index 61f8c505f..6b8a8ec38 100644 --- a/crates/horizon-core/src/storage.rs +++ b/crates/horizon-core/src/storage.rs @@ -88,6 +88,28 @@ impl PackedStorage { Ok(((self.words[word] >> shift) & self.mask) as u32) } + /// Reads `index` without a bounds check. Callers use this once they hold a + /// slice length or a section index they have already validated. + /// `index` must be less than `len`. + pub fn read(&self, index: usize) -> u32 { + debug_assert!(index < self.len); + let word = index / self.per_word; + let shift = (index % self.per_word) * usize::from(self.bits); + ((self.words[word] >> shift) & self.mask) as u32 + } + + /// Writes `value` at `index` without revalidating either. `index` must be + /// less than `len` and `value` must fit `bits`, which holds for a section + /// palette index from the same container. Returns the previous value. + pub fn write(&mut self, index: usize, value: u32) -> u32 { + debug_assert!(index < self.len && u64::from(value) <= self.mask); + let word = index / self.per_word; + let shift = (index % self.per_word) * usize::from(self.bits); + let old = ((self.words[word] >> shift) & self.mask) as u32; + self.words[word] = (self.words[word] & !(self.mask << shift)) | (u64::from(value) << shift); + old + } + /// Validation fails before mutation. Writes accept nonnegative Java ints; /// imported 32-bit words may still decode to any u32 bit pattern. pub fn set(&mut self, index: usize, value: u32) -> Result { diff --git a/crates/horizon-world/src/chunk.rs b/crates/horizon-world/src/chunk.rs index b7650a3d3..fdbefe259 100644 --- a/crates/horizon-world/src/chunk.rs +++ b/crates/horizon-world/src/chunk.rs @@ -442,7 +442,7 @@ impl<'r> BlockSection<'r> { if index >= 4096 { return Err(invalid("section index out of bounds")); } - let id = self.storage.as_ref().map_or(0, |s| s.get(index).unwrap()); + let id = self.storage.as_ref().map_or(0, |s| s.read(index)); Ok(self.palette[id as usize]) } pub fn set(&mut self, index: usize, state: BlockStateId) -> io::Result { @@ -454,7 +454,7 @@ impl<'r> BlockSection<'r> { } // The slot at `index` is the old state's id, so no palette lookup is // needed for it; only the new state has to be resolved. - let old_id = self.storage.as_ref().map_or(0, |s| s.get(index).unwrap()); + let old_id = self.storage.as_ref().map_or(0, |s| s.read(index)); let old = self.palette[old_id as usize]; if old == state { return Ok(old); @@ -481,7 +481,7 @@ impl<'r> BlockSection<'r> { .map_err(|e| invalid(e.to_string()))?; if let Some(old) = &self.storage { for i in 0..4096 { - storage.set(i, old.get(i).unwrap()).unwrap(); + storage.write(i, old.read(i)); } } self.storage = Some(storage); @@ -493,11 +493,7 @@ impl<'r> BlockSection<'r> { } } }; - self.storage - .as_mut() - .unwrap() - .set(index, id) - .map_err(|e| invalid(e.to_string()))?; + self.storage.as_mut().unwrap().write(index, id); self.counts[old_id as usize] -= 1; self.counts[id as usize] += 1; if self.counts[old_id as usize] == 0 { @@ -823,5 +819,41 @@ mod tests { times[1][0], times[0][0] / times[1][0] ); + + let mut get_times = [Vec::new(), Vec::new()]; + for round in 0..ROUNDS { + for optimized in [round % 2 == 0, round % 2 != 0] { + let started = Instant::now(); + let mut acc = 0u32; + for &(index, _) in &ops { + let state = if optimized { + fast.get(black_box(index)).unwrap() + } else { + baseline_get(&base, black_box(index)).unwrap() + }; + acc ^= state.0; + } + black_box(acc); + if round > 0 { + get_times[usize::from(optimized)] + .push(started.elapsed().as_secs_f64() * 1e9 / OPS as f64); + } + } + } + for values in &mut get_times { + values.sort_by(f64::total_cmp); + } + println!( + "section get median: baseline {:.3} ns/op optimized {:.3} ns/op speedup {:.3}x", + median(&get_times[0]), + median(&get_times[1]), + median(&get_times[0]) / median(&get_times[1]) + ); + println!( + "section get best: baseline {:.3} ns/op optimized {:.3} ns/op speedup {:.3}x", + get_times[0][0], + get_times[1][0], + get_times[0][0] / get_times[1][0] + ); } } diff --git a/docs/performance/paletted-container-access-results.md b/docs/performance/paletted-container-access-results.md index 233e320ab..08783d290 100644 --- a/docs/performance/paletted-container-access-results.md +++ b/docs/performance/paletted-container-access-results.md @@ -5,8 +5,9 @@ chunk workers: light and mesh workers unpack a section's 4,096 cells before propagating or meshing, and world-generation workers write one block at a time through `BlockSection::set`. -Two redundant lookups were removed from the write path, and the palette dispatch -was hoisted in the bulk readers. +The write path resolved the old cell and probed the palette three times; the +bulk readers dispatched the palette per cell; and the storage layer revalidated +indices and values a section had already checked. All three are addressed. ## Changes @@ -19,6 +20,14 @@ now read straight from storage, and the incoming state is resolved with a single map probe. The palette-reuse, free-slot and growth paths are unchanged, so the encoded palette, counts, free list and NBT output stay identical. +`BlockSection::get` and `set` validate the section index and the state id once, +then read and write through `PackedStorage::read`/`write` +(`crates/horizon-core/src/storage.rs`). These skip the storage-level bounds and +value checks and the `Result` that `PackedStorage::get`/`set` keep for external +callers: a section index is below 4,096 and a palette id always fits the width +`bits_for_palette` chose. Both new methods `debug_assert` their contract and are +used for the hot read, the hot write and the palette-growth re-encode. + `PalettedContainer::unpack` and `unpack_u16` (`crates/horizon-core/src/paletted.rs`) matched on the palette variant inside the per-cell loop. `Single`, `List` and `Global` are now dispatched once before the @@ -40,19 +49,21 @@ and NBT before timing. Rounds alternate baseline/optimized order with `black_box and report the median and best nanosecond- or microsecond-per-operation; the best round is the estimate of uninterrupted CPU time on a contended machine. -`benchmark_section_get_set` uses a 32-state registry and 50,000 random writes per -round over a section whose palette grows through the 4- and 5-bit widths (24 -distinct states); 51 rounds. `benchmark_unpack` fills a 4,096-entry list palette -(12 states) and the global palette, 2,000 calls per round, 25 rounds. +`benchmark_section_get_set` uses a 32-state registry and 50,000 random writes and +reads per round over a section whose palette grows through the 4- and 5-bit +widths (24 distinct states); 51 rounds. `benchmark_unpack` fills a 4,096-entry +list palette (12 states) and the global palette, 2,000 calls per round, 25 +rounds. AMD Ryzen 5 5600X, Linux x86_64, rustc 1.99.0, optimized `fast` profile, nonincremental, no LTO: | Operation | Original main | New | Speedup | | --- | ---: | ---: | ---: | -| `BlockSection::set` | 12.73 ns/write | 11.37 ns/write | 1.12x | -| `unpack`, list palette | 1.956 us/call | 1.860 us/call | 1.05x | -| `unpack`, global palette | 2.255 us/call | 2.172 us/call | 1.04x | +| `BlockSection::set` | 13.15 ns/write | 9.12 ns/write | 1.44x | +| `BlockSection::get` | 2.33 ns/read | 1.45 ns/read | 1.61x | +| `unpack`, list palette | 1.910 us/call | 1.834 us/call | 1.04x | +| `unpack`, global palette | 2.270 us/call | 2.179 us/call | 1.04x | The distinct-state differential test `chunk::tests::set_matches_baseline_under_random_edits` drives the optimized From 7de8c51f4eeb02d8194348edecdd99c4e7ee04e6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:29:46 +0200 Subject: [PATCH 20/67] Block entity tickers: sweep the list only after a slot is freed The list kept its slots for the whole tick; sweeping it every tick cost two hash lookups per ticker. Now it is swept only when a slot was freed or a block entity was taken out and not brought back. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/block_entities.rs | 31 +++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/block_entities.rs b/crates/horizon-client/src/block_entities.rs index f4b0c411d..93abb3711 100644 --- a/crates/horizon-client/src/block_entities.rs +++ b/crates/horizon-client/src/block_entities.rs @@ -24,7 +24,7 @@ //! registry; whether a slot is made is decided when the slot is promoted at //! the start of the next tick, from the block state then. use crate::containers::BlockEntity; -use crate::fast_hash::FxHashMap; +use crate::fast_hash::{FxHashMap, FxHashSet}; use std::collections::HashMap; pub type Pos = (i32, i32, i32); @@ -57,6 +57,12 @@ pub struct BlockEntities { /// not here any more was cancelled. pending: FxHashMap, next_slot: u32, + /// Whether a slot was freed since the list was last swept: otherwise + /// every slot in it is still live, unless its block entity is `out`. + freed: bool, + /// Positions whose block entity `take` took and `put` has not brought + /// back. + out: FxHashSet, /// `Ticking` by block state id; empty until the game builds it. states: Vec, } @@ -158,7 +164,7 @@ impl BlockEntities { } /// `removeBlockEntityTicker`: no slot, made or pending. fn cancel(&mut self, pos: &Pos) { - self.live.remove(pos); + self.freed |= self.live.remove(pos).is_some(); self.pending.remove(pos); } @@ -199,10 +205,15 @@ impl BlockEntities { /// The block entity at `pos`, out of the map for a while: its slot stays /// for `put` to bring it back (a tick working on it). pub fn take(&mut self, pos: &Pos) -> Option { - self.map.remove(pos) + let taken = self.map.remove(pos); + if taken.is_some() { + self.out.insert(*pos); + } + taken } /// Brings back what `take` took, in its slot. pub fn put(&mut self, pos: Pos, entity: BlockEntity) { + self.out.remove(&pos); if self.map.insert(pos, entity).is_none() && !self.live.contains_key(&pos) && !self.pending.contains_key(&pos) @@ -213,11 +224,11 @@ impl BlockEntities { /// Removes the block entities `keep` refuses, and their slots /// (`clearAllBlockEntities` of unloaded chunks). pub fn retain(&mut self, mut keep: impl FnMut(&Pos, &mut BlockEntity) -> bool) { - let (live, pending) = (&mut self.live, &mut self.pending); + let (live, pending, freed) = (&mut self.live, &mut self.pending, &mut self.freed); self.map.retain(|p, e| { let kept = keep(p, e); if !kept { - live.remove(p); + *freed |= live.remove(p).is_some(); pending.remove(p); } kept @@ -229,6 +240,8 @@ impl BlockEntities { self.live.clear(); self.added.clear(); self.pending.clear(); + self.out.clear(); + self.freed = false; } /// A loaded or generated chunk's block entities, in the order the chunk /// lists them; they tick in its block entity map's order @@ -280,6 +293,10 @@ impl BlockEntities { self.live.insert(pos, slot); self.tickers.push((pos, slot)); } + if !std::mem::take(&mut self.freed) && self.out.is_empty() { + return self.tickers.clone(); + } + self.out.clear(); let (live, map) = (&mut self.live, &self.map); self.tickers.retain(|(p, slot)| { let mine = live.get(p) == Some(slot); @@ -348,6 +365,10 @@ mod tests { let slots = b.tickers(|p| s.get(&p).copied().unwrap_or(0)); b.retain(|p, _| *p != (1, 0, 0)); assert!(b.is_live(slots[0]) && !b.is_live(slots[1])); + // A block entity taken and never brought back loses its slot. + b.take(&(0, 0, 0)); + assert_eq!(order(&mut b, &s), [(2, 0, 0)]); + assert_eq!(order(&mut b, &s), [(2, 0, 0)]); } #[test] From 5253e94262755c809473cb956617fe52b45679ba Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:29:46 +0200 Subject: [PATCH 21/67] Dedicated server: skip the client ticks of spawners, trial spawners and vaults Their particles, ambient sounds and the turning display mob are for a window. A dedicated or integrated server has none, yet made a display mob for every spawner it ticked. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/block_entity_ticks.rs | 10 +++++++--- crates/horizon-client/src/game/vault.rs | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index 7de960716..afe1224fb 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -57,9 +57,13 @@ impl Game<'_> { } self.update_song_volumes(); self.scan_for_hearts(); - self.client_tick_spawners(&spawners); - for key in trial_spawners { - self.client_tick_trial_spawner(key); + // The client ticks (particles, sounds, the turning mob) are for a + // window; a dedicated or integrated server has none. + if self.options.dedicated.is_none() { + self.client_tick_spawners(&spawners); + for key in trial_spawners { + self.client_tick_trial_spawner(key); + } } Ok(()) } diff --git a/crates/horizon-client/src/game/vault.rs b/crates/horizon-client/src/game/vault.rs index 8cf67804d..f3edfddb3 100644 --- a/crates/horizon-client/src/game/vault.rs +++ b/crates/horizon-client/src/game/vault.rs @@ -121,7 +121,8 @@ impl Game<'_> { .put(key, BlockEntity::Vault(vault)); Some(self.vault_events(key, events).map(|()| out)) } - /// The server tick of the vault at `key`, then its client tick. + /// The server tick of the vault at `key`, then its client tick when there + /// is a window. pub(super) fn tick_vault(&mut self, key: Pos) -> Result<()> { let pos = [key.0, key.1, key.2]; let ticked = self.with_vault(key, |v, l, random, state, ominous, roll, events| { @@ -130,7 +131,9 @@ impl Game<'_> { if let Some(result) = ticked { result?; } - self.client_tick_vault(key); + if self.options.dedicated.is_none() { + self.client_tick_vault(key); + } Ok(()) } /// `VaultBlock.useItemOn` with `held` in the hand that acts: true when the From 0eec25d14b6210e74915c7950f75750a6c319d7b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:45:49 +0200 Subject: [PATCH 22/67] Sculk: Fx hashing for the per-mob step and damage maps entity_motion_events looked up each moving mob in a SipHash map every tick. The keys are mob ids the server gives out, so the fast hasher is safe there. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/sculk.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 04e02d360..72a91d7da 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -6,13 +6,14 @@ use super::{Game, Result}; use horizon_client::{ containers::BlockEntity, + fast_hash::{FxHashMap, FxHashSet}, mobs::{Mob, Species, Who, warden}, particles::{ParticleOptions, ParticleType}, sculk::{self, Context, Info, SculkEntity, SculkKind, WardenSpawnTracker}, }; use horizon_core::{geometry::Direction, position::BlockPos, random::RandomSource}; use horizon_world::registry::BlockStateId; -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use winit::keyboard::KeyCode; pub const HELP: &[&str] = &[ @@ -70,8 +71,9 @@ pub(super) struct SculkState { /// The sculk block entities in the order they were added. index: Index, pub known: HashSet<(i32, i32)>, - steps: HashMap, - damaged: HashMap, + // Keyed by mob ids the server gives out: the fast hasher is safe. + steps: FxHashMap, + damaged: FxHashMap, last_health: Option, /// The darkness effect's blend factor, previous and current tick. darkness: (f32, f32), @@ -1528,7 +1530,7 @@ impl Game<'_> { } } } - let alive: HashSet = self.mobs.iter().map(|m| m.id).collect(); + let alive: FxHashSet = self.mobs.iter().map(|m| m.id).collect(); self.sculk.damaged.retain(|id, _| alive.contains(id)); self.sculk.steps.retain(|who, _| match who { Who::Player => true, From 9779eb7271e29c08424a6853db5a6bc598af7e8a Mon Sep 17 00:00:00 2001 From: lazy-importer Date: Sun, 18 Oct 2026 00:16:44 +0200 Subject: [PATCH 23/67] fix(game): allow always-edible food when full and apply consume effects (#750) --- crates/horizon-client/src/game.rs | 67 ++++++++++-- crates/horizon-client/src/game/potions.rs | 2 +- crates/horizon-client/src/items.rs | 124 ++++++++++++++++++++++ 3 files changed, 184 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 929c720d8..7c5846e2c 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2764,7 +2764,7 @@ impl Game<'_> { || !self .player .as_ref() - .is_some_and(|p| p.health.food.food < 20 || self.creative) + .is_some_and(|p| info.can_always_eat || p.health.food.food < 20 || self.creative) { return false; } @@ -2773,6 +2773,54 @@ impl Game<'_> { } true } + fn apply_consume_effect( + &mut self, + effect: &horizon_client::items::ConsumeEffect, + eye: [f64; 3], + ) { + use horizon_client::items::ConsumeEffect; + match effect { + ConsumeEffect::ApplyEffects { + effects, + probability, + } => { + if *probability >= 1.0 || self.world.random.next_float() <= *probability { + for eff in effects { + if let Some(def) = horizon_client::brewing::effect(&eff.id) { + if def.instant { + if let Some(a) = + horizon_client::effects::instant(def.id, eff.amplifier, 1.0) + { + self.apply_effect_action(a); + } + } else { + self.potions.effects.add( + horizon_client::effects::EffectInstance::new( + def.id, + eff.duration, + eff.amplifier, + ), + ); + } + } + } + } + } + ConsumeEffect::ClearAllEffects => { + self.potions.effects.clear(); + } + ConsumeEffect::RemoveEffects(to_remove) => { + for target in to_remove { + let id = target.trim_start_matches("minecraft:"); + self.potions.effects.list.retain(|e| e.id != id); + } + } + ConsumeEffect::TeleportRandomly => {} + ConsumeEffect::PlaySound(sound) => { + self.audio.play(sound, Some(eye), 1.0, 1.0); + } + } + } fn consume_held(&mut self) { if self.creative { return; @@ -2875,19 +2923,22 @@ impl Game<'_> { self.eat_particles(&stack, 16); // Consumable.onConsume. self.post_entity_event("minecraft:eat", horizon_client::mobs::Who::Player); - if let Some((nutrition, saturation)) = info.food - && let Some(p) = self.player.as_mut() - { - // FoodData.eat(nutrition, saturation) - let food = &mut p.health.food; - food.food = (food.food + nutrition as i32).clamp(0, 20); - food.saturation = (food.saturation + saturation).clamp(0.0, food.food as f32); + if let Some((nutrition, saturation)) = info.food { + if let Some(p) = self.player.as_mut() { + // FoodData.eat(nutrition, saturation) + let food = &mut p.health.food; + food.food = (food.food + nutrition as i32).clamp(0, 20); + food.saturation = (food.saturation + saturation).clamp(0.0, food.food as f32); + } self.audio.play( "minecraft:entity.player.burp", Some(eye), 0.5, self.world.random.next_float() * 0.1 + 0.9, ); + for effect in &info.consume_effects { + self.apply_consume_effect(effect, eye); + } self.trigger_item("minecraft:consume_item", &stack); self.consume_held(); } diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index ffa63b7c9..be5b24650 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -249,7 +249,7 @@ impl Game<'_> { } true } - fn apply_effect_action(&mut self, action: EffectAction) { + pub(super) fn apply_effect_action(&mut self, action: EffectAction) { let creative = self.creative; let rules = self.world.health_rules; let Some(p) = self.player.as_mut() else { diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index 54a505ab9..2782d254f 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -39,6 +39,25 @@ pub struct ToolRule { pub speed: Option, pub correct: Option, } +#[derive(Clone, Debug, PartialEq)] +pub struct ConsumeEffectSpec { + pub id: String, + pub duration: i32, + pub amplifier: i32, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum ConsumeEffect { + ApplyEffects { + effects: Vec, + probability: f32, + }, + ClearAllEffects, + RemoveEffects(Vec), + TeleportRandomly, + PlaySound(String), +} + #[derive(Clone, Debug)] pub struct ItemInfo { pub fire_resistant: bool, @@ -49,7 +68,9 @@ pub struct ItemInfo { pub tool: Vec, pub tool_default_speed: f32, pub food: Option<(u32, f32)>, + pub can_always_eat: bool, pub consumable: bool, + pub consume_effects: Vec, pub model: String, /// Main-hand `attack_damage` / `attack_speed` modifier amounts. pub attack_damage: f32, @@ -421,6 +442,79 @@ impl Items { f["saturation"].as_f64().unwrap_or(0.0) as f32, ) }); + let can_always_eat = c + .get("minecraft:food") + .and_then(|f| f.get("can_always_eat")) + .and_then(|v| v.as_bool()) + .unwrap_or(false); + let mut consume_effects = Vec::new(); + if let Some(effects) = c + .get("minecraft:consumable") + .and_then(|cons| cons.get("on_consume_effects")) + .and_then(|e| e.as_array()) + { + for effect in effects { + match effect.get("type").and_then(|t| t.as_str()) { + Some("minecraft:apply_effects") => { + let probability = effect + .get("probability") + .and_then(|p| p.as_f64()) + .map_or(1.0, |p| p as f32); + let mut specs = Vec::new(); + if let Some(list) = effect.get("effects").and_then(|l| l.as_array()) { + for e in list { + if let Some(id) = e.get("id").and_then(|i| i.as_str()) { + let duration = + e.get("duration").and_then(|d| d.as_i64()).unwrap_or(0) + as i32; + let amplifier = e + .get("amplifier") + .and_then(|a| a.as_i64()) + .unwrap_or(0) + as i32; + specs.push(ConsumeEffectSpec { + id: id.to_string(), + duration, + amplifier, + }); + } + } + } + consume_effects.push(ConsumeEffect::ApplyEffects { + effects: specs, + probability, + }); + } + Some("minecraft:clear_all_effects") => { + consume_effects.push(ConsumeEffect::ClearAllEffects); + } + Some("minecraft:remove_effects") => { + let mut targets = Vec::new(); + if let Some(eff) = effect.get("effects") { + if let Some(s) = eff.as_str() { + targets.push(s.to_string()); + } else if let Some(arr) = eff.as_array() { + for item in arr { + if let Some(s) = item.as_str() { + targets.push(s.to_string()); + } + } + } + } + consume_effects.push(ConsumeEffect::RemoveEffects(targets)); + } + Some("minecraft:teleport_randomly") => { + consume_effects.push(ConsumeEffect::TeleportRandomly); + } + Some("minecraft:play_sound") => { + if let Some(sound) = effect.get("sound").and_then(|s| s.as_str()) { + consume_effects.push(ConsumeEffect::PlaySound(sound.to_string())); + } + } + _ => {} + } + } + } items.insert( id.clone(), ItemInfo { @@ -435,7 +529,9 @@ impl Items { .as_f64() .unwrap_or(1.0) as f32, food, + can_always_eat, consumable: c.get("minecraft:consumable").is_some(), + consume_effects, model: c["minecraft:item_model"] .as_str() .unwrap_or(name) @@ -1496,6 +1592,34 @@ mod tests { .is_empty() ); } + + #[test] + fn always_edible_and_consume_effects_loaded() { + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3"); + if !root.join("items.json").exists() { + return; + } + let assets = crate::assets::Assets::load(&root).unwrap(); + let facts = crate::blocks::BlockFacts::load(&root, assets.registry).unwrap(); + let items = Items::load(&root, assets.registry, &facts).unwrap(); + + let apple = items.get("minecraft:golden_apple").expect("golden apple"); + assert!(apple.can_always_eat); + assert!(apple.food.is_some()); + assert!(!apple.consume_effects.is_empty()); + + let enchanted = items + .get("minecraft:enchanted_golden_apple") + .expect("enchanted golden apple"); + assert!(enchanted.can_always_eat); + assert!(enchanted.food.is_some()); + assert!(!enchanted.consume_effects.is_empty()); + + let beef = items.get("minecraft:cooked_beef").expect("cooked beef"); + assert!(!beef.can_always_eat); + assert!(beef.food.is_some()); + } } pub fn take_held_drop(slot: &mut Option, all: bool) -> Option { From ecdff7ad2d94e6cf14d27a0b0bd89a64ce50f12b Mon Sep 17 00:00:00 2001 From: lazy-importer Date: Sun, 18 Oct 2026 00:44:34 +0200 Subject: [PATCH 24/67] fix(game): apply absorption effect attribute modifiers and health (#750) --- crates/horizon-client/src/effects.rs | 31 +++++++++++++++++++ crates/horizon-client/src/game.rs | 4 ++- .../src/game/commands/effects.rs | 4 ++- crates/horizon-client/src/game/potions.rs | 27 ++++++++++++++-- crates/horizon-client/src/game/totem.rs | 4 +-- 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/effects.rs b/crates/horizon-client/src/effects.rs index 2e600333a..a258ccf3a 100644 --- a/crates/horizon-client/src/effects.rs +++ b/crates/horizon-client/src/effects.rs @@ -386,6 +386,12 @@ impl PlayerEffects { 4.0, "add_value", )), + "absorption" => Some(( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 4.0, + "add_value", + )), "luck" => Some(("minecraft:luck", "minecraft:effect.luck", 1.0, "add_value")), "unluck" => Some(( "minecraft:luck", @@ -538,4 +544,29 @@ mod tests { assert!((fx.mining_speed_factor() - 1.4).abs() < 1e-6); assert_eq!(fx.modifiers().len(), 1); } + + #[test] + fn absorption_modifier_scales_with_amplifier() { + let mut fx = PlayerEffects::default(); + fx.add(EffectInstance::new("absorption", 2400, 0)); + assert_eq!( + fx.modifiers(), + vec![( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 4.0, + "add_value", + )] + ); + fx.add(EffectInstance::new("absorption", 2400, 3)); + assert_eq!( + fx.modifiers(), + vec![( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 16.0, + "add_value", + )] + ); + } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7c5846e2c..26709516f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2794,7 +2794,7 @@ impl Game<'_> { self.apply_effect_action(a); } } else { - self.potions.effects.add( + self.add_player_effect( horizon_client::effects::EffectInstance::new( def.id, eff.duration, @@ -2808,12 +2808,14 @@ impl Game<'_> { } ConsumeEffect::ClearAllEffects => { self.potions.effects.clear(); + self.apply_effect_status(); } ConsumeEffect::RemoveEffects(to_remove) => { for target in to_remove { let id = target.trim_start_matches("minecraft:"); self.potions.effects.list.retain(|e| e.id != id); } + self.apply_effect_status(); } ConsumeEffect::TeleportRandomly => {} ConsumeEffect::PlaySound(sound) => { diff --git a/crates/horizon-client/src/game/commands/effects.rs b/crates/horizon-client/src/game/commands/effects.rs index 3e04f7179..86726fed9 100644 --- a/crates/horizon-client/src/game/commands/effects.rs +++ b/crates/horizon-client/src/game/commands/effects.rs @@ -86,7 +86,7 @@ impl Game<'_> { Target::Player => { let mut instance = EffectInstance::new(effect.id, duration, amplifier); instance.visible = particles; - self.potions.effects.add(instance) + self.add_player_effect(instance) } Target::Mob(id) => match self.mobs.iter_mut().find(|m| m.id == id) { Some(m) => { @@ -134,11 +134,13 @@ impl Game<'_> { (Target::Player, None) => { let any = !self.potions.effects.list.is_empty(); self.potions.effects.clear(); + self.apply_effect_status(); any } (Target::Player, Some(e)) => { let before = self.potions.effects.list.len(); self.potions.effects.list.retain(|x| x.id != e.id); + self.apply_effect_status(); self.potions.effects.list.len() != before } (Target::Mob(id), e) => match self.mobs.iter_mut().find(|m| m.id == id) { diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index be5b24650..bc4f3b6bc 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -228,7 +228,7 @@ impl Game<'_> { } let (lasting, now) = PlayerEffects::potion_effects(stack.potion(), 1.0); for e in lasting { - self.potions.effects.add(e); + self.add_player_effect(e); } for a in now { self.apply_effect_action(a); @@ -329,9 +329,24 @@ impl Game<'_> { } self.apply_effect_status(); } + /// Add an active effect to the player and update attributes / absorption. + pub(super) fn add_player_effect(&mut self, effect: EffectInstance) -> bool { + let id = effect.id; + let amp = effect.amplifier; + let changed = self.potions.effects.add(effect); + self.apply_effect_status(); + if id == "absorption" + && let Some(p) = self.player.as_mut() + { + let target = 4.0 * (amp + 1) as f32; + let current = p.health.absorption(); + let _ = p.set_health(current.max(target), true); + } + changed + } /// The effects' attribute modifiers, armor protection and status flags /// on `self.player`. - fn apply_effect_status(&mut self) { + pub(super) fn apply_effect_status(&mut self) { let modifiers = self.potions.effects.modifiers(); if modifiers != self.potions.applied && let Some(p) = self.player.as_mut() @@ -344,6 +359,12 @@ impl Game<'_> { } self.potions.applied = modifiers; } + if let Some(p) = self.player.as_mut() + && self.potions.effects.amplifier("absorption").is_none() + && p.health.absorption() > 0.0 + { + let _ = p.set_health(0.0, true); + } // EnchantmentHelper.getDamageProtection per damage type, recomputed on armor change. let armor: Vec> = self.armor.to_vec(); if self.potions.protection_for.as_ref() != Some(&armor) { @@ -495,7 +516,7 @@ impl Game<'_> { if d > 20 && let Some(e) = brewing::effect(id) { - self.potions.effects.add(EffectInstance::new(e.id, d, *amp)); + self.add_player_effect(EffectInstance::new(e.id, d, *amp)); } } } diff --git a/crates/horizon-client/src/game/totem.rs b/crates/horizon-client/src/game/totem.rs index 805360c8d..47b67d9f8 100644 --- a/crates/horizon-client/src/game/totem.rs +++ b/crates/horizon-client/src/game/totem.rs @@ -53,9 +53,7 @@ impl Game<'_> { ("fire_resistance", 800, 0), ] { if let Some(e) = horizon_client::brewing::effect(id) { - self.potions - .effects - .add(EffectInstance::new(e.id, ticks, amp)); + self.add_player_effect(EffectInstance::new(e.id, ticks, amp)); } } self.audio From d33929c8fab6a73321efc4d713c54e423a355d3d Mon Sep 17 00:00:00 2001 From: LookImEcho-Rick <62575172+LookImEcho-Rick@users.noreply.github.com> Date: Sat, 17 Oct 2026 18:22:40 -0500 Subject: [PATCH 25/67] Fix Horizon options scrollbar dragging --- crates/horizon-client/src/options_horizon.rs | 295 +++++++++++++++++-- crates/horizon-client/src/options_ui.rs | 3 +- 2 files changed, 269 insertions(+), 29 deletions(-) diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index 634bb8d75..d3ecab639 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -263,15 +263,29 @@ enum Target { Style(usize), Level(usize), } +struct HorizonScrollbar { + x: f32, + top: f32, + bottom: f32, + max: f32, + thumb: f32, +} +impl HorizonScrollbar { + fn contains(&self, x: f32, y: f32) -> bool { + x >= self.x && x < self.x + 6.0 && y >= self.top && y < self.bottom + } +} +struct HorizonLayout { + sliders: Vec<(Item, f32, f32)>, + scrollbar: Option, +} +fn horizon_batch(framebuffer: [i32; 2]) -> HudBatch { + HudBatch::new([framebuffer[0].max(1) as u32, framebuffer[1].max(1) as u32]) +} impl OptionsUi { /// Lay out (and draw, on a real frame) the Horizon options shell. - /// Returns the sliders' tracks (option, x, width) for dragging. - fn horizon_layout( - &mut self, - s: &Settings, - ui: &mut Ui, - backdrop: f32, - ) -> Vec<(Item, f32, f32)> { + /// Returns the sliders' tracks and the scroll area for pointer input. + fn horizon_layout(&mut self, s: &Settings, ui: &mut Ui, backdrop: f32) -> HorizonLayout { let (w, h) = (ui.w, ui.h); if self.in_world { ui.atmosphere(horizon_theme::VEIL, 1.0); @@ -499,6 +513,7 @@ impl OptionsUi { .filter(|h| h.rect[1] >= cy0 && h.rect[3] <= cy1) .collect(); ui.hits.extend(kept); + let mut scrollbar = None; if max > 0.0 { // A thin scroll indicator. let track = bottom - top; @@ -512,11 +527,18 @@ impl OptionsUi { 1.5, color::with_alpha(color::MUTED, 0.35), ); + scrollbar = Some(HorizonScrollbar { + x: x + cw + 14.0, + top: top + oy, + bottom: bottom + oy, + max, + thumb: bar, + }); } let _ = ox; ui.opacity = 1.0; ui.offset = (0.0, 0.0); - sliders + HorizonLayout { sliders, scrollbar } } pub(crate) fn draw_horizon( &mut self, @@ -530,25 +552,47 @@ impl OptionsUi { self.horizon_layout(s, &mut ui, backdrop); ui.finish(); } - fn horizon_target(&mut self, s: &Settings, atlas: &HudAtlas, x: f32, y: f32) -> Option { - let mut batch = t::scratch_batch(); + fn horizon_target( + &mut self, + s: &Settings, + atlas: &HudAtlas, + x: f32, + y: f32, + framebuffer: [i32; 2], + ) -> (Option, Option, (f32, f32)) { + let mut batch = horizon_batch(framebuffer); let mut ui = Ui::dry(atlas, &mut batch, (x, y)); - let sliders = self.horizon_layout(s, &mut ui, 1.0); - let hit = ui.hit((x, y))?; + let layout = self.horizon_layout(s, &mut ui, 1.0); + let scrollbar = layout.scrollbar; + // The UI layer transforms raw GUI-pixel input into its own dp. + let cursor = ui.cursor; + let Some(hit) = ui.hit((x, y)) else { + return (None, scrollbar, cursor); + }; let section = self.section.min(SECTIONS.len() - 1); if (NAV..NAV + SECTIONS.len() as u64).contains(&hit) { - return Some(Target::Nav((hit - NAV) as usize)); + return (Some(Target::Nav((hit - NAV) as usize)), scrollbar, cursor); } if hit == DONE { - return Some(Target::Done); + return (Some(Target::Done), scrollbar, cursor); } if hit == STYLE || hit == STYLE + 1 { - return Some(Target::Style((hit - STYLE) as usize)); + return ( + Some(Target::Style((hit - STYLE) as usize)), + scrollbar, + cursor, + ); } if hit == LEVEL || hit == LEVEL + 1 { - return Some(Target::Level((hit - LEVEL) as usize)); + return ( + Some(Target::Level((hit - LEVEL) as usize)), + scrollbar, + cursor, + ); } - let i = hit.checked_sub(ROW)? as usize; + let Some(i) = hit.checked_sub(ROW).map(|i| i as usize) else { + return (None, scrollbar, cursor); + }; let rows = (SECTIONS[section].rows)(s); let item = match rows.get(i) { Some(HRow::Opt(opt)) => Some(Item::Vanilla(*opt)), @@ -556,12 +600,16 @@ impl OptionsUi { _ => None, }; if let Some(item) = item - && let Some(&(_, sx, sw)) = sliders.iter().find(|(o, ..)| *o == item) + && let Some(&(_, sx, sw)) = layout.sliders.iter().find(|(o, ..)| *o == item) { let v = t::slider_value(&ui, sx, sw, x); - return Some(Target::Slider(item, v)); + return (Some(Target::Slider(item, v)), scrollbar, cursor); } - (i < rows.len()).then_some(Target::Row(i)) + ( + (i < rows.len()).then_some(Target::Row(i)), + scrollbar, + cursor, + ) } /// A click (or a drag with the button held) on the Horizon shell. pub(crate) fn click_horizon( @@ -576,14 +624,37 @@ impl OptionsUi { ) -> Vec { let mut events = Vec::new(); if drag { - if let Some(item) = self.hdragging { - let mut batch = t::scratch_batch(); - let mut ui = Ui::dry(atlas, &mut batch, (x, y)); - let sliders = self.horizon_layout(s, &mut ui, 1.0); - if let Some(&(_, sx, sw)) = sliders.iter().find(|(o, ..)| *o == item) { - set_item_slider(item, s, t::slider_value(&ui, sx, sw, x)); - events.push(Event::Changed); + let mut batch = horizon_batch(framebuffer); + let mut ui = Ui::dry(atlas, &mut batch, (x, y)); + let layout = self.horizon_layout(s, &mut ui, 1.0); + // Scrollbar geometry and stored drag positions are in Horizon dp. + let pointer_y = ui.cursor.1; + if let Some(previous_y) = self.scrollbar_drag_y { + if let Some(scrollbar) = layout.scrollbar { + // AbstractScrollArea.mouseDragged (inherited by AbstractSelectionList) + // moves by the pointer delta and sends out-of-range drags to an end. + let track = scrollbar.bottom - scrollbar.top; + self.hscroll = if pointer_y < scrollbar.top { + 0.0 + } else if pointer_y > scrollbar.bottom { + scrollbar.max + } else { + self.hscroll + + (pointer_y - previous_y) * scrollbar.max + / (track - scrollbar.thumb).max(1.0) + }; + self.hscroll = self.hscroll.clamp(0.0, scrollbar.max); + self.scrollbar_drag_y = Some(pointer_y); + } else { + self.scrollbar_drag_y = None; } + return events; + } + if let Some(item) = self.hdragging + && let Some(&(_, sx, sw)) = layout.sliders.iter().find(|(o, ..)| *o == item) + { + set_item_slider(item, s, t::slider_value(&ui, sx, sw, x)); + events.push(Event::Changed); } return events; } @@ -598,7 +669,16 @@ impl OptionsUi { }, ); } - match self.horizon_target(s, atlas, x, y) { + let (target, scrollbar, cursor) = self.horizon_target(s, atlas, x, y, framebuffer); + // AbstractScrollArea.updateScrolling grabs anywhere in the scrollbar column. + if !right + && let Some(scrollbar) = scrollbar + && scrollbar.contains(cursor.0, cursor.1) + { + self.scrollbar_drag_y = Some(cursor.1); + return events; + } + match target { Some(Target::Nav(i)) => { self.section = i; self.hscroll = 0.0; @@ -670,3 +750,162 @@ fn set_item_slider(item: Item, s: &mut Settings, v: f32) { Item::Horizon(_) => item.set_slider(s, v), } } + +#[cfg(test)] +mod tests { + use super::*; + + fn atlas() -> HudAtlas { + HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap() + } + + fn section_ui(name: &str) -> OptionsUi { + let mut ui = OptionsUi::default(); + ui.section = SECTIONS + .iter() + .position(|section| section.name == name) + .unwrap(); + ui + } + + fn extras_ui() -> OptionsUi { + section_ui("Extras") + } + + fn scrollbar( + ui: &mut OptionsUi, + settings: &Settings, + atlas: &HudAtlas, + framebuffer: [i32; 2], + ) -> HorizonScrollbar { + let mut batch = horizon_batch(framebuffer); + let mut draw = Ui::dry(atlas, &mut batch, (0.0, 0.0)); + ui.horizon_layout(settings, &mut draw, 1.0) + .scrollbar + .unwrap() + } + + fn gui_pointer(atlas: &HudAtlas, framebuffer: [i32; 2], point: (f32, f32)) -> (f32, f32) { + let mut batch = horizon_batch(framebuffer); + let ui = Ui::dry(atlas, &mut batch, (0.0, 0.0)); + let gui_per_dp = ui.density / ui.batch.scale; + (point.0 * gui_per_dp, point.1 * gui_per_dp) + } + + #[test] + fn horizon_scrollbar_track_drag_is_bounded_and_stops_on_release() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut settings = Settings::default(); + let mut ui = section_ui("Controls"); + let bar = scrollbar(&mut ui, &settings, &atlas, framebuffer); + let max = bar.max; + let track = bar.bottom - bar.top; + assert!(max > 0.0); + + // Start on the track below the thumb, then move horizontally away; + // the held scrollbar keeps following the pointer. + let x = bar.x + 2.0; + let y = bar.top + bar.thumb + (track - bar.thumb) / 2.0; + assert!(y < bar.bottom); + let (click_x, click_y) = gui_pointer(&atlas, framebuffer, (x, y)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + click_x, + click_y, + framebuffer, + false, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + let delta = 5.0; + let (drag_x, drag_y) = gui_pointer(&atlas, framebuffer, (x - 100.0, y + delta)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + drag_x, + drag_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert!((ui.hscroll - delta * max / (track - bar.thumb)).abs() < 0.001); + + let (below_x, below_y) = gui_pointer(&atlas, framebuffer, (x, bar.bottom + 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + below_x, + below_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, max); + let (above_x, above_y) = gui_pointer(&atlas, framebuffer, (x, bar.top - 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + above_x, + above_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + + assert!(ui.release().is_empty()); + let (below_x, below_y) = gui_pointer(&atlas, framebuffer, (x, bar.bottom + 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + below_x, + below_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + assert_eq!(settings.keys, Settings::default().keys); + } + + #[test] + fn horizon_key_rebinding_takes_precedence_over_scrollbar_press() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut settings = Settings::default(); + let mut ui = extras_ui(); + let bar = scrollbar(&mut ui, &settings, &atlas, framebuffer); + let dp_y = bar.top + bar.thumb + (bar.bottom - bar.top - bar.thumb) / 2.0; + ui.selected_key = Some(extras::SORT_KEY); + let (x, y) = gui_pointer(&atlas, framebuffer, (bar.x + 2.0, dp_y)); + + assert_eq!( + ui.click_horizon(&mut settings, &atlas, x, y, framebuffer, false, false,), + vec![Event::Changed] + ); + assert_eq!(settings.extras.sort_key, "key.mouse.left"); + assert_eq!(ui.selected_key, None); + assert_eq!(ui.hscroll, 0.0); + + let (x, drag_y) = gui_pointer(&atlas, framebuffer, (bar.x + 2.0, dp_y + 10.0)); + ui.click_horizon(&mut settings, &atlas, x, drag_y, framebuffer, true, false); + assert_eq!(ui.hscroll, 0.0); + } +} diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index dca180e15..fc80530d9 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -806,7 +806,8 @@ pub struct OptionsUi { /// when it last moved: the handle follows the pointer while the option /// keeps its stepped value, then snaps to it (`resetValue`). slider_value: Option<(Opt, f32, Instant)>, - scrollbar_drag_y: Option, + /// The pointer's last y while a Classic or Horizon scrollbar is held. + pub(crate) scrollbar_drag_y: Option, /// Horizon style: the selected sidebar section, the one last drawn and /// the rows' scroll in dp (`options_horizon`). pub section: usize, From 1ffdb5f3f5aeb9660bc3a2d5b644a905233cf257 Mon Sep 17 00:00:00 2001 From: LookImEcho-Rick <62575172+LookImEcho-Rick@users.noreply.github.com> Date: Sat, 17 Oct 2026 18:46:50 -0500 Subject: [PATCH 26/67] Add open mod folder buttons Expose the Horizon mods directory from both the Classic and Horizon mods screens, creating it if needed before opening it. Closes #780 --- crates/horizon-client/src/game/options.rs | 5 ++ crates/horizon-client/src/options_horizon.rs | 76 +++++++++++++++++++- crates/horizon-client/src/options_ui.rs | 41 ++++++++++- 3 files changed, 118 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index ed0dccb06..d625f938b 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -225,6 +225,11 @@ impl Game<'_> { let _ = std::fs::create_dir_all(&dir); super::packs::open_folder(&dir); } + Event::OpenModFolder => { + let dir = Self::options_path().with_file_name(horizon_client::mod_list::DIR); + let _ = std::fs::create_dir_all(&dir); + super::packs::open_folder(&dir); + } } } Ok(()) diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index d3ecab639..11bf3183f 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -36,6 +36,8 @@ pub enum HRow { Extra(extras::Item), /// Installed mod `i` (`settings.mods.installed`) and its switch. Mod(usize), + /// Opens the directory where Horizon mods are installed. + OpenModFolder, } /// A sidebar section: name, description, rows. pub struct Section { @@ -192,12 +194,15 @@ pub const SECTIONS: &[Section] = &[ name: "Mods", blurb: "Mods made for Horizon, from the horizon-mods folder. Each runs in a sandbox and stays off until you turn it on; check what it may do first.", rows: |s| { + let mut rows = vec![HRow::OpenModFolder]; if s.mods.installed.is_empty() { - return vec![HRow::Header( + rows.push(HRow::Header( "No mods: put .hmod files in the horizon-mods folder", - )]; + )); + } else { + rows.extend((0..s.mods.installed.len()).map(HRow::Mod)); } - (0..s.mods.installed.len()).map(HRow::Mod).collect() + rows }, }, ]; @@ -450,6 +455,18 @@ impl OptionsUi { ui.described_row(x, y, &installed.title, "", 0.0, cost, text, cw - 60.0); ui.switch(ROW + i as u64, control_x, y + ROW_H / 2.0, s.mods.is_on(m)); } + HRow::OpenModFolder => { + ui.row(x, y, "Mod folder", "", 0.0); + let label = "Open Folder"; + let bw = ui.button_width(label, false).max(96.0); + ui.button( + ROW + i as u64, + [control_x - bw, y + 12.0, bw, CONTROL], + label, + Button::Secondary, + true, + ); + } HRow::Extra(item) => { let cy = y + ROW_H / 2.0; let id = ROW + i as u64; @@ -730,6 +747,10 @@ impl OptionsUi { s.mods.toggle(*m); events.push(Event::Changed); } + Some(HRow::OpenModFolder) if !right => { + events.push(Event::ButtonClick); + events.push(Event::OpenModFolder); + } _ => {} } } @@ -908,4 +929,53 @@ mod tests { ui.click_horizon(&mut settings, &atlas, x, drag_y, framebuffer, true, false); assert_eq!(ui.hscroll, 0.0); } + + #[test] + fn mods_section_opens_folder_from_its_button() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut options = section_ui("Mods"); + let mut settings = Settings::default(); + let mut batch = horizon_batch(framebuffer); + let mut ui = Ui::dry(&atlas, &mut batch, (0.0, 0.0)); + options.horizon_layout(&settings, &mut ui, 1.0); + let rect = ui + .hits + .iter() + .find(|hit| hit.id == ROW) + .expect("Mods section has an Open Folder button") + .rect; + let scale = ui.density / ui.batch.scale; + let cursor = ( + (rect[0] + rect[2]) * 0.5 * scale, + (rect[1] + rect[3]) * 0.5 * scale, + ); + drop(ui); + + assert_eq!( + options.click_horizon( + &mut settings, + &atlas, + cursor.0, + cursor.1, + framebuffer, + false, + false + ), + vec![Event::ButtonClick, Event::OpenModFolder] + ); + assert!( + options + .click_horizon( + &mut settings, + &atlas, + cursor.0, + cursor.1, + framebuffer, + false, + true + ) + .is_empty() + ); + } } diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index fc80530d9..0b166241e 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -734,6 +734,7 @@ enum Act { /// Back to Horizon's own menus (Horizon only, see `list_rows`). HorizonMenus, ToggleMod(usize), + OpenModFolder, } enum WKind { Button { @@ -780,6 +781,7 @@ pub enum Event { /// The pack selection changed (reload resources). Packs(Vec), OpenPackFolder, + OpenModFolder, } /// `SliderableValueSet.applyValueImmediately`: these apply 600 ms after /// the last change (`OptionInstanceSliderButton.delayedApplyAt`). @@ -1626,7 +1628,18 @@ impl OptionsUi { } content = y + self.scroll - top + 2.0; out[list_start..].iter_mut().for_each(|w| w.list = true); - out.push(done(h - 27.0)); + if page == Page::Mods { + out.push(button( + cx - 154.0, + h - 27.0, + 150.0, + Text::literal("Open Mod Folder"), + Act::OpenModFolder, + )); + out.push(button(cx + 4.0, h - 27.0, 150.0, tr("gui.done"), Act::Done)); + } else { + out.push(done(h - 27.0)); + } } } // Layout may change the offset (language centering), or the list may @@ -2078,6 +2091,7 @@ impl OptionsUi { s.mods.toggle(i); events.push(Event::Changed); } + Act::OpenModFolder => events.push(Event::OpenModFolder), Act::None => {} } } @@ -2450,6 +2464,31 @@ mod tests { assert!(empty.iter().any(|w| { matches!(&w.kind, WKind::Text { text, .. } if text.plain().contains(".hmod")) })); + let open_folder = empty + .iter() + .find(|w| { + matches!( + &w.kind, + WKind::Button { + act: Act::OpenModFolder, + .. + } + ) + }) + .expect("Mods page has an Open Mod Folder button"); + assert_eq!( + ui.click( + &mut s, + &atlas, + open_folder.x + 1.0, + open_folder.y + 1.0, + size, + [480, 240], + false, + false, + ), + vec![Event::ButtonClick, Event::OpenModFolder] + ); for (id, dev) in [("normal", false), ("dev", true), ("", false)] { s.mods.installed.push(crate::mod_list::Installed { id: id.into(), From 3bb973efae05a899decbbb4644dcca83bd2ee5e3 Mon Sep 17 00:00:00 2001 From: lazy-importer Date: Sun, 18 Oct 2026 01:57:22 +0200 Subject: [PATCH 27/67] fix(game): persist creative flight status across world save and reload (#777) --- crates/horizon-client/src/game.rs | 6 +++ crates/horizon-client/src/lan/playerdata.rs | 2 + crates/horizon-client/src/motion.rs | 4 ++ crates/horizon-client/src/save.rs | 41 ++++++++++++++++++++- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 26709516f..b1bb31a34 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3156,6 +3156,11 @@ impl Game<'_> { player.may_fly = self.creative || spectator; player.invulnerable = self.creative || spectator; player.spectator = spectator; + if spectator { + player.flying = true; + } else if !player.may_fly { + player.flying = false; + } player.respiration = self.armor[0] .as_ref() .map_or(0, |s| s.enchantment_level("minecraft:respiration") as i32); @@ -8458,6 +8463,7 @@ impl Game<'_> { level.food = p.health.food.food; level.saturation = p.health.food.saturation; + level.flying = p.flying; } level.inventory = self .inventory diff --git a/crates/horizon-client/src/lan/playerdata.rs b/crates/horizon-client/src/lan/playerdata.rs index 44b269a8f..452218972 100644 --- a/crates/horizon-client/src/lan/playerdata.rs +++ b/crates/horizon-client/src/lan/playerdata.rs @@ -68,6 +68,7 @@ pub fn apply(guest: &mut Guest, level: &Level) { guest.xp_total = level.xp_total; guest.effects = crate::effects::PlayerEffects::from_saved(&level.active_effects); guest.game_mode = level.mode(); + guest.flying = level.flying; guest.selected = (level.selected_slot.max(0) as usize).min(8); guest.saved = level.player_extra.clone(); guest.recipe_book = level.recipe_book.clone(); @@ -128,6 +129,7 @@ pub fn store(guest: &Guest, level: &mut Level) { level.active_effects = guest.effects.saved(); level.game_mode = guest.game_mode; level.creative = guest.game_mode == 1; + level.flying = guest.flying; level.selected_slot = guest.selected as i32; level.on_ground = guest.on_ground; level.player_extra = guest.saved.clone(); diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 7dab3a5aa..153793a2a 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -309,6 +309,7 @@ impl PlayerMotion { self.set_health(level.health, false)?; self.health.food.food = level.food; self.health.food.saturation = level.saturation; + self.flying = level.flying; } Ok(()) } @@ -2610,6 +2611,7 @@ mod tests { food: 11, saturation: 2.25, player_extra: Some(extra), + flying: true, ..Default::default() }; let mut pending = true; @@ -2617,6 +2619,7 @@ mod tests { .load_saved_state_once(&mut pending, &level) .unwrap(); assert_eq!((w.player.fire_ticks, w.player.air), (160, 8)); + assert!(w.player.flying); assert_eq!( ( w.player.health.health(), @@ -2628,6 +2631,7 @@ mod tests { let mut respawn = PlayerMotion::new(&resources, Vec3::new(20.5, 4.0, 20.5)).unwrap(); respawn.load_saved_state_once(&mut pending, &level).unwrap(); assert_eq!((respawn.fire_ticks, respawn.air), (-20, 300)); + assert!(!respawn.flying); assert_eq!( (respawn.health.health(), respawn.health.food.food), (20.0, 20) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 29038849f..c60da2caa 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -2146,6 +2146,8 @@ pub struct Level { pub on_ground: bool, pub food_exhaustion: f32, pub food_tick_timer: i32, + /// Abilities.flying: whether creative/spectator flight is active. + pub flying: bool, /// The player compound as loaded, for fields Horizon does not model /// (ender chest, effects, abilities, recipe book, attributes, ...). pub player_extra: Option, @@ -2309,6 +2311,10 @@ impl Level { self.creative = mode == 1; self.game_mode = mode as i32; } + self.flying = p + .get("abilities") + .and_then(|a| number(a.get("flying"))) + .is_some_and(|v| v != 0); if let Some(d) = text(p.get("Dimension")) { self.dimension = d; } @@ -2440,7 +2446,9 @@ impl Level { } if spectator { put(&mut abilities, "flying", Tag::Byte(1)); - } else if !creative { + } else if creative { + put(&mut abilities, "flying", Tag::Byte(i8::from(self.flying))); + } else { put(&mut abilities, "flying", Tag::Byte(0)); } if let Tag::Compound(map) = &mut abilities { @@ -3153,6 +3161,7 @@ mod tests { world_spawn: Some([8, 64, 8]), spawn: Some([20.0, 70.0, 20.0]), difficulty_locked: true, + flying: true, ..Level::default() }; level.weather.raining = true; @@ -3164,6 +3173,7 @@ mod tests { assert_eq!(back.seed, -42); assert_eq!((back.time, back.day_time), (1234, 25000)); assert!(back.creative); + assert!(back.flying); assert_eq!(back.position, Some([1.5, 70.0, -3.25])); assert_eq!(back.rotation, [90.0, 10.0]); assert_eq!((back.health, back.food, back.saturation), (17.0, 15, 2.5)); @@ -3246,4 +3256,33 @@ mod tests { level.active_effects.clear(); assert!(level.player_tag().get("active_effects").is_none()); } + + #[test] + fn creative_flight_status_persists_in_save_and_resets_for_survival() { + let mut level = Level { + creative: true, + flying: true, + ..Level::default() + }; + let tag = level.player_tag(); + let flying_byte = tag + .get("abilities") + .and_then(|a| number(a.get("flying"))) + .unwrap_or(0); + assert_eq!(flying_byte, 1); + + let mut loaded = Level::default(); + loaded.read_player(&tag); + assert!(loaded.flying); + + // Survival mode resets flying ability to 0 in save. + level.creative = false; + level.game_mode = 0; + let tag_survival = level.player_tag(); + let flying_survival = tag_survival + .get("abilities") + .and_then(|a| number(a.get("flying"))) + .unwrap_or(0); + assert_eq!(flying_survival, 0); + } } From c78bf747446a1027732eed77530fc1a389153f2e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 23:36:29 +0200 Subject: [PATCH 28/67] Cave culling: search on a worker thread, frames never wait for it The section occlusion search (SectionOcclusionGraph) ran on the render thread whenever a section was meshed or the camera entered another section: at render distance 32 that is a breadth-first walk over about 124,000 cells, and while exploring it ran nearly every frame. As in vanilla 26.3, the full search now runs off the render thread. A worker keeps its own copy of every section's face connectivity, fed the changes with each job, and searches over dense arrays around the camera. Frames use the last finished graph and never wait for the next. The search starts at the top of the frame, as soon as the camera is known, so it usually lands before the frame builds its visible list. A section meshed in between propagates into the graph the frame has (vanilla's runPartialUpdate), so a wall that opens shows what is behind it at once; a section that closes goes at the next full search. A graph that never reached the camera's section (after a teleport, say) is not used: until the next one lands every section in the frustum is drawn. The search itself is unchanged: the same box, steps and first-entry rule, checked cell for cell against the previous code in a test. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 24 +- crates/horizon-client/src/gfx.rs | 27 +- crates/horizon-client/src/gfx/occlusion.rs | 614 +++++++++++++++++---- 3 files changed, 549 insertions(+), 116 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 26709516f..994da0fe7 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -8584,6 +8584,22 @@ impl Game<'_> { camera.y.floor() as i32, camera.z.floor() as i32, ]; + // The camera inside an opaque block sees nothing through it: disable. + let cave_culling = self.settings.horizon.cave_culling + && !self + .res + .facts + .get(self.world.block(block[0], block[1], block[2])) + .solid_render; + // The cave culling search runs on its worker while the frame is prepared. + if cave_culling + && self.title.is_none() + && let Some(renderer) = self.renderer.as_mut() + { + horizon_client::spans::time("occlusion", || { + renderer.start_occlusion(camera, (self.streaming.radius * 16) as f32) + }); + } let biome = self .world .chunks @@ -8772,13 +8788,7 @@ impl Game<'_> { skybox: self.dim == Dim::Overworld && self.settings.horizon.sky, end_sky: self.dim == Dim::End && self.settings.horizon.sky, environment_fog: (self.dim == Dim::Nether).then_some((sky.fog_start, sky.fog_end)), - // The camera inside an opaque block sees nothing through it: disable. - cave_culling: self.settings.horizon.cave_culling - && !self - .res - .facts - .get(self.world.block(block[0], block[1], block[2])) - .solid_render, + cave_culling, water_fog, fog: Some({ let mut fog = self.frame_fog(camera, &sky, biome); diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 3c232c755..be842c3c6 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -1978,6 +1978,13 @@ impl Renderer { pub fn has_section(&self, key: SectionKey) -> bool { self.sections.contains(key) } + /// Bring the cave culling graph up to date for `camera` without waiting: + /// a search this starts runs on its worker while the frame is prepared, + /// and `render` takes it if it has finished. + pub fn start_occlusion(&mut self, camera: DVec3, distance: f32) { + self.occlusion + .update(camera_section(camera), (distance / 16.0) as i32 + 2); + } pub fn upload(&mut self, key: SectionKey, mesh: &SectionMesh) { self.occlusion.set(key, mesh.visibility); if mesh.block_entities.is_empty() { @@ -2466,17 +2473,10 @@ impl Renderer { planes: frustum(view_proj), }; let camera = frame.camera; - let camera_section = ( - (camera.x.floor() as i32) >> 4, - (camera.y.floor() as i32 - crate::chunk::MIN_Y) >> 4, - (camera.z.floor() as i32) >> 4, - ); + let camera_section = camera_section(camera); let culling = !frame.title && frame.cave_culling; if culling { - let radius = (frame.distance / 16.0) as i32 + 2; - crate::spans::time("occlusion", || { - self.occlusion.update(camera_section, radius) - }); + crate::spans::time("occlusion", || self.start_occlusion(camera, frame.distance)); } let reachable = &self.occlusion; let mut visible = std::mem::take(&mut self.visible); @@ -3363,6 +3363,15 @@ enum Pack { Java(java_stages::JavaStages), } +/// The section the camera is in. +fn camera_section(camera: DVec3) -> SectionKey { + ( + (camera.x.floor() as i32) >> 4, + (camera.y.floor() as i32 - crate::chunk::MIN_Y) >> 4, + (camera.z.floor() as i32) >> 4, + ) +} + fn world_projection(fov_degrees: f32, aspect: f32, far: f32) -> Mat4 { Mat4::perspective_rh(fov_degrees.to_radians(), aspect, far, 0.05) } diff --git a/crates/horizon-client/src/gfx/occlusion.rs b/crates/horizon-client/src/gfx/occlusion.rs index e871c50c0..b63e7eae5 100644 --- a/crates/horizon-client/src/gfx/occlusion.rs +++ b/crates/horizon-client/src/gfx/occlusion.rs @@ -2,11 +2,18 @@ //! faces. Breadth-first from the camera section through the faces each //! section's VisGraph connects, never stepping back towards the camera. //! -//! The search runs over dense arrays around the camera rather than hash sets, -//! so it stays cheap enough to redo whenever a section is meshed. +//! As in vanilla, the full search runs off the render thread: a worker keeps +//! its own copy of every section's connectivity and searches over dense +//! arrays around the camera. Frames use the last finished graph and never wait +//! for the next one. A section meshed in between propagates into the graph the +//! frame has (vanilla's partial update), so what it opens shows at once; what +//! it closes goes at the next full search. use super::SectionKey; +use crate::mesher::ALL_VISIBLE; use std::collections::{HashMap, VecDeque}; +use std::sync::mpsc::{self, Receiver, Sender, TryRecvError}; +/// Vanilla's `Direction` order: down, up, north, south, west, east. const STEPS: [(i32, i32, i32); 6] = [ (0, -1, 0), (0, 1, 0), @@ -15,74 +22,92 @@ const STEPS: [(i32, i32, i32); 6] = [ (-1, 0, 0), (1, 0, 0), ]; +/// In `Graph::directions`: the cell has a node. +const REACHED: u8 = 1 << 7; +/// In `Graph::entered`: the camera's own section, entered through no face. const NONE: u8 = 6; -pub struct Occlusion { - /// VisGraph connectivity per column, one entry per section (bottom first). - columns: HashMap<(i32, i32), Box<[u64]>>, +/// VisGraph connectivity per column, one entry per section (bottom first). +/// A missing column or section connects every face. +struct Columns { sections: i32, - dirty: bool, - from: Option, + map: HashMap<(i32, i32), Box<[u64]>>, +} + +impl Columns { + fn new(sections: i32) -> Self { + Self { + sections, + map: HashMap::new(), + } + } + /// Whether the section's connectivity changed. + fn set(&mut self, key: SectionKey, vis: u64) -> bool { + if !(0..self.sections).contains(&key.1) { + return false; + } + let column = match self.map.get_mut(&(key.0, key.2)) { + Some(column) => column, + None if vis == ALL_VISIBLE => return false, + None => self + .map + .entry((key.0, key.2)) + .or_insert_with(|| vec![ALL_VISIBLE; self.sections as usize].into()), + }; + let slot = &mut column[key.1 as usize]; + if *slot == vis { + return false; + } + *slot = vis; + if vis == ALL_VISIBLE && column.iter().all(|&v| v == ALL_VISIBLE) { + self.map.remove(&(key.0, key.2)); + } + true + } + fn get(&self, key: SectionKey) -> u64 { + self.map + .get(&(key.0, key.2)) + .and_then(|column| column.get(usize::try_from(key.1).ok()?)) + .copied() + .unwrap_or(ALL_VISIBLE) + } + fn apply(&mut self, edit: Edit) { + match edit { + Edit::Set(key, vis) => { + self.set(key, vis); + } + Edit::Clear => self.map.clear(), + } + } +} + +#[derive(Clone, Copy)] +enum Edit { + Set(SectionKey, u64), + Clear, +} + +/// One search's result over the box around the camera: vanilla's +/// `GraphStorage.sectionToNodeMap`, as dense arrays. +#[derive(Default)] +struct Graph { + from: SectionKey, radius: i32, /// The searched box: lowest section y and its height in sections. y0: i32, height: i32, - reached: Vec, - vis: Vec, - queue: VecDeque<(u32, u8, u8)>, + /// Per cell: `REACHED`, and the directions travelled from the camera to + /// it (`Node.directions`). + directions: Vec, + /// Per cell: the face it was first entered through (`NONE` for the + /// camera's). Vanilla's `Node.sourceDirections` also keeps the faces it is + /// reached through later; Horizon keeps the first only. + entered: Vec, } -impl Occlusion { - pub fn new(sections: i32) -> Self { - Self { - columns: HashMap::new(), - sections, - dirty: true, - from: None, - radius: 0, - y0: 0, - height: 0, - reached: Vec::new(), - vis: Vec::new(), - queue: VecDeque::new(), - } - } - pub fn set(&mut self, key: SectionKey, vis: u64) { - if !(0..self.sections).contains(&key.1) { - return; - } - let sections = self.sections as usize; - let column = self - .columns - .entry((key.0, key.2)) - .or_insert_with(|| vec![crate::mesher::ALL_VISIBLE; sections].into()); - let slot = &mut column[key.1 as usize]; - if *slot != vis { - *slot = vis; - self.dirty = true; - } - } - pub fn remove(&mut self, key: SectionKey) { - let Some(column) = self.columns.get_mut(&(key.0, key.2)) else { - return; - }; - if let Some(slot) = column.get_mut(key.1 as usize) - && *slot != crate::mesher::ALL_VISIBLE - { - *slot = crate::mesher::ALL_VISIBLE; - self.dirty = true; - } - if column.iter().all(|&v| v == crate::mesher::ALL_VISIBLE) { - self.columns.remove(&(key.0, key.2)); - } - } - pub fn clear(&mut self) { - self.columns.clear(); - self.dirty = true; - } +impl Graph { fn index(&self, key: SectionKey) -> Option { - let from = self.from?; - let (dx, dz, dy) = (key.0 - from.0, key.2 - from.2, key.1 - self.y0); + let (dx, dz, dy) = (key.0 - self.from.0, key.2 - self.from.2, key.1 - self.y0); let side = 2 * self.radius + 1; if dx.abs() > self.radius || dz.abs() > self.radius || !(0..self.height).contains(&dy) { return None; @@ -90,86 +115,331 @@ impl Occlusion { Some((((dz + self.radius) * side + dx + self.radius) * self.height + dy) as usize) } fn key(&self, index: usize) -> SectionKey { - let from = self.from.expect("searched"); let side = 2 * self.radius + 1; let i = index as i32; - let dy = i % self.height; let column = i / self.height; ( - from.0 + column % side - self.radius, - self.y0 + dy, - from.2 + column / side - self.radius, + self.from.0 + column % side - self.radius, + self.y0 + i % self.height, + self.from.2 + column / side - self.radius, ) } - pub fn contains(&self, key: SectionKey) -> bool { - self.index(key).is_some_and(|i| self.reached[i]) + fn contains(&self, key: SectionKey) -> bool { + self.index(key) + .is_some_and(|i| self.directions[i] & REACHED != 0) } - /// Search again when sections changed or the camera moved to another section. - pub fn update(&mut self, from: SectionKey, radius: i32) { - if !self.dirty && self.from == Some(from) && self.radius == radius { - return; - } - self.from = Some(from); + /// The full search from `from` (vanilla's `scheduleFullUpdate`). + fn search( + &mut self, + columns: &Columns, + from: SectionKey, + radius: i32, + vis: &mut Vec, + queue: &mut VecDeque, + ) { + self.from = from; self.radius = radius; self.y0 = from.1.min(-1); - self.height = from.1.max(self.sections) - self.y0 + 1; + self.height = from.1.max(columns.sections) - self.y0 + 1; let side = (2 * radius + 1) as usize; let size = side * side * self.height as usize; - self.reached.clear(); - self.reached.resize(size, false); - self.vis.clear(); - self.vis.resize(size, crate::mesher::ALL_VISIBLE); + self.directions.clear(); + self.directions.resize(size, 0); + self.entered.clear(); + self.entered.resize(size, NONE); + vis.clear(); + vis.resize(size, ALL_VISIBLE); for dz in -radius..=radius { for dx in -radius..=radius { - let Some(column) = self.columns.get(&(from.0 + dx, from.2 + dz)) else { + let Some(column) = columns.map.get(&(from.0 + dx, from.2 + dz)) else { continue; }; let base = self .index((from.0 + dx, 0, from.2 + dz)) .expect("column inside the searched box"); - self.vis[base..base + column.len()].copy_from_slice(column); + vis[base..base + column.len()].copy_from_slice(column); } } let start = self.index(from).expect("camera inside the searched box"); - self.reached[start] = true; - self.queue.clear(); - self.queue.push_back((start as u32, NONE, 0)); - while let Some((index, entered, travelled)) = self.queue.pop_front() { - let vis = self.vis[index as usize]; - let key = self.key(index as usize); + self.directions[start] = REACHED; + queue.clear(); + queue.push_back(start as u32); + self.propagate(queue, columns.sections, |i, _| vis[i]); + } + /// Vanilla's `runUpdates`: take nodes from the queue and step through + /// every face their entry face connects to, away from the camera. + fn propagate( + &mut self, + queue: &mut VecDeque, + sections: i32, + vis: impl Fn(usize, SectionKey) -> u64, + ) { + while let Some(index) = queue.pop_front() { + let i = index as usize; + let key = self.key(i); + let directions = self.directions[i]; + let entered = self.entered[i]; + let exits = if entered == NONE { + 0x3f + } else { + (vis(i, key) >> (u32::from(entered) * 6)) as u8 & 0x3f + }; for (d, step) in STEPS.iter().enumerate() { - let opposite = d ^ 1; - if travelled & (1 << opposite) != 0 { - continue; - } - if entered != NONE && vis & (1 << (usize::from(entered) * 6 + d)) == 0 { + if exits & (1 << d) == 0 || directions & (1 << (d ^ 1)) != 0 { continue; } let next = (key.0 + step.0, key.1 + step.1, key.2 + step.2); - if next.1 < -1 || next.1 > self.sections { + if next.1 < -1 || next.1 > sections { continue; } let Some(n) = self.index(next) else { continue }; - if !self.reached[n] { - self.reached[n] = true; - self.queue - .push_back((n as u32, opposite as u8, travelled | (1 << d))); + if self.directions[n] & REACHED == 0 { + self.directions[n] = directions | 1 << d; + self.entered[n] = (d ^ 1) as u8; + queue.push_back(n as u32); } } } + } +} + +struct Job { + edits: Vec, + from: SectionKey, + radius: i32, + /// Buffers to reuse. + graph: Graph, +} + +/// The thread that runs full searches over its own copy of the columns. +struct Worker { + jobs: Sender, + done: Receiver<(Graph, Vec)>, +} + +impl Worker { + fn spawn(sections: i32) -> Option { + let (jobs, job_rx) = mpsc::channel::(); + let (done_tx, done) = mpsc::channel(); + std::thread::Builder::new() + .name("occlusion".into()) + .spawn(move || { + let mut columns = Columns::new(sections); + let (mut vis, mut queue) = (Vec::new(), VecDeque::new()); + while let Ok(mut job) = job_rx.recv() { + for edit in job.edits.drain(..) { + columns.apply(edit); + } + job.graph + .search(&columns, job.from, job.radius, &mut vis, &mut queue); + if done_tx.send((job.graph, job.edits)).is_err() { + break; + } + } + }) + .ok()?; + Some(Self { jobs, done }) + } +} + +pub struct Occlusion { + /// The render thread's copy, for partial updates (and searches when the + /// worker is gone). + columns: Columns, + /// Edits the worker has not been sent yet. + edits: Vec, + /// A full search is due: sections changed since the last one started. + dirty: bool, + /// The last finished search; `None` until one finishes, or after the + /// columns were cleared. + graph: Option, + spare: Graph, + worker: Option, + /// The camera section and radius of the running search. + running: Option<(SectionKey, i32)>, + /// The running search started before the columns were cleared: drop it. + discard: bool, + /// The camera section and radius `graph`'s search started from (or is + /// starting from). + searched: Option<(SectionKey, i32)>, + /// Sections changed since `graph`'s search started, to propagate from. + changed: Vec, + /// Sections changed since the running search started: they propagate into + /// its graph when it lands. + changed_late: Vec, + vis: Vec, + queue: VecDeque, +} + +impl Occlusion { + pub fn new(sections: i32) -> Self { + Self { + columns: Columns::new(sections), + edits: Vec::new(), + dirty: true, + graph: None, + spare: Graph::default(), + worker: Worker::spawn(sections), + running: None, + discard: false, + searched: None, + changed: Vec::new(), + changed_late: Vec::new(), + vis: Vec::new(), + queue: VecDeque::new(), + } + } + pub fn set(&mut self, key: SectionKey, vis: u64) { + if self.columns.set(key, vis) { + self.edits.push(Edit::Set(key, vis)); + self.dirty = true; + self.changed.push(key); + if self.running.is_some() { + self.changed_late.push(key); + } + } + } + pub fn remove(&mut self, key: SectionKey) { + self.set(key, ALL_VISIBLE); + } + pub fn clear(&mut self) { + self.columns.map.clear(); + self.edits.clear(); + self.edits.push(Edit::Clear); + self.dirty = true; + // Another world: the old graph says nothing about it. + if let Some(graph) = self.graph.take() { + self.spare = graph; + } + self.searched = None; + self.discard = self.running.is_some(); + self.changed.clear(); + self.changed_late.clear(); + } + /// Whether `key` is visible from the camera. Without a usable graph + /// (before the first search lands, or with the camera somewhere the graph + /// never reached) every section is. + pub fn contains(&self, key: SectionKey) -> bool { + self.graph.as_ref().is_none_or(|g| g.contains(key)) + } + /// Take a finished search, propagate changed sections into the graph, + /// and start a search when sections changed or the camera moved to + /// another section. Never waits for the worker. + pub fn update(&mut self, from: SectionKey, radius: i32) { + self.collect(false); + self.propagate_changes(); + if self.running.is_none() && (self.dirty || self.searched != Some((from, radius))) { + self.start(from, radius); + } + // A graph that never reached the camera's section hides everything + // around it: until the new one lands, cull by the frustum alone. + if self + .graph + .as_ref() + .is_some_and(|g| g.from != from && !g.contains(from)) + && let Some(graph) = self.graph.take() + { + self.spare = graph; + } + } + fn start(&mut self, from: SectionKey, radius: i32) { self.dirty = false; + self.searched = Some((from, radius)); + let graph = std::mem::take(&mut self.spare); + if let Some(worker) = &self.worker { + let job = Job { + edits: std::mem::take(&mut self.edits), + from, + radius, + graph, + }; + if worker.jobs.send(job).is_ok() { + self.running = Some((from, radius)); + self.changed_late.clear(); + return; + } + log::warn!("Horizon: the occlusion worker stopped; culling on the render thread"); + self.worker = None; + } + self.edits.clear(); + let mut graph = Graph::default(); + graph.search(&self.columns, from, radius, &mut self.vis, &mut self.queue); + self.land(graph); + } + fn collect(&mut self, wait: bool) { + let Some(worker) = &self.worker else { return }; + if self.running.is_none() { + return; + } + let result = if wait { + worker.done.recv().map_err(|_| TryRecvError::Disconnected) + } else { + worker.done.try_recv() + }; + match result { + Ok((graph, edits)) => { + self.running = None; + if self.edits.is_empty() { + self.edits = edits; + } + if std::mem::take(&mut self.discard) { + self.spare = graph; + return; + } + self.land(graph); + self.changed.clear(); + self.changed.append(&mut self.changed_late); + } + Err(TryRecvError::Empty) => {} + Err(TryRecvError::Disconnected) => { + // The edits it held are lost with it: search here from now on. + self.worker = None; + self.running = None; + self.discard = false; + self.dirty = true; + } + } + } + fn land(&mut self, graph: Graph) { + if let Some(old) = self.graph.replace(graph) { + self.spare = old; + } + } + /// Vanilla's `runPartialUpdate`: what changed sections now connect to. + fn propagate_changes(&mut self) { + let Some(graph) = &mut self.graph else { + self.changed.clear(); + return; + }; + self.queue.clear(); + for key in self.changed.drain(..) { + if let Some(i) = graph.index(key) + && graph.directions[i] & REACHED != 0 + { + self.queue.push_back(i as u32); + } + } + let columns = &self.columns; + graph.propagate(&mut self.queue, columns.sections, |_, key| columns.get(key)); + } + /// Wait for the running search: tests, and A/B timing of the old + /// blocking search. + #[cfg(test)] + fn settle(&mut self, from: SectionKey, radius: i32) { + self.update(from, radius); + self.collect(true); + self.propagate_changes(); } } #[cfg(test)] mod tests { use super::*; - use crate::mesher::ALL_VISIBLE; #[test] fn open_space_reaches_the_whole_box() { let mut o = Occlusion::new(4); - o.update((0, 1, 0), 2); + o.settle((0, 1, 0), 2); for x in -2..=2 { for z in -2..=2 { for y in -1..=4 { @@ -186,11 +456,155 @@ mod tests { for (dx, dy, dz) in STEPS { o.set((dx, 1 + dy, dz), 0); } - o.update((0, 1, 0), 3); + o.settle((0, 1, 0), 3); assert!(o.contains((1, 1, 0)), "a neighbour is seen"); assert!(!o.contains((2, 1, 0)), "nothing shows through it"); o.set((1, 1, 0), ALL_VISIBLE); - o.update((0, 1, 0), 3); + o.settle((0, 1, 0), 3); assert!(o.contains((2, 1, 0))); } + + #[test] + fn frames_never_wait_and_partial_updates_open_at_once() { + let mut o = Occlusion::new(4); + for (dx, dy, dz) in STEPS { + o.set((dx, 1 + dy, dz), 0); + } + o.settle((0, 1, 0), 3); + assert!(!o.contains((2, 1, 0))); + // Opening the wall shows what is behind it in the same frame, from the + // graph the frame has, whether or not the full search has finished. + o.set((1, 1, 0), ALL_VISIBLE); + o.update((0, 1, 0), 3); + assert!(o.contains((2, 1, 0))); + assert!(o.contains((3, 1, 0))); + // Closing it again lasts until the next full search lands. + o.set((1, 1, 0), 0); + o.settle((0, 1, 0), 3); + o.settle((0, 1, 0), 3); + assert!(!o.contains((2, 1, 0))); + } + + #[test] + fn a_graph_that_lost_the_camera_culls_nothing() { + let mut o = Occlusion::new(4); + for (dx, dy, dz) in STEPS { + o.set((dx, 1 + dy, dz), 0); + } + o.settle((0, 1, 0), 3); + assert!(!o.contains((3, 1, 0))); + // The camera jumped past the wall; the next search is still running. + let o = &mut o; + o.update((3, 1, 0), 3); + if o.running.is_some() { + assert!(o.contains((3, 1, 0))); + } + o.settle((3, 1, 0), 3); + assert!(o.contains((3, 1, 0))); + assert!( + !o.contains((0, 1, 0)), + "the old camera section is walled in" + ); + } + + /// The search before it moved to the worker (main at ce38c8d7), over the + /// same box: whether each cell is reached. + fn reference(columns: &Columns, from: SectionKey, radius: i32) -> Vec { + let y0 = from.1.min(-1); + let height = from.1.max(columns.sections) - y0 + 1; + let side = 2 * radius + 1; + let index = |k: SectionKey| { + let (dx, dz, dy) = (k.0 - from.0, k.2 - from.2, k.1 - y0); + (dx.abs() <= radius && dz.abs() <= radius && (0..height).contains(&dy)) + .then(|| (((dz + radius) * side + dx + radius) * height + dy) as usize) + }; + let size = (side * side * height) as usize; + let mut reached = vec![false; size]; + let start = index(from).unwrap(); + reached[start] = true; + let mut queue = VecDeque::from([(from, NONE, 0u8)]); + while let Some((key, entered, travelled)) = queue.pop_front() { + let vis = columns.get(key); + for (d, step) in STEPS.iter().enumerate() { + let opposite = d ^ 1; + if travelled & (1 << opposite) != 0 { + continue; + } + if entered != NONE && vis & (1 << (usize::from(entered) * 6 + d)) == 0 { + continue; + } + let next = (key.0 + step.0, key.1 + step.1, key.2 + step.2); + if next.1 < -1 || next.1 > columns.sections { + continue; + } + let Some(n) = index(next) else { continue }; + if !reached[n] { + reached[n] = true; + queue.push_back((next, opposite as u8, travelled | (1 << d))); + } + } + } + reached + } + + fn random_world(o: &mut Occlusion, seed: u64, count: usize) { + let mut seed = seed; + let mut next = || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed + }; + for _ in 0..count { + let r = next(); + let key = ( + (r % 13) as i32 - 6, + ((r >> 8) % 6) as i32, + ((r >> 16) % 13) as i32 - 6, + ); + // Open, half open or nearly closed sections. + let vis = match r >> 40 & 3 { + 0 => next() & next() & next(), + 1 => next(), + _ => next() | next(), + }; + o.set(key, vis & ALL_VISIBLE); + } + } + + /// The graph is the search the render thread did before, cell for cell. + #[test] + fn same_sections_as_before() { + let mut hidden = 0; + for seed in [0x9e37_79b9, 7, 0xdead_beef] { + let mut o = Occlusion::new(6); + random_world(&mut o, seed, 900); + for from in [(0, 2, 0), (3, 0, -2), (-5, 5, 4), (1, -1, 1), (0, 7, 0)] { + o.settle(from, 6); + o.settle(from, 6); + let graph = o.graph.as_ref().expect("searched"); + let reached: Vec = + graph.directions.iter().map(|&d| d & REACHED != 0).collect(); + assert_eq!(reached, reference(&o.columns, from, 6), "{seed} {from:?}"); + hidden += reached.iter().filter(|&&r| !r).count(); + } + } + assert!(hidden > 1000, "the worlds hide sections ({hidden})"); + } + + /// The worker's graph is the one the render thread would compute. + #[test] + fn the_worker_matches_a_search_on_the_render_thread() { + let mut o = Occlusion::new(6); + random_world(&mut o, 0x9e37_79b9, 800); + for from in [(0, 2, 0), (3, 0, -2), (-5, 5, 4)] { + o.settle(from, 6); + o.settle(from, 6); + let mut local = Graph::default(); + local.search(&o.columns, from, 6, &mut Vec::new(), &mut VecDeque::new()); + let graph = o.graph.as_ref().expect("searched"); + assert_eq!(graph.directions, local.directions); + assert_eq!(graph.entered, local.entered); + } + } } From 01f4c56dc10baa1ddaf442b4542c29207a193d65 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:21:10 +0200 Subject: [PATCH 29/67] Cull boats, paintings, item frames, armor stands, TNT and tridents like vanilla These entities were drawn every frame wherever they were in the loaded world: every armor stand, item frame and painting of every loaded village or base went through its model, item and per-vertex occlusion test even far behind the camera. EntityRenderer.shouldRender now decides, as for mobs and minecarts: - Entity.shouldRenderAtSqrDistance from the current position: 64 times the bounding box's mean edge times the entity distance option (boats about 71 blocks, primed TNT 63, paintings by size); item frames 1024 blocks; armor stands 256 times the box's mean edge whatever the option (4 for a marker's empty box); tridents ten times the default (AbstractArrow); firework rockets 64 blocks. - the interpolated culling box grown by 0.5 in the view; tridents grow it by 1.5 more (ThrownTridentRenderer), armor stands with a dragon head by 0.5 (LivingEntityRenderer), item frames holding a map use the block-wide box. The fishing bobber is unchanged: vanilla never frustum-culls it (FishingHookRenderer.affectedByCulling) and Horizon only draws the player's own, always within its 64 blocks. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/armor_stands.rs | 26 ++++++++ crates/horizon-client/src/game/boats.rs | 13 +++- .../horizon-client/src/game/entity_culling.rs | 61 +++++++++++++++++++ crates/horizon-client/src/game/fireworks.rs | 14 +++++ crates/horizon-client/src/game/frames.rs | 18 ++++++ crates/horizon-client/src/game/paintings.rs | 9 +++ crates/horizon-client/src/game/tnt.rs | 17 ++++++ crates/horizon-client/src/game/trident.rs | 19 ++++++ 8 files changed, 176 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/armor_stands.rs b/crates/horizon-client/src/game/armor_stands.rs index 80e42169d..4ea8df359 100644 --- a/crates/horizon-client/src/game/armor_stands.rs +++ b/crates/horizon-client/src/game/armor_stands.rs @@ -413,6 +413,32 @@ impl Game<'_> { if a.invisible { continue; } + // ArmorStand.shouldRenderAtSqrDistance: 256 times the box's mean + // edge (4 for a marker's empty box), whatever the entity distance. + // LivingEntityRenderer grows the culling box by 0.5 for a dragon head. + let (width, height) = match (a.marker, a.small) { + (true, _) => (0.0, 0.0), + (false, true) => (0.25, 0.9875), + (false, false) => (0.5, 1.975), + }; + let edge = (width + height + width) / 3.0 * 4.0; + let range = if edge == 0.0 { 4.0 } else { edge } * 64.0; + let dragon = a.equipment[5] + .as_ref() + .is_some_and(|i| &*i.item == "minecraft:dragon_head"); + let position = super::entity_culling::at(a.position); + if !self.standing_entity_in_view( + camera, + position, + position, + alpha, + width, + height, + range, + if dragon { 0.5 } else { 0.0 }, + ) { + continue; + } let Some(model) = (if a.small { &assets.small } else { &assets.main }) else { continue; }; diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs index a5a609cac..8f2cad5f5 100644 --- a/crates/horizon-client/src/game/boats.rs +++ b/crates/horizon-client/src/game/boats.rs @@ -2,7 +2,7 @@ //! paddling controls, collisions, riding (`positionRider`, `clampRotation`, //! dismounting), `BoatItem` placement, `VehicleEntity.hurtServer` breaking, //! saving and `AbstractBoatRenderer` with animated paddles. -use super::{Game, Keys, view_vector}; +use super::{Game, Keys, entity_culling::at, view_vector}; use horizon_client::{ blocks::Fluid, collisions::SceneCollisions, @@ -700,8 +700,19 @@ impl Game<'_> { /// AbstractBoatRenderer. pub(super) fn boat_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let a = alpha as f32; + let range = super::entity_culling::default_range( + glam::DVec3::new(WIDTH, HEIGHT, WIDTH), + self.settings.entity_distance, + ); for b in &self.boats { let entity_start = out.len(); + // EntityRenderer.shouldRender. + let (previous, position) = (at(b.previous), at(b.position)); + if !self.standing_entity_in_view( + camera, previous, position, alpha, WIDTH, HEIGHT, range, 0.0, + ) { + continue; + } let models = if b.chest { &self.boat_models.chest_models } else { diff --git a/crates/horizon-client/src/game/entity_culling.rs b/crates/horizon-client/src/game/entity_culling.rs index 71ab13378..b710f4e73 100644 --- a/crates/horizon-client/src/game/entity_culling.rs +++ b/crates/horizon-client/src/game/entity_culling.rs @@ -49,3 +49,64 @@ impl Game<'_> { } } } + +/// Entity.shouldRenderAtSqrDistance's default range: 64 times the bounding +/// box's mean edge, scaled by the entity distance option. +pub(super) fn default_range(size: glam::DVec3, view_scale: f32) -> f64 { + let edge = (size.x + size.y + size.z) / 3.0; + let edge = if edge.is_nan() { 1.0 } else { edge }; + edge * 64.0 * f64::from(view_scale) +} + +impl Game<'_> { + /// EntityRenderer.shouldRender: the entity within `range` blocks of the + /// camera, measured from its current `position`, and its culling box + /// (`lo`..`hi`, interpolated, in world space) grown by 0.5 in the view. + pub(super) fn entity_in_view( + &self, + camera: glam::DVec3, + position: glam::DVec3, + range: f64, + lo: glam::DVec3, + hi: glam::DVec3, + ) -> bool { + if (position - camera).length_squared() >= range * range { + return false; + } + self.view_frustum.as_ref().is_none_or(|view| { + view.intersects( + (lo - camera - glam::DVec3::splat(0.5)).as_vec3(), + (hi - camera + glam::DVec3::splat(0.5)).as_vec3(), + ) + }) + } +} + +/// An entity's position as a `DVec3`. +pub(super) fn at(p: horizon_core::geometry::Vec3) -> glam::DVec3 { + glam::DVec3::new(p.x, p.y, p.z) +} + +impl Game<'_> { + /// `entity_in_view` for an entity whose box stands on its position, + /// `width` wide and `height` tall, interpolated by `alpha` and grown by + /// `grow` (a renderer's getBoundingBoxForCulling). + #[allow(clippy::too_many_arguments)] + pub(super) fn standing_entity_in_view( + &self, + camera: glam::DVec3, + previous: glam::DVec3, + position: glam::DVec3, + alpha: f64, + width: f64, + height: f64, + range: f64, + grow: f64, + ) -> bool { + let p = previous.lerp(position, alpha); + let half = glam::DVec3::new(width / 2.0 + grow, grow, width / 2.0 + grow); + let lo = p - half; + let hi = p + glam::DVec3::new(half.x, height + grow, half.z); + self.entity_in_view(camera, position, range, lo, hi) + } +} diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 0e074025f..0d8dd0b12 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -253,6 +253,20 @@ impl Game<'_> { pub(super) fn rocket_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let orientation = glam::Mat4::from_quat(self.camera_orientation()); for r in self.rockets.iter().filter(|r| !r.attached) { + // FireworkRocketEntity.shouldRenderAtSqrDistance: within 64 blocks; + // EntityRenderer.shouldRender: its 0.25 box in the view. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(r.previous), + super::entity_culling::at(r.position), + alpha, + 0.25, + 0.25, + 64.0, + 0.0, + ) { + continue; + } let p = glam::DVec3::new( r.previous.x + (r.position.x - r.previous.x) * alpha, r.previous.y + (r.position.y - r.previous.y) * alpha, diff --git a/crates/horizon-client/src/game/frames.rs b/crates/horizon-client/src/game/frames.rs index 3ffa0e67e..2977ad483 100644 --- a/crates/horizon-client/src/game/frames.rs +++ b/crates/horizon-client/src/game/frames.rs @@ -304,9 +304,27 @@ impl Game<'_> { let sprites = &self.atlas.sprites; let mut maps: Vec<(i32, glam::Mat4, u32)> = Vec::new(); let mut items: Vec<(ItemStack, u32, glam::Mat4, u8)> = Vec::new(); + // ItemFrame.shouldRenderAtSqrDistance: 1024 blocks at 100%. + let range = 16.0 * 64.0 * f64::from(self.settings.entity_distance); for f in &self.item_frames { let entity_start = out.len(); let map = f.item.as_ref().and_then(super::maps::map_id); + // EntityRenderer.shouldRender: its box (a block wide with a map), + // measured from the box's centre. + let (lo, hi) = f.bounds(); + let (mut lo, mut hi) = (glam::DVec3::from(lo), glam::DVec3::from(hi)); + if map.is_some() { + let s = f.step(); + for i in 0..3 { + if s[i] == 0 { + let c = (lo[i] + hi[i]) / 2.0; + (lo[i], hi[i]) = (c - 0.5, c + 0.5); + } + } + } + if !self.entity_in_view(camera, (lo + hi) / 2.0, range, lo, hi) { + continue; + } let s = f.step(); // Pose: to the block centre, then X/Y rotation by facing. let (x_rot, y_rot) = if s[1] == 0 { diff --git a/crates/horizon-client/src/game/paintings.rs b/crates/horizon-client/src/game/paintings.rs index 2e5542ccc..c65dbbb89 100644 --- a/crates/horizon-client/src/game/paintings.rs +++ b/crates/horizon-client/src/game/paintings.rs @@ -255,6 +255,15 @@ impl Game<'_> { }; for p in &self.paintings { let entity_start = out.len(); + // EntityRenderer.shouldRender: the default range and its box, + // measured from the box's centre (HangingEntity's position). + let (lo, hi) = p.bounds(); + let (lo, hi) = (glam::DVec3::from(lo), glam::DVec3::from(hi)); + let range = + super::entity_culling::default_range(hi - lo, self.settings.entity_distance); + if !self.entity_in_view(camera, (lo + hi) / 2.0, range, lo, hi) { + continue; + } let Some(v) = self.painting_variants.get(&p.variant) else { continue; }; diff --git a/crates/horizon-client/src/game/tnt.rs b/crates/horizon-client/src/game/tnt.rs index 78f107599..ad77f67c1 100644 --- a/crates/horizon-client/src/game/tnt.rs +++ b/crates/horizon-client/src/game/tnt.rs @@ -241,7 +241,24 @@ impl Game<'_> { .and_then(|name| self.atlas.sprites.get(name).copied()); } } + let range = super::entity_culling::default_range( + glam::DVec3::splat(PrimedTnt::SIZE), + self.settings.entity_distance, + ); for t in &self.tnts { + // EntityRenderer.shouldRender. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(t.previous), + super::entity_culling::at(t.position), + alpha, + PrimedTnt::SIZE, + PrimedTnt::SIZE, + range, + 0.0, + ) { + continue; + } let fuse = t.fuse as f32 - alpha as f32 + 1.0; let scale = if fuse < 10.0 { let g = (1.0 - fuse / 10.0).clamp(0.0, 1.0); diff --git a/crates/horizon-client/src/game/trident.rs b/crates/horizon-client/src/game/trident.rs index 8c372fa46..2a5a89e1a 100644 --- a/crates/horizon-client/src/game/trident.rs +++ b/crates/horizon-client/src/game/trident.rs @@ -350,7 +350,26 @@ impl Game<'_> { else { return; }; + // AbstractArrow.shouldRenderAtSqrDistance: ten times the default. + let range = 10.0 + * super::entity_culling::default_range( + glam::DVec3::splat(2.0 * HALF), + self.settings.entity_distance, + ); for t in &self.tridents { + // EntityRenderer.shouldRender; ThrownTridentRenderer grows the box by 1.5. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(t.previous), + super::entity_culling::at(t.position), + alpha, + 2.0 * HALF, + 2.0 * HALF, + range, + 1.5, + ) { + continue; + } let p = glam::DVec3::new( t.previous.x + (t.position.x - t.previous.x) * alpha, t.previous.y + (t.position.y - t.previous.y) * alpha, From 2faa99e5f1632f9efbe5a15300a7a0e901708142 Mon Sep 17 00:00:00 2001 From: LookImEcho-Rick <62575172+LookImEcho-Rick@users.noreply.github.com> Date: Sat, 17 Oct 2026 19:30:23 -0500 Subject: [PATCH 30/67] Fix item tooltip display parity --- .../examples/tooltip_display_fixture.rs | 40 +++++++ crates/horizon-client/src/game/stations.rs | 83 +++++++++----- crates/horizon-client/src/item_data.rs | 40 ++++--- crates/horizon-client/src/item_tooltip.rs | 101 ++++++++++++++++++ .../parity/VanillaTooltipDisplayOracle.java | 36 +++++++ tools/test_tooltip_display_parity.py | 31 ++++++ 6 files changed, 292 insertions(+), 39 deletions(-) create mode 100644 crates/horizon-client/examples/tooltip_display_fixture.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTooltipDisplayOracle.java create mode 100644 tools/test_tooltip_display_parity.py diff --git a/crates/horizon-client/examples/tooltip_display_fixture.rs b/crates/horizon-client/examples/tooltip_display_fixture.rs new file mode 100644 index 000000000..ec61159cd --- /dev/null +++ b/crates/horizon-client/examples/tooltip_display_fixture.rs @@ -0,0 +1,40 @@ +//! Tooltip display visibility oracle companion for tools/test_tooltip_display_parity.py. +use horizon_client::{item_tooltip::TooltipDisplay, items::ItemStack}; +use horizon_world::nbt::Tag; +use std::{error::Error, fmt::Write}; + +fn main() -> Result<(), Box> { + let args: Vec = std::env::args().collect(); + let mut output = String::new(); + for line in std::fs::read_to_string(&args[1])?.lines() { + let fields: Vec<&str> = line.split('\t').collect(); + let hide_tooltip: bool = fields[0].parse()?; + let creative: bool = fields[1].parse()?; + let hide_attributes: bool = fields[2].parse()?; + let mut component = Vec::new(); + if hide_tooltip { + component.push(("hide_tooltip", Tag::Byte(1))); + } + if hide_attributes { + component.push(( + "hidden_components", + Tag::List(vec![Tag::String("minecraft:attribute_modifiers".into())]), + )); + } + let mut stack = ItemStack::new("minecraft:diamond_sword", 1); + if !component.is_empty() { + stack + .data_mut() + .set_other("minecraft:tooltip_display", Some(Tag::compound(component))); + } + let display = TooltipDisplay::for_stack(&stack); + writeln!( + output, + "{}\t{}", + display.name_visible(creative), + display.shows("minecraft:attribute_modifiers") + )?; + } + std::fs::write(&args[2], output)?; + Ok(()) +} diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index 8cd751548..332f06b23 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -1885,17 +1885,27 @@ impl Game<'_> { } else { self.pack.names.item(&stack.item) }; - let mut lines = vec![(name, color)]; + let display = horizon_client::item_tooltip::TooltipDisplay::for_stack(stack); + let mut lines = Vec::new(); + if display.name_visible(self.screen == Screen::Creative) { + lines.push((name, color)); + } let translate = |k: &str| e.translate(k).map(str::to_owned); // ItemContainerContents.addToTooltip. let hover_name = |s: &ItemStack| { s.custom_name() .map_or_else(|| self.pack.names.item(&s.item), str::to_owned) }; - for line in horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate) { - lines.push((line, u32::MAX)); + if display.shows("minecraft:container") { + for line in + horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate) + { + lines.push((line, u32::MAX)); + } } - if stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow" { + if display.shows("minecraft:potion_contents") + && (stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow") + { let scale = match &*stack.item { "minecraft:lingering_potion" => 0.25, "minecraft:tipped_arrow" => 0.125, @@ -1912,7 +1922,9 @@ impl Game<'_> { lines.push((line, c)); } } - if let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone()) { + if display.shows("minecraft:trim") + && let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone()) + { lines.push(( e.translate("item.minecraft.smithing_template.upgrade") .unwrap_or("Upgrade:") @@ -1929,30 +1941,44 @@ impl Game<'_> { lines.push((format!(" {p}"), rgba(170, 170, 170, 255))); lines.push((format!(" {m}"), rgba(170, 170, 170, 255))); } - for (text, curse) in e - .tooltip(stack.stored_enchantments()) - .into_iter() - .chain(e.tooltip(stack.enchantments())) - { - lines.push(( - text, - if curse { - rgba(255, 85, 85, 255) - } else { - rgba(170, 170, 170, 255) - }, - )); + if display.shows("minecraft:stored_enchantments") { + for (text, curse) in e.tooltip(stack.stored_enchantments()) { + lines.push(( + text, + if curse { + rgba(255, 85, 85, 255) + } else { + rgba(170, 170, 170, 255) + }, + )); + } + } + if display.shows("minecraft:enchantments") { + for (text, curse) in e.tooltip(stack.enchantments()) { + lines.push(( + text, + if curse { + rgba(255, 85, 85, 255) + } else { + rgba(170, 170, 170, 255) + }, + )); + } } // BannerPatternLayers.addToTooltip: up to six layer names. - for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) { - let key = self.res.stations.banners.layer_key(layer); - lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255))); + if display.shows("minecraft:banner_patterns") { + for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) { + let key = self.res.stations.banners.layer_key(layer); + lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255))); + } } // Spawner.appendHoverText: what a spawner or trial spawner item spawns. - if matches!( - &*stack.item, - "minecraft:spawner" | "minecraft:trial_spawner" - ) { + if display.shows("minecraft:block_entity_data") + && matches!( + &*stack.item, + "minecraft:spawner" | "minecraft:trial_spawner" + ) + { let gray = rgba(170, 170, 170, 255); match block_entity_item::spawner_entity(stack) { Some(id) => { @@ -1969,7 +1995,9 @@ impl Game<'_> { } } } - if let Some(info) = self.res.items.get(&stack.item) { + if display.shows("minecraft:attribute_modifiers") + && let Some(info) = self.res.items.get(&stack.item) + { let base = |name: &str, default| { self.player .as_ref() @@ -1997,7 +2025,8 @@ impl Game<'_> { &translate, )); } - if let Some(info) = self.res.items.get(&stack.item) + if display.shows("minecraft:damage") + && let Some(info) = self.res.items.get(&stack.item) && info.max_damage > 0 && stack.damage > 0 && self.options_advanced_tooltips() diff --git a/crates/horizon-client/src/item_data.rs b/crates/horizon-client/src/item_data.rs index d54564b7a..adc4e0f18 100644 --- a/crates/horizon-client/src/item_data.rs +++ b/crates/horizon-client/src/item_data.rs @@ -110,6 +110,16 @@ pub const OMINOUS_LAYERS: [(&str, &str); 8] = [ ("circle", "light_gray"), ("border", "black"), ]; + +/// The raid captain banner's default tooltip setting, stored by vanilla as a +/// component but represented by `StackData::ominous_banner` in Horizon. +fn ominous_tooltip_display() -> Tag { + Tag::compound([( + "hidden_components", + Tag::List(vec![Tag::String("minecraft:banner_patterns".into())]), + )]) +} + /// `WrittenBookContent` with the pages as plain text. #[derive(Clone, Debug, Default, PartialEq)] pub struct WrittenBook { @@ -610,13 +620,9 @@ pub fn components_tag(damage: u32, data: Option<&StackData>) -> Option { "minecraft:banner_patterns", Tag::List(OMINOUS_LAYERS.iter().map(|(p, c)| layer(p, c)).collect()), )); - entries.push(( - "minecraft:tooltip_display", - Tag::compound([( - "hidden_components", - Tag::List(vec![Tag::String("minecraft:banner_patterns".into())]), - )]), - )); + if d.other("minecraft:tooltip_display").is_none() { + entries.push(("minecraft:tooltip_display", ominous_tooltip_display())); + } entries.push(( "minecraft:item_name", Tag::compound([( @@ -793,10 +799,8 @@ pub fn decode_components(components: Option<&Tag>) -> (u32, Option { + component: Option<&'a Tag>, + ominous_banner: bool, +} + +impl<'a> TooltipDisplay<'a> { + pub fn for_stack(stack: &'a ItemStack) -> Self { + Self { + component: stack + .data() + .and_then(|data| data.other("minecraft:tooltip_display")), + ominous_banner: stack.data().is_some_and(|data| data.ominous_banner), + } + } + + fn hides_tooltip(&self) -> bool { + matches!( + self.component.and_then(|display| display.get("hide_tooltip")), + Some(Tag::Byte(value)) if *value != 0 + ) + } + + /// `ItemStack.getTooltipLines`: `hide_tooltip` suppresses the name unless + /// the tooltip flag is creative. + pub fn name_visible(&self, creative: bool) -> bool { + creative || !self.hides_tooltip() + } + + /// `TooltipDisplay.shows`: whether details for a data component are shown. + pub fn shows(&self, component: &str) -> bool { + if self.hides_tooltip() { + return false; + } + // `item_data::decode_components` models the raid captain banner's + // default hidden pattern tooltip as `ominous_banner` instead of + // retaining its redundant `tooltip_display` component. + if self.ominous_banner + && self.component.is_none() + && component == "minecraft:banner_patterns" + { + return false; + } + let Some(Tag::List(hidden)) = self + .component + .and_then(|display| display.get("hidden_components")) + else { + return true; + }; + let component = horizon_world::nbt::JavaString::from(component); + !hidden + .iter() + .any(|tag| matches!(tag, Tag::String(id) if id == &component)) + } +} /// `EquipmentSlotGroup` in declaration order, the order tooltips list them in. const SLOT_GROUPS: [&str; 11] = [ @@ -348,6 +406,49 @@ pub fn lines( mod tests { use super::*; + #[test] + fn tooltip_display_controls_name_and_component_visibility() { + let mut stack = ItemStack::new("minecraft:diamond_sword", 1); + let display = TooltipDisplay::for_stack(&stack); + assert!(display.name_visible(false)); + assert!(display.shows("minecraft:attribute_modifiers")); + + stack.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::compound([( + "hidden_components", + Tag::List(vec![Tag::String("minecraft:attribute_modifiers".into())]), + )])), + ); + let display = TooltipDisplay::for_stack(&stack); + assert!(display.name_visible(false)); + assert!(!display.shows("minecraft:attribute_modifiers")); + assert!(display.shows("minecraft:enchantments")); + + stack.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::compound([("hide_tooltip", Tag::Byte(1))])), + ); + let display = TooltipDisplay::for_stack(&stack); + assert!(!display.name_visible(false)); + assert!(display.name_visible(true)); + assert!(!display.shows("minecraft:attribute_modifiers")); + } + + #[test] + fn ominous_banner_keeps_its_default_hidden_pattern_tooltip() { + let mut banner = crate::mobs::raider::ominous_banner(); + let display = TooltipDisplay::for_stack(&banner); + assert!(!display.shows("minecraft:banner_patterns")); + + banner.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::Compound(Default::default())), + ); + let display = TooltipDisplay::for_stack(&banner); + assert!(display.shows("minecraft:banner_patterns")); + } + #[test] fn ominous_banner_name_and_rarity_use_the_stack() { let translate = |key: &str| match key { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTooltipDisplayOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTooltipDisplayOracle.java new file mode 100644 index 000000000..c31dce98c --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTooltipDisplayOracle.java @@ -0,0 +1,36 @@ +package dev.horizonlab.parity; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.LinkedHashSet; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.core.component.DataComponents; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.component.TooltipDisplay; + +/** The name and attribute lines returned for a diamond sword's tooltip. */ +public final class VanillaTooltipDisplayOracle { + public static void main(String[] args) throws Exception { + ParityData.init(); + var context = Item.TooltipContext.of(ParityData.lookup); + var output = new ArrayList(); + for (String line : Files.readAllLines(Path.of(args[0]))) { + String[] fields = line.split("\t"); + boolean hideTooltip = Boolean.parseBoolean(fields[0]); + boolean creative = Boolean.parseBoolean(fields[1]); + boolean hideAttributes = Boolean.parseBoolean(fields[2]); + var hidden = new LinkedHashSet>(); + if (hideAttributes) hidden.add(DataComponents.ATTRIBUTE_MODIFIERS); + var stack = new ItemStack(Items.DIAMOND_SWORD, 1); + stack.set(DataComponents.TOOLTIP_DISPLAY, new TooltipDisplay(hideTooltip, hidden)); + TooltipFlag flag = creative ? TooltipFlag.Default.NORMAL.asCreative() : TooltipFlag.Default.NORMAL; + var tooltip = stack.getTooltipLines(context, null, flag); + output.add((!tooltip.isEmpty()) + "\t" + (tooltip.size() > 1)); + } + Files.write(Path.of(args[1]), output); + } +} diff --git a/tools/test_tooltip_display_parity.py b/tools/test_tooltip_display_parity.py new file mode 100644 index 000000000..e98807d1b --- /dev/null +++ b/tools/test_tooltip_display_parity.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +"""Compare item tooltip display visibility with the pinned vanilla client.""" +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + + +def cases(build): + values = [ + (False, False, False), + (False, False, True), + (False, True, True), + (True, False, False), + (True, True, False), + (True, True, True), + ] + path = build / 'cases.txt' + path.write_text('\n'.join('\t'.join(map(str, row)) for row in values) + '\n') + return path, len(values) + + +if __name__ == '__main__': + harness.differential( + 'tooltip-display-parity', 'VanillaTooltipDisplayOracle', 'tooltip_display_fixture', cases, + 'Diamond sword name and attribute tooltip visibility for hide_tooltip, hidden_components, and creative flags', + sources=[ + lab.ROOT / 'crates/horizon-client/src/game/stations.rs', + lab.ROOT / 'crates/horizon-client/src/item_tooltip.rs', + ], + expected_lines=6) From 2c244f188824aedc48212955ac87333863c67a30 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:04:14 +0200 Subject: [PATCH 31/67] Creaking hearts: scan only sections whose palette holds a heart scan_for_hearts read all 4,096 blocks of every non-air section of each new chunk on the server thread to find creaking hearts. A section whose palette holds no heart state (PalettedContainer.maybeHas) cannot have one, so it is skipped. Same block entities, same order. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/creaking.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game/creaking.rs b/crates/horizon-client/src/game/creaking.rs index 099f3d7d8..4a8a5239b 100644 --- a/crates/horizon-client/src/game/creaking.rs +++ b/crates/horizon-client/src/game/creaking.rs @@ -327,13 +327,16 @@ impl Game<'_> { } let hearts = &self.creaking.hearts; let _ = heart; + let is_heart = |id: u32| hearts.get(id as usize).copied().unwrap_or(false); for (si, section) in chunk.sections.iter().enumerate() { - if section.is_uniform(0) { + // Only a section whose palette holds a heart state can have + // one: the others are skipped without reading their 4,096 blocks. + if !section.blocks.maybe_has(is_heart) { continue; } for i in 0..4096 { let id = section.get(i); - if !hearts.get(id as usize).copied().unwrap_or(false) { + if !is_heart(id) { continue; } let (lx, lz, ly) = ((i & 15) as i32, ((i >> 4) & 15) as i32, (i >> 8) as i32); From 43e8b54918739c858df6a14cd7a9e6c4baf2c6bb Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:57:33 +0200 Subject: [PATCH 32/67] Server: track boats, minecarts, armor stands, frames and paintings to clients A server sent its clients only mobs and dropped items. Boats, every kind of minecart, armor stands, item frames, paintings and the entities Horizon keeps without simulating are now tracked as ServerEntity does: the AddEntity data (hanging entities at their block, with their direction), the synched data that differs from the defaults, equipment, passengers (ClientboundSetPassengersPacket at pairing and on change; a passenger syncs only its rotation and resyncs its position when it dismounts), and the type's update interval and range. Also vanilla-exact on the way, as the new harness found: - mobs pair with their health (always non-default) and Mob flags (no AI, left-handed); - /summon without NBT summons any type from an empty compound; - armor stands move before gravity (LivingEntity.travelInAir), so a stand at rest keeps vanilla's -0.0784 vertical motion. tools/test_server_entities_parity.py compares, from a vanilla-codec client, what vanilla's dedicated server and Horizon's track for a scene of 20 entities. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entities.rs | 17 +- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/commands.rs | 8 +- crates/horizon-client/src/game/lan.rs | 18 +- .../horizon-client/src/game/lan_entities.rs | 259 ++++++++++++++++++ crates/horizon-client/src/game/minecarts.rs | 4 + crates/horizon-client/src/lan/entity.rs | 183 +++++++++---- crates/horizon-client/src/lan/hub.rs | 7 +- crates/horizon-protocol/src/packets/play.rs | 9 + .../horizonlab/parity/EntityTranscript.java | 160 +++++++++++ .../parity/VanillaProtocolClient.java | 36 ++- tools/test_server_entities_parity.py | 174 ++++++++++++ 12 files changed, 811 insertions(+), 65 deletions(-) create mode 100644 crates/horizon-client/src/game/lan_entities.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java create mode 100644 tools/test_server_entities_parity.py diff --git a/crates/horizon-client/src/entities.rs b/crates/horizon-client/src/entities.rs index 4ceafb3d0..5c905ac54 100644 --- a/crates/horizon-client/src/entities.rs +++ b/crates/horizon-client/src/entities.rs @@ -535,15 +535,16 @@ impl ArmorStand { Vec3::new(p.x + w, p.y + h, p.z + w), ) } - /// LivingEntity.travel with gravity 0.08 and 0.98 drag, falling until it - /// lands (armor stands have no other movement). + /// LivingEntity.travelInAir (armor stands have no other movement): move + /// by the velocity (a landing stops the fall, + /// `Block.updateEntityMovementAfterFallOn`), then gravity 0.08 and the + /// 0.98 drag, so a stand at rest on the ground keeps -0.0784 as vanilla's. pub fn tick(&mut self, scene: &mut crate::collisions::SceneCollisions) { if self.marker { return; } - let mut v = self.velocity; - v.y -= 0.08; - let wanted = v; + let wanted = self.velocity; + let mut y = wanted.y; if let Ok(moved) = scene.resolve(self.bounds(), wanted, 0.0, self.on_ground) { self.position = Vec3::new( self.position.x + moved.x, @@ -551,9 +552,11 @@ impl ArmorStand { self.position.z + moved.z, ); self.on_ground = wanted.y < 0.0 && moved.y > wanted.y; - v = Vec3::new(0.0, if moved.y != wanted.y { 0.0 } else { v.y * 0.98 }, 0.0); + if moved.y != wanted.y { + y = 0.0; + } } - self.velocity = v; + self.velocity = Vec3::new(0.0, (y - 0.08) * f64::from(0.98f32), 0.0); } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e126c533a..436802be2 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -64,6 +64,7 @@ mod jukebox; mod lan; mod lan_actions; mod lan_crafting; +mod lan_entities; mod lan_mobs; mod lan_pvp; mod lan_screen; diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 3ca938d6a..27f89b243 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -1481,7 +1481,13 @@ impl Game<'_> { self.carts.push(cart); name = Text::translate(&format!("entity.minecraft.{k}"), &[]); } - _ => return Err(err("commands.summon.failed", &[])), + // SummonCommand.createEntity: any other type from an empty compound. + _ => { + let Some(target) = self.summon_with_nbt(&kind, pos, "{}")? else { + return Err(err("commands.summon.failed", &[])); + }; + name = self.target_name(target); + } } } self.feedback(Text::translate("commands.summon.success", &[name])); diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 57c7b9ee0..209c34286 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -73,7 +73,7 @@ pub(super) struct Owner { } /// A stable UUID for an entity that has none. -fn entity_uuid(id: u32) -> u128 { +pub(super) fn entity_uuid(id: u32) -> u128 { 0x4856_5a4e_0000_0000_0000_0000_0000_0000u128 | u128::from(id) } @@ -144,11 +144,15 @@ fn mob_meta(m: &Mob, hub: &Hub) -> Vec<(u8, i32, Value)> { meta.push((3, serializer::BOOLEAN, Value::Bool(true))); } } - if m.health < m.kind.max_health { - meta.push((9, serializer::FLOAT, Value::Float(m.health.max(0.0)))); + // LivingEntity.DATA_HEALTH_ID: defined as 1, then set to the max health. + let health = m.health.max(0.0); + if health != 1.0 { + meta.push((9, serializer::FLOAT, Value::Float(health))); } - if kind == "minecraft:rabbit" && m.no_ai { - meta.push((15, serializer::BYTE, Value::Byte(1))); + // Mob.DATA_MOB_FLAGS_ID: no AI 1, left-handed 2. + let flags = i8::from(m.no_ai) | i8::from(m.extra.left_handed) << 1; + if flags != 0 { + meta.push((15, serializer::BYTE, Value::Byte(flags))); } if m.is_baby() && let Some(index) = baby_index(kind) @@ -713,7 +717,7 @@ impl Game<'_> { }) } - /// Mobs and items as the entities guests track. + /// The entities guests track: mobs, items and the rest (game/lan_entities.rs). fn lan_entities(&self, hub: &mut Hub) -> Vec { let mut out = Vec::with_capacity(self.mobs.len() + self.dropped.len() + self.orbs.len()); for m in self.mobs.iter().filter(|m| !m.removed) { @@ -770,6 +774,7 @@ impl Game<'_> { equipment: Vec::new(), }); } + self.lan_object_states(hub, &mut out); out } @@ -785,6 +790,7 @@ impl Game<'_> { if state.is_some() || host.hub.config.dedicated { let has_guests = !host.hub.guests.is_empty(); let entities = if has_guests { + self.lan_riding(state.as_ref().map(|s| s.entity_id), &mut host.hub.riding); self.lan_entities(&mut host.hub) } else { Vec::new() diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs new file mode 100644 index 000000000..9064447aa --- /dev/null +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -0,0 +1,259 @@ +//! The entities beyond mobs and items that a server tracks to its clients +//! (`ChunkMap.TrackedEntity`, `ServerEntity`): boats, minecarts, armor +//! stands, item frames, paintings and the entities Horizon keeps without +//! simulating them, each as vanilla pairs it (`getAddEntityPacket` and the +//! synched data that differs from the defaults), and who rides what. +use super::Game; +use horizon_client::lan::{ + entity::{EntityState, Riding}, + hub::Hub, +}; +use horizon_protocol::entity_data::{Value, serializer}; +use horizon_world::nbt::Tag; + +/// `ArmorStand`'s default poses (head, body, left arm, right arm, left leg, +/// right leg), DATA_HEAD_POSE to DATA_RIGHT_LEG_POSE (entries 16 to 21), +/// under their `Pose` keys. +const STAND_POSES: [(&str, [f32; 3]); 6] = [ + ("Head", [0.0, 0.0, 0.0]), + ("Body", [0.0, 0.0, 0.0]), + ("LeftArm", [-10.0, 0.0, -10.0]), + ("RightArm", [-15.0, 0.0, 10.0]), + ("LeftLeg", [-1.0, 0.0, -1.0]), + ("RightLeg", [1.0, 0.0, 1.0]), +]; + +fn state(id: u32, kind: &str, position: [f64; 3], yaw: f32, on_ground: bool) -> EntityState { + EntityState { + id: id as i32, + uuid: super::lan::entity_uuid(id), + kind: kind.to_owned(), + position, + yaw, + pitch: 0.0, + head_yaw: 0.0, + on_ground, + velocity: [0.0; 3], + data: 0, + meta: Vec::new(), + equipment: Vec::new(), + } +} + +/// `VehicleEntity`'s DATA_ID_HURT, DATA_ID_HURTDIR and DATA_ID_DAMAGE +/// (entries 8 to 10) when they differ from their defaults (0, 1, 0). +fn vehicle_meta(hurt_time: i32, hurt_dir: i32, damage: f32) -> Vec<(u8, i32, Value)> { + let mut meta = Vec::new(); + if hurt_time != 0 { + meta.push((8, serializer::INT, Value::Int(hurt_time))); + } + if hurt_dir != 1 { + meta.push((9, serializer::INT, Value::Int(hurt_dir))); + } + if damage != 0.0 { + meta.push((10, serializer::FLOAT, Value::Float(damage))); + } + meta +} + +/// An armor stand's pose from its compound (`ArmorStandPose`), each part +/// that differs from the default. +fn stand_poses(raw: Option<&Tag>) -> Vec<(u8, i32, Value)> { + let Some(pose) = raw.and_then(|t| t.get("Pose")) else { + return Vec::new(); + }; + let mut meta = Vec::new(); + for (i, (key, default)) in STAND_POSES.iter().enumerate() { + let Some(Tag::List(values)) = pose.get(key) else { + continue; + }; + let mut r = *default; + for (k, v) in values.iter().take(3).enumerate() { + if let Tag::Float(bits) = v { + r[k] = f32::from_bits(*bits); + } + } + if r != *default { + meta.push(( + 16 + i as u8, + serializer::ROTATIONS, + Value::Floats(r.to_vec()), + )); + } + } + meta +} + +impl Game<'_> { + /// Who rides what (`Entity.getPassengers`): mobs on mobs, and the + /// host in its boat or minecart. + pub(super) fn lan_riding(&self, host_entity: Option, riding: &mut Riding) { + riding.clear(); + for m in self.mobs.iter().filter(|m| !m.removed) { + if let Some(vehicle) = m.extra.mount { + riding.add(vehicle as i32, m.id as i32); + } + } + if let (Some(host), Some(vehicle)) = (host_entity, self.riding) { + riding.add(vehicle as i32, host); + } + } + + /// Boats, minecarts, armor stands, item frames, paintings and the + /// entities kept unsimulated, as guests track them. + pub(super) fn lan_object_states(&self, hub: &mut Hub, out: &mut Vec) { + for b in &self.boats { + let p = b.position; + let mut e = state(b.id, &b.entity_type(), [p.x, p.y, p.z], b.yaw, b.on_ground); + e.velocity = [b.velocity.x, b.velocity.y, b.velocity.z]; + e.meta = vehicle_meta(b.hurt_time, b.hurt_dir, b.damage); + // AbstractBoat.DATA_ID_PADDLE_LEFT and DATA_ID_PADDLE_RIGHT. + for (k, paddling) in b.paddling.iter().enumerate() { + if *paddling { + e.meta + .push((11 + k as u8, serializer::BOOLEAN, Value::Bool(true))); + } + } + out.push(e); + } + for c in &self.carts { + let p = c.position; + let mut e = state(c.id, c.kind.id(), [p.x, p.y, p.z], c.yaw, c.on_ground()); + e.velocity = [c.velocity.x, c.velocity.y, c.velocity.z]; + e.meta = vehicle_meta(c.hurt_time, c.hurt_dir, c.damage); + match c.kind { + // MinecartFurnace.DATA_ID_FUEL. + super::minecarts::CartKind::Furnace if c.fuel > 0 => { + e.meta.push((13, serializer::BOOLEAN, Value::Bool(true))); + } + // MinecartCommandBlock.DATA_ID_COMMAND_NAME. + super::minecarts::CartKind::CommandBlock => { + if let Some(Tag::String(command)) = + self.persist.raw.get(&c.id).and_then(|t| t.get("Command")) + { + let command = command.to_string_lossy(); + if !command.is_empty() { + e.meta + .push((13, serializer::STRING, Value::String(command))); + } + } + } + _ => {} + } + out.push(e); + } + for a in &self.armor_stands { + let p = a.position; + let mut e = state( + a.id, + "minecraft:armor_stand", + [p.x, p.y, p.z], + a.yaw, + a.on_ground, + ); + e.head_yaw = a.yaw; + e.velocity = [a.velocity.x, a.velocity.y, a.velocity.z]; + if a.fire_ticks > 0 { + e.meta.push((0, serializer::BYTE, Value::Byte(1))); + } + // LivingEntity.DATA_HEALTH_ID: 1 until the stand's max health is set. + e.meta.push((9, serializer::FLOAT, Value::Float(a.health))); + // ArmorStand.DATA_CLIENT_FLAGS: small 1, arms 4, no base plate 8, marker 16. + let flags = i8::from(a.small) + | i8::from(a.show_arms) << 2 + | i8::from(a.no_base_plate) << 3 + | i8::from(a.marker) << 4; + if flags != 0 { + e.meta.push((15, serializer::BYTE, Value::Byte(flags))); + } + e.meta.extend(stand_poses(self.persist.raw.get(&a.id))); + // Stored main hand, off hand, feet, legs, chest, head: the wire's slots 0 to 5. + for (slot, item) in a.equipment.iter().enumerate() { + if item.is_some() { + e.equipment.push((slot as u8, hub.stack(item))); + } + } + out.push(e); + } + for f in &self.item_frames { + // ItemFrame.getAddEntityPacket: the block it hangs in and its direction. + let mut e = state(f.id, f.kind(), f.pos.map(f64::from), 0.0, false); + e.pitch = match f.facing { + 0 => 90.0, + 1 => -90.0, + _ => 0.0, + }; + e.yaw = match f.facing { + 2 => 180.0, + 4 => 90.0, + 5 => 270.0, + _ => 0.0, + }; + e.data = i32::from(f.facing); + // HangingEntity.DATA_DIRECTION, south by default. + if f.facing != 3 { + e.meta + .push((8, serializer::DIRECTION, Value::Id(i32::from(f.facing)))); + } + if f.item.is_some() { + e.meta + .push((9, serializer::ITEM_STACK, Value::Item(hub.stack(&f.item)))); + } + if f.rotation != 0 { + e.meta + .push((10, serializer::INT, Value::Int(i32::from(f.rotation)))); + } + out.push(e); + } + for p in &self.paintings { + let mut e = state(p.id, "minecraft:painting", p.pos.map(f64::from), 0.0, false); + // Direction.get2DDataValue (south 0, west 1, north 2, east 3) to its + // 3D data value and yaw. + let facing = [3, 4, 2, 5][usize::from(p.facing & 3)]; + e.yaw = f32::from(p.facing & 3) * 90.0; + e.data = facing; + if facing != 3 { + e.meta.push((8, serializer::DIRECTION, Value::Id(facing))); + } + // Painting.DATA_PAINTING_VARIANT_ID, a registry holder (id + 1), + // set unless it is the registry's first variant (VariantUtils.getAny). + if let Some(id) = hub.registry_id("minecraft:painting_variant", &p.variant) + && id != 0 + { + e.meta + .push((9, serializer::PAINTING_VARIANT, Value::Id(id + 1))); + } + out.push(e); + } + for o in &self.others { + let p = o.position; + let mut e = state(o.id, &o.kind, [p.x, p.y, p.z], o.yaw, false); + e.pitch = o.pitch; + e.head_yaw = o.yaw; + out.push(e); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn armor_stand_poses_send_only_what_differs() { + let tag = + horizon_world::nbt::parse_snbt("{Pose:{Head:[10f,20f,0f],LeftArm:[-10f,0f,-10f]}}") + .unwrap() + .0; + let meta = stand_poses(Some(&tag)); + assert_eq!( + meta, + vec![( + 16, + serializer::ROTATIONS, + Value::Floats(vec![10.0, 20.0, 0.0]) + )] + ); + assert!(stand_poses(None).is_empty()); + } +} diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index de440433d..1e0b42b50 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -157,6 +157,10 @@ pub(super) struct Rail { powered: bool, } impl Minecart { + /// `Entity.onGround`. + pub(super) fn on_ground(&self) -> bool { + self.on_ground + } pub fn new(id: u32, kind: CartKind, position: Vec3) -> Self { Self { id, diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index a37010d79..178f9bd44 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -11,7 +11,7 @@ use horizon_protocol::{ packets::play::{ self, AddEntity, EntityPositionSync, MoveEntity, MoveEntityPos, MoveEntityPosRot, MoveEntityRot, PositionPath, RotateHead, SetEntityData, SetEntityMotion, SetEquipment, - VecDelta, + SetPassengers, VecDelta, }, }; @@ -54,6 +54,40 @@ struct Sent { /// The type's update interval (0: never) and whether velocity is tracked. interval: u32, track_deltas: bool, + /// lastPassengers, and wasRiding: a passenger's position isn't synced, + /// and the first update after it dismounts is a full position sync. + passengers: Vec, + was_riding: bool, +} + +/// Who rides what this tick (`Entity.getPassengers`, `isPassenger`). +#[derive(Default)] +pub struct Riding { + /// Each vehicle's passengers, in order. + pub passengers: crate::fast_hash::FxHashMap>, + /// Each passenger's vehicle. + pub vehicle: crate::fast_hash::FxHashMap, +} + +impl Riding { + pub fn clear(&mut self) { + self.passengers.clear(); + self.vehicle.clear(); + } + /// `passenger` rides `vehicle`, after the ones already riding it. + pub fn add(&mut self, vehicle: i32, passenger: i32) { + self.passengers.entry(vehicle).or_default().push(passenger); + self.vehicle.insert(passenger, vehicle); + } + pub fn passengers(&self, id: i32) -> &[i32] { + self.passengers.get(&id).map_or(&[], Vec::as_slice) + } + fn set_passengers(&self, vehicle: i32) -> play::Clientbound { + play::Clientbound::SetPassengers(SetPassengers { + vehicle, + passengers: self.passengers(vehicle).to_vec(), + }) + } } #[derive(Default)] @@ -94,7 +128,12 @@ impl Tracker { } /// The packets that introduce an entity (ServerEntity.sendPairingData). - pub fn spawn(&mut self, e: &EntityState, entity_type: i32) -> Vec { + pub fn spawn( + &mut self, + e: &EntityState, + entity_type: i32, + riding: &Riding, + ) -> Vec { let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw)); let head = pack_degrees(e.head_yaw); let (_, interval, track_deltas) = tracking::settings(&e.kind); @@ -121,6 +160,13 @@ impl Tracker { slots: e.equipment.clone(), })); } + let passengers = riding.passengers(e.id).to_vec(); + if !passengers.is_empty() { + out.push(riding.set_passengers(e.id)); + } + if let Some(&vehicle) = riding.vehicle.get(&e.id) { + out.push(riding.set_passengers(vehicle)); + } self.sent.insert( e.id, Sent { @@ -135,6 +181,8 @@ impl Tracker { teleport_delay: 0, interval, track_deltas, + passengers, + was_riding: false, }, ); out @@ -144,17 +192,43 @@ impl Tracker { /// rotation every update interval (positions at least every 60 ticks, a /// full position sync every 400 or when the entity lands or leaves the /// ground), velocity for types that track it, changed data at once. - pub fn update(&mut self, e: &EntityState) -> Vec { + pub fn update(&mut self, e: &EntityState, riding: &Riding) -> Vec { let Some(s) = self.sent.get_mut(&e.id) else { return Vec::new(); }; let mut out = Vec::new(); + let passengers = riding.passengers(e.id); + if passengers != s.passengers.as_slice() { + out.push(riding.set_passengers(e.id)); + s.passengers = passengers.to_vec(); + } let dirty = e.meta != s.meta; let due = s.interval != 0 && s.tick.is_multiple_of(s.interval); if due || dirty { let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw)); let turned = |a: i8, b: i8| (i32::from(a) - i32::from(b)).abs() >= 1; let rotate = turned(rotation.1, s.rotation.1) || turned(rotation.0, s.rotation.0); + if riding.vehicle.contains_key(&e.id) { + // A passenger: its rotation alone (its vehicle places it). + if rotate { + out.push(play::Clientbound::MoveEntityRot(MoveEntityRot( + MoveEntity { + id: e.id, + delta: None, + rotation: Some((rotation.1, rotation.0)), + on_ground: e.on_ground, + }, + ))); + s.rotation = rotation; + } + s.base = e.position; + s.was_riding = true; + Self::send_dirty(e, s, dirty, &mut out); + Self::send_head(e, s, &mut out); + s.tick += 1; + Self::send_equipment(e, s, &mut out); + return out; + } s.teleport_delay += 1; let d: f64 = (0..3).map(|k| (e.position[k] - s.base[k]).powi(2)).sum(); let send_position = d >= 7.629_394_5e-6 || s.tick.is_multiple_of(60); @@ -166,9 +240,11 @@ impl Tracker { on_ground: e.on_ground, }; // ServerEntity.createMovePacket. - let packet = if s.teleport_delay > 400 || s.was_on_ground != e.on_ground { + let packet = if s.teleport_delay > 400 || s.was_riding || s.was_on_ground != e.on_ground + { s.was_on_ground = e.on_ground; s.teleport_delay = 0; + s.was_riding = false; Some(play::Clientbound::EntityPositionSync(sync)) } else if send_position { let delta = [0, 1, 2].map(|k| encode(e.position[k]) - encode(s.base[k])); @@ -227,33 +303,47 @@ impl Tracker { s.rotation = rotation; } } - // ServerEntity.sendDirtyEntityData. - if dirty { - let changed: Vec<_> = e - .meta - .iter() - .filter(|m| !s.meta.contains(m)) - .cloned() - .collect(); - if !changed.is_empty() { - out.push(play::Clientbound::SetEntityData(SetEntityData { - id: e.id, - values: changed, - })); - } - s.meta = e.meta.clone(); - } - let head = pack_degrees(e.head_yaw); - if turned(head, s.head) { - out.push(play::Clientbound::RotateHead(RotateHead { - id: e.id, - head_yaw: head, - })); - s.head = head; - } + Self::send_dirty(e, s, dirty, &mut out); + Self::send_head(e, s, &mut out); } s.tick += 1; - // LivingEntity.detectEquipmentUpdates: changed slots every tick. + Self::send_equipment(e, s, &mut out); + out + } + + /// ServerEntity.sendDirtyEntityData. + fn send_dirty(e: &EntityState, s: &mut Sent, dirty: bool, out: &mut Vec) { + if !dirty { + return; + } + let changed: Vec<_> = e + .meta + .iter() + .filter(|m| !s.meta.contains(m)) + .cloned() + .collect(); + if !changed.is_empty() { + out.push(play::Clientbound::SetEntityData(SetEntityData { + id: e.id, + values: changed, + })); + } + s.meta = e.meta.clone(); + } + + fn send_head(e: &EntityState, s: &mut Sent, out: &mut Vec) { + let head = pack_degrees(e.head_yaw); + if (i32::from(head) - i32::from(s.head)).abs() >= 1 { + out.push(play::Clientbound::RotateHead(RotateHead { + id: e.id, + head_yaw: head, + })); + s.head = head; + } + } + + /// LivingEntity.detectEquipmentUpdates: changed slots every tick. + fn send_equipment(e: &EntityState, s: &mut Sent, out: &mut Vec) { if e.equipment != s.equipment { // Cleared slots are sent as empty stacks. let mut slots: Vec<(u8, Option)> = e @@ -275,7 +365,6 @@ impl Tracker { } s.equipment = e.equipment.clone(); } - out } } @@ -303,24 +392,24 @@ mod tests { #[test] fn moves_go_out_every_update_interval() { let mut t = Tracker::default(); - let spawn = t.spawn(&state(0.0), 100); + let spawn = t.spawn(&state(0.0), 100, &Riding::default()); assert!(matches!(spawn[0], play::Clientbound::AddEntity(_))); // Tick 0 is due and a multiple of 60: the position goes out unchanged. assert!(matches!( - t.update(&state(0.0))[..], + t.update(&state(0.0), &Riding::default())[..], [play::Clientbound::MoveEntityPos(_)] )); // A zombie updates every third tick. - assert!(t.update(&state(0.5)).is_empty()); - assert!(t.update(&state(0.5)).is_empty()); + assert!(t.update(&state(0.5), &Riding::default()).is_empty()); + assert!(t.update(&state(0.5), &Riding::default()).is_empty()); assert!(matches!( - t.update(&state(0.5))[..], + t.update(&state(0.5), &Riding::default())[..], [play::Clientbound::MoveEntityPos(_)] )); for _ in 0..2 { - t.update(&state(40.0)); + t.update(&state(40.0), &Riding::default()); } - let jump = t.update(&state(40.0)); + let jump = t.update(&state(40.0), &Riding::default()); assert!(matches!(jump[0], play::Clientbound::EntityPositionSync(_))); } @@ -328,13 +417,13 @@ mod tests { fn landing_sends_a_position_sync() { let mut t = Tracker::default(); let mut e = state(0.0); - t.spawn(&e, 100); - t.update(&e); + t.spawn(&e, 100, &Riding::default()); + t.update(&e, &Riding::default()); e.on_ground = false; - t.update(&e); - t.update(&e); + t.update(&e, &Riding::default()); + t.update(&e, &Riding::default()); assert!(matches!( - t.update(&e)[..], + t.update(&e, &Riding::default())[..], [play::Clientbound::EntityPositionSync(_)] )); } @@ -343,11 +432,11 @@ mod tests { fn metadata_and_equipment_send_only_the_changes() { let mut t = Tracker::default(); let mut e = state(0.0); - t.spawn(&e, 100); - t.update(&e); + t.spawn(&e, 100, &Riding::default()); + t.update(&e, &Riding::default()); e.meta.push((0, 0, Value::Byte(1))); e.equipment.push((0, Some(Stack::new(5, 1)))); - let out = t.update(&e); + let out = t.update(&e, &Riding::default()); assert!( out.iter() .any(|p| matches!(p, play::Clientbound::SetEntityData(_))) @@ -356,9 +445,9 @@ mod tests { out.iter() .any(|p| matches!(p, play::Clientbound::SetEquipment(_))) ); - assert!(t.update(&e).is_empty()); + assert!(t.update(&e, &Riding::default()).is_empty()); e.equipment.clear(); - let out = t.update(&e); + let out = t.update(&e, &Riding::default()); let Some(play::Clientbound::SetEquipment(eq)) = out.first() else { panic!("expected equipment"); }; diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index b18187835..d710c239e 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -124,6 +124,8 @@ pub struct Hub { /// The `player_movement_check` and `elytra_movement_check` game rules, /// set by the game before each tick. pub movement_checks: (bool, bool), + /// Who rides what, set by the game before each tick. + pub riding: super::entity::Riding, last_clock: i64, last_rate: f32, ticking: (f32, bool), @@ -237,6 +239,7 @@ impl Hub { sync, ticks: 0, movement_checks: (true, true), + riding: Default::default(), last_clock: i64::MIN, last_rate: 0.0, ticking: (20.0, false), @@ -1501,14 +1504,14 @@ impl Hub { continue; } if self.guests[gi].tracker.is_tracking(e.id) { - let packets = self.guests[gi].tracker.update(e); + let packets = self.guests[gi].tracker.update(e, &self.riding); self.guests[gi].send_all(&packets); } else if visible(&self.guests[gi], ei) { let Some(kind) = self.entity_type(&e.kind) else { continue; }; // Players need their list entry first; guests got it at join. - let packets = self.guests[gi].tracker.spawn(e, kind); + let packets = self.guests[gi].tracker.spawn(e, kind, &self.riding); self.guests[gi].send_all(&packets); } } diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 95ce869a4..40b70624a 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -590,6 +590,10 @@ packet!(SetCursorItem { item: Option => Slot }); packet!(SetDefaultSpawnPosition { pos: GlobalPos => GlobalPos, yaw: f32 => Float, pitch: f32 => Float }); packet!(SetEntityData { id: i32 => VarInt, values: Vec<(u8, i32, Value)> => EntityData }); packet!(SetEntityMotion { id: i32 => VarInt, velocity: [f64; 3] => LpVec3 }); +packet!( + /// ClientboundSetPassengersPacket: a vehicle and every entity riding it. + SetPassengers { vehicle: i32 => VarInt, passengers: Vec => List } +); /// ClientboundSetEquipmentPacket: (EquipmentSlot ordinal, stack) pairs. pub struct Equipment; impl Codec)>> for Equipment { @@ -1426,6 +1430,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { SetHealth(SetHealth) = SET_HEALTH, SetHeldSlot(SetHeldSlot) = SET_HELD_SLOT, SetObjective(SetObjective) = SET_OBJECTIVE, + SetPassengers(SetPassengers) = SET_PASSENGERS, SetPlayerInventory(SetPlayerInventory) = SET_PLAYER_INVENTORY, SetPlayerTeam(SetPlayerTeam) = SET_PLAYER_TEAM, SetScore(SetScore) = SET_SCORE, @@ -2052,6 +2057,10 @@ mod tests { id: 2, velocity: [0.0; 3], }), + Clientbound::SetPassengers(SetPassengers { + vehicle: 2, + passengers: vec![3, 300], + }), Clientbound::SetEquipment(SetEquipment { id: 2, slots: vec![(0, None), (5, Some(Stack::new(1, 1)))], diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java b/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java new file mode 100644 index 000000000..4d7cc9f2c --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java @@ -0,0 +1,160 @@ +package dev.horizonlab.parity; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; +import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket; +import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket; +import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket; +import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket; +import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; +import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket; +import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket; +import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket; +import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket; +import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket; +import net.minecraft.network.protocol.game.VecDeltaCodec; +import net.minecraft.world.entity.EntityTypes; +import net.minecraft.world.phys.Vec3; + +/** + * What a vanilla client knows about the entities a server tracks to it, as + * ClientPacketListener would apply the packets: each entity's spawn packet, + * the entity data it was paired with and all it was sent since, its + * equipment, attributes and passengers, and where the position syncs leave + * it (VecDeltaCodec, as handleMoveEntity and handleEntityPositionSync). + * Entity ids differ between servers: an entity is named by its type and the + * block it was added in, passengers by those names. Players are left out. + */ +final class EntityTranscript { + private static final class Tracked { + final String type; + final String add; + final VecDeltaCodec codec = new VecDeltaCodec(); + String name; + String pairing; + final Map data = new TreeMap<>(); + final Map equipment = new TreeMap<>(); + final Map attributes = new TreeMap<>(); + int[] passengers = new int[0]; + float yRot; + float xRot; + float headRot; + boolean onGround; + String motion; + boolean removed; + + Tracked(ClientboundAddEntityPacket p) { + type = BuiltInRegistries.ENTITY_TYPE.getKey(p.getType()).toString(); + Vec3 at = new Vec3(p.getX(), p.getY(), p.getZ()); + codec.setBase(at); + name = type + " " + (int) Math.floor(at.x) + "," + (int) Math.floor(at.y) + "," + (int) Math.floor(at.z); + add = "at " + at + " rot " + p.getXRot() + "," + p.getYRot() + " head " + p.getYHeadRot() + " data " + p.getData() + + " motion " + p.getMovement(); + yRot = p.getYRot(); + xRot = p.getXRot(); + headRot = p.getYHeadRot(); + motion = String.valueOf(p.getMovement()); + } + } + + private final Map byId = new HashMap<>(); + private final List all = new ArrayList<>(); + private int unknown; + + /** Applies one packet; false when it isn't about a tracked entity. */ + boolean accept(Packet packet) { + if (packet instanceof ClientboundAddEntityPacket p) { + if (p.getType() == EntityTypes.PLAYER) return true; + Tracked t = new Tracked(p); + byId.put(p.getId(), t); + all.add(t); + return true; + } + if (packet instanceof ClientboundRemoveEntitiesPacket p) { + p.entityIds().forEach(id -> { + Tracked t = byId.remove(id); + if (t != null) t.removed = true; + }); + return true; + } + Tracked t = switch (packet) { + case ClientboundSetEntityDataPacket p -> byId.get(p.id()); + case ClientboundSetEquipmentPacket p -> byId.get(p.getEntity()); + case ClientboundUpdateAttributesPacket p -> byId.get(p.getEntityId()); + case ClientboundSetPassengersPacket p -> byId.get(p.getVehicle()); + case ClientboundEntityPositionSyncPacket p -> byId.get(p.id()); + case ClientboundSetEntityMotionPacket p -> byId.get(p.id()); + case ClientboundTeleportEntityPacket p -> byId.get(p.id()); + case ClientboundMoveEntityPacket p -> byId.get(VanillaProtocolClient.moveEntityId(p)); + case ClientboundRotateHeadPacket p -> byId.get(VanillaProtocolClient.rotateHeadId(p)); + default -> null; + }; + if (t == null) { + return false; + } + switch (packet) { + case ClientboundSetEntityDataPacket p -> { + for (var v : p.packedItems()) t.data.put(v.id(), net.minecraft.network.syncher.EntityDataSerializers.getSerializedId(v.serializer()) + " " + v.value()); + if (t.pairing == null) { + StringBuilder b = new StringBuilder(); + for (var v : p.packedItems()) b.append(v.id()).append('=').append(v.value()).append(' '); + t.pairing = b.toString().trim(); + } + } + case ClientboundSetEquipmentPacket p -> p.getSlots().forEach(s -> t.equipment.put(s.getFirst().getName(), String.valueOf(s.getSecond()))); + case ClientboundUpdateAttributesPacket p -> p.getValues().forEach(a -> t.attributes.put(a.attribute().getRegisteredName(), + a.base() + " " + a.modifiers())); + case ClientboundSetPassengersPacket p -> t.passengers = p.getPassengers(); + case ClientboundEntityPositionSyncPacket p -> { + t.codec.setBase(p.position().endPosition()); + t.yRot = p.yRot(); + t.xRot = p.xRot(); + t.onGround = p.onGround(); + } + case ClientboundMoveEntityPacket p -> { + if (p.hasPosition()) t.codec.setBase(p.getPositionDelta().decode(t.codec).endPosition()); + if (p.hasRotation()) { + t.yRot = p.getYRot(); + t.xRot = p.getXRot(); + } + t.onGround = p.isOnGround(); + } + case ClientboundSetEntityMotionPacket p -> t.motion = String.valueOf(p.movement()); + case ClientboundTeleportEntityPacket p -> { + t.codec.setBase(p.change().position()); + t.onGround = p.onGround(); + } + case ClientboundRotateHeadPacket p -> t.headRot = p.getYHeadRot(); + default -> { + } + } + return true; + } + + /** One line per entity, sorted, each field named. */ + List lines() { + Map seen = new HashMap<>(); + for (Tracked t : all) { + int n = seen.merge(t.name, 1, Integer::sum); + if (n > 1) t.name += " #" + n; + } + Map names = new HashMap<>(); + byId.forEach((id, t) -> names.put(id, t.name)); + List out = new ArrayList<>(); + for (Tracked t : all) { + List riders = new ArrayList<>(); + for (int id : t.passengers) riders.add(names.getOrDefault(id, "player or unknown")); + out.add("entity " + t.name + " | add " + t.add + " | pairing " + t.pairing + " | data " + t.data + " | equipment " + t.equipment + + " | attributes " + t.attributes + " | passengers " + riders + " | now " + t.codec.getBase() + " rot " + t.xRot + "," + + t.yRot + " head " + t.headRot + " ground " + t.onGround + " motion " + t.motion + (t.removed ? " | removed" : "")); + } + out.sort(null); + return out; + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java index f87e9cf0f..ca366705b 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java @@ -113,6 +113,8 @@ public final class VanillaProtocolClient { private ProtocolInfo inbound; private ProtocolInfo outbound; private String state; + /** With a fifth argument {@code entities}: the entity transcript mode (EntityTranscript). */ + private EntityTranscript entities; public static void main(String[] args) throws Exception { ParityData.init(); @@ -121,6 +123,7 @@ public final class VanillaProtocolClient { String name = args[2]; Path output = Path.of(args[3]); VanillaProtocolClient client = new VanillaProtocolClient(); + if (args.length > 4 && args[4].equals("entities")) client.entities = new EntityTranscript(); try { client.status(host, port); client.join(host, port, name); @@ -405,6 +408,18 @@ public final class VanillaProtocolClient { play(); } + /** The entity mode's step, and how long it records the entities for (ms). */ + private static final int ENTITIES = 1000; + private static final long ENTITY_WAIT = 20_000; + + static int moveEntityId(net.minecraft.network.protocol.game.ClientboundMoveEntityPacket p) { + return (int) field(p, net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.class, "entityId"); + } + + static int rotateHeadId(net.minecraft.network.protocol.game.ClientboundRotateHeadPacket p) { + return (int) field(p, p.getClass(), "entityId"); + } + private static final String[] REPLIES = {"random value 7..7", "random value x", "trigger nope"}; private void play() throws Exception { @@ -461,6 +476,7 @@ public final class VanillaProtocolClient { if (r != null) { Packet packet = r.packet; counts.merge(r.type, 1, Integer::sum); + if (entities != null && step >= ENTITIES) entities.accept(packet); if (r.type.equals("minecraft:chunk_batch_start")) ordered = false; if (respawning && !respawned) { String line = respawnLine(r); @@ -581,8 +597,20 @@ public final class VanillaProtocolClient { commandTree(p); } } + if (entities != null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { + // Entity mode: the harness's console summons the scene once it reads this + // line; everything tracked from then on is recorded until the client leaves. + record("play-chunks", String.valueOf(chunks.size())); + send(new ServerboundChatPacket("summon the entities", Instant.now(), 0L, Optional.empty(), + new LastSeenMessages.Update(0, new BitSet(), (byte) 0))); + step = ENTITIES; + stepAt = now; + } else if (step == ENTITIES && now - stepAt > ENTITY_WAIT) { + for (String line : entities.lines()) record("play-entities", line); + return; + } // Once the chunks stop coming: chat, then /list, then leave. - if (loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { + if (entities == null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { record("play-chunks", chunks.size() + " " + chunks); BLOCK_ENTITIES.forEach((k, v) -> record("play-block-entities", k + " " + v)); // Which light layers exist depends on the neighbouring @@ -910,8 +938,12 @@ public final class VanillaProtocolClient { } private static Object field(Object owner, String name) { + return field(owner, owner.getClass(), name); + } + + private static Object field(Object owner, Class declaring, String name) { try { - var f = owner.getClass().getDeclaredField(name); + var f = declaring.getDeclaredField(name); f.setAccessible(true); return f.get(owner); } catch (ReflectiveOperationException e) { diff --git a/tools/test_server_entities_parity.py b/tools/test_server_entities_parity.py new file mode 100644 index 000000000..f97e3f15a --- /dev/null +++ b/tools/test_server_entities_parity.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Entities other than mobs and items, tracked to a vanilla client, against vanilla's dedicated server. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first, with the same server.properties (offline, view distance 4). A vanilla-codec +client (VanillaProtocolClient in its entity mode) joins, and once its chunks are in it asks for the scene: the +console freezes the ticks, builds a wall and a rail and summons, around the spawn, every kind of minecart (one +set rolling along the rail), boats and a raft (one falling onto the ground), armor stands (posed, small, with +arms, wearing a helmet, one falling), a chicken jockey, item frames (one holding a turned diamond) and paintings. The client records +for 20 seconds, the ticks running again after 3, what ClientPacketListener would make of the packets (EntityTranscript): each entity's spawn +packet (position, rotations, data, motion), the entity data it was paired with and all it was sent since, its +equipment and passengers, and where its position syncs leave it. Both transcripts must match exactly, +entity by entity (named by type and the block it was added in). Attributes are recorded but not compared yet: +Horizon sends no ClientboundUpdateAttributesPacket, for mobs either. +""" +import os +from pathlib import Path +import platform +import re +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server + from .test_locator_bar_parity import generate_world, server_dir +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server + from test_locator_bar_parity import generate_world, server_dir + +NAME = 'server-entities-parity' +PLAYER = 'Tester' + +# Around the spawn at 0 -60 0 on the flat world's grass: a wall to the north for the frames and paintings, +# a rail to the south. +SCENE = [ + 'fill -7 -60 -6 7 -57 -6 minecraft:stone', + 'fill -7 -60 9 7 -60 9 minecraft:rail', + # Minecarts. + 'summon minecraft:minecart -5.5 -60 3.5', + 'summon minecraft:chest_minecart -3.5 -60 3.5', + 'summon minecraft:furnace_minecart -1.5 -60 3.5', + 'summon minecraft:hopper_minecart 1.5 -60 3.5', + 'summon minecraft:tnt_minecart 3.5 -60 3.5', + 'summon minecraft:command_block_minecart 5.5 -60 1.5 {Command:"say hi"}', + 'summon minecraft:minecart -6.5 -60 9.5 {Motion:[0.5d,0d,0d]}', + # Boats. + 'summon minecraft:oak_boat -5.5 -60 6.5 {Rotation:[45f,0f]}', + 'summon minecraft:birch_chest_boat -2.5 -60 6.5', + 'summon minecraft:bamboo_raft 0.5 -60 6.5', + 'summon minecraft:oak_boat 4.5 -56 6.5', + # Armor stands. + 'summon minecraft:armor_stand -2.5 -60 -2.5 {Rotation:[90f,0f]}', + 'summon minecraft:armor_stand 6.5 -57 -2.5', + 'summon minecraft:armor_stand -4.5 -60 -2.5 {ShowArms:1b,Small:1b,NoBasePlate:1b,' + 'Pose:{Head:[10f,20f,0f],RightArm:[-30f,0f,15f]},equipment:{head:{id:"minecraft:diamond_helmet",count:1}}}', + # A chicken jockey (passengers). + 'summon minecraft:chicken -6.5 -60 -2.5 {NoAI:1b,Passengers:[{id:"minecraft:husk",IsBaby:1b,NoAI:1b}]}', + # Frames and paintings on the wall's south face. + 'summon minecraft:item_frame -3.5 -59 -4.97 {block_pos:[I;-4,-59,-5],Facing:3b,' + 'Item:{id:"minecraft:diamond",count:1},ItemRotation:3b}', + 'summon minecraft:glow_item_frame -1.5 -59 -4.97 {block_pos:[I;-2,-59,-5],Facing:3b}', + 'summon minecraft:painting 1.5 -59 -4.97 {block_pos:[I;1,-59,-5],facing:0b,variant:"minecraft:kebab"}', + 'summon minecraft:painting 4 -59 -4.97 {block_pos:[I;4,-59,-5],facing:0b,variant:"minecraft:courbet"}', +] + + +def scene_lines(lines): + """The transcript's entities in the scene's area (the world's own animals and slimes wander elsewhere), + without their attributes: ClientboundUpdateAttributesPacket isn't sent by Horizon yet, for mobs either.""" + out = [] + for line in lines: + m = re.match(r'play-entities entity \S+ (-?\d+),-?\d+,(-?\d+)', line) + if m and -8 <= int(m.group(1)) <= 8 and -7 <= int(m.group(2)) <= 10: + out.append(re.sub(r' \| attributes \{[^|]*\}', '', line)) + return out + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}-client.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), PLAYER, str(out), 'entities'], + cwd=lab.ROOT, stdout=log, stderr=subprocess.STDOUT, text=True) + try: + server.wait_for(rf'<{PLAYER}> summon the entities', timeout=180) + # Frozen, so that every entity is paired as it was summoned whenever each server tracks it in + # its tick; then they fall and roll. + server.console('tick freeze') + for command in SCENE: + server.console(command) + time.sleep(3) + server.console('tick unfreeze') + client.wait(timeout=120) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + lines = out.read_text(encoding='utf-8').splitlines() + (build / f'{label}.txt').write_text('\n'.join(lines) + '\n', encoding='utf-8') + return lines + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + # SERVER_ENTITIES_SIDES=vanilla (or horizon) runs one side and reads the other's last transcript. + sides = [s for s in os.environ.get('SERVER_ENTITIES_SIDES', 'vanilla,horizon').split(',') if s] + if 'horizon' in sides: + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies, + 'dev.horizonlab.parity.VanillaProtocolClient'] + + world = generate_world(vanilla_server, build) + results = {} + for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]: + if label in sides: + results[label] = run_side(label, command, client, build, world) + else: + results[label] = (build / f'{label}.txt').read_text(encoding='utf-8').splitlines() + vanilla, horizon = scene_lines(results['vanilla']), scene_lines(results['horizon']) + errors = [l for l in results['vanilla'] + results['horizon'] if l.startswith('error')] + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=0, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:8000]) + if len(vanilla) < len(SCENE) - 2: + raise AssertionError(f'only {len(vanilla)} entities tracked: the scene did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + entities=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Minecarts (one rolling), boats (one falling), armor stands, a chicken jockey, item frames and paintings tracked to a ' + 'vanilla-codec client by a dedicated server: spawn packets, entity data, equipment, passengers and the ' + 'positions the syncs leave them at.')) + print(f'server entities: {len(vanilla)} entities identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') From 583502f0f4022c7ff18c5a594457b4c7d749d5e4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:25:30 +0200 Subject: [PATCH 33/67] Clients of a server show its boats, minecarts, armor stands, frames and paintings ClientPacketListener.handleAddEntity for the objects a server tracks: a Horizon client of a server (a LAN host, a dedicated server, single player's server thread) now places the boats, minecarts, armor stands, item frames and paintings it is sent, where the server says. - Spawn: carts by kind, boats and rafts by wood (with a chest), armor stands, frames and paintings at their block with the direction from the spawn data. - Entity data: frame items and rotation, painting variants (a registry holder), armor stand flags and health, boat paddles, vehicle hurt. - Equipment: what an armor stand wears and holds. - Moves interpolate like mobs'; rotation and ground state follow. - They are cleared with the connection, a respawn or a dimension change. `status` lists them (`objects`), and lan_e2e has an `objects` check: the host summons them, the guest sees each as the host has it. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/control.rs | 2 + crates/horizon-client/src/game/remote.rs | 29 +- .../horizon-client/src/game/remote_objects.rs | 270 ++++++++++++++++++ tools/lan_e2e.py | 60 +++- 5 files changed, 357 insertions(+), 5 deletions(-) create mode 100644 crates/horizon-client/src/game/remote_objects.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 436802be2..4def60790 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -108,6 +108,7 @@ mod powder_snow; mod raids; mod recipe_book; mod remote; +mod remote_objects; mod render_control; mod render_tick; mod respawn_anchor; diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index fa72c7a40..58733bf45 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -758,6 +758,8 @@ impl Game<'_> { .map(|e| self.shown_name_tags(glam::DVec3::from_array(e))), }); status["physical_keys"] = json!(physical_keys); + // Boats, minecarts, armor stands, item frames and paintings. + status["objects"] = json!(self.objects_status()); status } } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 991bc42b2..0edf4ade7 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -80,7 +80,7 @@ pub(super) struct Remote { pub brand: String, builtin: Arc, lang: Arc, - registries: Arc, + pub(super) registries: Arc, joined: bool, status: String, entity_id: i32, @@ -245,6 +245,7 @@ impl Game<'_> { self.player = None; self.spawn = None; self.mobs.clear(); + self.clear_remote_objects(); self.creatures.projectiles.clear(); self.dropped.clear(); self.particles.clear(); @@ -363,6 +364,7 @@ impl Game<'_> { self.world = World::new(self.res.facts.air, 0); self.player = None; self.mobs.clear(); + self.clear_remote_objects(); self.dropped.clear(); self.particles.clear(); self.pickups.clear(); @@ -445,7 +447,7 @@ impl Game<'_> { .and_then(|i| i.id(name)) .unwrap_or(0) } - fn to_stack(&self, s: &Option) -> Option { + pub(super) fn to_stack(&self, s: &Option) -> Option { let s = s.as_ref()?; let mut out = ItemStack { item: self.item_name(s.item), @@ -608,6 +610,7 @@ impl Game<'_> { self.world.day_time = day; self.world.weather = weather; self.mobs.clear(); + self.clear_remote_objects(); self.dropped.clear(); self.particles.clear(); self.pickups.clear(); @@ -993,11 +996,13 @@ impl Game<'_> { if let Some(m) = mob { m.on_ground = on_ground; } + self.turn_remote_object(id, rotation.map(|(yaw, _)| yaw), on_ground); } fn remove_entity(&mut self, id: i32) { self.mobs.retain(|m| m.id != id as u32); self.orbs.retain(|o| o.id != id as u32); self.dropped.retain(|d| d.id != id as u32); + self.remove_remote_object(id as u32); if let Some(r) = self.remote.as_mut() { r.entities.remove(&id); r.lerps.remove(&id); @@ -1261,6 +1266,7 @@ impl Game<'_> { } } self.spawn_entity(p.id, &kind, p.position, rotation); + self.spawn_remote_object(p.id, &kind, p.position, rotation[0], p.data); } C::RemoveEntities(p) => { for id in p.ids { @@ -1369,10 +1375,12 @@ impl Game<'_> { o.value = (*v).max(0) as u32; } } + self.remote_object_data(p.id, &p.values); } // ClientPacketListener.handleSetEquipment: what a mob or another // player holds and wears (EquipmentSlot ordinals). C::SetEquipment(p) => { + self.remote_object_equipment(p.id, &p.slots); let items: Vec<(u8, Option>)> = p .slots .iter() @@ -2535,8 +2543,23 @@ impl Game<'_> { l.steps -= 1; } } - lerps.retain(|_, l| l.steps > 0); head_lerps.retain(|_, (_, steps)| *steps > 0); + if running { + // Boats, minecarts and armor stands interpolate like mobs. + self.tick_remote_objects(|id, at| match lerps.get_mut(&(id as i32)) { + Some(l) if l.steps > 0 => { + let n = f64::from(l.steps); + l.steps -= 1; + Vec3::new( + at.x + (l.target[0] - at.x) / n, + at.y + (l.target[1] - at.y) / n, + at.z + (l.target[2] - at.z) / n, + ) + } + _ => at, + }); + } + lerps.retain(|_, l| l.steps > 0); if let Some(r) = self.remote.as_mut() { for (id, l) in lerps { r.lerps.entry(id).or_insert(l); diff --git a/crates/horizon-client/src/game/remote_objects.rs b/crates/horizon-client/src/game/remote_objects.rs new file mode 100644 index 000000000..12a15bc34 --- /dev/null +++ b/crates/horizon-client/src/game/remote_objects.rs @@ -0,0 +1,270 @@ +//! Boats, minecarts, armor stands, item frames and paintings on a server's +//! client (`ClientPacketListener.handleAddEntity`, `handleSetEntityData`, +//! `handleSetEquipment`): the server simulates them, the client places them +//! where it says and interpolates their moves like a mob's. +use super::Game; +use horizon_client::entities::{ArmorStand, ItemFrame, Painting}; +use horizon_core::geometry::Vec3; +use horizon_protocol::entity_data::Value; +use std::sync::Arc; + +impl Game<'_> { + /// `handleAddEntity` for an object type; false when `kind` isn't one. + /// Hanging entities come at their block, with their direction as the + /// spawn data (`Direction.from3DDataValue`). + pub(super) fn spawn_remote_object( + &mut self, + id: i32, + kind: &str, + position: [f64; 3], + yaw: f32, + data: i32, + ) -> bool { + let id = id as u32; + let at = Vec3::new(position[0], position[1], position[2]); + let block = position.map(|v| v.floor() as i32); + self.remove_remote_object(id); + if let Some(cart) = super::minecarts::CartKind::from_id(kind) { + let mut c = super::minecarts::Minecart::new(id, cart, at); + c.yaw = yaw; + c.previous_yaw = yaw; + self.carts.push(c); + } else if let Some((wood, chest)) = super::boats::any_boat_wood(kind) + && self.boat_models.has(wood, chest) + { + let mut b = super::boats::Boat::new(id, Arc::from(wood), at, yaw); + if chest { + b = b.with_chest(); + } + self.boats.push(b); + } else if kind == "minecraft:armor_stand" { + self.armor_stands.push(ArmorStand::new(id, at, yaw)); + } else if kind == "minecraft:item_frame" || kind == "minecraft:glow_item_frame" { + self.item_frames.push(ItemFrame { + id, + pos: block, + facing: data.clamp(0, 5) as u8, + glow: kind == "minecraft:glow_item_frame", + item: None, + rotation: 0, + drop_chance: 1.0, + fixed: false, + invisible: false, + check_interval: 0, + }); + } else if kind == "minecraft:painting" { + // The variant comes with the entity data; until then the + // registry's first one (VariantUtils.getAny). + let (variant, width, height) = self + .remote_painting_variant(0) + .map_or((Arc::from("minecraft:kebab"), 1, 1), |v| { + (v.id, v.width, v.height) + }); + // Direction 3D data value (north 2, south 3, west 4, east 5) to 2D. + let facing = match data { + 2 => 2, + 4 => 1, + 5 => 3, + _ => 0, + }; + self.paintings.push(Painting { + id, + pos: block, + facing, + variant, + width, + height, + check_interval: 0, + }); + } else { + return false; + } + true + } + + /// A `painting_variant` by its network id, from the server's registry. + fn remote_painting_variant(&self, id: i32) -> Option { + let r = self.remote.as_ref()?; + let name = r.registries.name("minecraft:painting_variant", id)?; + self.painting_variants.get(name).cloned() + } + + /// `handleSetEntityData` for an object: what changes how it looks. + pub(super) fn remote_object_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { + let id = id as u32; + for (index, _, value) in values { + if self.item_frames.iter().any(|f| f.id == id) { + // HangingEntity.DATA_DIRECTION, ItemFrame.DATA_ITEM and DATA_ROTATION. + let item = match (index, value) { + (9, Value::Item(stack)) => Some(self.to_stack(stack)), + _ => None, + }; + let Some(f) = self.item_frames.iter_mut().find(|f| f.id == id) else { + continue; + }; + match (index, value) { + (8, Value::Id(d)) => f.facing = (*d).clamp(0, 5) as u8, + (10, Value::Int(r)) => f.rotation = r.rem_euclid(8) as u8, + _ => {} + } + if let Some(item) = item { + f.item = item; + } + } else if self.paintings.iter().any(|p| p.id == id) { + // Painting.DATA_PAINTING_VARIANT_ID: a registry holder (id + 1). + if let (9, Value::Id(holder)) = (index, value) + && let Some(v) = self.remote_painting_variant(holder - 1) + && let Some(p) = self.paintings.iter_mut().find(|p| p.id == id) + { + p.variant = v.id; + p.width = v.width; + p.height = v.height; + } + } else if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id) { + match (index, value) { + (0, Value::Byte(flags)) => a.invisible = flags & 0x20 != 0, + (9, Value::Float(health)) => a.health = *health, + // ArmorStand.DATA_CLIENT_FLAGS. + (15, Value::Byte(flags)) => { + a.small = flags & 1 != 0; + a.show_arms = flags & 4 != 0; + a.no_base_plate = flags & 8 != 0; + a.marker = flags & 16 != 0; + } + _ => {} + } + } else if let Some(b) = self.boats.iter_mut().find(|b| b.id == id) { + // VehicleEntity's hurt, direction and damage; the paddles. + match (index, value) { + (8, Value::Int(v)) => b.hurt_time = *v, + (9, Value::Int(v)) => b.hurt_dir = *v, + (10, Value::Float(v)) => b.damage = *v, + (11, Value::Bool(v)) => b.paddling[0] = *v, + (12, Value::Bool(v)) => b.paddling[1] = *v, + _ => {} + } + } else if let Some(c) = self.carts.iter_mut().find(|c| c.id == id) { + match (index, value) { + (8, Value::Int(v)) => c.hurt_time = *v, + (9, Value::Int(v)) => c.hurt_dir = *v, + (10, Value::Float(v)) => c.damage = *v, + _ => {} + } + } + } + } + + /// `handleSetEquipment` for an armor stand (EquipmentSlot ordinals 0 to + /// 5: main hand, off hand, feet, legs, chest, head, its storage order). + pub(super) fn remote_object_equipment( + &mut self, + id: i32, + slots: &[(u8, Option)], + ) { + if !self.armor_stands.iter().any(|a| a.id == id as u32) { + return; + } + let items: Vec<_> = slots + .iter() + .map(|(slot, stack)| (usize::from(*slot), self.to_stack(stack))) + .collect(); + if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id as u32) { + for (slot, item) in items { + if let Some(s) = a.equipment.get_mut(slot) { + *s = item; + } + } + } + } + + /// A move's rotation and ground state for an object. + pub(super) fn turn_remote_object(&mut self, id: i32, yaw: Option, on_ground: bool) { + let id = id as u32; + if let Some(b) = self.boats.iter_mut().find(|b| b.id == id) { + if let Some(yaw) = yaw { + b.yaw = yaw; + } + b.on_ground = on_ground; + } else if let Some(c) = self.carts.iter_mut().find(|c| c.id == id) { + if let Some(yaw) = yaw { + c.yaw = yaw; + } + } else if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id) { + if let Some(yaw) = yaw { + a.yaw = yaw; + } + a.on_ground = on_ground; + } + } + + /// One tick of interpolation: `step` moves a position a step toward + /// the entity's last sent one, if it has a move in progress. + pub(super) fn tick_remote_objects(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) { + for c in &mut self.carts { + c.previous = c.position; + c.previous_yaw = c.yaw; + c.position = step(c.id, c.position); + } + for b in &mut self.boats { + b.previous = b.position; + b.previous_yaw = b.yaw; + b.position = step(b.id, b.position); + } + for a in &mut self.armor_stands { + a.position = step(a.id, a.position); + } + } + + /// The boats, minecarts, armor stands, item frames and paintings in the + /// level (a server's client: the ones it was sent), for `status`. + pub(super) fn objects_status(&self) -> Vec { + use serde_json::json; + let at = |p: Vec3| [p.x, p.y, p.z]; + let mut out = Vec::new(); + for b in &self.boats { + out.push(json!({"id": b.id, "kind": b.entity_type(), "position": at(b.position), "yaw": b.yaw})); + } + for c in &self.carts { + out.push( + json!({"id": c.id, "kind": c.kind.id(), "position": at(c.position), "yaw": c.yaw}), + ); + } + for a in &self.armor_stands { + let items: Vec<_> = a + .equipment + .iter() + .map(|i| i.as_ref().map(|i| &*i.item)) + .collect(); + out.push(json!({"id": a.id, "kind": "minecraft:armor_stand", "position": at(a.position), "yaw": a.yaw, + "small": a.small, "arms": a.show_arms, "no_base_plate": a.no_base_plate, "equipment": items})); + } + for f in &self.item_frames { + out.push( + json!({"id": f.id, "kind": f.kind(), "position": f.pos, "facing": f.facing, + "item": f.item.as_ref().map(|i| &*i.item), "rotation": f.rotation}), + ); + } + for p in &self.paintings { + out.push(json!({"id": p.id, "kind": "minecraft:painting", "position": p.pos, "facing": p.facing, + "variant": &*p.variant})); + } + out + } + + /// Every object a server sent (a new connection, level or dimension). + pub(super) fn clear_remote_objects(&mut self) { + self.carts.clear(); + self.boats.clear(); + self.armor_stands.clear(); + self.item_frames.clear(); + self.paintings.clear(); + } + + pub(super) fn remove_remote_object(&mut self, id: u32) { + self.carts.retain(|c| c.id != id); + self.boats.retain(|b| b.id != id); + self.armor_stands.retain(|a| a.id != id); + self.item_frames.retain(|f| f.id != id); + self.paintings.retain(|p| p.id != id); + } +} diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index b1e7ec098..01375c79a 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -42,6 +42,8 @@ address. The script then checks, from both sides, what a guest's game used to ge counts up) and the server eats one loaf and feeds the guest. - xp: experience orbs 4 blocks away fly to the guest, show on the guest's screen and give the guest their points (not the host). +- objects: boats, minecarts, armor stands, item frames and paintings the host summons reach the + guest as the host has them (kind, place, rotation, what they hold or wear, the variant). Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -74,7 +76,8 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'effects', 'portals'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'effects', 'portals', + 'objects'] class Game: @@ -1044,6 +1047,59 @@ def check_xp(host, guest, out): return {'seen': seen, 'guest': xp, 'host': host_xp} +def objects_near(game, centre): + """`status`'s objects within 8 blocks of centre, without their ids (each side numbers its own).""" + out = [] + for o in game('status')['objects']: + if all(abs(o['position'][i] - centre[i]) <= 8 for i in (0, 2)): + o = dict(o) + o.pop('id') + o['position'] = [round(v, 2) for v in o['position']] + if 'yaw' in o: # as the packets carry it, in 256ths of a turn + o['yaw'] = round(o['yaw'] * 256 / 360) % 256 + out.append(o) + return sorted(out, key=json.dumps) + + +def check_objects(host, guest, out): + x, y, z = (int(v // 1) for v in pos_of(host)) + commands = [ + f'fill {x - 6} {y} {z - 6} {x + 6} {y + 3} {z - 6} minecraft:stone', + f'summon minecraft:minecart {x - 4.5} {y} {z + 3.5}', + f'summon minecraft:chest_minecart {x - 2.5} {y} {z + 3.5}', + f'summon minecraft:furnace_minecart {x + 1.5} {y} {z + 3.5}', + f'summon minecraft:tnt_minecart {x + 4.5} {y} {z + 3.5}', + f'summon minecraft:oak_boat {x - 4.5} {y} {z + 6.5} {{Rotation:[45f,0f]}}', + f'summon minecraft:birch_chest_boat {x - 1.5} {y} {z + 6.5}', + f'summon minecraft:bamboo_raft {x + 2.5} {y} {z + 6.5}', + f'summon minecraft:armor_stand {x - 2.5} {y} {z - 2.5} {{Rotation:[90f,0f]}}', + f'summon minecraft:armor_stand {x - 4.5} {y} {z - 2.5} {{ShowArms:1b,Small:1b,NoBasePlate:1b,' + 'equipment:{head:{id:"minecraft:diamond_helmet",count:1}}}', + f'summon minecraft:item_frame {x - 3.5} {y + 1} {z - 4.97} {{block_pos:[I;{x - 4},{y + 1},{z - 5}],Facing:3b,' + 'Item:{id:"minecraft:diamond",count:1},ItemRotation:3b}', + f'summon minecraft:glow_item_frame {x - 1.5} {y + 1} {z - 4.97} {{block_pos:[I;{x - 2},{y + 1},{z - 5}],Facing:3b}}', + f'summon minecraft:painting {x + 1.5} {y + 1} {z - 4.97} {{block_pos:[I;{x + 1},{y + 1},{z - 5}],facing:0b,' + 'variant:"minecraft:kebab"}', + f'summon minecraft:painting {x + 4} {y + 1} {z - 4.97} {{block_pos:[I;{x + 4},{y + 1},{z - 5}],facing:0b,' + 'variant:"minecraft:courbet"}', + ] + try: + for command in commands: + host('command', command=command) + host.wait(60) + guest.raw('look-at', x, y, z - 5) + guest.wait(10) + guest.shot(out, 'objects') + host.shot(out, 'objects') + mine, theirs = objects_near(host, (x, y, z)), objects_near(guest, (x, y, z)) + finally: + host('command', command=f'kill @e[type=!minecraft:player,x={x - 8},y={y - 2},z={z - 8},dx=16,dy=8,dz=16]') + host('command', command=f'fill {x - 6} {y} {z - 6} {x + 6} {y + 3} {z - 6} minecraft:air') + assert len(mine) == len(commands) - 1, f'the host has {len(mine)} objects: {mine}' + assert theirs == mine, f'the guest sees {theirs}\nthe host has {mine}' + return f'{len(theirs)} objects identical' + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'sign': check_sign, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, @@ -1051,7 +1107,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls, 'effects': check_effects, 'portals': check_portals, 'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push, 'eat': check_eat, - 'xp': check_xp} + 'xp': check_xp, 'objects': check_objects} def run(ctl, out, resources, checks): From 866e772227f1440bfaa72dff5ff9834d0bb1db2e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:39:00 +0200 Subject: [PATCH 34/67] Server: track primed TNT, falling blocks, projectiles, tridents, fireworks and crystals These entities had no id in Horizon, so a server could not send them to its clients. Each now has one, given from the entity counter before it is first tracked, and goes to guests as vanilla's ServerEntity sends it: - primed TNT with its fuse (sent each tick it changes) and falling blocks with their block state and start position; both sync their exact position while they touch a block (requiresPrecisePosition); - arrows and tridents send their position and rotation every update (AbstractArrow), with their flags, pierce level, in-ground state, loyalty and foil; their owner is the spawn data; - fireballs, wither skulls and wind charges bundle their velocity with ClientboundProjectilePowerPacket (new in horizon-protocol); - snowballs, eggs, pearls, llama spit, shulker bullets and splash potions; - firework rockets with their item, the player they boost and whether they were shot at an angle; end crystals with their beam and base. The entity harness's scene gains primed TNT, a falling block, an arrow, a snowball and a trident that land, a fireball, a firework and a crystal. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 3 + crates/horizon-client/src/game/dragon.rs | 6 + crates/horizon-client/src/game/fireworks.rs | 13 +- crates/horizon-client/src/game/lan.rs | 16 +- .../horizon-client/src/game/lan_entities.rs | 192 +++++++++++++++++- crates/horizon-client/src/game/persist.rs | 1 + crates/horizon-client/src/game/tnt.rs | 4 + crates/horizon-client/src/game/trident.rs | 19 +- crates/horizon-client/src/lan/entity.rs | 53 ++++- crates/horizon-client/src/lan/hub.rs | 1 + crates/horizon-client/src/projectiles.rs | 3 + crates/horizon-client/tests/lan_bot.rs | 1 + crates/horizon-protocol/src/packets/play.rs | 6 + .../horizonlab/parity/EntityTranscript.java | 6 +- tools/test_server_entities_parity.py | 16 +- 15 files changed, 313 insertions(+), 27 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 4def60790..64120b353 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -649,6 +649,8 @@ impl LevelAccess for GameLevel<'_> { /// FallingBlockEntity carrying a block state. #[derive(Clone, Debug)] struct FallingBlock { + /// The entity id a server tracks it by (0 until it is tracked). + id: u32, state: BlockStateId, /// `DATA_START_POS`: spawn position used to select weighted block models. start_pos: BlockPos, @@ -2054,6 +2056,7 @@ impl Game<'_> { f64::from(pos.z) + 0.5, ); self.falling.push(FallingBlock { + id: 0, state, start_pos: pos, position: p, diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index ff91df583..e76eb16fb 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -74,6 +74,8 @@ impl Respawn { } #[derive(Clone, Debug)] pub(super) struct Crystal { + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, pub position: Vec3, pub show_bottom: bool, /// EndCrystal.time (the bob and spin), starting random. @@ -553,6 +555,7 @@ impl Game<'_> { } let time = self.world.random.next_int_bound(100_000).unwrap_or(0); self.crystals.push(Crystal { + id: 0, position: at, show_bottom: true, time, @@ -2301,6 +2304,7 @@ impl Crystal { pub(super) fn from_nbt(tag: &Tag, time: i32) -> Option { let (position, _, _) = horizon_client::entity_nbt::boat_from_nbt(tag)?; Some(Self { + id: 0, position, show_bottom: !matches!(tag.get("ShowBottom"), Some(Tag::Byte(0))), time, @@ -2403,6 +2407,7 @@ impl Game<'_> { } let time = self.world.random.next_int_bound(100_000).unwrap_or(0); self.crystals.push(Crystal { + id: 0, position: Vec3::new(x + 0.5, y, z + 0.5), show_bottom: false, time, @@ -2665,6 +2670,7 @@ impl Game<'_> { .retain(|c| c.position.distance_squared(at) > 0.01); let time = self.world.random.next_int_bound(100_000).unwrap_or(0); self.crystals.push(Crystal { + id: 0, position: at, show_bottom: true, time, diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 0e074025f..a7e071c52 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -18,19 +18,21 @@ use horizon_core::{ }; pub(super) struct Rocket { - position: Vec3, + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, + pub(super) position: Vec3, previous: Vec3, - velocity: Vec3, + pub(super) velocity: Vec3, life: i32, lifetime: i32, explosions: Vec, /// Boosting the player (attachedToEntity). - attached: bool, + pub(super) attached: bool, /// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on /// the first block it hits. - angled: bool, + pub(super) angled: bool, /// `DATA_ID_FIREWORKS_ITEM`: the stack it was launched from. - item: ItemStack, + pub(super) item: ItemStack, } impl Rocket { /// Position and motion (the dispenser parity fixture). @@ -56,6 +58,7 @@ impl Game<'_> { + r.next_int_bound(6).unwrap_or(0) + r.next_int_bound(7).unwrap_or(0); Rocket { + id: 0, position: at, previous: at, velocity, diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 209c34286..aba46fe03 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -718,7 +718,7 @@ impl Game<'_> { } /// The entities guests track: mobs, items and the rest (game/lan_entities.rs). - fn lan_entities(&self, hub: &mut Hub) -> Vec { + fn lan_entities(&self, host_entity: Option, hub: &mut Hub) -> Vec { let mut out = Vec::with_capacity(self.mobs.len() + self.dropped.len() + self.orbs.len()); for m in self.mobs.iter().filter(|m| !m.removed) { let kind = m.kind.id.as_str(); @@ -738,6 +738,7 @@ impl Game<'_> { data: 0, meta, equipment, + precise: false, }); } for d in &self.dropped { @@ -755,6 +756,7 @@ impl Game<'_> { data: 0, meta: vec![(8, serializer::ITEM_STACK, Value::Item(stack))], equipment: Vec::new(), + precise: false, }); } for o in &self.orbs { @@ -772,9 +774,10 @@ impl Game<'_> { // ExperienceOrb.DATA_VALUE. meta: vec![(8, serializer::INT, Value::Int(o.value as i32))], equipment: Vec::new(), + precise: false, }); } - self.lan_object_states(hub, &mut out); + self.lan_object_states(host_entity, hub, &mut out); out } @@ -790,8 +793,10 @@ impl Game<'_> { if state.is_some() || host.hub.config.dedicated { let has_guests = !host.hub.guests.is_empty(); let entities = if has_guests { - self.lan_riding(state.as_ref().map(|s| s.entity_id), &mut host.hub.riding); - self.lan_entities(&mut host.hub) + let host_entity = state.as_ref().map(|s| s.entity_id); + self.lan_assign_ids(); + self.lan_riding(host_entity, &mut host.hub.riding); + self.lan_entities(host_entity, &mut host.hub) } else { Vec::new() }; @@ -974,7 +979,8 @@ impl Game<'_> { log::error!("could not open {}: {e}", level.id()); return; } - theirs = self.lan_entities(&mut host.hub); + // A dedicated server has no host player. + theirs = self.lan_entities(None, &mut host.hub); &theirs[..] } else { entities diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs index 9064447aa..3ea172482 100644 --- a/crates/horizon-client/src/game/lan_entities.rs +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -37,6 +37,7 @@ fn state(id: u32, kind: &str, position: [f64; 3], yaw: f32, on_ground: bool) -> data: 0, meta: Vec::new(), equipment: Vec::new(), + precise: false, } } @@ -101,7 +102,12 @@ impl Game<'_> { /// Boats, minecarts, armor stands, item frames, paintings and the /// entities kept unsimulated, as guests track them. - pub(super) fn lan_object_states(&self, hub: &mut Hub, out: &mut Vec) { + pub(super) fn lan_object_states( + &self, + host_entity: Option, + hub: &mut Hub, + out: &mut Vec, + ) { for b in &self.boats { let p = b.position; let mut e = state(b.id, &b.entity_type(), [p.x, p.y, p.z], b.yaw, b.on_ground); @@ -225,6 +231,7 @@ impl Game<'_> { } out.push(e); } + self.lan_projectile_states(host_entity, hub, out); for o in &self.others { let p = o.position; let mut e = state(o.id, &o.kind, [p.x, p.y, p.z], o.yaw, false); @@ -235,6 +242,189 @@ impl Game<'_> { } } +impl Game<'_> { + /// Gives the entities Horizon keeps without an id one (`Entity.id`, from + /// the same counter as the rest) before they are first tracked. + pub(super) fn lan_assign_ids(&mut self) { + let next = &mut self.next_entity; + let mut give = |id: &mut u32| { + if *id == 0 { + *next += 1; + *id = *next; + } + }; + self.tnts.iter_mut().for_each(|t| give(&mut t.id)); + self.falling.iter_mut().for_each(|f| give(&mut f.id)); + self.creatures + .projectiles + .iter_mut() + .for_each(|p| give(&mut p.id)); + self.tridents.iter_mut().for_each(|t| give(&mut t.id)); + self.rockets.iter_mut().for_each(|r| give(&mut r.id)); + self.crystals.iter_mut().for_each(|c| give(&mut c.id)); + } + + /// Primed TNT, falling blocks, projectiles, tridents, firework rockets and + /// end crystals, as guests track them. + fn lan_projectile_states( + &self, + host_entity: Option, + hub: &mut Hub, + out: &mut Vec, + ) { + let owner = |who: Option| match who { + Some(horizon_client::mobs::Who::Mob(id)) => id as i32, + Some(horizon_client::mobs::Who::Player) => host_entity.unwrap_or(0), + None => 0, + }; + let at = |p: horizon_core::geometry::Vec3| [p.x, p.y, p.z]; + for t in self.tnts.iter().filter(|t| t.id != 0) { + let mut e = state(t.id, "minecraft:tnt", at(t.position), 0.0, t.on_ground); + e.velocity = at(t.velocity); + // PrimedTnt.DATA_FUSE_ID, 80 by default; requiresPrecisePosition + // while it touches a block. + if t.fuse != 80 { + e.meta.push((8, serializer::INT, Value::Int(t.fuse))); + } + e.precise = t.on_ground; + out.push(e); + } + for f in self.falling.iter().filter(|f| f.id != 0) { + let mut e = state(f.id, "minecraft:falling_block", at(f.position), 0.0, false); + e.velocity = at(f.velocity); + // getAddEntityPacket: the block state; DATA_START_POS. + e.data = f.state.0 as i32; + if f.no_gravity { + e.meta.push((5, serializer::BOOLEAN, Value::Bool(true))); + } + let start = [f.start_pos.x, f.start_pos.y, f.start_pos.z]; + if start != [0; 3] { + e.meta.push((8, serializer::BLOCK_POS, Value::Pos(start))); + } + out.push(e); + } + for p in self.creatures.projectiles.iter().filter(|p| p.id != 0) { + use horizon_client::projectiles::ProjectileKind as K; + let kind = match &p.kind { + K::Arrow { .. } => "minecraft:arrow", + K::Snowball => "minecraft:snowball", + K::Egg => "minecraft:egg", + K::EnderPearl => "minecraft:ender_pearl", + K::SmallFireball => "minecraft:small_fireball", + K::LargeFireball { .. } => "minecraft:fireball", + K::LlamaSpit => "minecraft:llama_spit", + K::WindCharge => "minecraft:wind_charge", + K::BreezeWindCharge => "minecraft:breeze_wind_charge", + K::ShulkerBullet { .. } => "minecraft:shulker_bullet", + K::WitherSkull { .. } => "minecraft:wither_skull", + K::Potion { .. } => "minecraft:splash_potion", + }; + let mut e = state(p.id, kind, at(p.position), p.yaw, false); + e.pitch = p.pitch; + e.velocity = at(p.velocity); + e.data = owner(p.owner); + match &p.kind { + K::Arrow { critical, .. } => { + let a = p.arrow.as_deref(); + e.meta.extend(arrow_meta( + *critical, + a.is_some_and(|a| a.no_physics), + a.map_or(0, |a| a.pierce), + a.is_some_and(|a| a.in_ground) || p.stuck, + )); + } + // WitherSkull.DATA_DANGEROUS. + K::WitherSkull { dangerous: true } => { + e.meta.push((8, serializer::BOOLEAN, Value::Bool(true))); + } + _ => {} + } + out.push(e); + } + for t in self.tridents.iter().filter(|t| t.id != 0) { + let mut e = state(t.id, "minecraft:trident", at(t.position), t.yaw, false); + e.pitch = t.pitch; + e.velocity = at(t.velocity); + e.meta = arrow_meta(false, t.no_physics, 0, t.in_ground); + // ThrownTrident.ID_LOYALTY and ID_FOIL. + if t.loyalty != 0 { + e.meta + .push((11, serializer::BYTE, Value::Byte(t.loyalty as i8))); + } + if t.stack.has_any_enchantments() { + e.meta.push((12, serializer::BOOLEAN, Value::Bool(true))); + } + out.push(e); + } + for r in self.rockets.iter().filter(|r| r.id != 0) { + let mut e = state( + r.id, + "minecraft:firework_rocket", + at(r.position), + 0.0, + false, + ); + e.velocity = at(r.velocity); + // DATA_ID_FIREWORKS_ITEM once set from a stack, DATA_ATTACHED_TO_TARGET, + // DATA_SHOT_AT_ANGLE. + let plain = &*r.item.item == "minecraft:firework_rocket" + && r.item.count == 1 + && r.item.extra.is_none(); + if !plain { + e.meta.push(( + 8, + serializer::ITEM_STACK, + Value::Item(hub.stack(&Some(r.item.clone()))), + )); + } + if r.attached + && let Some(host) = host_entity + { + e.meta + .push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(host + 1))); + } + if r.angled { + e.meta.push((10, serializer::BOOLEAN, Value::Bool(true))); + } + out.push(e); + } + for c in self.crystals.iter().filter(|c| c.id != 0) { + let mut e = state(c.id, "minecraft:end_crystal", at(c.position), 0.0, false); + // EndCrystal.DATA_BEAM_TARGET and DATA_SHOW_BOTTOM (true by default). + if c.beam.is_some() { + e.meta + .push((8, serializer::OPTIONAL_BLOCK_POS, Value::OptPos(c.beam))); + } + if !c.show_bottom { + e.meta.push((9, serializer::BOOLEAN, Value::Bool(false))); + } + out.push(e); + } + } +} + +/// `AbstractArrow`'s ID_FLAGS (critical 1, no physics 2), PIERCE_LEVEL and +/// IN_GROUND, when they differ from the defaults. +fn arrow_meta( + critical: bool, + no_physics: bool, + pierce: u8, + in_ground: bool, +) -> Vec<(u8, i32, Value)> { + let mut meta = Vec::new(); + let flags = i8::from(critical) | i8::from(no_physics) << 1; + if flags != 0 { + meta.push((8, serializer::BYTE, Value::Byte(flags))); + } + if pierce != 0 { + meta.push((9, serializer::BYTE, Value::Byte(pierce as i8))); + } + if in_ground { + meta.push((10, serializer::BOOLEAN, Value::Bool(true))); + } + meta +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 27fe36694..50016522e 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -202,6 +202,7 @@ impl Game<'_> { { let (no_gravity, drop_item) = entity_nbt::falling_block_flags(&tag); self.falling.push(FallingBlock { + id: 0, state, // DATA_START_POS is not persisted by vanilla. start_pos: horizon_core::position::BlockPos::default(), diff --git a/crates/horizon-client/src/game/tnt.rs b/crates/horizon-client/src/game/tnt.rs index 78f107599..38a5c381e 100644 --- a/crates/horizon-client/src/game/tnt.rs +++ b/crates/horizon-client/src/game/tnt.rs @@ -12,6 +12,8 @@ use horizon_world::level::flags as level_flags; #[derive(Clone, Debug)] pub(super) struct PrimedTnt { + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, pub position: Vec3, pub previous: Vec3, pub velocity: Vec3, @@ -140,6 +142,7 @@ impl Game<'_> { f64::from(pos.z) + 0.5, ); self.tnts.push(PrimedTnt { + id: 0, position, previous: position, velocity: Vec3::new(-rot.sin() * 0.02, f64::from(0.2f32), -rot.cos() * 0.02), @@ -484,6 +487,7 @@ mod tests { world.set(21, 4, 20, flowing).unwrap(); let at = Vec3::new(20.5, 4.0, 20.5); let mut tnt = PrimedTnt { + id: 0, position: at, previous: at, velocity: Vec3::ZERO, diff --git a/crates/horizon-client/src/game/trident.rs b/crates/horizon-client/src/game/trident.rs index 8c372fa46..2077cfada 100644 --- a/crates/horizon-client/src/game/trident.rs +++ b/crates/horizon-client/src/game/trident.rs @@ -15,17 +15,19 @@ use horizon_core::{ }; pub(super) struct ThrownTrident { - stack: ItemStack, - position: Vec3, + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, + pub(super) stack: ItemStack, + pub(super) position: Vec3, previous: Vec3, - velocity: Vec3, - yaw: f32, - pitch: f32, - in_ground: bool, + pub(super) velocity: Vec3, + pub(super) yaw: f32, + pub(super) pitch: f32, + pub(super) in_ground: bool, in_ground_time: u32, dealt_damage: bool, - loyalty: u32, - no_physics: bool, + pub(super) loyalty: u32, + pub(super) no_physics: bool, returning: u32, creative_only: bool, } @@ -163,6 +165,7 @@ impl Game<'_> { on_ground, ); self.tridents.push(ThrownTrident { + id: 0, loyalty: thrown.enchantment_level("minecraft:loyalty"), stack: thrown, position: start, diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index 178f9bd44..a2775b4dc 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -35,6 +35,30 @@ pub struct EntityState { /// EquipmentSlot ordinals (0 main hand, 1 off hand, 2 feet, 3 legs, /// 4 chest, 5 head, 6 body, 7 saddle) and their stacks. pub equipment: Vec<(u8, Option)>, + /// `Entity.getRequiresPrecisePosition` (primed TNT and falling blocks + /// touching a block): moves are sent as full position syncs. + pub precise: bool, +} + +/// `AbstractArrow`: every update sends its position and rotation. +fn is_arrow(kind: &str) -> bool { + matches!( + kind, + "minecraft:arrow" | "minecraft:spectral_arrow" | "minecraft:trident" + ) +} + +/// `AbstractHurtingProjectile.accelerationPower` (0.1, none for wind +/// charges), sent with each velocity update. +fn acceleration_power(kind: &str) -> Option { + match kind { + "minecraft:fireball" + | "minecraft:small_fireball" + | "minecraft:dragon_fireball" + | "minecraft:wither_skull" => Some(0.1), + "minecraft:wind_charge" | "minecraft:breeze_wind_charge" => Some(0.0), + _ => None, + } } /// What a client was last told about one entity (ServerEntity's fields). @@ -207,7 +231,9 @@ impl Tracker { if due || dirty { let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw)); let turned = |a: i8, b: i8| (i32::from(a) - i32::from(b)).abs() >= 1; - let rotate = turned(rotation.1, s.rotation.1) || turned(rotation.0, s.rotation.0); + let arrow = is_arrow(&e.kind); + let rotate = + arrow || turned(rotation.1, s.rotation.1) || turned(rotation.0, s.rotation.0); if riding.vehicle.contains_key(&e.id) { // A passenger: its rotation alone (its vehicle places it). if rotate { @@ -231,7 +257,7 @@ impl Tracker { } s.teleport_delay += 1; let d: f64 = (0..3).map(|k| (e.position[k] - s.base[k]).powi(2)).sum(); - let send_position = d >= 7.629_394_5e-6 || s.tick.is_multiple_of(60); + let send_position = arrow || d >= 7.629_394_5e-6 || s.tick.is_multiple_of(60); let sync = EntityPositionSync { id: e.id, path: PositionPath::Linear(e.position), @@ -240,7 +266,10 @@ impl Tracker { on_ground: e.on_ground, }; // ServerEntity.createMovePacket. - let packet = if s.teleport_delay > 400 || s.was_riding || s.was_on_ground != e.on_ground + let packet = if e.precise + || s.teleport_delay > 400 + || s.was_riding + || s.was_on_ground != e.on_ground { s.was_on_ground = e.on_ground; s.teleport_delay = 0; @@ -282,10 +311,23 @@ impl Tracker { let still = e.velocity.iter().all(|v| *v == 0.0); if diff > 1.0e-7 || diff > 0.0 && still { s.velocity = e.velocity; - out.push(play::Clientbound::SetEntityMotion(SetEntityMotion { + let motion = play::Clientbound::SetEntityMotion(SetEntityMotion { id: e.id, velocity: e.velocity, - })); + }); + match acceleration_power(&e.kind) { + // A bundle: the motion and the projectile's power. + Some(power) => out.extend([ + play::Clientbound::BundleDelimiter(horizon_protocol::packets::Empty), + motion, + play::Clientbound::ProjectilePower(play::ProjectilePower { + id: e.id, + power, + }), + play::Clientbound::BundleDelimiter(horizon_protocol::packets::Empty), + ]), + None => out.push(motion), + } } } if let Some(packet) = packet { @@ -386,6 +428,7 @@ mod tests { data: 0, meta: Vec::new(), equipment: Vec::new(), + precise: false, } } diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index d710c239e..caacfd2f6 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1378,6 +1378,7 @@ impl Hub { data: 0, meta, equipment, + precise: false, } } diff --git a/crates/horizon-client/src/projectiles.rs b/crates/horizon-client/src/projectiles.rs index bfd0e8a97..c2d4b4e3a 100644 --- a/crates/horizon-client/src/projectiles.rs +++ b/crates/horizon-client/src/projectiles.rs @@ -63,6 +63,8 @@ pub enum ProjectileKind { } #[derive(Clone, Debug, PartialEq)] pub struct Projectile { + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, pub kind: ProjectileKind, pub owner: Option, pub position: Vec3, @@ -128,6 +130,7 @@ impl Projectile { pub fn new(kind: ProjectileKind, position: Vec3, owner: Option) -> Self { let kind_is_arrow = matches!(kind, ProjectileKind::Arrow { .. }); Self { + id: 0, kind, owner, position, diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 4d6cd4e86..7a8209939 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -422,6 +422,7 @@ fn guests_see_each_other_and_the_world_entities() { data: 0, meta: Vec::new(), equipment: Vec::new(), + precise: false, }; let first = thread::spawn(move || { let mut bot = connect(port, "Alex", false)?; diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 40b70624a..20a25d078 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -567,6 +567,10 @@ packet!(PlayerPosition { pitch: f32 => Float, relatives: i32 => Int, }); +packet!( + /// ClientboundProjectilePowerPacket: an AbstractHurtingProjectile's accelerationPower. + ProjectilePower { id: i32 => VarInt, power: f64 => Double } +); packet!(PlayerRotation { yaw: f32 => Float, relative_yaw: bool => Bool, pitch: f32 => Float, relative_pitch: bool => Bool }); packet!(RemoveEntities { ids: Vec => List }); packet!(Respawn { spawn: SpawnInfo => SpawnInfo, keep: i8 => Byte }); @@ -1431,6 +1435,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { SetHeldSlot(SetHeldSlot) = SET_HELD_SLOT, SetObjective(SetObjective) = SET_OBJECTIVE, SetPassengers(SetPassengers) = SET_PASSENGERS, + ProjectilePower(ProjectilePower) = PROJECTILE_POWER, SetPlayerInventory(SetPlayerInventory) = SET_PLAYER_INVENTORY, SetPlayerTeam(SetPlayerTeam) = SET_PLAYER_TEAM, SetScore(SetScore) = SET_SCORE, @@ -2061,6 +2066,7 @@ mod tests { vehicle: 2, passengers: vec![3, 300], }), + Clientbound::ProjectilePower(ProjectilePower { id: 4, power: 0.1 }), Clientbound::SetEquipment(SetEquipment { id: 2, slots: vec![(0, None), (5, Some(Stack::new(1, 1)))], diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java b/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java index 4d7cc9f2c..ffac86a59 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java @@ -10,6 +10,7 @@ import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket; import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket; +import net.minecraft.network.protocol.game.ClientboundProjectilePowerPacket; import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket; import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket; import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; @@ -47,6 +48,7 @@ final class EntityTranscript { float headRot; boolean onGround; String motion; + String power = ""; boolean removed; Tracked(ClientboundAddEntityPacket p) { @@ -91,6 +93,7 @@ final class EntityTranscript { case ClientboundEntityPositionSyncPacket p -> byId.get(p.id()); case ClientboundSetEntityMotionPacket p -> byId.get(p.id()); case ClientboundTeleportEntityPacket p -> byId.get(p.id()); + case ClientboundProjectilePowerPacket p -> byId.get(p.getId()); case ClientboundMoveEntityPacket p -> byId.get(VanillaProtocolClient.moveEntityId(p)); case ClientboundRotateHeadPacket p -> byId.get(VanillaProtocolClient.rotateHeadId(p)); default -> null; @@ -131,6 +134,7 @@ final class EntityTranscript { t.onGround = p.onGround(); } case ClientboundRotateHeadPacket p -> t.headRot = p.getYHeadRot(); + case ClientboundProjectilePowerPacket p -> t.power = " power " + p.getAccelerationPower(); default -> { } } @@ -152,7 +156,7 @@ final class EntityTranscript { for (int id : t.passengers) riders.add(names.getOrDefault(id, "player or unknown")); out.add("entity " + t.name + " | add " + t.add + " | pairing " + t.pairing + " | data " + t.data + " | equipment " + t.equipment + " | attributes " + t.attributes + " | passengers " + riders + " | now " + t.codec.getBase() + " rot " + t.xRot + "," - + t.yRot + " head " + t.headRot + " ground " + t.onGround + " motion " + t.motion + (t.removed ? " | removed" : "")); + + t.yRot + " head " + t.headRot + " ground " + t.onGround + " motion " + t.motion + t.power + (t.removed ? " | removed" : "")); } out.sort(null); return out; diff --git a/tools/test_server_entities_parity.py b/tools/test_server_entities_parity.py index f97e3f15a..35d3c99f6 100644 --- a/tools/test_server_entities_parity.py +++ b/tools/test_server_entities_parity.py @@ -6,7 +6,9 @@ world vanilla generated first, with the same server.properties (offline, view di client (VanillaProtocolClient in its entity mode) joins, and once its chunks are in it asks for the scene: the console freezes the ticks, builds a wall and a rail and summons, around the spawn, every kind of minecart (one set rolling along the rail), boats and a raft (one falling onto the ground), armor stands (posed, small, with -arms, wearing a helmet, one falling), a chicken jockey, item frames (one holding a turned diamond) and paintings. The client records +arms, wearing a helmet, one falling), a chicken jockey, item frames (one holding a turned diamond), paintings, +primed TNT, a falling block, an arrow, a snowball and a trident that land, a fireball, a firework rocket and an +end crystal. The client records for 20 seconds, the ticks running again after 3, what ClientPacketListener would make of the packets (EntityTranscript): each entity's spawn packet (position, rotations, data, motion), the entity data it was paired with and all it was sent since, its equipment and passengers, and where its position syncs leave it. Both transcripts must match exactly, @@ -66,6 +68,15 @@ SCENE = [ 'summon minecraft:glow_item_frame -1.5 -59 -4.97 {block_pos:[I;-2,-59,-5],Facing:3b}', 'summon minecraft:painting 1.5 -59 -4.97 {block_pos:[I;1,-59,-5],facing:0b,variant:"minecraft:kebab"}', 'summon minecraft:painting 4 -59 -4.97 {block_pos:[I;4,-59,-5],facing:0b,variant:"minecraft:courbet"}', + # Primed TNT that won't go off, a falling block, projectiles that land, a fireball, a firework, a crystal. + 'summon minecraft:tnt 6.5 -60 6.5 {fuse:2000s}', + 'summon minecraft:falling_block 2.5 -50 -0.5 {BlockState:{Name:"minecraft:sand"}}', + 'summon minecraft:arrow 0.5 -55 -1.5 {Motion:[0.3d,0d,0d]}', + 'summon minecraft:snowball -0.5 -55 1.5', + 'summon minecraft:trident -2.5 -55 -0.5', + 'summon minecraft:fireball 3.5 -57 -3.5', + 'summon minecraft:firework_rocket 5.5 -60 -0.5', + 'summon minecraft:end_crystal -6.5 -60 6.5 {ShowBottom:0b}', ] @@ -161,7 +172,8 @@ def main(): schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], entities=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), - scope='Minecarts (one rolling), boats (one falling), armor stands, a chicken jockey, item frames and paintings tracked to a ' + scope='Minecarts (one rolling), boats (one falling), armor stands, a chicken jockey, item frames, paintings, primed ' + 'TNT, a falling block, projectiles, a fireball, a firework and an end crystal tracked to a ' 'vanilla-codec client by a dedicated server: spawn packets, entity data, equipment, passengers and the ' 'positions the syncs leave them at.')) print(f'server entities: {len(vanilla)} entities identical') From 946bf848189a54c2577a061f712fbf544a4e4461 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 23:46:37 +0200 Subject: [PATCH 35/67] Load summoned and saved primed TNT as live TNT; step the entity harness `/summon minecraft:tnt` and primed TNT in a saved chunk were kept as an unsimulated entity. They now become primed TNT with the fuse (80 when absent) and motion they were saved with (PrimedTnt.readAdditionalSaveData). The entity harness runs its scene for a fixed 200 ticks (`tick step`), so what still changes when the client stops recording (a fuse) ends the same on both servers. Its scene has two primed TNT (one falling), a floating falling block, an arrow that lands and an end crystal. Unit tests cover arrows' position and rotation, fireballs' bundled power and precise position syncs. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/persist.rs | 17 +++++++++ crates/horizon-client/src/lan/entity.rs | 46 +++++++++++++++++++++++ tools/test_server_entities_parity.py | 21 +++++------ 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 50016522e..53651dc6c 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -221,6 +221,23 @@ impl Game<'_> { }); } } + // PrimedTnt.readAdditionalSaveData: its fuse (80 when absent) and motion. + "minecraft:tnt" => { + if let Some(position) = entity_nbt::position(&tag) { + let fuse = match tag.get("fuse") { + Some(Tag::Short(f)) => i32::from(*f), + _ => 80, + }; + self.tnts.push(super::tnt::PrimedTnt { + id: 0, + position, + previous: position, + velocity: entity_nbt::motion(&tag), + fuse, + on_ground: matches!(tag.get("OnGround"), Some(Tag::Byte(1))), + }); + } + } k if super::boats::any_boat_wood(k) .is_some_and(|(wood, chest)| self.boat_models.has(wood, chest)) => { diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index a2775b4dc..36e153c02 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -456,6 +456,52 @@ mod tests { assert!(matches!(jump[0], play::Clientbound::EntityPositionSync(_))); } + #[test] + fn fireballs_bundle_their_motion_with_their_power() { + let mut t = Tracker::default(); + let mut e = state(0.0); + e.kind = "minecraft:fireball".into(); + e.on_ground = false; + t.spawn(&e, 100, &Riding::default()); + e.velocity = [0.0, 0.0, 0.5]; + let out = t.update(&e, &Riding::default()); + assert!(matches!( + out[..], + [ + play::Clientbound::BundleDelimiter(_), + play::Clientbound::SetEntityMotion(_), + play::Clientbound::ProjectilePower(play::ProjectilePower { power, .. }), + play::Clientbound::BundleDelimiter(_), + play::Clientbound::MoveEntityPos(_), + ] if power == 0.1 + )); + } + + #[test] + fn arrows_send_position_and_rotation_every_update() { + let mut t = Tracker::default(); + let mut e = state(0.0); + e.kind = "minecraft:arrow".into(); + t.spawn(&e, 100, &Riding::default()); + assert!(matches!( + t.update(&e, &Riding::default())[..], + [play::Clientbound::MoveEntityPosRot(_)] + )); + } + + #[test] + fn precise_entities_sync_their_position() { + let mut t = Tracker::default(); + let mut e = state(0.0); + e.kind = "minecraft:tnt".into(); + e.precise = true; + t.spawn(&e, 100, &Riding::default()); + assert!(matches!( + t.update(&e, &Riding::default())[..], + [play::Clientbound::EntityPositionSync(_)] + )); + } + #[test] fn landing_sends_a_position_sync() { let mut t = Tracker::default(); diff --git a/tools/test_server_entities_parity.py b/tools/test_server_entities_parity.py index 35d3c99f6..e512239e8 100644 --- a/tools/test_server_entities_parity.py +++ b/tools/test_server_entities_parity.py @@ -7,9 +7,8 @@ client (VanillaProtocolClient in its entity mode) joins, and once its chunks are console freezes the ticks, builds a wall and a rail and summons, around the spawn, every kind of minecart (one set rolling along the rail), boats and a raft (one falling onto the ground), armor stands (posed, small, with arms, wearing a helmet, one falling), a chicken jockey, item frames (one holding a turned diamond), paintings, -primed TNT, a falling block, an arrow, a snowball and a trident that land, a fireball, a firework rocket and an -end crystal. The client records -for 20 seconds, the ticks running again after 3, what ClientPacketListener would make of the packets (EntityTranscript): each entity's spawn +primed TNT (one falling), a floating falling block, an arrow that lands and an end crystal. The client records +for 20 seconds, the ticks running for 200 more after 3, what ClientPacketListener would make of the packets (EntityTranscript): each entity's spawn packet (position, rotations, data, motion), the entity data it was paired with and all it was sent since, its equipment and passengers, and where its position syncs leave it. Both transcripts must match exactly, entity by entity (named by type and the block it was added in). Attributes are recorded but not compared yet: @@ -35,6 +34,8 @@ except ImportError: from test_locator_bar_parity import generate_world, server_dir NAME = 'server-entities-parity' +# Ticks the scene runs for once it is summoned: 10 seconds, within the client's 20. +STEPS = 200 PLAYER = 'Tester' # Around the spawn at 0 -60 0 on the flat world's grass: a wall to the north for the frames and paintings, @@ -68,14 +69,11 @@ SCENE = [ 'summon minecraft:glow_item_frame -1.5 -59 -4.97 {block_pos:[I;-2,-59,-5],Facing:3b}', 'summon minecraft:painting 1.5 -59 -4.97 {block_pos:[I;1,-59,-5],facing:0b,variant:"minecraft:kebab"}', 'summon minecraft:painting 4 -59 -4.97 {block_pos:[I;4,-59,-5],facing:0b,variant:"minecraft:courbet"}', - # Primed TNT that won't go off, a falling block, projectiles that land, a fireball, a firework, a crystal. + # Primed TNT that won't go off (one falling), a floating falling block, an arrow that lands, an end crystal. 'summon minecraft:tnt 6.5 -60 6.5 {fuse:2000s}', - 'summon minecraft:falling_block 2.5 -50 -0.5 {BlockState:{Name:"minecraft:sand"}}', + 'summon minecraft:tnt 6.5 -56 4.5 {fuse:2000s}', + 'summon minecraft:falling_block 2.5 -58 -0.5 {BlockState:{Name:"minecraft:sand"},NoGravity:1b}', 'summon minecraft:arrow 0.5 -55 -1.5 {Motion:[0.3d,0d,0d]}', - 'summon minecraft:snowball -0.5 -55 1.5', - 'summon minecraft:trident -2.5 -55 -0.5', - 'summon minecraft:fireball 3.5 -57 -3.5', - 'summon minecraft:firework_rocket 5.5 -60 -0.5', 'summon minecraft:end_crystal -6.5 -60 6.5 {ShowBottom:0b}', ] @@ -110,7 +108,8 @@ def run_side(label, server_command, client_command, build, world): for command in SCENE: server.console(command) time.sleep(3) - server.console('tick unfreeze') + # A fixed number of ticks, so that what still changes (a TNT fuse) ends the same. + server.console(f'tick step {STEPS}') client.wait(timeout=120) finally: if client.poll() is None: @@ -173,7 +172,7 @@ def main(): java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], entities=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), scope='Minecarts (one rolling), boats (one falling), armor stands, a chicken jockey, item frames, paintings, primed ' - 'TNT, a falling block, projectiles, a fireball, a firework and an end crystal tracked to a ' + 'TNT, a falling block, an arrow and an end crystal tracked to a ' 'vanilla-codec client by a dedicated server: spawn packets, entity data, equipment, passengers and the ' 'positions the syncs leave them at.')) print(f'server entities: {len(vanilla)} entities identical') From 5e1abf2b4e07c69311c577e862c8d86578d9dcc6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 23:55:42 +0200 Subject: [PATCH 36/67] Clients of a server show its primed TNT, falling blocks, projectiles and crystals A Horizon client of a server places what PR #766 sends: primed TNT with its fuse, falling blocks with their block state and start position, arrows (critical, in the ground), snowballs, eggs, pearls, fireballs, wither skulls, wind charges and llama spit, tridents, firework rockets and end crystals with their beam and base. They interpolate like mobs, turn with the move packets, and are cleared with the connection, a respawn or a dimension change. `status` lists them (`projectiles`), and lan_e2e has a `projectiles` check: the guest sees the host's primed TNT, end crystal and landed arrow where the host has them, and a falling block lands on both sides. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/control.rs | 2 + crates/horizon-client/src/game/fireworks.rs | 23 ++ crates/horizon-client/src/game/remote.rs | 26 +- .../src/game/remote_projectiles.rs | 229 ++++++++++++++++++ crates/horizon-client/src/game/trident.rs | 26 ++ tools/lan_e2e.py | 39 ++- 7 files changed, 338 insertions(+), 8 deletions(-) create mode 100644 crates/horizon-client/src/game/remote_projectiles.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 64120b353..bf882b86a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -109,6 +109,7 @@ mod raids; mod recipe_book; mod remote; mod remote_objects; +mod remote_projectiles; mod render_control; mod render_tick; mod respawn_anchor; diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 58733bf45..6827d49c9 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -760,6 +760,8 @@ impl Game<'_> { status["physical_keys"] = json!(physical_keys); // Boats, minecarts, armor stands, item frames and paintings. status["objects"] = json!(self.objects_status()); + // Primed TNT, falling blocks, projectiles, tridents and end crystals. + status["projectiles"] = json!(self.projectiles_status()); status } } diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index a7e071c52..7b5e08f42 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -35,6 +35,29 @@ pub(super) struct Rocket { pub(super) item: ItemStack, } impl Rocket { + /// A rocket a server sent (`handleAddEntity`): placed and moved by it. + pub(super) fn remote(id: u32, position: Vec3, velocity: Vec3) -> Self { + let item = ItemStack::new("minecraft:firework_rocket", 1); + let (_, explosions) = fireworks_of(&item); + Self { + id, + position, + previous: position, + velocity, + life: 0, + lifetime: i32::MAX, + explosions, + attached: false, + angled: false, + item, + } + } + /// One tick of a server's client: the previous position, then `step`. + pub(super) fn remote_step(&mut self, step: impl FnOnce(Vec3) -> Vec3) { + self.previous = self.position; + self.life += 1; + self.position = step(self.position); + } /// Position and motion (the dispenser parity fixture). pub(super) fn motion(&self) -> (Vec3, Vec3) { (self.position, self.velocity) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 0edf4ade7..b9f0e5df5 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -246,6 +246,7 @@ impl Game<'_> { self.spawn = None; self.mobs.clear(); self.clear_remote_objects(); + self.clear_remote_projectiles(); self.creatures.projectiles.clear(); self.dropped.clear(); self.particles.clear(); @@ -365,6 +366,7 @@ impl Game<'_> { self.player = None; self.mobs.clear(); self.clear_remote_objects(); + self.clear_remote_projectiles(); self.dropped.clear(); self.particles.clear(); self.pickups.clear(); @@ -611,6 +613,7 @@ impl Game<'_> { self.world.weather = weather; self.mobs.clear(); self.clear_remote_objects(); + self.clear_remote_projectiles(); self.dropped.clear(); self.particles.clear(); self.pickups.clear(); @@ -997,12 +1000,14 @@ impl Game<'_> { m.on_ground = on_ground; } self.turn_remote_object(id, rotation.map(|(yaw, _)| yaw), on_ground); + self.turn_remote_projectile(id, rotation); } fn remove_entity(&mut self, id: i32) { self.mobs.retain(|m| m.id != id as u32); self.orbs.retain(|o| o.id != id as u32); self.dropped.retain(|d| d.id != id as u32); self.remove_remote_object(id as u32); + self.remove_remote_projectile(id as u32); if let Some(r) = self.remote.as_mut() { r.entities.remove(&id); r.lerps.remove(&id); @@ -1266,7 +1271,16 @@ impl Game<'_> { } } self.spawn_entity(p.id, &kind, p.position, rotation); - self.spawn_remote_object(p.id, &kind, p.position, rotation[0], p.data); + if !self.spawn_remote_object(p.id, &kind, p.position, rotation[0], p.data) { + self.spawn_remote_projectile( + p.id, + &kind, + p.position, + (rotation[0], rotation[1]), + p.velocity, + p.data, + ); + } } C::RemoveEntities(p) => { for id in p.ids { @@ -1376,6 +1390,7 @@ impl Game<'_> { } } self.remote_object_data(p.id, &p.values); + self.remote_projectile_data(p.id, &p.values); } // ClientPacketListener.handleSetEquipment: what a mob or another // player holds and wears (EquipmentSlot ordinals). @@ -2545,8 +2560,9 @@ impl Game<'_> { } head_lerps.retain(|_, (_, steps)| *steps > 0); if running { - // Boats, minecarts and armor stands interpolate like mobs. - self.tick_remote_objects(|id, at| match lerps.get_mut(&(id as i32)) { + // Boats, minecarts, armor stands, projectiles and the rest + // interpolate like mobs. + let mut step = |id: u32, at: Vec3| match lerps.get_mut(&(id as i32)) { Some(l) if l.steps > 0 => { let n = f64::from(l.steps); l.steps -= 1; @@ -2557,7 +2573,9 @@ impl Game<'_> { ) } _ => at, - }); + }; + self.tick_remote_objects(&mut step); + self.tick_remote_projectiles(&mut step); } lerps.retain(|_, l| l.steps > 0); if let Some(r) = self.remote.as_mut() { diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs new file mode 100644 index 000000000..23cbb1913 --- /dev/null +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -0,0 +1,229 @@ +//! Primed TNT, falling blocks, projectiles, tridents, firework rockets and +//! end crystals on a server's client (`ClientPacketListener.handleAddEntity` +//! and `handleSetEntityData`): the server simulates them, the client draws +//! them where it says and interpolates their moves. +use super::{FallingBlock, Game}; +use horizon_client::projectiles::{Projectile, ProjectileKind}; +use horizon_core::{geometry::Vec3, position::BlockPos}; +use horizon_protocol::entity_data::Value; +use horizon_world::registry::BlockStateId; + +/// The projectile a type id names, as Horizon models it. +fn projectile_kind(kind: &str) -> Option { + Some(match kind { + "minecraft:arrow" | "minecraft:spectral_arrow" => ProjectileKind::Arrow { + base_damage: 2.0, + critical: false, + effect: None, + }, + "minecraft:snowball" => ProjectileKind::Snowball, + "minecraft:egg" => ProjectileKind::Egg, + "minecraft:ender_pearl" => ProjectileKind::EnderPearl, + "minecraft:small_fireball" => ProjectileKind::SmallFireball, + "minecraft:fireball" => ProjectileKind::LargeFireball { power: 1 }, + "minecraft:llama_spit" => ProjectileKind::LlamaSpit, + "minecraft:wind_charge" => ProjectileKind::WindCharge, + "minecraft:breeze_wind_charge" => ProjectileKind::BreezeWindCharge, + "minecraft:wither_skull" => ProjectileKind::WitherSkull { dangerous: false }, + _ => return None, + }) +} + +impl Game<'_> { + /// `handleAddEntity` for these types; false when `kind` isn't one. + pub(super) fn spawn_remote_projectile( + &mut self, + id: i32, + kind: &str, + position: [f64; 3], + rotation: (f32, f32), + velocity: [f64; 3], + data: i32, + ) -> bool { + let id = id as u32; + let at = Vec3::new(position[0], position[1], position[2]); + let velocity = Vec3::new(velocity[0], velocity[1], velocity[2]); + let (yaw, pitch) = rotation; + self.remove_remote_projectile(id); + match kind { + "minecraft:tnt" => self.tnts.push(super::tnt::PrimedTnt { + id, + position: at, + previous: at, + velocity, + fuse: 80, + on_ground: false, + }), + // FallingBlockEntity.recreateFromPacket: the block state is the data. + "minecraft:falling_block" => self.falling.push(FallingBlock { + id, + state: BlockStateId(data.max(0) as u32), + start_pos: BlockPos::default(), + position: at, + previous: at, + velocity, + age: 0, + no_gravity: false, + drop_item: false, + fall_distance: 0.0, + damage: Default::default(), + saved: None, + }), + "minecraft:trident" => { + let mut t = super::trident::ThrownTrident::remote(id, at, yaw, pitch); + t.velocity = velocity; + self.tridents.push(t); + } + "minecraft:firework_rocket" => { + self.rockets + .push(super::fireworks::Rocket::remote(id, at, velocity)); + } + "minecraft:end_crystal" => self.crystals.push(super::dragon::Crystal { + id, + position: at, + show_bottom: true, + time: 0, + beam: None, + invulnerable: false, + }), + _ => { + let Some(k) = projectile_kind(kind) else { + return false; + }; + let mut p = Projectile::new(k, at, None); + p.id = id; + p.velocity = velocity; + p.yaw = yaw; + p.pitch = pitch; + self.creatures.projectiles.push(p); + } + } + true + } + + /// `handleSetEntityData` for these types: what changes how they look. + pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { + let id = id as u32; + for (index, _, value) in values { + if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id) { + // PrimedTnt.DATA_FUSE_ID. + if let (8, Value::Int(fuse)) = (index, value) { + t.fuse = *fuse; + } + } else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id) { + // FallingBlockEntity.DATA_START_POS (the model's seed). + if let (8, Value::Pos(p)) = (index, value) { + f.start_pos = BlockPos::new(p[0], p[1], p[2]); + } + } else if let Some(c) = self.crystals.iter_mut().find(|c| c.id == id) { + // EndCrystal.DATA_BEAM_TARGET and DATA_SHOW_BOTTOM. + match (index, value) { + (8, Value::OptPos(beam)) => c.beam = *beam, + (9, Value::Bool(show)) => c.show_bottom = *show, + _ => {} + } + } else if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| p.id == id) { + match (&mut p.kind, index, value) { + // AbstractArrow.ID_FLAGS (critical) and IN_GROUND. + (ProjectileKind::Arrow { critical, .. }, 8, Value::Byte(flags)) => { + *critical = flags & 1 != 0; + } + (ProjectileKind::Arrow { .. }, 10, Value::Bool(in_ground)) => { + p.stuck = *in_ground; + } + // WitherSkull.DATA_DANGEROUS. + (ProjectileKind::WitherSkull { dangerous }, 8, Value::Bool(d)) => { + *dangerous = *d; + } + _ => {} + } + } + } + } + + /// A move's rotation for a projectile or trident. + pub(super) fn turn_remote_projectile(&mut self, id: i32, rotation: Option<(f32, f32)>) { + let (id, Some((yaw, pitch))) = (id as u32, rotation) else { + return; + }; + if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| p.id == id) { + p.yaw = yaw; + p.pitch = pitch; + } else if let Some(t) = self.tridents.iter_mut().find(|t| t.id == id) { + t.yaw = yaw; + t.pitch = pitch; + } + } + + /// One tick of interpolation, as `tick_remote_objects`. + pub(super) fn tick_remote_projectiles(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) { + for t in &mut self.tnts { + t.previous = t.position; + t.position = step(t.id, t.position); + } + for f in &mut self.falling { + f.previous = f.position; + f.age += 1; + f.position = step(f.id, f.position); + } + for p in &mut self.creatures.projectiles { + p.previous = p.position; + p.age += 1; + p.position = step(p.id, p.position); + } + for t in &mut self.tridents { + let id = t.id; + t.remote_step(|at| step(id, at)); + } + for r in &mut self.rockets { + let id = r.id; + r.remote_step(|at| step(id, at)); + } + for c in &mut self.crystals { + c.time += 1; + c.position = step(c.id, c.position); + } + } + + pub(super) fn remove_remote_projectile(&mut self, id: u32) { + self.tnts.retain(|t| t.id != id); + self.falling.retain(|f| f.id != id); + self.creatures.projectiles.retain(|p| p.id != id); + self.tridents.retain(|t| t.id != id); + self.rockets.retain(|r| r.id != id); + self.crystals.retain(|c| c.id != id); + } + + /// Every one a server sent (a new connection, level or dimension). + pub(super) fn clear_remote_projectiles(&mut self) { + self.tnts.clear(); + self.falling.clear(); + self.creatures.projectiles.clear(); + self.tridents.clear(); + self.rockets.clear(); + self.crystals.clear(); + } + + /// Their kind and place, for `status`. + pub(super) fn projectiles_status(&self) -> Vec { + use serde_json::json; + let at = |p: Vec3| [p.x, p.y, p.z]; + let mut out = Vec::new(); + for t in &self.tnts { + out.push(json!({"id": t.id, "kind": "minecraft:tnt", "position": at(t.position), "fuse": t.fuse})); + } + for f in &self.falling { + out.push(json!({"id": f.id, "kind": "minecraft:falling_block", "position": at(f.position), "state": f.state.0})); + } + for c in &self.crystals { + out.push(json!({"id": c.id, "kind": "minecraft:end_crystal", "position": at(c.position), "show_bottom": c.show_bottom})); + } + for t in &self.tridents { + out.push(json!({"id": t.id, "kind": "minecraft:trident", "position": at(t.position)})); + } + for p in &self.creatures.projectiles { + out.push(json!({"id": p.id, "kind": format!("{:?}", p.kind).split([' ', '{']).next().unwrap_or(""), "position": at(p.position)})); + } + out + } +} diff --git a/crates/horizon-client/src/game/trident.rs b/crates/horizon-client/src/game/trident.rs index 2077cfada..ecfea890f 100644 --- a/crates/horizon-client/src/game/trident.rs +++ b/crates/horizon-client/src/game/trident.rs @@ -31,6 +31,32 @@ pub(super) struct ThrownTrident { returning: u32, creative_only: bool, } +impl ThrownTrident { + /// A trident a server sent (`handleAddEntity`): placed and moved by it. + pub(super) fn remote(id: u32, position: Vec3, yaw: f32, pitch: f32) -> Self { + Self { + id, + stack: ItemStack::new("minecraft:trident", 1), + position, + previous: position, + velocity: Vec3::ZERO, + yaw, + pitch, + in_ground: false, + in_ground_time: 0, + dealt_damage: false, + loyalty: 0, + no_physics: false, + returning: 0, + creative_only: false, + } + } + /// One tick of a server's client: the previous position, then `step`. + pub(super) fn remote_step(&mut self, step: impl FnOnce(Vec3) -> Vec3) { + self.previous = self.position; + self.position = step(self.position); + } +} const HALF: f64 = 0.25; fn bounds(p: Vec3) -> Aabb { Aabb::new( diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 01375c79a..80f4a03c8 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -44,6 +44,8 @@ address. The script then checks, from both sides, what a guest's game used to ge guest their points (not the host). - objects: boats, minecarts, armor stands, item frames and paintings the host summons reach the guest as the host has them (kind, place, rotation, what they hold or wear, the variant). +- projectiles: primed TNT (one falling), an end crystal and an arrow that lands reach the guest where + the host has them; a falling block lands on both sides. Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -77,7 +79,7 @@ GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'effects', 'portals', - 'objects'] + 'objects', 'projectiles'] class Game: @@ -1047,10 +1049,10 @@ def check_xp(host, guest, out): return {'seen': seen, 'guest': xp, 'host': host_xp} -def objects_near(game, centre): +def objects_near(game, centre, key='objects'): """`status`'s objects within 8 blocks of centre, without their ids (each side numbers its own).""" out = [] - for o in game('status')['objects']: + for o in game('status')[key]: if all(abs(o['position'][i] - centre[i]) <= 8 for i in (0, 2)): o = dict(o) o.pop('id') @@ -1100,6 +1102,35 @@ def check_objects(host, guest, out): return f'{len(theirs)} objects identical' +def check_projectiles(host, guest, out): + x, y, z = (int(v // 1) for v in pos_of(host)) + commands = [ + f'summon minecraft:tnt {x + 4.5} {y} {z + 4.5} {{fuse:2000s}}', + f'summon minecraft:end_crystal {x - 4.5} {y} {z + 4.5} {{ShowBottom:0b}}', + f'summon minecraft:tnt {x + 2.5} {y + 5} {z - 3.5} {{fuse:2000s}}', + f'summon minecraft:arrow {x - 2.5} {y + 4} {z - 2.5} {{Motion:[0.2d,0d,0d]}}', + f'summon minecraft:falling_block {x - 0.5} {y + 6} {z - 4.5} {{BlockState:{{Name:"minecraft:sand"}}}}', + ] + try: + for command in commands: + host('command', command=command) + host.wait(80) + guest.raw('look-at', x, y, z) + guest.wait(5) + guest.shot(out, 'projectiles') + mine = objects_near(host, (x, y, z), 'projectiles') + theirs = objects_near(guest, (x, y, z), 'projectiles') + finally: + host('command', command=f'kill @e[type=!minecraft:player,x={x - 8},y={y - 2},z={z - 8},dx=16,dy=10,dz=16]') + host('command', command=f'setblock {x - 1} {y} {z - 5} minecraft:air') + assert len(mine) == len(commands) - 1, f'the host has {len(mine)}: {mine}' + # The fuse counts down every tick: the guest's, read a moment later, may be a tick or two behind. + fuses = [(m.pop('fuse', 0), t.pop('fuse', 0)) for m, t in zip(mine, theirs)] + assert theirs == mine, f'the guest sees {theirs}\nthe host has {mine}' + assert all(abs(a - b) <= 2 for a, b in fuses), f'fuses (host, guest): {fuses}' + return f'{len(theirs)} identical, fuses (host, guest) {fuses}' + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'sign': check_sign, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, @@ -1107,7 +1138,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls, 'effects': check_effects, 'portals': check_portals, 'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push, 'eat': check_eat, - 'xp': check_xp, 'objects': check_objects} + 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles} def run(ctl, out, resources, checks): From a1af7fdad593d26821cc15a147f7a7e2defa37df Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 23:07:55 +0200 Subject: [PATCH 37/67] Worlds without structures generate none `generate-structures=false` (server.properties, stored by #733) and the world screen's Generate Structures were written to the world's settings but the generator ignored them. VanillaTerrain now builds its generator from the world's `generate_structures` (OverworldGenerator::for_world): without structures there are no starts, terrain adaptation or structure pieces, as in ChunkStatusTasks.generateStructureStarts, and `locate structure` and eyes of ender find nothing (ChunkGenerator.findNearestMapStructure). The server world gen harness's world without structures now also compares `locate structure` for a village and a mineshaft and the saved chunks' structure starts and references. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 5 ++- crates/horizon-client/src/vanillagen.rs | 15 ++++++- .../horizon-world/src/worldgen/generator.rs | 12 ++++++ tools/test_server_world_gen_parity.py | 42 +++++++++++++++++-- 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e126c533a..33aa81cb4 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6569,7 +6569,10 @@ impl Game<'_> { res.threads, ) } else { - let inner = Arc::new(VanillaTerrain::for_dimension(&self.options.resources.join("worldgen"), pack.assets.registry, &res.biomes, seed, dim.id()).map_err(|e| { + // WorldOptions.generateStructures, from the world's settings. + let structures = + horizon_client::save::WorldGen::read(world_root).is_none_or(|w| w.structures); + let inner = Arc::new(VanillaTerrain::for_world(&self.options.resources.join("worldgen"), pack.assets.registry, &res.biomes, seed, dim.id(), structures).map_err(|e| { format!("{e}\nPrepare the worldgen bundle with the launcher, or: horizon-prepare --prepare") })?); self.end.set_locator(Some(inner.clone())); diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index 657817bb9..6c94ea67d 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -56,6 +56,19 @@ impl VanillaTerrain { biomes: &Biomes, seed: i64, dimension: &str, + ) -> crate::Result { + Self::for_world(dir, registry, biomes, seed, dimension, true) + } + /// A dimension of a world whose `generate_structures` is `structures` + /// (`ChunkGenerator.createStructures` and `findNearestMapStructure` do + /// nothing without). + pub fn for_world( + dir: &Path, + registry: &'static BlockRegistry, + biomes: &Biomes, + seed: i64, + dimension: &str, + structures: bool, ) -> crate::Result { let data: Arc = shared_data(dir, registry)?; let biome_map = data @@ -64,7 +77,7 @@ impl VanillaTerrain { .map(|name| biomes.id(name)) .collect(); let generator = - OverworldGenerator::for_dimension(data, seed, "minecraft:normal", dimension)?; + OverworldGenerator::for_world(data, seed, "minecraft:normal", dimension, structures)?; let unsupported = generator.unsupported_features(); if !unsupported.is_empty() { log::warn!( diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 12dfc10d9..7b73d35a8 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -1764,6 +1764,18 @@ impl OverworldGenerator { ) -> Result { Self::build(data, seed, preset, dimension, true) } + /// A dimension of a world preset, with or without structures (the + /// world's `generate_structures`: without, no starts, terrain adaptation, + /// structure pieces or structure locating). + pub fn for_world( + data: Arc, + seed: i64, + preset: &str, + dimension: &str, + structures: bool, + ) -> Result { + Self::build(data, seed, preset, dimension, structures) + } fn build( data: Arc, seed: i64, diff --git a/tools/test_server_world_gen_parity.py b/tools/test_server_world_gen_parity.py index 0ad0ed25e..f55e55809 100644 --- a/tools/test_server_world_gen_parity.py +++ b/tools/test_server_world_gen_parity.py @@ -10,7 +10,10 @@ to the preset's classic ones) and `generate-structures`. Each case starts vanilla's server and Horizon's (`horizon-client --dedicated --nogui`) in a new folder, waits for `Done` and stops them. Compared exactly: the world's `data/minecraft/world_gen_settings.dat`, tag types included, and the warnings and errors logged while the world's settings are read (the `Worker-Main` lines; -the worker's number is left out). +the worker's number is left out). In a world without structures (`generate-structures=false`), also what +`locate structure` answers on the console for a village and a mineshaft (ChunkGenerator.findNearestMapStructure +finds none) and how many of the saved chunks hold structure starts or references (none: +ChunkStatusTasks.generateStructureStarts does nothing). Horizon generates the normal and flat presets only: `large_biomes`, `amplified`, `single_biome_surface` and `debug_all_block_states` aren't tested. @@ -23,6 +26,7 @@ import re import shutil import subprocess import sys +import time try: from . import anvil_dump, lab, test_rust_parity as base @@ -60,6 +64,32 @@ CASES = { 'unknown': {'level-type': 'Some Type', 'level-seed': '42'}, } WORKER = re.compile(r'\[Worker-Main-\d+/(WARN|ERROR)\]: (.*)') +LOCATE = ['locate structure #minecraft:village', 'locate structure minecraft:mineshaft'] +REPLY = re.compile(r'\]: (?:System chat: )?((?:Could not find|The nearest|Unknown|Incorrect).*)') + + +def console_reply(server, command, timeout=300): + """The first command reply the console logs after `command`.""" + start = len(server.lines) + server.console(command) + deadline = time.time() + timeout + while time.time() < deadline: + for line in server.lines[start:]: + if m := REPLY.search(line): + return m.group(1) + time.sleep(0.2) + raise AssertionError(f'no reply to {command!r} in {timeout} s:\n' + '\n'.join(server.lines[-20:])) + + +def structure_chunks(world): + """How many saved overworld chunks hold structure starts, and how many references.""" + starts = references = 0 + for region in sorted((world / 'region').glob('*.mca')): + for chunk in anvil_dump.region_chunks(region).values(): + structures = anvil_dump.plain(chunk, True).get('structures', {}) + starts += bool(structures.get('starts')) + references += bool(structures.get('References')) + return f'chunks with structure starts: {starts}, with references: {references}' def escape(value): @@ -77,11 +107,16 @@ def run_case(label, command, build, case): encoding='iso-8859-1') (directory / 'eula.txt').write_text('eula=true\n') server = Server(command, directory, f'{label}-{case}') + replies = [] try: server.wait_for(r'Done \(') + if CASES[case].get('generate-structures') == 'false': + replies = [f'{c}: {console_reply(server, c)}' for c in LOCATE] finally: server.stop() - out = [f'# {case}'] + out = [f'# {case}', *replies] + if replies: + out.append(structure_chunks(directory / 'world')) out += [f'{m.group(1)}: {m.group(2)}' for l in server.lines if (m := WORKER.search(l))] settings = anvil_dump.plain(anvil_dump.read_dat(directory / 'world/data/minecraft/world_gen_settings.dat'), True, typed=True) @@ -121,7 +156,8 @@ def main(): java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], cases=list(CASES), lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), scope='a new dedicated-server world: level-type (flat, normal, unknown), a flat world\'s ' - 'generator-settings, generate-structures, as world_gen_settings.dat and the lines logged.')) + 'generator-settings, generate-structures, as world_gen_settings.dat and the lines logged; without ' + 'structures, locate structure\'s replies and the saved chunks\' structure starts and references.')) print(f'server world gen: {len(CASES)} cases, {len(vanilla)} lines identical') From 9a2d87ec9708d417d6fa4e19fa99110da8e78e39 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 02:14:03 +0200 Subject: [PATCH 38/67] fix(server): move players to another dimension with execute in ... run tp On a dedicated server, `execute in run tp @s ...` from a player moved them inside the level they were in. It now does what vanilla's TeleportCommand does: ServerPlayer.teleport into the other level, with the Respawn packet keeping all data and no portal sound, then the feedback. The players there are tracked to them and they are removed from the ones they left. tools/test_server_dimension_players_parity.py joins two players to a vanilla and a Horizon server and compares, step by step, the players each is sent and loses and the dimensions it respawns into, while they go to the Nether and the End by command and through an End portal. LocatorBarClient gains `players on` and `still NAME` (the position reminder a still client sends). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 3 + .../horizon-client/src/game/command_blocks.rs | 1 + crates/horizon-client/src/game/commands.rs | 16 ++ .../src/game/commands/execute.rs | 7 + crates/horizon-client/src/game/lan_actions.rs | 2 + .../horizon-client/src/game/server_travel.rs | 55 ++++++- crates/horizon-client/src/lan/hub.rs | 27 +++- .../horizonlab/parity/LocatorBarClient.java | 38 ++++- tools/test_server_dimension_players_parity.py | 150 ++++++++++++++++++ 9 files changed, 287 insertions(+), 12 deletions(-) create mode 100644 tools/test_server_dimension_players_parity.py diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e126c533a..7d3e5c56b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -984,6 +984,8 @@ struct Game<'a> { /// A server's players on their way to another level, each waiting for /// the destination to load around them. server_trips: Vec, + /// Players a command sends to another level, once it has run. + level_teleports: Vec, /// A server's levels other than the one in place (game/levels.rs). levels: levels::Levels, /// Which pass of a server tick runs (`tick_server`): the first one reads @@ -10295,6 +10297,7 @@ impl<'a> Game<'a> { world_root: PathBuf::new(), travelling: None, server_trips: Vec::new(), + level_teleports: Vec::new(), levels: Default::default(), level_pass: Default::default(), portals: Default::default(), diff --git a/crates/horizon-client/src/game/command_blocks.rs b/crates/horizon-client/src/game/command_blocks.rs index 5a5fa11c8..f85be2fd3 100644 --- a/crates/horizon-client/src/game/command_blocks.rs +++ b/crates/horizon-client/src/game/command_blocks.rs @@ -357,6 +357,7 @@ impl Game<'_> { silent: false, store: None, elsewhere: false, + level: None, block: Some(pos), }; // performPrefixedCommand: a leading slash is allowed. diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 3ca938d6a..e0a0d1204 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -1124,7 +1124,23 @@ impl Game<'_> { (-(dy.atan2(horizontal).to_degrees())) as f32, ]); } + // `execute in` another dimension: a server's player moves to that + // level (ServerPlayer.teleport into another level), after the command. + let level = self + .command_source + .and_then(|s| s.level) + .filter(|d| *d != self.dim && self.options.dedicated.is_some()); for &t in &targets { + if let (Some(dim), Target::Player, Some(conn)) = (level, t, self.lan_acting) { + self.level_teleports + .push(super::server_travel::LevelTeleport { + conn, + dim, + at: pos, + rotation, + }); + continue; + } self.teleport_to(t, pos, rotation); } let [x, y, z] = pos.map(|v| Text::literal(format_double(v))); diff --git a/crates/horizon-client/src/game/commands/execute.rs b/crates/horizon-client/src/game/commands/execute.rs index 1fa67c8b6..01488db2c 100644 --- a/crates/horizon-client/src/game/commands/execute.rs +++ b/crates/horizon-client/src/game/commands/execute.rs @@ -26,6 +26,9 @@ pub(in crate::game) struct Source { /// `execute in` another dimension than the one Horizon has loaded: none /// of its chunks are loaded. pub elsewhere: bool, + /// The dimension `execute in` named, if any: a server teleports its + /// players there (`TeleportCommand` into the source's level). + pub level: Option, /// The command block whose output this source is /// (`BaseCommandBlock.CloseableCommandBlockSource`). pub block: Option<(i32, i32, i32)>, @@ -77,6 +80,7 @@ impl Game<'_> { silent: false, store: None, elsewhere: false, + level: None, block: None, } } @@ -90,6 +94,7 @@ impl Game<'_> { silent: true, store: None, elsewhere: false, + level: None, block: None, } } @@ -441,6 +446,7 @@ impl Game<'_> { pos: self.target_position(t)?, rot: self.target_rotation(t), elsewhere: false, + level: None, ..s }) }) @@ -544,6 +550,7 @@ impl Game<'_> { } vec![Source { elsewhere: dim != self.dim.id(), + level: Some(super::super::Dim::from_id(dim)), ..s }] } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 68b29a360..eba49f2d0 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -928,6 +928,8 @@ impl Game<'_> { .map(|t| horizon_client::lan::data::component_nbt(&t.component())) .collect::>() }); + // A teleport into another level happens before its feedback is sent. + self.run_level_teleports(host); for content in lines.unwrap_or_default() { host.hub.send_to( conn, diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs index 6312254c7..6c0ec48e7 100644 --- a/crates/horizon-client/src/game/server_travel.rs +++ b/crates/horizon-client/src/game/server_travel.rs @@ -22,6 +22,17 @@ pub(super) struct Trip { end: bool, /// The Nether portal plan, whose exit is found or built on arrival. plan: Option, + /// Sent by a command: the player is already where it goes. + placed: bool, +} + +/// `TeleportCommand` into another level for a server's player: where, and +/// the rotation it sets (none keeps the player's). +pub(super) struct LevelTeleport { + pub conn: u32, + pub dim: Dim, + pub at: [f64; 3], + pub rotation: Option<[f32; 2]>, } /// A player's box: Player's standing dimensions. @@ -177,7 +188,8 @@ impl Game<'_> { }; let at = [destination.x, destination.y, destination.z]; let ctx = self.server_ctx(); - host.hub.change_level(i, at, yaw, &ctx); + // TeleportTransition.PLAY_PORTAL_SOUND. + host.hub.change_level(i, at, yaw, None, true, &ctx); self.as_guest(host, conn, |g| g.trigger_dimension(source, target)); self.server_trips.push(Trip { conn, @@ -185,6 +197,7 @@ impl Game<'_> { target: destination, end, plan, + placed: false, }); if let Err(e) = self.enter_level(Some(host), source) { log::error!("could not reopen {}: {e}", source.id()); @@ -214,6 +227,9 @@ impl Game<'_> { continue; } let trip = self.server_trips.remove(k); + if trip.placed { + continue; + } self.resolve_arrivals(); let yaw = host.hub.guests[i].yaw; let (position, yaw) = if trip.end { @@ -248,6 +264,43 @@ impl Game<'_> { } } + /// The players the command just run sends to another level + /// (`ServerPlayer.teleport` from TeleportCommand): a Respawn packet + /// keeping all data and the teleport, with no portal sound. The server + /// leaves the body alone until the level has loaded around it. + pub(super) fn run_level_teleports(&mut self, host: &mut LanHost) { + for t in std::mem::take(&mut self.level_teleports) { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == t.conn) else { + continue; + }; + let source = self.dim; + if let Err(e) = self.enter_level(Some(host), t.dim) { + log::error!("could not open {}: {e}", t.dim.id()); + continue; + } + let g = &mut host.hub.guests[i]; + let [yaw, pitch] = t.rotation.unwrap_or([g.yaw, g.pitch]); + // A new body there: no fall distance or motion carried over. + g.motion = None; + let ctx = self.server_ctx(); + host.hub + .change_level(i, t.at, yaw, Some(pitch), false, &ctx); + self.as_guest(host, t.conn, |g| g.trigger_dimension(source, t.dim)); + self.server_trips.retain(|trip| !trip.is(t.conn)); + self.server_trips.push(Trip { + conn: t.conn, + dim: t.dim, + target: Vec3::new(t.at[0], t.at[1], t.at[2]), + end: false, + plan: None, + placed: true, + }); + if let Err(e) = self.enter_level(Some(host), source) { + log::error!("could not reopen {}: {e}", source.id()); + } + } + } + /// The hub's view of the server for packets sent outside its own tick. pub(super) fn server_ctx(&mut self) -> TickCtx<'_> { let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]); diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index b18187835..bb4eea345 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -2199,7 +2199,15 @@ impl Hub { /// names (`set_dimension`): ClientboundRespawnPacket keeping all data, /// then vanilla's packet order. Nothing tracked in the old level is /// removed one by one: the client drops its whole level. - pub fn change_level(&mut self, i: usize, at: [f64; 3], yaw: f32, ctx: &TickCtx) { + pub fn change_level( + &mut self, + i: usize, + at: [f64; 3], + yaw: f32, + pitch: Option, + portal_sound: bool, + ctx: &TickCtx, + ) { let mode = self.guests[i].game_mode; let info = self.spawn_info(mode); self.close_container_for(i); @@ -2210,6 +2218,9 @@ impl Hub { g.chunk_queue.clear(); g.center = None; g.yaw = yaw; + if let Some(pitch) = pitch { + g.pitch = pitch; + } g.position = at; g.previous_position = at; g.send(&C::Respawn(play::Respawn { @@ -2241,12 +2252,14 @@ impl Hub { // ServerPlayer.sendPostEffects. g.send(&raw(ids::POST_EFFECTS, vec![0])); // TeleportTransition.PLAY_PORTAL_SOUND. - g.send(&C::LevelEvent(play::LevelEvent { - kind: 1032, - pos: [0, 0, 0], - data: 0, - global: false, - })); + if portal_sound { + g.send(&C::LevelEvent(play::LevelEvent { + kind: 1032, + pos: [0, 0, 0], + data: 0, + global: false, + })); + } // lastSentExp, lastSentHealth and lastSentFood. g.sent_health = None; g.sent_xp = None; diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java index e41c693ee..a29b95231 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java @@ -76,11 +76,16 @@ import net.minecraft.world.phys.Vec3; * line at a time: {@code step LABEL} (the label the next packets are recorded * under), {@code join NAME}, {@code cmd NAME COMMAND...}, {@code shift NAME * on|off} (the shift key, as ServerboundPlayerInputPacket), {@code fly NAME - * on|off} (ServerboundPlayerAbilitiesPacket), {@code quit NAME} and {@code - * end}, which writes each player's lines to OUT. + * on|off} (ServerboundPlayerAbilitiesPacket), {@code still NAME} (the + * position reminder LocalPlayer.sendPosition sends every 20 ticks standing + * still), {@code quit NAME}, {@code + * players on|off} (whether each player also records the other players added + * to and removed from its level, and its own respawns into a dimension) and + * {@code end}, which writes each player's lines to OUT. */ public final class LocatorBarClient { private static volatile String step = "start"; + private static volatile boolean players; private static final Map NAMES = new LinkedHashMap<>(); public static void main(String[] args) throws Exception { @@ -110,7 +115,9 @@ public final class LocatorBarClient { abilities.flying = w[2].equals("on"); players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities)); } + case "still" -> players.get(w[1]).still(); case "quit" -> players.get(w[1]).close(); + case "players" -> LocatorBarClient.players = w[1].equals("on"); case "end" -> { try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) { for (Player p : players.values()) { @@ -166,8 +173,10 @@ public final class LocatorBarClient { private int threshold = -1; private ProtocolInfo inbound; private ProtocolInfo outbound; - private PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0); + private volatile PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0); private boolean loaded; + /** The other players this one was sent, by entity id. */ + private final Map seen = new java.util.HashMap<>(); private final Object replyLock = new Object(); private int replies; @@ -198,6 +207,14 @@ public final class LocatorBarClient { } } + void still() throws Exception { + PositionMoveRotation at = position; + // A tick of its own: the server takes one position per client tick (receivedPositionThisTick). + send(net.minecraft.network.protocol.game.ServerboundClientTickEndPacket.INSTANCE); + send(new ServerboundMovePlayerPacket.PosRot(at.position(), at.yRot(), at.xRot(), false, false)); + send(net.minecraft.network.protocol.game.ServerboundClientTickEndPacket.INSTANCE); + } + void close() throws Exception { if (socket != null) socket.close(); } @@ -266,7 +283,20 @@ public final class LocatorBarClient { while (true) { Received r = read(); Packet p = r.packet; - if (p instanceof ClientboundTrackedWaypointPacket) { + if (p instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket a + && a.getType() == net.minecraft.world.entity.EntityTypes.PLAYER) { + String who = NAMES.getOrDefault(a.getUUID(), "uuid?"); + seen.put(a.getId(), who); + if (players) record("add " + who + " at " + a.getX() + "," + a.getY() + "," + a.getZ()); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket rm) { + rm.entityIds().forEach(id -> { + String who = seen.remove(id); + if (who != null && players) record("remove " + who); + }); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundRespawnPacket rs) { + if (players) record("respawn " + rs.commonPlayerSpawnInfo().dimension().identifier()); + seen.clear(); + } else if (p instanceof ClientboundTrackedWaypointPacket) { record(waypoint(r.payload)); } else if (p instanceof ClientboundSystemChatPacket) { synchronized (replyLock) { diff --git a/tools/test_server_dimension_players_parity.py b/tools/test_server_dimension_players_parity.py new file mode 100644 index 000000000..77e1428e9 --- /dev/null +++ b/tools/test_server_dimension_players_parity.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 +"""Players in different dimensions on a dedicated server, against vanilla's, seen by vanilla clients. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first, with Alice and Bob as operators in creative. LocatorBarClient joins them both +(players made of vanilla's protocol code) and records, step by step, the other players each is sent and loses +(ClientboundAddEntityPacket, ClientboundRemoveEntitiesPacket) and the dimensions it respawns into +(ClientboundRespawnPacket), with the locator bar's waypoints, while they change dimensions (ServerPlayer.teleport +through `execute in ... run tp`, and an End portal): Bob goes to the Nether and back, Alice to the End, Bob +follows her there, both come back to the Overworld, then Bob steps into an End portal and comes back. Between +the steps each client sends its position (the reminder a still client sends every second). Each player's lines in each step are compared exactly. +""" +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server + from .test_locator_bar_parity import generate_world, server_dir +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server + from test_locator_bar_parity import generate_world, server_dir + +NAME = 'server-dimension-players-parity' +NETHER, END, OVERWORLD = 'minecraft:the_nether', 'minecraft:the_end', 'minecraft:overworld' +# (label, what the players do). +SCRIPT = [ + ('bob-nether', [f'cmd Bob execute in {NETHER} run tp @s 0.5 100 0.5']), + ('bob-back', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']), + ('alice-end', [f'cmd Alice execute in {END} run tp @s 0.5 100 0.5']), + ('bob-end', [f'cmd Bob execute in {END} run tp @s 2.5 100 0.5']), + ('alice-back', [f'cmd Alice execute in {OVERWORLD} run tp @s 0.5 100 0.5']), + ('bob-back-again', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']), + # Through an End portal (Entity.handlePortal), and back by command. + ('bob-portal', ['cmd Bob setblock 2 101 3 minecraft:end_portal', 'cmd Bob tp @s 2.5 101 3.5']), + ('bob-portal-back', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']), +] +STEP_SECONDS = 5.0 + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + + def tell(line): + client.stdin.write(line + '\n') + client.stdin.flush() + try: + tell('players on') + tell('step joins') + for name, x in (('Alice', 0.5), ('Bob', 2.5)): + tell(f'join {name}') + server.wait_for(rf'{name} joined the game', timeout=120) + tell(f'fly {name} on') + tell(f'cmd {name} tp @s {x} 100 0.5') + time.sleep(STEP_SECONDS) + for step, actions in SCRIPT: + tell(f'step {step}') + for action in actions: + tell(action) + # Standing still, a client still sends its position every 20 ticks + # (LocalPlayer.sendPosition): the server then tracks to it what it has sent the + # chunks of since (ChunkMap.move). + time.sleep(STEP_SECONDS / 2) + tell('still Alice') + tell('still Bob') + time.sleep(STEP_SECONDS / 2) + tell('end') + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + return out.read_text(encoding='utf-8').splitlines() + + +def shown(lines): + """Each player's adds, removes and respawns, without the waypoints (the locator bar harness has those).""" + return [l for l in lines if l.split(' ', 2)[2:3] and l.split(' ', 2)[2].split(' ')[0] in ('add', 'remove', 'respawn')] + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = generate_world(vanilla_server, build) + vanilla_all = run_side('vanilla', vanilla_server, client, build, world) + horizon_all = run_side('horizon', horizon_server, client, build, world) + errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla_all), shown(horizon_all) + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if sum(' respawn ' in l for l in vanilla) < 6: + raise AssertionError(f'vanilla sent too few respawns: the script did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Two players on a dedicated server changing dimensions (Nether, End, back): the other players each ' + 'is sent and loses, and the dimensions it respawns into.')) + print(f'server dimension players: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') From 4ac29bf1c14e7785fa1c33f5e0aceaaef0aa9c4c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 02:15:47 +0200 Subject: [PATCH 39/67] docs(hosting): players travel between dimensions on their own Co-Authored-By: Claude Opus 5.5 --- docs/hosting.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/hosting.md b/docs/hosting.md index f07983484..bb5d6db4d 100644 --- a/docs/hosting.md +++ b/docs/hosting.md @@ -22,8 +22,11 @@ plan are in [docs/server.md](https://github.com/ludovic111/horizon/blob/main/doc (Tailscale, ZeroTier), or share it only with friends you trust. - **No secure chat.** Chat isn't signed, and `enforce-secure-profile` has no effect. Vanilla clients show chat from the server as not secure. -- **Nether and End:** the world follows a player through portals when they are alone on the server - (v2026.10.17.09 and newer). With several players online, portals don't move remote players yet. +- **Nether and End:** each player travels on their own through portals, and operators with `execute in + run tp`. The players in the level they leave stop seeing them, and the ones in the level they + arrive in start to, as on vanilla (checked against vanilla with two players, by command and through an End + portal: `tools/test_server_dimension_players_parity.py`). The console can't send another player to a + dimension yet (`execute in ... run tp NAME`). - **Movement checks and chunk pacing** (release v2026.10.17.16 and newer). The server pulls back players who move too fast ("moved too quickly"), as vanilla does, and sends chunks as fast as each client acknowledges them. From 209b898c029a9161f232392c2d9dd775d0cd5069 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 01:05:10 +0200 Subject: [PATCH 40/67] The console's and players' command feedback reaches the operators CommandSourceStack.broadcastToAdmins beyond a guest on a dedicated server: - The dedicated server's console tells every operator what its commands did, as `[Server: ...]`, when `broadcast-console-to-ops` is on (DedicatedServer.shouldInformAdmins). This covers the game's commands and the dedicated-only ones vanilla broadcasts (op, deop, ban, pardon, kick, whitelist, save-all, save-on/off, stop). - In a world open to LAN, the host, an operator when commands are allowed, hears of its guests' commands, and op guests hear of the host's. - A player's name in the line is their display name, with the `/tell` suggestion, the entity hover and the insertion (Player.getDisplayName). New harness test_server_ops_feedback_parity: two vanilla-codec operators' system chat while the console and one of them run commands, deop and op, toggle send_command_feedback and save, with broadcast-console-to-ops on and off. LocatorBarClient can record system chat (`chat on`). lan_e2e's feedback check also asserts the host is told. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 5 + crates/horizon-client/src/game/commands.rs | 62 +++++-- crates/horizon-client/src/game/dedicated.rs | 55 +++++- crates/horizon-client/src/game/lan.rs | 4 + crates/horizon-client/src/game/lan_actions.rs | 19 +- crates/horizon-client/src/text.rs | 12 ++ .../horizonlab/parity/LocatorBarClient.java | 14 +- tools/lan_e2e.py | 9 +- tools/test_server_ops_feedback_parity.py | 162 ++++++++++++++++++ 9 files changed, 313 insertions(+), 29 deletions(-) create mode 100644 tools/test_server_ops_feedback_parity.py diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e126c533a..da46d57b4 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1155,6 +1155,10 @@ struct Game<'a> { /// returns: the line, whether operators get it and whether the console /// logs it. admin_feedback: Vec<(horizon_client::text::Text, bool, bool)>, + /// The server console is running a command: whether its feedback goes to + /// the operators (`DedicatedServer.shouldInformAdmins`, + /// `broadcast-console-to-ops`). + console_broadcast: Option, /// State of `/spectate`, `/posteffect`, `/debug`, `/waypoint` and friends. cmds: commands::extra::CommandExtras, ui: chat::UiState, @@ -10403,6 +10407,7 @@ impl<'a> Game<'a> { lan_acting_uuid: None, chat_capture: None, admin_feedback: Vec::new(), + console_broadcast: None, cmds: Default::default(), ui: Default::default(), combat: Default::default(), diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 3ca938d6a..9126eea7e 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -165,14 +165,20 @@ impl Game<'_> { } self.broadcast_to_admins(message); } - /// CommandSourceStack.broadcastToAdmins, for a guest's command on a - /// dedicated server (`sendSuccess` with `broadcast`): `[name: message]`, - /// gray and italic, for the other operators when `send_command_feedback` - /// and the console when `log_admin_commands`. Queued while the guest acts; - /// `send_admin_feedback` sends it. - fn broadcast_to_admins(&mut self, message: Text) { - if self.lan_acting.is_none() - || self.options.dedicated.is_none() + /// CommandSourceStack.broadcastToAdmins (`sendSuccess` with `broadcast`) + /// for a guest's command, the host's in a world open to LAN and the + /// dedicated server console's when `broadcast-console-to-ops` is on: + /// `[name: message]`, gray and italic, for the other operators when + /// `send_command_feedback`, and for a player's command on a dedicated + /// server the console when `log_admin_commands`. Queued while the + /// command runs; `send_admin_feedback` sends it. + pub(super) fn broadcast_to_admins(&mut self, message: Text) { + let informs = match self.console_broadcast { + Some(broadcast) => broadcast, + // While a guest acts the host's LanHost is out of `self.lan`. + None => self.lan_acting.is_some() || self.lan.is_some(), + }; + if !informs || message .translation_key() .is_some_and(|k| admin::QUIET.contains(&k)) @@ -180,20 +186,46 @@ impl Game<'_> { return; } let ops = self.ui.rules.bool("send_command_feedback"); - let console = self.ui.rules.bool("log_admin_commands"); + // MinecraftServer.sendSystemMessage, unless the server is the source. + let console = self.console_broadcast.is_none() + && self.lan_acting.is_some() + && self.options.dedicated.is_some() + && self.ui.rules.bool("log_admin_commands"); if !ops && !console { return; } - // getDisplayName: the entity the command runs as (`execute as`). - let who = self - .command_source - .and_then(|s| s.entity) - .unwrap_or(Target::Player); - let line = Text::translate("chat.type.admin", &[self.target_name(who), message]) + // getDisplayName: the entity the command runs as (`execute as`), or + // the server's own source, "Server". + let who = match self.command_source.and_then(|s| s.entity) { + Some(entity) => self.target_name(entity), + None if self.console_broadcast.is_some() => Text::literal("Server".to_owned()), + None => self.player_display_name(), + }; + let line = Text::translate("chat.type.admin", &[who, message]) .with_color(text::GRAY) .italic(); self.admin_feedback.push((line, ops, console)); } + /// Player.getDisplayName: the name (team-formatted), suggesting a + /// `/tell`, showing the player on hover, inserting the name. + fn player_display_name(&self) -> Text { + let name = self.acting_player_name().to_owned(); + let Some(uuid) = self.lan_acting_uuid else { + return self.target_name(Target::Player); + }; + self.target_name(Target::Player).with_component_fields(vec![ + ( + "click_event".into(), + serde_json::json!({"action": "suggest_command", "command": format!("/tell {name} ")}), + ), + ( + "hover_event".into(), + serde_json::json!({"action": "show_entity", "id": "minecraft:player", + "uuid": horizon_protocol::crypto::uuid_string(uuid), "name": name}), + ), + ("insertion".into(), name.into()), + ]) + } fn source_position(&self) -> [f64; 3] { if let Some(s) = &self.command_source { return s.pos; diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 49b77eb13..7348f3f9e 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -402,8 +402,32 @@ struct ServerState { /// When the last passes of the loop ended, for the tick rate horizon-server shows. ticks: std::collections::VecDeque, paused: bool, + /// `broadcast-console-to-ops`. + broadcast_console: bool, } +/// The dedicated-only commands' replies vanilla sends with `broadcast` +/// (`sendSuccess(..., true)`): the operators hear of them. +const BROADCAST: &[&str] = &[ + "commands.ban.success", + "commands.banip.info", + "commands.banip.success", + "commands.deop.success", + "commands.kick.success", + "commands.op.success", + "commands.pardon.success", + "commands.pardonip.success", + "commands.save.disabled", + "commands.save.enabled", + "commands.save.success", + "commands.stop.stopping", + "commands.whitelist.add.success", + "commands.whitelist.disabled", + "commands.whitelist.enabled", + "commands.whitelist.reloaded", + "commands.whitelist.remove.success", +]; + impl Game<'_> { /// DedicatedServer.initServer and MinecraftServer.runServer. pub(super) fn run_dedicated(&mut self) -> Result<()> { @@ -612,6 +636,7 @@ impl Game<'_> { empty_ticks: 0, ticks: Default::default(), paused: false, + broadcast_console: props.broadcast_console_to_ops, }; let pause_after = props.pause_when_empty_seconds * 20; let idle_timeout = props.player_idle_timeout; @@ -619,7 +644,7 @@ impl Game<'_> { while !state.stop { let tick_start = Instant::now(); while let Ok(line) = state.console.try_recv() { - self.console_command(&mut state, &line); + self.console_line(&mut state, &line); } self.poll_server_control(&mut state); let players = self.lan.as_ref().map_or(0, |h| h.hub.guests.len()); @@ -767,14 +792,36 @@ impl Game<'_> { }) }); if found.is_none() { - self.console_reply(Text::translate("argument.player.unknown", &[])); + let unknown = Text::translate("argument.player.unknown", &[]); + log("INFO", &format!("System chat: {}", unknown.plain())); } found } - /// MinecraftServer.sendSystemMessage: what the console is told. - fn console_reply(&self, text: Text) { + /// MinecraftServer.sendSystemMessage: what the console is told; while + /// the console runs a command, the replies vanilla broadcasts go to the + /// operators too. + fn console_reply(&mut self, text: Text) { log("INFO", &format!("System chat: {}", text.plain())); + if self.console_broadcast.is_some() + && text + .translation_key() + .is_some_and(|k| BROADCAST.contains(&k)) + { + self.broadcast_to_admins(text); + } + } + + /// One console line, then the operators' share of its feedback. + fn console_line(&mut self, state: &mut ServerState, line: &str) { + self.console_broadcast = Some(state.broadcast_console); + self.console_command(state, line); + self.console_broadcast = None; + if let Some(mut host) = self.lan.take() { + self.send_admin_feedback(&mut host, None); + self.lan = Some(host); + } + self.admin_feedback.clear(); } /// DedicatedServer.handleConsoleInputs: one console line, run as the diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 57c7b9ee0..2921f41fb 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -780,6 +780,10 @@ impl Game<'_> { }; // What the tick sends a guest goes to its connection at the end. let holding = lan::Holding::start(); + // The host's own commands' broadcastToAdmins lines. + if !self.admin_feedback.is_empty() { + self.send_admin_feedback(&mut host, None); + } let state = self.lan_host_state(&host); // A dedicated server runs the hub without a host player. if state.is_some() || host.hub.config.dedicated { diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 68b29a360..beec8718b 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -725,7 +725,7 @@ impl Game<'_> { }), ); } - self.send_admin_feedback(host, conn); + self.send_admin_feedback(host, Some(conn)); } } S::Unknown(8, payload) => { @@ -937,13 +937,19 @@ impl Game<'_> { }), ); } - self.send_admin_feedback(host, conn); + self.send_admin_feedback(host, Some(conn)); } - /// The `broadcastToAdmins` lines a guest's command queued: every other + /// The `broadcastToAdmins` lines a command queued (`source`: the guest + /// whose it was; none for the host's or the console's): every other /// operator gets them, and the console logs them as the server's own /// system messages (MinecraftServer.sendSystemMessage). - fn send_admin_feedback(&mut self, host: &mut LanHost, source: u32) { + pub(super) fn send_admin_feedback(&mut self, host: &mut LanHost, source: Option) { + // PlayerList.isOp for the host of a world open to LAN: the owner when + // the world allows commands, everyone when the LAN does. + let host_op = source.is_some() + && !host.hub.config.dedicated + && (self.allow_commands() || host.hub.config.guest_commands); for (line, ops, console) in std::mem::take(&mut self.admin_feedback) { if ops { let content = horizon_client::lan::data::component_nbt(&line.component()); @@ -951,13 +957,16 @@ impl Game<'_> { .hub .guests .iter() - .filter(|g| g.conn != source && g.permission > 0) + .filter(|g| Some(g.conn) != source && g.permission > 0) { g.send(&C::SystemChat(play::SystemChat { content: content.clone(), overlay: false, })); } + if host_op { + self.system_message(line.clone()); + } } if console { super::dedicated::log("INFO", &format!("System chat: {}", line.plain())); diff --git a/crates/horizon-client/src/text.rs b/crates/horizon-client/src/text.rs index e0b144714..ea11d4163 100644 --- a/crates/horizon-client/src/text.rs +++ b/crates/horizon-client/src/text.rs @@ -267,6 +267,18 @@ impl Text { _ => c, } } + /// Style fields the spans don't model (an entity hover, an insertion), + /// set on the component sent to vanilla only. + pub fn with_component_fields(mut self, fields: Vec<(String, Value)>) -> Self { + let mut c = self.component(); + if let Value::Object(o) = &mut c { + for (k, v) in fields { + o.entry(k).or_insert(v); + } + } + self.source = Some(c.into()); + self + } pub fn with_color(self, color: u32) -> Self { self.with_style(&Style::color(color)) } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java index e41c693ee..2127657d4 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java @@ -76,11 +76,13 @@ import net.minecraft.world.phys.Vec3; * line at a time: {@code step LABEL} (the label the next packets are recorded * under), {@code join NAME}, {@code cmd NAME COMMAND...}, {@code shift NAME * on|off} (the shift key, as ServerboundPlayerInputPacket), {@code fly NAME - * on|off} (ServerboundPlayerAbilitiesPacket), {@code quit NAME} and {@code - * end}, which writes each player's lines to OUT. + * on|off} (ServerboundPlayerAbilitiesPacket), {@code quit NAME}, {@code chat + * on|off} (whether the system chat each player receives is recorded too, as + * component JSON) and {@code end}, which writes each player's lines to OUT. */ public final class LocatorBarClient { private static volatile String step = "start"; + private static volatile boolean chat; private static final Map NAMES = new LinkedHashMap<>(); public static void main(String[] args) throws Exception { @@ -111,6 +113,7 @@ public final class LocatorBarClient { players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities)); } case "quit" -> players.get(w[1]).close(); + case "chat" -> chat = w[1].equals("on"); case "end" -> { try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) { for (Player p : players.values()) { @@ -268,7 +271,12 @@ public final class LocatorBarClient { Packet p = r.packet; if (p instanceof ClientboundTrackedWaypointPacket) { record(waypoint(r.payload)); - } else if (p instanceof ClientboundSystemChatPacket) { + } else if (p instanceof ClientboundSystemChatPacket c) { + if (chat) { + record("chat " + (c.overlay() ? "overlay " : "") + net.minecraft.network.chat.ComponentSerialization.CODEC + .encodeStart(ParityData.access.createSerializationContext(com.mojang.serialization.JsonOps.INSTANCE), c.content()) + .getOrThrow()); + } synchronized (replyLock) { replies++; replyLock.notifyAll(); diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index b1e7ec098..9ab2ddc3f 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -30,7 +30,8 @@ address. The script then checks, from both sides, what a guest's game used to ge tree (Tab), and runs what it completed; - recipes: the guest's recipe book lists the planks recipe the logs unlock, and clicking it places a log in the inventory's crafting grid (the server places it). -- feedback: the guest's command feedback names the guest and shows the numbers. +- feedback: the guest's command feedback names the guest and shows the numbers, and the host (an + operator) is told of it as `[Guest: ...]`. - clicks: with real mouse presses (not click-slot, which skips the screen's press and drag handling), the guest puts a stack into a chest and a furnace, drags planks across its crafting grid and gathers a stack with a double click; the server has each. @@ -688,6 +689,8 @@ def check_feedback(host, guest, out): guest('command', command=f'tp @s {x:.1f} {y:.1f} {z:.1f}') guest.wait(10) chat = guest.raw('messages', 5)['chat'] + # The host is an operator: broadcastToAdmins tells it, as `[Guest: ...]`. + host_chat = host.raw('messages', 5)['chat'] guest.raw('key', 't', 'tap') guest.wait(5) guest.shot(out, 'feedback') @@ -698,7 +701,9 @@ def check_feedback(host, guest, out): assert any(replaced in line for line in chat), f'no "{replaced}" in {chat}' assert any(line.startswith(f'Teleported {GUEST_NAME} to {x:.6f}') for line in chat), f'teleport feedback: {chat}' assert screen in (None, 'playing'), f'the chat did not close: {screen}' - return chat + told = f'[{GUEST_NAME}: Teleported {GUEST_NAME} to {x:.6f}' + assert any(line.startswith(told) for line in host_chat), f'no "{told}" for the host: {host_chat}' + return chat + host_chat def chat_input(game): diff --git a/tools/test_server_ops_feedback_parity.py b/tools/test_server_ops_feedback_parity.py new file mode 100644 index 000000000..2dd405517 --- /dev/null +++ b/tools/test_server_ops_feedback_parity.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Command feedback the operators get on a dedicated server (`[Server: ...]`), against vanilla's. + +CommandSourceStack.broadcastToAdmins: a command's success that vanilla sends with `broadcast` goes, gray and +italic as `[name: message]`, to every other operator while `send_command_feedback` is on, including the +console's own commands when `broadcast-console-to-ops` is (DedicatedServer.shouldInformAdmins). + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first, with the same server.properties and Alice and Bob as operators. LocatorBarClient +joins them both (players made of vanilla's protocol code) and records the system chat each receives while, step +by step, the console sets the time, asks for the difficulty (a query, told to nobody), whitelists a player, +deops Bob (who stops hearing of it) and ops him again, turns `send_command_feedback` off and on and saves, and +Alice runs a command herself (Bob hears of it as an operator). Then again with `broadcast-console-to-ops=false`. +Each player's chat in each step is compared exactly. +""" +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server + from .test_locator_bar_parity import generate_world, server_dir +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server + from test_locator_bar_parity import generate_world, server_dir + +NAME = 'server-ops-feedback-parity' +# (label, what the console does, what the players do). +BROADCAST = [ + ('console-time', ['time set 1000'], []), + ('console-query', ['difficulty'], []), + ('console-whitelist', ['whitelist add Alice'], []), + ('console-deop', ['deop Bob'], []), + ('bob-not-op', ['time set 2000'], []), + ('alice-bob-not-op', [], ['cmd Alice time set 3000']), + ('console-op', ['op Bob'], []), + ('alice-bob-op', [], ['cmd Alice time set 4000']), + ('feedback-off', ['gamerule send_command_feedback false'], []), + ('console-feedback-off', ['time set 5000'], []), + ('feedback-on', ['gamerule send_command_feedback true'], []), + ('console-save', ['save-all'], []), +] +QUIET = [ + ('console-time', ['time set 1000'], []), + ('console-whitelist', ['whitelist add Alice'], []), + ('alice', [], ['cmd Alice time set 3000']), +] +CASES = {'broadcast': ({}, BROADCAST), 'quiet': ({'broadcast-console-to-ops': 'false'}, QUIET)} +STEP_SECONDS = 2.0 + + +def run_side(label, case, server_command, client_command, build, world): + properties, script = CASES[case] + directory = build / label / case + directory.parent.mkdir(parents=True, exist_ok=True) + port = server_dir(directory, world) + with (directory / 'server.properties').open('a', encoding='iso-8859-1') as f: + f.writelines(f'{k}={v}\n' for k, v in properties.items()) + out = build / f'{label}-{case}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, f'{label}-{case}') + try: + server.wait_for(r'Done \(') + with (build / f'{label}-{case}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + + def tell(line): + client.stdin.write(line + '\n') + client.stdin.flush() + try: + for name in ('Alice', 'Bob'): + tell(f'join {name}') + server.wait_for(rf'{name} joined the game', timeout=120) + time.sleep(STEP_SECONDS) + tell('chat on') + for step, console, actions in script: + tell(f'step {step}') + for command in console: + server.console(command) + for action in actions: + tell(action) + time.sleep(STEP_SECONDS) + tell('end') + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label} {case}: the client wrote no transcript (see {build}/{label}-{case}-client.log)') + lines = out.read_text(encoding='utf-8').splitlines() + errors = [l for l in lines if ' error ' in f' {l} ' or 'disconnected' in l] + return [f'{case} {l}' for l in lines if l.split(' ', 2)[2:3] and l.split(' ', 2)[2].startswith('chat ')], errors + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = generate_world(vanilla_server, build) + sides = {'vanilla': ([], []), 'horizon': ([], [])} + for case in CASES: + for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]: + lines, errors = run_side(label, case, command, client, build, world) + sides[label][0].extend(lines) + sides[label][1].extend(errors) + (vanilla, vanilla_errors), (horizon, horizon_errors) = sides['vanilla'], sides['horizon'] + (build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8') + (build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8') + errors = vanilla_errors + horizon_errors + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if sum('[Server' in l or 'chat.type.admin' in l for l in vanilla) < 5: + raise AssertionError(f'vanilla sent the operators too little: the script did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='The system chat two operators get on a dedicated server while the console and one of them run ' + 'commands: broadcastToAdmins for the console (broadcast-console-to-ops on and off) and a player, ' + 'queries, deop and op, send_command_feedback off and on, and save-all.')) + print(f'server ops feedback: {len(vanilla)} chat lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') From c6531e4bdee77cfd85a9ae13508226e0487bb59d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:12:18 +0200 Subject: [PATCH 41/67] Server players finish vanilla's FULL square, not a 1.5x circle DistanceManager.PlayerTicketTracker gives every chunk within the server's view distance (Chebyshev) of a player a PLAYER_LOADING ticket at level 31, so FULL (33) reaches two chunks past it and FEATURES and TERRAIN one and two more. A server player's streaming area was a circle of radius (view distance + 2) * 3 / 2 + 1, finished one further: 2.1x the chunks at view distance 12. It is now that square, and lan status reports the server world's chunk counts. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/lan.rs | 18 ++++- crates/horizon-client/src/streaming.rs | 101 +++++++++++++++++++------ 2 files changed, 92 insertions(+), 27 deletions(-) diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 57c7b9ee0..acd3c4cc5 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -363,6 +363,12 @@ impl Game<'_> { "open": true, "host": h.host_name, "port": h.settings.port, + // The server's own world: its finished chunks and how many it + // has generated or loaded so far. + "world": { + "chunks": self.world.chunks.len(), + "generated": self.streaming.generated_total, + }, "guests": h.hub.guests.iter().map(|g| json!({ "name": g.name, "position": g.position, @@ -1063,14 +1069,20 @@ impl Game<'_> { /// Keep the chunks around every guest loaded. fn lan_update_streaming(&mut self, host: &LanHost) { + // DistanceManager.PlayerTicketTracker: every chunk within the server's + // view distance (Chebyshev) of a player holds a PLAYER_LOADING ticket + // at the entity-ticking level (31), whatever the player asked for; + // levels rise by one a chunk, so FULL (33) reaches two chunks past + // it. An extra area finishes its chunks out to its radius + 1. That + // covers what the player tracks (`ChunkTrackingView`, within its view + // distance + 1) and those chunks' lit neighbours. + let radius = host.hub.config.view_distance + 1; self.streaming.extra = host .hub .guests .iter() .filter(|g| host.hub.here(g)) - // The tracked square's corners and their lit neighbours lie - // within (view distance + 2) * √2 of the player's chunk. - .map(|g| (g.chunk(), (g.view_distance + 2) * 3 / 2 + 1)) + .map(|g| (g.chunk(), radius)) .collect(); } diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 7b3f3dd50..bff0a8fd6 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -200,8 +200,10 @@ pub struct Streaming { /// Chunks come from a server (`insert_remote`/`forget`): nothing is /// generated and columns are only unloaded when the server says so. pub remote: bool, - /// More areas to keep loaded (centre chunk, radius), e.g. around guests - /// of a world open to the network. They are generated and kept but not meshed. + /// More areas to keep loaded (centre chunk, radius), e.g. around the + /// players of a server. Each is a square, like vanilla's ticket levels: + /// chunks finish out to `radius + 1` (Chebyshev) and generation reaches + /// past that as around the player. They are generated and kept but not meshed. pub extra: Vec<((i32, i32), i32)>, /// Mesh the sections for drawing (false on a server thread). pub mesh: bool, @@ -788,20 +790,17 @@ impl Streaming { world.dirty_sections.retain(|k| !far.contains(&(k.0, k.2))); } // Drop far proto chunks that are not checked out. - let areas: Vec<((i32, i32), i64)> = - std::iter::once((self.center, i64::from(self.radius + 4))) - .chain(self.extra.iter().map(|&(c, r)| (c, i64::from(r + 4)))) - .collect(); - let forced = &self.forced; + let (center, r4) = (self.center, i64::from(self.radius + 4)); + let ranges = self.ranges(); let far: Vec<(i32, i32)> = self .protos .iter() .filter(|&(&(x, z), p)| { p.is_some() - && !areas.iter().any(|&((cx, cz), r4)| { - i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2) <= r4 * r4 - }) - && !forced + && i64::from(x - center.0).pow(2) + i64::from(z - center.1).pow(2) > r4 * r4 + && !ranges.in_extra(x, z, 4) + && !ranges + .forced .iter() .any(|f| (x - f.0).abs() <= 3 && (z - f.1).abs() <= 3) }) @@ -1277,21 +1276,21 @@ impl Ranges<'_> { /// circles, so a fixed margin alone misses the corners of those squares /// (render distance 2 never finished the chunks next to the player). fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool { - let near = |c: (i32, i32), radius: i32| { - let gap = |d: i32| i64::from((d.abs() - reach).max(0)); - let (dx, dz) = (gap(x - c.0), gap(z - c.1)); - let r = i64::from(radius + 1); - dx * dx + dz * dz <= r * r - }; - near(self.center, self.radius) || self.extra.iter().any(|&(c, r)| near(c, r)) + let gap = |d: i32| i64::from((d.abs() - reach).max(0)); + let (dx, dz) = (gap(x - self.center.0), gap(z - self.center.1)); + let r = i64::from(self.radius + 1); + // The extra areas are squares: their finished square grown by `reach`. + dx * dx + dz * dz <= r * r || self.in_extra(x, z, 1 + reach) } fn in_range(&self, x: i32, z: i32, margin: i32) -> bool { - self.in_main_range(x, z, margin) - || self.extra.iter().any(|&(c, radius)| { - let r = i64::from(radius + margin); - let (dx, dz) = (i64::from(x - c.0), i64::from(z - c.1)); - dx * dx + dz * dz <= r * r - }) + self.in_main_range(x, z, margin) || self.in_extra(x, z, margin) + } + /// Within `margin` of an extra area's square (Chebyshev distance, as + /// vanilla's ticket levels spread). + fn in_extra(&self, x: i32, z: i32, margin: i32) -> bool { + self.extra + .iter() + .any(|&(c, radius)| (x - c.0).abs().max((z - c.1).abs()) <= radius + margin) } /// The columns meshed for drawing: those vanilla draws around the /// player (`in_view_distance`), and forced ones. @@ -1778,4 +1777,58 @@ mod tests { } } } + + /// A server player's area (`lan_update_streaming`: the server view + /// distance + 1) finishes vanilla's FULL square: PLAYER_LOADING tickets at + /// level 31 out to the view distance, FULL (33) two chunks past it. Every + /// chunk the player tracks has its 3x3 neighbourhood finished, so it can + /// be lit and sent, and generation reaches vanilla's FEATURES (34) and + /// TERRAIN (35) rings. + #[test] + fn server_player_areas_finish_vanillas_full_square() { + let forced = HashSet::new(); + for view in 2..=32 { + let extra = [((40, -9), view + 1)]; + let r = Ranges { + // The window's own area, far away. + center: (-400, 300), + radius: 2, + extra: &extra, + forced: &forced, + }; + let level = |x: i32, z: i32| 31 + ((x - 40).abs().max((z + 9).abs()) - view).max(0); + for x in 40 - view - 8..=40 + view + 8 { + for z in -9 - view - 8..=-9 + view + 8 { + assert_eq!( + r.in_range(x, z, 1), + level(x, z) <= 33, + "view {view}: ({x}, {z})" + ); + assert_eq!( + r.requested(x, z, true), + level(x, z) <= 35, + "view {view}: ({x}, {z})" + ); + assert_eq!( + r.unloaded(x, z, true), + level(x, z) > 35, + "view {view}: ({x}, {z})" + ); + // ChunkTrackingView.contains, at any view distance the player may ask for. + let tracked = |v: i64| { + let gap = |d: i32| i64::from(((d).abs() - 2).max(0)); + gap(x - 40).pow(2) + gap(z + 9).pow(2) < v * v + }; + if tracked(i64::from(view)) { + for (dx, dz) in (-1..=1).flat_map(|dx| (-1..=1).map(move |dz| (dx, dz))) { + assert!( + r.in_range(x + dx, z + dz, 1), + "view {view}: ({x}, {z}) tracked, not lit" + ); + } + } + } + } + } + } } From 65d5a35cd22aa723695b0885a5ecd7d5e3a4b710 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:40:57 +0200 Subject: [PATCH 42/67] Server: a dead guest is sent its health of 0, as ServerPlayer.doTick does The hub held SetHealth back once a guest died, so the guest's health stayed at its last positive value under the death screen. ServerPlayer sends it every tick it changes, dead or not, and only when the health or the food level changes or the saturation reaches or leaves zero: the hub sent it on every change of saturation too. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/lan/guest.rs | 3 ++- crates/horizon-client/src/lan/hub.rs | 18 ++++++++---------- tools/lan_e2e.py | 5 ++++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index ea12c2edb..780be4a33 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -137,7 +137,8 @@ pub struct Guest { /// What the client believes the inventory menu holds (for change detection). pub menu_sent: Vec>, pub menu_state: i32, - pub sent_health: Option<(f32, i32, f32)>, + /// `lastSentHealth`, `lastSentFood` and `lastFoodSaturationZero`. + pub sent_health: Option<(f32, i32, bool)>, pub sent_xp: Option<(f32, i32, i32)>, /// Where the player last died (Player.lastDeathLocation), sent with the respawn. pub last_death: Option<[i32; 3]>, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index b18187835..845f094eb 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1585,17 +1585,15 @@ impl Hub { self.flush_advancements(i, true); } } - let health = { - let g = &self.guests[i]; - (g.health, g.food, g.saturation) - }; - // A dead player's health isn't sent (the death screen shows from the combat kill). - if self.guests[i].sent_health != Some(health) && !self.guests[i].dead { - let g = &mut self.guests[i]; + // Sent when the health or the food level changes, or the saturation + // reaches or leaves zero; a dead player's health of 0 too. + let g = &mut self.guests[i]; + let health = (g.health, g.food, g.saturation == 0.0); + if g.sent_health != Some(health) { g.send(&C::SetHealth(play::SetHealth { - health: health.0, - food: health.1, - saturation: health.2, + health: g.health, + food: g.food, + saturation: g.saturation, })); g.sent_health = Some(health); } diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index b1e7ec098..790778968 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -454,6 +454,9 @@ def check_hazards(host, guest, out): # A fresh life: full health, so no regeneration eats food while the guest runs. guest('command', command='kill @s') guest.wait(20) + # ServerPlayer.doTick sends the dead player's health of 0 too. + died = {'screen': guest('state').get('screen'), 'health': remote(guest)['health']} + assert died == {'screen': 'dead', 'health': 0.0}, f'the guest was killed: {died}' guest.raw('respawn') guest.wait(40) # Sprint-jumping down a stone lane: ServerPlayer.jumpFromGround and checkMovementStatistics. @@ -506,7 +509,7 @@ def check_hazards(host, guest, out): assert spent > 1.0, f'no hunger from moving: {hunger} ({spent:.2f} spent)' for name, (before, after) in hurt.items(): assert after < before, f'{name} never hurt the guest: health {before} -> {after} (all: {hurt})' - return {'hunger': hunger, **hurt} + return {'died': died, 'hunger': hunger, **hurt} def check_pvp(host, guest, out): From c12f5cf41adfc96551da6715b2f086203e8d7a72 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:46:30 +0200 Subject: [PATCH 43/67] Server titles: /title reaches the guest, and its HUD shows and clears them TitleCommand sends the targeted player ClientboundSetTitleText, SetSubtitleText, SetActionBarText, SetTitlesAnimation and ClearTitles. A guest's /title (the window's own player on the server thread too) only changed the server's HUD; it is now sent to the guest. The remote client showed titles and action bars as a bare line and ignored subtitles, times and clearing; it now feeds the HUD's titles as ClientPacketListener does (the action bar of a SystemChat overlay too) and ticks them (Gui.tick). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 3 ++ .../src/game/commands/text_commands.rs | 43 +++++++++++++---- crates/horizon-client/src/game/lan_actions.rs | 3 ++ crates/horizon-client/src/game/remote.rs | 46 +++++++++---------- tools/lan_e2e.py | 21 ++++++++- 5 files changed, 80 insertions(+), 36 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 38edb3605..789479710 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1152,6 +1152,8 @@ struct Game<'a> { lan_acting_uuid: Option, /// Command feedback to forward to a guest. chat_capture: Option>, + /// Packets for the guest acting now, sent once its action returns. + guest_packets: Vec, /// `broadcastToAdmins` lines a guest's command queued, sent once it /// returns: the line, whether operators get it and whether the console /// logs it. @@ -10374,6 +10376,7 @@ impl<'a> Game<'a> { lan_entity_events: Vec::new(), lid_openers: HashMap::new(), lan_acting: None, + guest_packets: Vec::new(), lan_acting_name: None, lan_acting_uuid: None, chat_capture: None, diff --git a/crates/horizon-client/src/game/commands/text_commands.rs b/crates/horizon-client/src/game/commands/text_commands.rs index da8485849..dead07d71 100644 --- a/crates/horizon-client/src/game/commands/text_commands.rs +++ b/crates/horizon-client/src/game/commands/text_commands.rs @@ -1,6 +1,7 @@ //! `/tellraw` (TellRawCommand) and `/title` (TitleCommand). use super::{Arg, Context, Game, R, Target, err}; -use horizon_client::text::Text; +use horizon_client::{hud_overlays::Titles, lan::data::component_nbt, text::Text}; +use horizon_protocol::packets::play::{self, Clientbound as C}; impl Game<'_> { /// ComponentArgument with selectors resolved for the source @@ -153,30 +154,45 @@ impl Game<'_> { let name = self.target_name(Target::Player); let single = n == 1; let key = if ctx.has("clear") { - self.titles.clear(); + self.titles_or_packet(Titles::clear, C::ClearTitles(play::Bool1 { value: false })); "cleared" } else if ctx.has("reset") { - self.titles.clear(); - self.titles.reset_times(); + self.titles_or_packet( + |t| { + t.clear(); + t.reset_times(); + }, + C::ClearTitles(play::Bool1 { value: true }), + ); "reset" } else if ctx.has("times") { let get = |k: &str| match ctx.get(k) { Some(Arg::Int(v)) => *v, _ => -1, }; - self.titles - .set_times(get("fadeIn"), get("stay"), get("fadeOut")); + let (fade_in, stay, fade_out) = (get("fadeIn"), get("stay"), get("fadeOut")); + self.titles_or_packet( + |t| t.set_times(fade_in, stay, fade_out), + C::SetTitlesAnimation(play::TitlesAnimation { + fade_in, + stay, + fade_out, + }), + ); "times" } else { let text = self.component_arg(ctx, "title")?; + let sent = play::Text { + text: component_nbt(&text.component()), + }; if ctx.has("subtitle") { - self.titles.set_subtitle(text); + self.titles_or_packet(|t| t.set_subtitle(text), C::SetSubtitleText(sent)); "show.subtitle" } else if ctx.has("actionbar") { - self.titles.set_overlay(text, false); + self.titles_or_packet(|t| t.set_overlay(text, false), C::SetActionBarText(sent)); "show.actionbar" } else { - self.titles.set_title(text); + self.titles_or_packet(|t| t.set_title(text), C::SetTitleText(sent)); "show.title" } }; @@ -191,4 +207,13 @@ impl Game<'_> { self.feedback(message); Ok(n as i32) } + /// The player the title is for: the guest acting now is sent the packet + /// (TitleCommand's `connection.send`), the window's own player shows it. + fn titles_or_packet(&mut self, local: impl FnOnce(&mut Titles), packet: C) { + if self.lan_acting.is_some() { + self.guest_packets.push(packet); + } else { + local(&mut self.titles); + } + } } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 68b29a360..9c0f0ec88 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -203,6 +203,9 @@ impl Game<'_> { self.lan_acting_uuid = Some(g.uuid); let result = f(self); + for packet in std::mem::take(&mut self.guest_packets) { + host.hub.send_to(conn, &packet); + } self.lan_acting = None; self.lan_acting_name = None; self.lan_acting_uuid = None; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 991bc42b2..ef7fad329 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -40,13 +40,15 @@ use horizon_protocol::{ text::{Language, format_java, plain}, }; use horizon_world::{nbt::Tag, registry::BlockStateId}; -use std::{ - collections::HashMap, - sync::Arc, - time::{Duration, Instant}, -}; +use std::{collections::HashMap, sync::Arc, time::Instant}; use winit::{event::MouseButton, keyboard::KeyCode}; +/// A text component the server sent, for the HUD. +fn remote_text(tag: &Tag) -> horizon_client::text::Text { + use horizon_client::scoreboard::{comp_text, tag_to_json}; + comp_text(&tag_to_json(tag)) +} + /// Streaming needs a generator; a server world never generates. struct NoTerrain; impl ChunkGenerator for NoTerrain { @@ -120,7 +122,6 @@ pub(super) struct Remote { pub chat: Vec<(String, Instant)>, /// The server's command tree (ClientboundCommandsPacket). pub commands: Option, - action_bar: Option<(String, Instant)>, players: HashMap, /// `ClientPacketListener.playerInfoMap` as player-list entries, and /// which of them are listed. @@ -306,7 +307,6 @@ impl Game<'_> { game_mode: 0, chat: Vec::new(), commands: None, - action_bar: None, players: HashMap::new(), tab_players: HashMap::new(), skin_textures: HashMap::new(), @@ -1659,10 +1659,9 @@ impl Game<'_> { .map(|r| r.lang.clone()) .unwrap_or_default(), ); + // ChatListener.handleOverlay. if p.overlay { - if let Some(r) = self.remote.as_mut() { - r.action_bar = Some((text, Instant::now())); - } + self.titles.set_overlay(remote_text(&p.content), false); } else { self.chat_line(text); } @@ -1893,15 +1892,16 @@ impl Game<'_> { self.stats.values.entry(ty).or_default().insert(key, count); } } - C::SetActionBarText(p) | C::SetTitleText(p) => { - let lang = self - .remote - .as_ref() - .map(|r| r.lang.clone()) - .unwrap_or_default(); - let text = plain(&p.text, &lang); - if let Some(r) = self.remote.as_mut() { - r.action_bar = Some((text, Instant::now())); + // ClientPacketListener.setActionBarText, setTitleText, + // setSubtitleText, setTitlesAnimation and handleTitlesClear. + C::SetActionBarText(p) => self.titles.set_overlay(remote_text(&p.text), false), + C::SetTitleText(p) => self.titles.set_title(remote_text(&p.text)), + C::SetSubtitleText(p) => self.titles.set_subtitle(remote_text(&p.text)), + C::SetTitlesAnimation(p) => self.titles.set_times(p.fade_in, p.stay, p.fade_out), + C::ClearTitles(p) => { + self.titles.clear(); + if p.value { + self.titles.reset_times(); } } _ => {} @@ -2252,6 +2252,8 @@ impl Game<'_> { self.tick_client_effects(); // Player.tick's sleep counter (the darkening and the wake-up fade). self.sleep_timer = super::tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); + // Gui.tick: the titles, the action bar and the held item's name. + self.tick_hud(); let ghast_collisions = self.happy_ghast_player_collisions(); let pushed = self.remote_pushes(); let Some(player) = self.player.as_mut() else { @@ -3507,12 +3509,6 @@ impl Game<'_> { batch.text(atlas, text, 4.0, y, rgba(255, 255, 255, a), true); y -= 9.0; } - if let Some((text, at)) = &r.action_bar - && now.duration_since(*at) < Duration::from_secs(3) - { - let tw = HudBatch::text_width(atlas, text); - batch.text(atlas, text, (w - tw) / 2.0, h - 68.0, u32::MAX, true); - } if self.screen == Screen::Dead && let Some(message) = &r.death_message { diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index b1e7ec098..1532d1cf5 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -74,7 +74,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'effects', 'portals'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'effects', 'portals'] class Game: @@ -775,6 +775,22 @@ def check_recipes(host, guest, out): return {'known': known, 'grid': grid} +def check_titles(host, guest, out): + # TitleCommand sends the guest its title packets; its HUD shows and clears them. + for command in ('title @s times 10 400 10', 'title @s subtitle "Under"', 'title @s title "Over"', + 'title @s actionbar "Bar"'): + guest('command', command=command) + guest.wait(5) + shown = {k: guest.raw('messages', 1)[k] for k in ('title', 'subtitle', 'actionbar')} + guest.shot(out, 'titles') + guest('command', command='title @s clear') + guest.wait(5) + cleared = {k: guest.raw('messages', 1)[k] for k in ('title', 'subtitle')} + assert shown == {'title': 'Over', 'subtitle': 'Under', 'actionbar': 'Bar'}, f'titles shown: {shown}' + assert cleared == {'title': None, 'subtitle': None}, f'titles cleared: {cleared}' + return {'shown': shown, 'cleared': cleared} + + def png_rows(path): """The rows of an 8-bit RGB or RGBA PNG, unfiltered, as bytes of 4 (or 3) per pixel.""" data = Path(path).read_bytes() @@ -1047,7 +1063,8 @@ def check_xp(host, guest, out): CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'sign': check_sign, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, - 'containers': check_containers, 'feedback': check_feedback, 'commands': check_commands, + 'containers': check_containers, 'feedback': check_feedback, 'titles': check_titles, + 'commands': check_commands, 'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls, 'effects': check_effects, 'portals': check_portals, 'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push, 'eat': check_eat, From d370364f1235e3828b2cd1c328628c34bbe8aa38 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 18 Oct 2026 02:44:11 +0100 Subject: [PATCH 44/67] Specialize constant material rule branches after binding --- .../src/worldgen/material_condition.rs | 8 + .../src/worldgen/material_condition/rule.rs | 20 +- .../tests/material_rule_specialization.rs | 172 ++++++++++++++++++ 3 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 crates/horizon-world/tests/material_rule_specialization.rs diff --git a/crates/horizon-world/src/worldgen/material_condition.rs b/crates/horizon-world/src/worldgen/material_condition.rs index 30eb26ec8..6433b09a4 100644 --- a/crates/horizon-world/src/worldgen/material_condition.rs +++ b/crates/horizon-world/src/worldgen/material_condition.rs @@ -300,6 +300,14 @@ enum Kind { Not(Box>), } impl Compiled { + fn constant_result(&self) -> Option { + match &self.kind { + Kind::Constant(value) => Some(*value), + Kind::Not(child) => child.constant_result().map(|value| !value), + _ => None, + } + } + fn compile( condition: MaterialCondition, root: &R::Positional, diff --git a/crates/horizon-world/src/worldgen/material_condition/rule.rs b/crates/horizon-world/src/worldgen/material_condition/rule.rs index 9dfc2ff1e..cf4fc8547 100644 --- a/crates/horizon-world/src/worldgen/material_condition/rule.rs +++ b/crates/horizon-world/src/worldgen/material_condition/rule.rs @@ -70,6 +70,7 @@ impl From for MaterialRuleError { } pub(super) enum CompiledRule { + Empty, Block(BlockStateId), OreVein { blocks: [BlockStateId; 3], @@ -127,7 +128,12 @@ impl CompiledRule { // whether the predicate will match any subsequent position. let condition = Compiled::compile(if_true, root, context, depth + 1)?; let rule = Box::new(Self::compile(*then_run, root, context, depth + 1, nodes)?); - Self::Condition { condition, rule } + // Compile both sides first: binding and errors must retain their order. + match condition.constant_result() { + Some(false) => Self::Empty, + Some(true) => *rule, + None => Self::Condition { condition, rule }, + } } MaterialRule::Sequence(mut children) => { if children.len() == 1 { @@ -135,9 +141,16 @@ impl CompiledRule { } let mut result = Vec::with_capacity(children.len()); for child in children { - result.push(Self::compile(child, root, context, depth + 1, nodes)?); + let child = Self::compile(child, root, context, depth + 1, nodes)?; + if !matches!(child, Self::Empty) { + result.push(child); + } + } + match result.len() { + 0 => Self::Empty, + 1 => result.pop().unwrap(), + _ => Self::Sequence(result), } - Self::Sequence(result) } }) } @@ -147,6 +160,7 @@ impl CompiledRule { context: &mut Context, ) -> Result, MaterialRuleError> { match self { + Self::Empty => Ok(None), Self::Block(block) => Ok(Some(*block)), Self::OreVein { blocks, diff --git a/crates/horizon-world/tests/material_rule_specialization.rs b/crates/horizon-world/tests/material_rule_specialization.rs new file mode 100644 index 000000000..e953e908e --- /dev/null +++ b/crates/horizon-world/tests/material_rule_specialization.rs @@ -0,0 +1,172 @@ +use horizon_core::{ + density::{ConstantSampler, DensitySampler}, + position::BlockPos, + random::{RandomSource, XoroshiroRandom}, +}; +use horizon_world::{ + registry::BlockStateId, + worldgen::{ + DecodeError, + anchor::WorldGenerationContext, + material_condition::{ + MaterialCondition, MaterialConditions, MaterialRule, MaterialRuleError, + MaterialSurfaceQueries, + }, + }, +}; +use std::{collections::BTreeSet, sync::Arc}; + +#[derive(Default)] +struct Queries { + bindings: Vec, + temperatures: usize, + biomes: usize, + fail_binding: bool, + fail_temperature: bool, +} +impl MaterialSurfaceQueries for Queries { + fn possible_biomes(&self) -> Option> { + Some(BTreeSet::from(["minecraft:plains".into()])) + } + fn surface_depth(&mut self, _: i32, _: i32) -> i32 { + 1 + } + fn surface_secondary(&mut self, _: i32, _: i32) -> f64 { + 0. + } + fn bind_density( + &mut self, + _: Arc, + eager: bool, + ) -> Result { + self.bindings.push(eager); + if self.fail_binding { + return Err(DecodeError::Field("test binding failure")); + } + Ok(self.bindings.len() - 1) + } + fn biome(&mut self, _: BlockPos) -> Result>, MaterialRuleError> { + self.biomes += 1; + Ok(Some(Arc::from("minecraft:plains"))) + } + fn cold_enough_to_snow( + &mut self, + _: &str, + _: BlockPos, + _: i32, + ) -> Result { + self.temperatures += 1; + if self.fail_temperature { + Err(MaterialRuleError::BiomeQueryFailed) + } else { + Ok(true) + } + } +} +fn worker(surface: Queries) -> MaterialConditions { + MaterialConditions::new( + WorldGenerationContext::new(-64, 384, 63), + XoroshiroRandom::new(7).fork_positional(), + surface, + ) +} +fn condition(if_true: MaterialCondition, then_run: MaterialRule) -> MaterialRule { + MaterialRule::Condition { + if_true, + then_run: Box::new(then_run), + } +} +fn biome(name: &str) -> MaterialCondition { + MaterialCondition::Biome(vec![name.into()]) +} +fn ore() -> MaterialRule { + MaterialRule::OreVein { + ore: BlockStateId(1), + raw_ore: BlockStateId(2), + filler: BlockStateId(3), + raw_ore_chance: 0.02, + density: Arc::new(ConstantSampler(1.)), + richness: Arc::new(ConstantSampler(1.)), + filler_gap: Arc::new(ConstantSampler(-1.)), + } +} +#[test] +fn constant_branches_keep_binding_order_and_short_circuit_queries() { + let mut w = worker(Queries::default()); + let rule = w + .add_rule(MaterialRule::Sequence(vec![ + condition(biome("minecraft:desert"), ore()), + condition( + MaterialCondition::Not(Box::new(biome("minecraft:desert"))), + MaterialRule::Block(BlockStateId(9)), + ), + ore(), + ])) + .unwrap(); + w.update_xz(4, 5, 0, 0); + w.update_y(1, 1, i32::MIN, 64); + for _ in 0..2 { + assert_eq!( + w.try_apply(rule, BlockPos::new(4, 64, 5)), + Ok(Some(BlockStateId(9))) + ); + } + let q = w.into_surface(); + assert_eq!(q.bindings, [true, true, false, true, true, false]); + assert_eq!(q.biomes, 0); + assert_eq!(q.temperatures, 0); +} +#[test] +fn unreachable_children_still_fail_compilation_without_adding_a_rule() { + for preceding_block in [false, true] { + let mut w = worker(Queries { + fail_binding: true, + ..Queries::default() + }); + let dead = condition(biome("minecraft:desert"), ore()); + let rule = if preceding_block { + MaterialRule::Sequence(vec![MaterialRule::Block(BlockStateId(9)), dead]) + } else { + dead + }; + assert!(w.add_rule(rule).is_err()); + assert_eq!( + w.try_apply(0, BlockPos::new(0, 0, 0)), + Err(MaterialRuleError::UnknownRule) + ); + assert_eq!(w.into_surface().bindings, [true]); + } +} +#[test] +fn dynamic_predicate_on_empty_child_retains_each_temperature_call_and_error() { + for fail in [false, true] { + let mut w = worker(Queries { + fail_temperature: fail, + ..Queries::default() + }); + let rule = w + .add_rule(condition( + MaterialCondition::Temperature, + condition( + biome("minecraft:desert"), + MaterialRule::Block(BlockStateId(9)), + ), + )) + .unwrap(); + w.update_xz(0, 0, 0, 0); + w.update_y(1, 1, i32::MIN, 64); + for _ in 0..2 { + assert_eq!( + w.try_apply(rule, BlockPos::new(0, 64, 0)), + if fail { + Err(MaterialRuleError::BiomeQueryFailed) + } else { + Ok(None) + } + ); + } + let q = w.into_surface(); + assert_eq!(q.temperatures, 2); + assert_eq!(q.biomes, 1); + } +} From 4cfa6a507cd2ebdaa4f05a9ffbb094c7871d4c94 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 18 Oct 2026 03:11:34 +0100 Subject: [PATCH 45/67] Document incremental surface optimization measurements --- docs/surface-generation-benchmark.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 docs/surface-generation-benchmark.md diff --git a/docs/surface-generation-benchmark.md b/docs/surface-generation-benchmark.md new file mode 100644 index 000000000..e40849c19 --- /dev/null +++ b/docs/surface-generation-benchmark.md @@ -0,0 +1,15 @@ +# Constant surface-rule specialization benchmark + +The upstream PR contains only the surface change and tests, reapplied to upstream main. The measurements below were collected with #796 present; they are historical incremental measurements, not new timings for the rebased upstream head. + +This surface-only change is based on the bulk unary density optimization in [upstream PR #796](https://github.com/ludovic111/horizon/pull/796). Measurements compare against `eb3789038e3184c98a1459fe638947200217d360`, which already includes that optimization; its savings are not counted again. + +With one worker, primary initialized Overworld terrain time improved from 10.664 to 10.298 ms/input chunk (3.43%). Terrain plus decoration improved from 12.645 to 12.242 (3.19%). With four workers, terrain improved 2.48% and combined time 2.07%. Nether terrain improved 4.16% / 3.93% with one/four workers; combined improved 3.76% / 3.63%. The second Overworld site improved terrain 2.53% / 2.81%. End improved 2.95% with one worker but regressed 2.93% with four, with substantial paired noise. This meets the revised 3% median gate on selected representative cases, not the original 5% gate or every workload. + +Cases were declared before editing. Each used one discarded pair followed by five alternating baseline/candidate pairs, with identical work and builds. The primary case used seed 12345, center (0,0), 256 terrain chunks and 196 decorated interior chunks, at one/four workers across all three dimensions. The second Overworld case used seed -1 at (32,-32). An offset 9×9 generation pass initialized the same seeded generator without pre-generating the measured region. Wall time is elapsed/input count; combined time includes the complete decoration phase and required borders. Cold startup was measured separately. + +Machine: AMD Ryzen 5 5600X, 32 GiB RAM, Linux, Rust 1.99.0. Optimized `fast` profile: release optimization, LTO off, 16 codegen units. Runs and builds were sequential; background desktop activity was not isolated. Memory use was effectively unchanged. The benchmark used the existing chunkgen benchmark with identical optional initialization and exact timing instrumentation on both revisions. `crates/horizon-world/examples/chunkgen_bench.rs` is the standard workload entry point; its thread-multiplied human-readable ms/chunk display differs from actual elapsed/count. + +Validation passed: 102 targeted Rust tests; 162 world-crate tests; 24 exact complete-field baseline/candidate terrain and decorated comparisons across three dimensions, two sites/seeds, and one/four workers; and six fresh pinned Java 26.3 client/server suites covering material stages, rule library, conditions, temperature, extensions, and surface generation. Differential snapshots included block states/palettes, quart biomes, binary entities/block entities, scheduled ticks, all six heightmaps, ordered postprocessing, non-air counts, and chunk metadata. Compilation/binding errors, dynamic queries, and short-circuit behavior have regression coverage. The final production executable matched the measured candidate byte-for-byte. + +The repository quick PR check also passed public-tree validation, Python tests, formatting, clippy, and world tests. Validation is scoped to the declared corpora and does not establish complete gameplay parity. Generated timing files, diagnostic runners, patches, and validation logs are retained locally outside the repository and are not included in this PR. From e8eaaf59d79332a5861f1410e17c4e0e17791a99 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 04:35:23 +0200 Subject: [PATCH 46/67] Rerun CI (flaky options_ui test, #802) Co-Authored-By: Claude Opus 5.5 From 0f482a45c868a8e2cab8803e4fcc8e01411edd8c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 04:01:54 +0200 Subject: [PATCH 47/67] Dedicated server: never mesh sections A dedicated server draws nothing, like the integrated server, yet its streaming still meshed every section it lit (Streaming::mesh followed only options.integrated). It now skips meshing too. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index fd47d2986..8366c5ec9 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6566,8 +6566,9 @@ impl Game<'_> { streaming.biome_zoom = BiomeZoom::new(obfuscate_seed(seed)); streaming.sky_light = dim != Dim::Nether; streaming.nether_shading = dim == Dim::Nether; - // The integrated server draws nothing (the dedicated server keeps its behaviour). - streaming.mesh = !self.options.integrated; + // A server draws nothing: neither the integrated server nor the + // dedicated one meshes sections. + streaming.mesh = !self.options.integrated && self.options.dedicated.is_none(); Ok((streaming, save)) } /// Change dimension through a portal (Entity.changeDimension with From ff6e01de03f5d3df0bb51b2bdc945955fbd01aca Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:04:14 +0200 Subject: [PATCH 48/67] Command suggestions: ask the server for nodes the tree marks ask_server A remote client's chat now sends ServerboundCommandSuggestionPacket for an argument node whose provider is minecraft:ask_server (bossbar ids, data storage, datapacks, team names, functions...), as ClientSuggestionProvider.customSuggestion does. The suggestions are done when ClientboundCommandSuggestionsPacket answers that id; its completions join the local ones (Suggestions.merge). Replies to older requests are dropped. The remote tree takes each node's provider from the packet only: a local provider the server didn't send stood in for the server's before. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/chat.rs | 88 ++++++++++++++++++-- crates/horizon-client/src/chat_tests.rs | 55 ++++++++++++ crates/horizon-client/src/command/mod.rs | 27 +++++- crates/horizon-client/src/command/network.rs | 11 ++- crates/horizon-client/src/game/chat.rs | 17 ++++ crates/horizon-client/src/game/remote.rs | 55 ++++++++++++ tools/lan_e2e.py | 20 ++++- 7 files changed, 257 insertions(+), 16 deletions(-) diff --git a/crates/horizon-client/src/chat.rs b/crates/horizon-client/src/chat.rs index 6156a041d..7c51a40f1 100644 --- a/crates/horizon-client/src/chat.rs +++ b/crates/horizon-client/src/chat.rs @@ -386,6 +386,12 @@ pub struct ChatScreen { parse: Option, /// The completions at the cursor (`pendingSuggestions`): range start (bytes) and list. pending: Option<(usize, Vec)>, + /// Completions waiting for the server's (an `ask_server` node at the + /// cursor): `pendingSuggestions` is not done until they come. + awaiting: Option, + /// The request to send (`ServerboundCommandSuggestionPacket`): id and + /// the command up to the cursor. + request: Option<(i32, String)>, suggestions: Option, usage: Vec, usage_position: i32, @@ -394,6 +400,16 @@ pub struct ChatScreen { keep_suggestions: bool, pub auto_suggestions: bool, } +/// Local completions waiting for the server's reply to request `id`. +struct Awaiting { + id: i32, + /// The command up to the cursor, as sent. + truncated: String, + local: Vec, +} +/// `ClientSuggestionProvider.pendingSuggestionsId`: one counter for the +/// connection, so a reply to an older request never matches. +static SUGGESTION_ID: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0); /// `CommandSuggestions`' fill colour. const FILL: i32 = -805306368; fn byte_at(s: &str, chars: usize) -> usize { @@ -442,6 +458,8 @@ impl ChatScreen { history_pos: history_len, parse: None, pending: None, + awaiting: None, + request: None, suggestions: None, usage: Vec::new(), usage_position: 0, @@ -518,24 +536,84 @@ impl ChatScreen { } if cursor >= 1 && (self.suggestions.is_none() || !self.keep_suggestions) { let parse = self.parse.as_ref().expect("parsed above"); - let (range, list) = c - .dispatcher - .client_suggestions(parse, cursor_b, c.registries); - self.pending = Some((range.0, list)); - self.update_usage_info(m, c); + let (range, list, ask) = + c.dispatcher + .client_suggestions_asking(parse, cursor_b, c.registries); + if ask { + // ClientSuggestionProvider.customSuggestion: the + // suggestions are done when the server answers. + let id = SUGGESTION_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + let truncated = command[..cursor_b].to_owned(); + self.request = Some((id, truncated.clone())); + self.awaiting = Some(Awaiting { + id, + truncated, + local: list, + }); + self.pending = None; + } else { + self.awaiting = None; + self.pending = Some((range.0, list)); + self.update_usage_info(m, c); + } } } else if !command.chars().all(char::is_whitespace) { let partial = &command[..cursor_b]; let mut builder = SuggestionsBuilder::new(partial, last_word_index(partial)); builder.suggest_matching(c.registries.players.iter().map(String::as_str)); let (range, list) = merge_suggestions(partial, builder.out); + self.awaiting = None; self.pending = Some((range.0, list)); self.usage_width = 0; self.usage_position = 0; } else { + self.awaiting = None; self.pending = None; } } + /// The completion request to send to the server, if one is waiting. + pub fn take_request(&mut self) -> Option<(i32, String)> { + self.request.take() + } + /// `ClientSuggestionProvider.completeCustomSuggestions`: the server's + /// completions for request `id`, with `range` in UTF-16 units of the + /// command sent. They join the local ones (`Suggestions.merge`) and the + /// pending suggestions are done. + pub fn server_suggestions( + &mut self, + m: &Measure, + c: &Commands, + id: i32, + range: (usize, usize), + list: Vec<(String, Option)>, + ) { + let Some(awaiting) = self.awaiting.take_if(|a| a.id == id) else { + return; + }; + let input = &awaiting.truncated; + let byte = |units: usize| { + let mut n = 0; + input + .char_indices() + .find(|(_, ch)| { + let past = n >= units; + n += ch.len_utf16(); + past + }) + .map_or(input.len(), |(i, _)| i) + }; + let range = (byte(range.0), byte(range.0 + range.1)); + let mut all = awaiting.local; + all.extend(list.into_iter().map(|(text, tooltip)| Suggestion { + range, + text, + tooltip, + int_value: None, + })); + let (range, list) = merge_suggestions(input, all); + self.pending = Some((range.0, list)); + self.update_usage_info(m, c); + } /// `CommandSuggestions.updateUsageInfo`. fn update_usage_info(&mut self, m: &Measure, c: &Commands) { let Some(parse) = &self.parse else { return }; diff --git a/crates/horizon-client/src/chat_tests.rs b/crates/horizon-client/src/chat_tests.rs index 3cbac071b..e1bf2feda 100644 --- a/crates/horizon-client/src/chat_tests.rs +++ b/crates/horizon-client/src/chat_tests.rs @@ -230,3 +230,58 @@ fn mod_commands_are_coloured_and_complete_like_vanilla_ones() { screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false); assert_eq!(screen.value(), "/coords"); } + +#[test] +fn ask_server_nodes_wait_for_the_servers_completions() { + // ClientSuggestionProvider.customSuggestion: an `ask_server` node at the + // cursor sends the command so far, and the list is done with the reply. + let font = font(); + let m = Measure(&font); + let registries = Registries { + permission: 4, + ..Registries::default() + }; + let mut d = crate::command::vanilla::game_dispatcher().clone(); + let child = |d: &crate::command::Dispatcher, n: usize, name: &str| { + d.nodes[n] + .children + .iter() + .copied() + .find(|&c| d.nodes[c].name() == name) + .expect(name) + }; + let id = child(&d, child(&d, child(&d, 0, "bossbar"), "get"), "id"); + d.nodes[id].suggests = Some(crate::command::Suggests::AskServer); + let c = Commands { + dispatcher: &d, + registries: ®istries, + }; + let tab = |screen: &mut ChatScreen| { + screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false); + }; + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360); + screen.type_text(&m, &c, "/bossbar get pt"); + let (request, sent) = screen.take_request().expect("the server is asked"); + assert_eq!(sent, "/bossbar get pt"); + assert_eq!(screen.take_request(), None); + // Nothing to complete until the reply. + tab(&mut screen); + assert_eq!(screen.value(), "/bossbar get pt"); + // A reply to an older request is dropped; the range is in UTF-16 units. + let reply = |text: &str| vec![(text.to_owned(), None)]; + screen.server_suggestions(&m, &c, request - 1, (13, 2), reply("pt6:old")); + tab(&mut screen); + assert_eq!(screen.value(), "/bossbar get pt"); + screen.server_suggestions(&m, &c, request, (13, 2), reply("pt6:check")); + tab(&mut screen); + assert_eq!(screen.value(), "/bossbar get pt6:check"); + // Without the server's provider the local list is done at once. + let local = crate::command::vanilla::game_dispatcher(); + let c = Commands { + dispatcher: local, + registries: ®istries, + }; + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360); + screen.type_text(&m, &c, "/bossbar get pt"); + assert_eq!(screen.take_request(), None); +} diff --git a/crates/horizon-client/src/command/mod.rs b/crates/horizon-client/src/command/mod.rs index 8fcb30830..e9aeaae51 100644 --- a/crates/horizon-client/src/command/mod.rs +++ b/crates/horizon-client/src/command/mod.rs @@ -312,6 +312,9 @@ pub enum Suggests { /// ScoreboardCommand.suggestTriggers: trigger objectives some of the /// parsed `targets` have no score in, or a locked one. EnableTriggers, + /// SuggestionProviders.ASK_SERVER: the server completes the node + /// (ClientSuggestionProvider.customSuggestion); nothing locally. + AskServer, } impl Node { pub fn name(&self) -> &str { @@ -691,7 +694,7 @@ impl Suggests { Suggests::Triggers => { builder.suggest_matching(reg.trigger_objectives.iter().map(String::as_str)) } - Suggests::Nothing => {} + Suggests::Nothing | Suggests::AskServer => {} } } } @@ -955,7 +958,8 @@ impl Dispatcher { cursor: usize, reg: &Registries, ) -> (Range, Vec) { - self.suggestions_within(parse, cursor, reg, u8::MAX) + let (range, list, _) = self.suggestions_within(parse, cursor, reg, u8::MAX); + (range, list) } /// The suggestions the player's chat shows: the client's tree only holds /// the nodes the player may use (Commands.sendCommands prunes by canUse). @@ -965,6 +969,18 @@ impl Dispatcher { cursor: usize, reg: &Registries, ) -> (Range, Vec) { + let (range, list, _) = self.suggestions_within(parse, cursor, reg, reg.permission); + (range, list) + } + /// [`Self::client_suggestions`], and whether a node at the cursor asks + /// the server (`ask_server`): its completions come with the server's + /// reply, to be merged with these. + pub fn client_suggestions_asking( + &self, + parse: &ParseResults, + cursor: usize, + reg: &Registries, + ) -> (Range, Vec, bool) { self.suggestions_within(parse, cursor, reg, reg.permission) } fn suggestions_within( @@ -973,15 +989,17 @@ impl Dispatcher { cursor: usize, reg: &Registries, permission: u8, - ) -> (Range, Vec) { + ) -> (Range, Vec, bool) { let (parent, start, context) = parse.context.suggestion_context(cursor); let start = start.min(cursor); let truncated = &parse.input[..cursor.min(parse.input.len())]; let mut all = Vec::new(); + let mut ask = false; for &child in &self.nodes[parent].children { if self.nodes[child].permission > permission { continue; } + ask |= matches!(self.nodes[child].suggests, Some(Suggests::AskServer)); let mut builder = SuggestionsBuilder::new(truncated, start); match &self.nodes[child].kind { NodeKind::Literal(l) => { @@ -997,7 +1015,8 @@ impl Dispatcher { } all.extend(builder.out); } - merge_suggestions(truncated, all) + let (range, list) = merge_suggestions(truncated, all); + (range, list, ask) } fn smart_usage_of(&self, node: usize, optional: bool, deep: bool, level: u8) -> Option { let n = &self.nodes[node]; diff --git a/crates/horizon-client/src/command/network.rs b/crates/horizon-client/src/command/network.rs index 32f446f5c..8fed97db0 100644 --- a/crates/horizon-client/src/command/network.rs +++ b/crates/horizon-client/src/command/network.rs @@ -83,10 +83,9 @@ pub fn from_packet(packet: &Commands, local: &Dispatcher) -> Dispatcher { } _ => NodeKind::Root, }; - let suggests = match local_node { - Some(l) => l.suggests.clone(), - None => node.suggestions.as_deref().and_then(provider), - }; + // The server's providers, as the client knows them + // (ArgumentNodeStub.build): its own custom ones arrive as `ask_server`. + let suggests = node.suggestions.as_deref().and_then(provider); nodes[index(i)] = Some(Node { kind: if i == root { NodeKind::Root } else { kind }, children: node.children.iter().filter_map(valid).map(index).collect(), @@ -104,10 +103,10 @@ pub fn from_packet(packet: &Commands, local: &Dispatcher) -> Dispatcher { d } -/// SuggestionProviders by id (`ask_server` asks the server; the client's -/// own argument suggestions stand in for it). +/// SuggestionProviders by id (`SuggestionProviders.getProvider`). fn provider(id: &str) -> Option { match id { + "minecraft:ask_server" => Some(Suggests::AskServer), "minecraft:summonable_entities" => Some(Suggests::SummonableEntities), _ => None, } diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index b19cd9d01..cb8a3a0b5 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -533,6 +533,23 @@ impl Game<'_> { } } } + /// The server's completions for an `ask_server` request + /// (ClientPacketListener.handleCommandSuggestions). + pub(super) fn chat_server_suggestions( + &mut self, + id: i32, + range: (usize, usize), + list: Vec<(String, Option)>, + ) { + let m = Measure(&self.atlas.font); + let c = Commands { + dispatcher: chat_tree(&self.remote, &self.mods), + registries: &self.ui.registries, + }; + if let Some(screen) = self.ui.chat_screen.as_mut() { + screen.server_suggestions(&m, &c, id, range, list); + } + } /// Text typed into the chat box (control socket `chat`). pub(super) fn chat_type(&mut self, text: &str) { let m = Measure(&self.atlas.font); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 991bc42b2..b15793003 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -558,8 +558,58 @@ impl Game<'_> { break; } } + self.send_suggestion_request(); Ok(()) } + /// ClientSuggestionProvider.customSuggestion: the chat's completion + /// request for an `ask_server` node goes to the server. + fn send_suggestion_request(&mut self) { + if self.remote.is_none() { + return; + } + let Some((id, command)) = self.ui.chat_screen.as_mut().and_then(|s| s.take_request()) + else { + return; + }; + let mut w = horizon_protocol::buf::Writer::new(); + w.varint(id); + w.string(&command); + self.send(S::Unknown( + horizon_protocol::ids::play::serverbound::COMMAND_SUGGESTION, + w.data, + )); + } + /// ClientboundCommandSuggestionsPacket: id, range start and length, then + /// each suggestion's text and optional tooltip. + fn handle_command_suggestions(&mut self, data: &[u8]) { + let lang = self + .remote + .as_ref() + .map(|r| r.lang.clone()) + .unwrap_or_default(); + let mut r = horizon_protocol::buf::Reader::new(data); + let decoded = (|| { + let id = r.varint()?; + let start = r.varint()?; + let length = r.varint()?; + let n = r.count(usize::MAX)?; + let mut list = Vec::with_capacity(n.min(1000)); + for _ in 0..n { + let text = r.string(32767)?; + let tooltip = if r.bool()? { + Some(plain(&r.nbt()?, &lang)) + } else { + None + }; + list.push((text, tooltip)); + } + horizon_protocol::Result::Ok((id, start, length, list)) + })(); + if let Ok((id, start, length, list)) = decoded { + let range = (start.max(0) as usize, length.max(0) as usize); + self.chat_server_suggestions(id, range, list); + } + } fn set_registries(&mut self, registries: Arc) { let biomes: Vec = registries .registries @@ -1641,6 +1691,11 @@ impl Game<'_> { self.capture(true); } } + C::Unknown(id, data) + if id == horizon_protocol::ids::play::clientbound::COMMAND_SUGGESTIONS => + { + self.handle_command_suggestions(&data); + } C::Commands(p) => { let tree = horizon_client::command::network::from_packet( &p, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index b1e7ec098..13b1d48aa 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -732,11 +732,29 @@ def check_commands(host, guest, out): screen = guest('state').get('screen') guest('command', command='gamemode survival') guest.wait(10) + # A node the tree marks ask_server (a bossbar id): the guest asks the + # server, which completes it from its own bossbars. + host('command', command='bossbar add pt6:check "Check"') + host.wait(5) + guest.raw('key', 'slash', 'tap') + guest.wait(3) + guest.raw('type', 'bossbar get pt') + guest.wait(10) + guest.raw('key', 'tab', 'tap') + guest.wait(2) + asked = chat_input(guest) + for _ in range(3): + if guest('screen').get('screen') != 'chat': + break + guest.raw('key', 'esc', 'tap') + guest.wait(3) + host('command', command='bossbar remove pt6:check') assert literal == '/gamemode', f'Tab after /gamem: {literal!r}' assert argument == '/gamemode creative', f'Tab after /gamemode cr: {argument!r}' assert mode == 'creative', f'the completed command did not run: {mode}' assert screen in (None, 'playing'), f'the chat did not close: {screen}' - return {'literal': literal, 'argument': argument, 'mode': mode} + assert asked == '/bossbar get pt6:check', f'Tab after /bossbar get pt: {asked!r}' + return {'literal': literal, 'argument': argument, 'mode': mode, 'ask_server': asked} def centre(rect): From 6f7096b39644e070d79e670389f9b22e3ef9272b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 05:00:16 +0200 Subject: [PATCH 49/67] Ore veins: sample shared richness once, index the volume without division 26.3's copper and iron vein rules both bind minecraft:overworld/ore_vein/richness eagerly, so the material stage sampled the same root over the same chunk volume twice. Bindings of one compiled root now share its buffer; sampling is deterministic, so the values are identical. The two vein density reads per solid block went through DensityVolume::index_of_block, which divides and takes the remainder by the step on each axis. The stage's volumes are unit-step, so index them directly (falling back for strided volumes). Co-Authored-By: Claude Opus 5.5 --- .../worldgen/material_condition/density.rs | 153 ++++++++++++++++-- 1 file changed, 142 insertions(+), 11 deletions(-) diff --git a/crates/horizon-world/src/worldgen/material_condition/density.rs b/crates/horizon-world/src/worldgen/material_condition/density.rs index 5c98c75e0..b10ea815a 100644 --- a/crates/horizon-world/src/worldgen/material_condition/density.rs +++ b/crates/horizon-world/src/worldgen/material_condition/density.rs @@ -15,6 +15,8 @@ pub struct MaterialDensitySources { surface: S, expected: DensityVolume, densities: Vec, + /// Eagerly sampled volumes, shared by bindings of the same density root. + buffers: Vec>, retained_samples: usize, plane: DensityVolume, sampler: Arc, @@ -23,7 +25,7 @@ pub struct MaterialDensitySources { } struct BoundDensity { sampler: Arc, - buffer: Option>, + buffer: Option, } impl MaterialDensitySources { pub fn sampler_context_mut(&mut self) -> &mut SamplerContext { @@ -50,6 +52,7 @@ impl MaterialDensitySources { surface, expected, densities: Vec::new(), + buffers: Vec::new(), retained_samples: 0, plane, sampler, @@ -71,13 +74,23 @@ impl MaterialSurfaceQueries for MaterialDensitySource { return Err(DecodeError::Field("material density storage bound")); } - let buffer = if eager { - let mut buffer = vec![0.0; self.expected.size()]; - sampler.sample_volume(&mut self.context, &mut buffer, self.expected)?; - self.retained_samples += buffer.len(); - Some(buffer) - } else { - None + let shared = self + .densities + .iter() + .find(|bound| bound.buffer.is_some() && Arc::ptr_eq(&bound.sampler, &sampler)) + .and_then(|bound| bound.buffer); + let buffer = match (eager, shared) { + (false, _) => None, + // Sampling a root over the same volume and context is deterministic, + // so a second binding (iron and copper veins' richness) reuses it. + (true, Some(buffer)) => Some(buffer), + (true, None) => { + let mut buffer = vec![0.0; self.expected.size()]; + sampler.sample_volume(&mut self.context, &mut buffer, self.expected)?; + self.retained_samples += buffer.len(); + self.buffers.push(buffer); + Some(self.buffers.len() - 1) + } }; let index = self.densities.len(); self.densities.push(BoundDensity { sampler, buffer }); @@ -89,9 +102,9 @@ impl MaterialSurfaceQueries for MaterialDensitySource .get(index) .ok_or(MaterialRuleError::MissingDensityProvider)?; let position = [position.x, position.y, position.z]; - if let Some(buffer) = &source.buffer { - if let Some(index) = self.expected.index_of_block(position) { - return Ok(buffer[index]); + if let Some(buffer) = source.buffer { + if let Some(index) = block_index(self.expected, position) { + return Ok(self.buffers[buffer][index]); } } Ok(source.sampler.sample_value(&mut self.context, position)?) @@ -145,3 +158,121 @@ impl MaterialSurfaceQueries for MaterialDensitySource self.surface.band(position) } } + +/// `DensityVolume::index_of_block` without its per-axis divisions for the +/// unit-step volumes the material stage samples (one or two lookups per block). +#[inline] +fn block_index(volume: DensityVolume, position: [i32; 3]) -> Option { + if volume.steps() != [1; 3] { + return volume.index_of_block(position); + } + let [mx, my, mz] = volume.minimum(); + let [sx, sy, sz] = volume.sizes(); + let x = position[0].wrapping_sub(mx) as u32 as usize; + let y = position[1].wrapping_sub(my) as u32 as usize; + let z = position[2].wrapping_sub(mz) as u32 as usize; + (x < sx && y < sy && z < sz).then(|| y + (x + z * sx) * sy) +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_core::density::SampleError; + use std::sync::atomic::{AtomicUsize, Ordering}; + + struct Surface; + impl MaterialSurfaceQueries for Surface { + fn surface_depth(&mut self, _: i32, _: i32) -> i32 { + 0 + } + fn surface_secondary(&mut self, _: i32, _: i32) -> f64 { + 0.0 + } + } + /// Position-dependent values; counts bulk samples. + #[derive(Debug, Default)] + struct Counted(AtomicUsize); + impl DensitySampler for Counted { + fn sample_value(&self, _: &mut SamplerContext, p: [i32; 3]) -> Result { + Ok((p[0] * 10_000 + p[1] * 100 + p[2]) as f32) + } + fn sample_volume( + &self, + context: &mut SamplerContext, + buffer: &mut [f32], + volume: DensityVolume, + ) -> Result<(), SampleError> { + self.0.fetch_add(1, Ordering::Relaxed); + for z in 0..volume.sizes()[2] { + for x in 0..volume.sizes()[0] { + for y in 0..volume.sizes()[1] { + let [mx, my, mz] = volume.minimum(); + let p = [mx + x as i32, my + y as i32, mz + z as i32]; + buffer[volume.index([x, y, z]).unwrap()] = self.sample_value(context, p)?; + } + } + } + Ok(()) + } + } + fn sources() -> MaterialDensitySources { + let volume = DensityVolume::new([16, 24, 16], [32, -64, -16], [1; 3]).unwrap(); + MaterialDensitySources::new( + Surface, + volume, + Arc::new(Counted::default()), + SamplerContext::default(), + ) + .unwrap() + } + + #[test] + fn eager_bindings_of_one_root_share_a_single_sample() { + let mut sources = sources(); + let shared = Arc::new(Counted::default()); + let other = Arc::new(Counted::default()); + let a = sources.bind_density(shared.clone(), true).unwrap(); + let lazy = sources.bind_density(shared.clone(), false).unwrap(); + let b = sources.bind_density(shared.clone(), true).unwrap(); + let c = sources.bind_density(other.clone(), true).unwrap(); + assert_eq!(shared.0.load(Ordering::Relaxed), 1); + assert_eq!(other.0.load(Ordering::Relaxed), 1); + assert_eq!([a, lazy, b, c], [0, 1, 2, 3]); + for p in [[32, -64, -16], [47, -41, -1], [40, -50, -9], [48, 0, 0]] { + let position = BlockPos::new(p[0], p[1], p[2]); + let expected = (p[0] * 10_000 + p[1] * 100 + p[2]) as f32; + for index in [a, lazy, b, c] { + assert_eq!(sources.density(index, position).unwrap(), expected); + } + } + assert_eq!(shared.0.load(Ordering::Relaxed), 1); + } + + #[test] + fn block_index_matches_index_of_block() { + let unit = DensityVolume::new([16, 24, 16], [i32::MAX - 7, -64, -16], [1; 3]).unwrap(); + let strided = DensityVolume::new([5, 3, 5], [-8, -64, 0], [4, 8, 4]).unwrap(); + for volume in [unit, strided] { + let [mx, my, mz] = volume.minimum(); + for dx in [-1, 0, 1, 4, 7, 8, 15, 16, 17, 20] { + for dy in [-9, -1, 0, 8, 16, 23, 24] { + for dz in [-1, 0, 3, 15, 16, 20] { + let p = [ + mx.wrapping_add(dx), + my.wrapping_add(dy), + mz.wrapping_add(dz), + ]; + assert_eq!(block_index(volume, p), volume.index_of_block(p), "{p:?}"); + } + } + } + for p in [ + [i32::MIN, 0, 0], + [i32::MAX, i32::MAX, i32::MAX], + [0, i32::MIN, 0], + ] { + assert_eq!(block_index(volume, p), volume.index_of_block(p), "{p:?}"); + } + } + } +} From 65fc3b9f941d4573aded7e041bb2683c0860f655 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:23:11 +0200 Subject: [PATCH 50/67] Content mods: structure mods' worldgen joins the worldgen bundles A content mod's data//worldgen/ files were copied into the content root, where nothing reads them: the worldgen loader reads bundles. The build now adds a mod's structures, structure sets, template pools, processor lists and configured and placed features to their bundles, its data//structure/ templates to worldgen/structure//, and its biome tags to the biome tag bundle. The loader refuses a biome tag naming what it lacks (another mod's biomes, a loader's convention tags), so those entries are dropped, as they are from block and item tags. Biomes, noise and density functions stay out: they would change vanilla's terrain. Structure and placement types Horizon does not have stay unsupported, as before; carry-over maps the common mod ones next. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/mod_content.rs | 100 +++++++++++++++++- .../horizon-client/src/mod_content_tests.rs | 81 ++++++++++++++ 2 files changed, 177 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index 8176ddfae..1cd155297 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -674,6 +674,28 @@ pub fn tag_namespaces(root: &Path) -> Vec { /// Bundles of block tag files (tag id → the tag file's text). const TAG_BUNDLES: [&str; 2] = ["block-tags.json", "worldgen/block_tags.json"]; +/// The worldgen loader's bundle of biome tags. +const BIOME_TAGS: &str = "worldgen/biome_tags.json"; +/// Worldgen registries a content mod may add to, and the bundle the +/// worldgen loader reads each from (id → the file's text): what structure +/// mods hold. Biomes, noise and density functions change the terrain and +/// are not read from mods. +const WORLDGEN_BUNDLES: [(&str, &str); 6] = [ + ("structure", "worldgen/structures.json"), + ("structure_set", "worldgen/structure_sets.json"), + ("template_pool", "worldgen/template_pools.json"), + ("processor_list", "worldgen/processor_lists.json"), + ("configured_feature", "worldgen/features.json"), + ("placed_feature", "worldgen/placed_features.json"), +]; + +/// A mod file that adds to a worldgen bundle: (bundle, id). +fn worldgen_entry(file: &str) -> Option<(&'static str, String)> { + let (ns, rest) = file.strip_prefix("data/")?.split_once("/worldgen/")?; + let (kind, path) = rest.split_once('/')?; + let (_, bundle) = WORLDGEN_BUNDLES.iter().find(|(k, _)| *k == kind)?; + Some((bundle, format!("{ns}:{}", path.strip_suffix(".json")?))) +} /// The overlay being written: `out` mirrors `base` through links, and a /// directory becomes real only when something inside it is written. @@ -773,6 +795,9 @@ fn merge_tag(text: Option<&str>, more: &[J], what: &str) -> Result { struct Known { blocks: std::collections::BTreeSet, items: std::collections::BTreeSet, + /// The worldgen loader's biomes and biome tags. + biomes: std::collections::BTreeSet, + biome_tags: std::collections::BTreeSet, /// `data//tags//.json` files the mods add. tag_files: std::collections::BTreeSet, } @@ -786,14 +811,27 @@ impl Known { items.extend(o.into_iter().map(|(k, _)| k)); } items.extend(plan.items.iter().map(|i| i.rename.to.clone())); + let keys = |name: &str| -> Result> { + Ok(match base.join(name).exists() { + true => match read_json(&base.join(name))? { + J::Obj(o) => o.into_iter().map(|(k, _)| k).collect(), + _ => Default::default(), + }, + false => Default::default(), + }) + }; Ok(Known { blocks, items, + biomes: keys("worldgen/biomes.json")?, + biome_tags: keys(BIOME_TAGS)?, tag_files: tags.keys().cloned().collect(), }) } /// Whether tag `file` keeps entry `v`: a known id or an existing tag, - /// for block and item tags (other registries are kept as they are). + /// for block, item and biome tags (other registries are kept as they + /// are). The worldgen loader refuses a biome tag naming what it lacks + /// (another mod's biomes, a loader's tags), so those entries go. fn keeps(&self, file: &str, v: &J) -> bool { let id = match v { J::Str(s) => s.as_str(), @@ -803,23 +841,32 @@ impl Known { }, _ => return true, }; - let Some(kind) = file + let Some(rest) = file .strip_prefix("data/") .and_then(|r| r.split_once("/tags/")) - .and_then(|(_, rest)| Some(rest.split_once('/')?.0)) + .map(|(_, rest)| rest) else { return true; }; + let kind = match rest.strip_prefix("worldgen/biome/") { + Some(_) => "worldgen/biome", + None => rest.split_once('/').map_or("", |(k, _)| k), + }; let ids = match kind { "block" => &self.blocks, "item" => &self.items, + "worldgen/biome" => &self.biomes, _ => return true, }; match id.strip_prefix('#') { Some(tag) => { let (tns, path) = tag.split_once(':').unwrap_or(("minecraft", tag)); let rel = format!("data/{tns}/tags/{kind}/{path}.json"); - self.tag_files.contains(&rel) || tns == "minecraft" + self.tag_files.contains(&rel) + || match kind { + "worldgen/biome" => self.biome_tags.contains(&format!("{tns}:{path}")), + _ => tns == "minecraft", + } } None => ids.contains(id), } @@ -856,6 +903,7 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { } // Mod files, and every tag addition gathered per tag file. let mut tags: BTreeMap> = BTreeMap::new(); + let mut worldgen: BTreeMap<&str, Vec<(String, String)>> = BTreeMap::new(); for m in mods { let declarations = format!("data/{}/horizon/", m.id); for file in &m.content.files { @@ -878,6 +926,18 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { .extend(values.iter().cloned()); } else if !file.starts_with(&declarations) { overlay.write(file, &bytes)?; + if let Some((bundle, id)) = worldgen_entry(file) { + let text = String::from_utf8(bytes).map_err(|e| format!("{file}: {e}"))?; + worldgen.entry(bundle).or_default().push((id, text)); + } else if let Some(template) = file + .strip_prefix("data/") + .and_then(|r| r.split_once("/structure/")) + .filter(|(_, path)| path.ends_with(".nbt")) + { + // The worldgen loader's templates: worldgen/structure//.nbt. + let (ns, path) = template; + overlay.write(&format!("worldgen/structure/{ns}/{path}"), &bytes)?; + } } } } @@ -921,6 +981,38 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { for (name, doc) in &bundles { overlay.write(name, &serde_json::to_vec(doc)?)?; } + // Biome tags, merged into the worldgen loader's bundle as block tags are. + let biome_tags: Vec<(String, &Vec)> = tags + .iter() + .filter_map(|(file, values)| { + let (ns, path) = file + .strip_prefix("data/")? + .split_once("/tags/worldgen/biome/")?; + Some((format!("{ns}:{}", path.strip_suffix(".json")?), values)) + }) + .collect(); + if !biome_tags.is_empty() && base.join(BIOME_TAGS).exists() { + let mut doc = read_json(&base.join(BIOME_TAGS))?; + for (id, values) in biome_tags { + let text = match doc.get(&id) { + Some(J::Str(t)) => Some(t.clone()), + _ => None, + }; + doc.set(&id, J::Str(merge_tag(text.as_deref(), values, BIOME_TAGS)?)); + } + overlay.write(BIOME_TAGS, &serde_json::to_vec(&doc)?)?; + } + // Structures and the pools, sets, processors and features they use. + for (bundle, entries) in &worldgen { + if !base.join(bundle).exists() { + continue; + } + let mut doc = read_json(&base.join(bundle))?; + for (id, text) in entries { + doc.set(id, J::Str(text.clone())); + } + overlay.write(bundle, &serde_json::to_vec(&doc)?)?; + } // A block without its own blockstate looks like its template, so every // state still has a model. for m in mods { diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs index c086f8c0c..614705562 100644 --- a/crates/horizon-client/src/mod_content_tests.rs +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -537,3 +537,84 @@ fn content_root_is_reused_until_a_mod_changes() { ); let _ = fs::remove_dir_all(&scratch); } + +/// A structure mod's worldgen reaches the worldgen loader: its structure, +/// set, pool and templates join the bundles, and its biome tag keeps only +/// what the loader knows. +#[test] +fn content_root_carries_a_structure_mod() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-worldgen-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let dir = scratch.join("huts"); + let files = [ + ( + "horizon-mod.toml", + "id = \"huts\"\nname = \"Huts\"\nversion = \"1.0\"\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n", + ), + ( + "data/huts/worldgen/structure/hut.json", + r##"{"type": "minecraft:jigsaw", "biomes": "#huts:has_hut", "step": "surface_structures", + "spawn_overrides": {}, "terrain_adaptation": "beard_thin", "start_pool": "huts:hut/start", + "size": 1, "start_height": {"absolute": 0}, "project_start_to_heightmap": "WORLD_SURFACE_WG", + "max_distance_from_center": 80}"##, + ), + ( + "data/huts/worldgen/template_pool/hut/start.json", + r#"{"fallback": "minecraft:empty", "elements": [{"weight": 1, "element": { + "element_type": "minecraft:single_pool_element", "location": "huts:hut", + "projection": "rigid", "processors": "minecraft:empty"}}]}"#, + ), + ( + "data/huts/worldgen/structure_set/huts.json", + r#"{"structures": [{"structure": "huts:hut", "weight": 1}], + "placement": {"type": "minecraft:random_spread", "salt": 7, "spacing": 4, "separation": 2}}"#, + ), + ( + "data/huts/tags/worldgen/biome/has_hut.json", + r##"{"values": ["#minecraft:is_overworld", "#c:is_plains", "othermod:glade", "minecraft:plains"]}"##, + ), + ]; + for (name, text) in files { + let path = dir.join(name); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, text).unwrap(); + } + // The template: vanilla's smallest plains house, as the mod's own file. + let house = + base.join("worldgen/structure/minecraft/village/plains/houses/plains_small_house_1.nbt"); + fs::create_dir_all(dir.join("data/huts/structure")).unwrap(); + fs::copy(&house, dir.join("data/huts/structure/hut.nbt")).unwrap(); + let package = horizon_mods::Package::read(&dir).unwrap(); + let m = ContentMod { + id: "huts".into(), + content: Content::read(&dir, &package.manifest).unwrap(), + }; + let root = resource_root(&base, &[m], &scratch.join("cache")).unwrap(); + assert!(root.join("worldgen/structure/huts/hut.nbt").exists()); + let tags: BTreeMap = + serde_json::from_slice(&fs::read(root.join("worldgen/biome_tags.json")).unwrap()).unwrap(); + let hut: serde_json::Value = serde_json::from_str(&tags["huts:has_hut"]).unwrap(); + assert_eq!( + hut["values"], + serde_json::json!(["#minecraft:is_overworld", "minecraft:plains"]) + ); + + let assets = Assets::load(&root).unwrap(); + let biomes = crate::biome::Biomes::load(&root).unwrap(); + let terrain = crate::vanillagen::VanillaTerrain::new( + &root.join("worldgen"), + assets.registry, + &biomes, + 1234, + ) + .unwrap(); + let (name, at) = terrain + .locate(&["huts:hut".to_owned()], [0, 64, 0], 16) + .expect("the mod's structure generates"); + assert_eq!(name, "huts:hut"); + assert!(at[0].abs() < 1024 && at[2].abs() < 1024, "{at:?}"); + let _ = fs::remove_dir_all(&scratch); +} From cf97a5e1dc164d51d3fef86bf8b0f56d33f45902 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 05:44:08 +0200 Subject: [PATCH 51/67] Decoration order: dense grids instead of hash maps vanilla_order replays vanilla's ticket scheduler over every chunk in view each time the player crosses into another chunk, on the render thread. At render distance 32 that is about 200,000 level updates in SipHash maps: 8 ms per call. Over dense grids covering the square it can touch, the same replay takes 1 ms. A test checks the order against the hash map version for several centres and radii. Co-Authored-By: Claude Opus 5.5 --- .../src/worldgen/decoration_order.rs | 155 +++++++++++++++--- 1 file changed, 133 insertions(+), 22 deletions(-) diff --git a/crates/horizon-world/src/worldgen/decoration_order.rs b/crates/horizon-world/src/worldgen/decoration_order.rs index 52649ceed..6b0fb2252 100644 --- a/crates/horizon-world/src/worldgen/decoration_order.rs +++ b/crates/horizon-world/src/worldgen/decoration_order.rs @@ -121,7 +121,7 @@ fn ticket_hash(x: i32, z: i32) -> u64 { /// The chunks decorated around a player at `center`, in vanilla's order. pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { use std::cmp::Reverse; - use std::collections::{BinaryHeap, HashSet, VecDeque}; + use std::collections::{BinaryHeap, VecDeque}; let mut tickets: Vec<(i32, i32)> = (-radius..=radius) .flat_map(|dx| (-radius..=radius).map(move |dz| (center.0 + dx, center.1 + dz))) .collect(); @@ -133,35 +133,45 @@ pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { ) }); let mut tickets: VecDeque<_> = tickets.into(); + // Every chunk this touches lies within `half` of the centre: tickets + // reach `LIGHT_LEVEL - TICKET_LEVEL` further, and FULL tasks decorate + // one more ring. Dense grids over that square replace hash maps. + let reach = LIGHT_LEVEL - TICKET_LEVEL; + let half = radius.max(0) + reach + 1; + let side = (2 * half + 1) as usize; + let cell = |(x, z): (i32, i32)| { + ((x - center.0 + half) as usize) * side + (z - center.1 + half) as usize + }; let mut active: Vec<(i32, i32)> = Vec::new(); - // Queued task per chunk (level, sequence); stale heap entries are skipped. - let mut queued: HashMap<(i32, i32), (i32, u64)> = HashMap::new(); + // Queued task per chunk (level, sequence), `NONE` when there is none; + // stale heap entries are skipped. + const NONE: (i32, u64) = (i32::MAX, 0); + let mut queued = vec![NONE; side * side]; let mut heap = BinaryHeap::new(); let mut sequence = 0u64; - let mut full = HashSet::new(); - let mut seen = HashSet::new(); + let mut full = vec![false; side * side]; + let mut seen = vec![false; side * side]; let mut order = Vec::new(); // Each ticket lowers the levels around it to 31 + its Chebyshev distance. - let mut levels: HashMap<(i32, i32), i32> = HashMap::new(); + let mut levels = vec![i32::MAX; side * side]; let mut add = |ticket: (i32, i32), active: &mut Vec<(i32, i32)>, - queued: &mut HashMap<(i32, i32), (i32, u64)>, + queued: &mut [(i32, u64)], heap: &mut BinaryHeap>, - full: &HashSet<(i32, i32)>| { + full: &[bool]| { active.push(ticket); - let reach = LIGHT_LEVEL - TICKET_LEVEL; for x in ticket.0 - reach..=ticket.0 + reach { for z in ticket.1 - reach..=ticket.1 + reach { let own = TICKET_LEVEL + (x - ticket.0).abs().max((z - ticket.1).abs()); - let l = levels.entry((x, z)).or_insert(i32::MAX); - *l = (*l).min(own); - let l = *l; - if full.contains(&(x, z)) { + let i = cell((x, z)); + levels[i] = levels[i].min(own); + let l = levels[i]; + if full[i] { continue; } - if queued.get(&(x, z)).is_none_or(|&(old, _)| l < old) { + if l < queued[i].0 { sequence += 1; - queued.insert((x, z), (l, sequence)); + queued[i] = (l, sequence); heap.push(Reverse((l, sequence, (x, z)))); } } @@ -173,24 +183,25 @@ pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { } } while let Some(Reverse((l, s, chunk))) = heap.pop() { - if queued.get(&chunk) != Some(&(l, s)) { + let i = cell(chunk); + if queued[i] != (l, s) { continue; } - queued.remove(&chunk); + queued[i] = NONE; let r = i32::from(l <= FULL_LEVEL); for x in chunk.0 - r..=chunk.0 + r { for z in chunk.1 - r..=chunk.1 + r { - if seen.insert((x, z)) { + let seen = &mut seen[cell((x, z))]; + if !*seen { + *seen = true; order.push((x, z)); } } } if l <= FULL_LEVEL { - full.insert(chunk); + full[i] = true; } - active.retain(|t| { - !(-2..=2).all(|dx| (-2..=2).all(|dz| full.contains(&(t.0 + dx, t.1 + dz)))) - }); + active.retain(|t| !(-2..=2).all(|dx| (-2..=2).all(|dz| full[cell((t.0 + dx, t.1 + dz))]))); while active.len() < TICKETS_IN_FLIGHT { let Some(t) = tickets.pop_front() else { break }; add(t, &mut active, &mut queued, &mut heap, &full); @@ -203,6 +214,106 @@ pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { mod tests { use super::*; + /// The hash map version this module had before its dense grids. + fn hashed_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { + use std::cmp::Reverse; + use std::collections::{BinaryHeap, HashSet, VecDeque}; + let mut tickets: Vec<(i32, i32)> = (-radius..=radius) + .flat_map(|dx| (-radius..=radius).map(move |dz| (center.0 + dx, center.1 + dz))) + .collect(); + // A fastutil open hash set iterates its slots from the last to the first. + tickets.sort_by_key(|&(x, z)| { + ( + (x - center.0).abs().max((z - center.1).abs()), + Reverse(ticket_hash(x, z) & (TICKET_SET_CAPACITY - 1)), + ) + }); + let mut tickets: VecDeque<_> = tickets.into(); + let mut active: Vec<(i32, i32)> = Vec::new(); + // Queued task per chunk (level, sequence); stale heap entries are skipped. + let mut queued: HashMap<(i32, i32), (i32, u64)> = HashMap::new(); + let mut heap = BinaryHeap::new(); + let mut sequence = 0u64; + let mut full = HashSet::new(); + let mut seen = HashSet::new(); + let mut order = Vec::new(); + // Each ticket lowers the levels around it to 31 + its Chebyshev distance. + let mut levels: HashMap<(i32, i32), i32> = HashMap::new(); + let mut add = |ticket: (i32, i32), + active: &mut Vec<(i32, i32)>, + queued: &mut HashMap<(i32, i32), (i32, u64)>, + heap: &mut BinaryHeap>, + full: &HashSet<(i32, i32)>| { + active.push(ticket); + let reach = LIGHT_LEVEL - TICKET_LEVEL; + for x in ticket.0 - reach..=ticket.0 + reach { + for z in ticket.1 - reach..=ticket.1 + reach { + let own = TICKET_LEVEL + (x - ticket.0).abs().max((z - ticket.1).abs()); + let l = levels.entry((x, z)).or_insert(i32::MAX); + *l = (*l).min(own); + let l = *l; + if full.contains(&(x, z)) { + continue; + } + if queued.get(&(x, z)).is_none_or(|&(old, _)| l < old) { + sequence += 1; + queued.insert((x, z), (l, sequence)); + heap.push(Reverse((l, sequence, (x, z)))); + } + } + } + }; + for _ in 0..TICKETS_IN_FLIGHT { + if let Some(t) = tickets.pop_front() { + add(t, &mut active, &mut queued, &mut heap, &full); + } + } + while let Some(Reverse((l, s, chunk))) = heap.pop() { + if queued.get(&chunk) != Some(&(l, s)) { + continue; + } + queued.remove(&chunk); + let r = i32::from(l <= FULL_LEVEL); + for x in chunk.0 - r..=chunk.0 + r { + for z in chunk.1 - r..=chunk.1 + r { + if seen.insert((x, z)) { + order.push((x, z)); + } + } + } + if l <= FULL_LEVEL { + full.insert(chunk); + } + active.retain(|t| { + !(-2..=2).all(|dx| (-2..=2).all(|dz| full.contains(&(t.0 + dx, t.1 + dz)))) + }); + while active.len() < TICKETS_IN_FLIGHT { + let Some(t) = tickets.pop_front() else { break }; + add(t, &mut active, &mut queued, &mut heap, &full); + } + } + order + } + + #[test] + fn dense_grids_keep_the_hashed_order() { + for (center, radius) in [ + ((0, 0), -1), + ((0, 0), 0), + ((0, 0), 1), + ((12, -15), 8), + ((-30_000, 29_999), 5), + ((7, 3), 12), + ((-61, 1234), 32), + ] { + assert_eq!( + vanilla_order(center, radius), + hashed_order(center, radius), + "{center:?} radius {radius}" + ); + } + } + #[test] fn first_wave_matches_vanilla() { // Seed 1891623539983467248, player teleported into chunk 12,-15 with From b540ccb642a20d6a22c75383dd49fd4817410e95 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 07:17:44 +0200 Subject: [PATCH 52/67] Brewing stands tick in the level's block entity ticker list Brewing stands were the last server ticker with a pass of its own over the whole block entity map every tick. They now tick in their place in Level.blockEntityTickers, as BrewingStandBlock.getTicker makes them: in list order, only in block-ticking range and inside the world border, with their bottle states written as each one ticks. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 1 - .../src/game/block_entity_ticks.rs | 4 ++- crates/horizon-client/src/game/stations.rs | 32 +++++++++---------- tools/test_block_entity_tickers_parity.py | 4 +-- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2ab3a7c07..d4a595a3f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3617,7 +3617,6 @@ impl Game<'_> { horizon_client::spans::time("tick_block_entities", || self.tick_block_entities())?; horizon_client::spans::time("tick_sculk_entities", || self.tick_sculk_entities()); horizon_client::spans::time("tick_sculk_blocks", || self.tick_sculk_blocks())?; - horizon_client::spans::time("tick_brewing_stands", || self.tick_brewing_stands())?; } if running { // ClientLevel.tickBlockEntities (not while paused or frozen): diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index afe1224fb..f596f2d8b 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -52,6 +52,7 @@ impl Game<'_> { Some(BlockEntity::Vault(_)) => self.tick_vault(pos)?, Some(BlockEntity::Beehive(_)) => self.tick_beehive(pos), Some(BlockEntity::Furnace(_)) => self.tick_furnace(pos)?, + Some(BlockEntity::Brewing(_)) => self.tick_brewing_stand(pos)?, _ => {} } } @@ -85,7 +86,8 @@ impl Game<'_> { | "minecraft:trial_spawner" | "minecraft:vault" | "minecraft:beehive" - | "minecraft:bee_nest" => Ticking::Yes, + | "minecraft:bee_nest" + | "minecraft:brewing_stand" => Ticking::Yes, "minecraft:jukebox" => { let record = registry .state(state) diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index 332f06b23..7b70cb1f1 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -2173,22 +2173,22 @@ impl Game<'_> { // ---------------------------------------------------------------- brewing stand - /// Ticks every brewing stand (`BrewingStandBlockEntity.serverTick`). - pub(super) fn tick_brewing_stands(&mut self) -> horizon_client::Result<()> { - let mut changes = Vec::new(); - for (pos, entity) in self.world.block_entities.iter_mut() { - if let BlockEntity::Brewing(b) = entity { - let out = b.tick( - &self.res.stations.brewing, - &self.res.items, - &self.res.providers, - ); - if out != brewing::BrewTick::default() { - changes.push((*pos, out)); - } - } - } - for ((x, y, z), out) in changes { + /// `BrewingStandBlockEntity.serverTick` of the brewing stand at `pos`, in + /// its place among the level's block entity tickers. + pub(super) fn tick_brewing_stand( + &mut self, + pos: (i32, i32, i32), + ) -> horizon_client::Result<()> { + let Some(BlockEntity::Brewing(b)) = self.world.block_entities.get_mut(&pos) else { + return Ok(()); + }; + let out = b.tick( + &self.res.stations.brewing, + &self.res.items, + &self.res.providers, + ); + if out != brewing::BrewTick::default() { + let (x, y, z) = pos; self.world.modified.insert((x >> 4, z >> 4)); if out.brewed { self.play_at("minecraft:block.brewing_stand.brew", [x, y, z], 1.0, 1.0); diff --git a/tools/test_block_entity_tickers_parity.py b/tools/test_block_entity_tickers_parity.py index 58604b269..459e7a926 100644 --- a/tools/test_block_entity_tickers_parity.py +++ b/tools/test_block_entity_tickers_parity.py @@ -9,7 +9,7 @@ of a server level and prints, after each tick, the positions of the live tickers in list order; `examples/block_entity_tickers_fixture` runs the same on `horizon_client::block_entities::BlockEntities`. The block entities are the kinds the game ticks in that list (hoppers, furnaces, beacons, spawners, -trial spawners, vaults, hives, crafters, jukebox), with chests and barrels, +trial spawners, vaults, hives, crafters, brewing stands, jukebox), with chests and barrels, which have no ticker, among them. """ import argparse @@ -25,7 +25,7 @@ except ImportError: PARITY = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity' TICKING = ['hopper', 'furnace', 'blast_furnace', 'smoker', 'beacon', 'spawner', 'trial_spawner', 'vault', - 'beehive', 'bee_nest', 'crafter', 'jukebox_record'] + 'beehive', 'bee_nest', 'crafter', 'brewing_stand', 'jukebox_record'] KINDS = TICKING + ['jukebox', 'chest', 'barrel'] From 29327041567b12ea441352d6803232e181331090 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 06:55:44 +0200 Subject: [PATCH 53/67] Region files: write each saved chunk in place, as vanilla's RegionFile Saving a chunk read every chunk of its region file and wrote them all to a new file. The writer now writes only the saved chunk, into the first free run of sectors (RegionBitmap), points the header at it and then frees its old sectors (RegionFile.write / clear), with one flush per region and batch. Readers and the writer share one open file per region. A chunk queued again before the writer reached it is written once, by its newest batch (IOWorker.pendingWrites). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/save.rs | 163 ++++++++++++------------ crates/horizon-world/src/region.rs | 165 ++++++++++++++++++++++++- crates/horizon-world/tests/world_io.rs | 118 ++++++++++++++++++ 3 files changed, 357 insertions(+), 89 deletions(-) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index c60da2caa..d94bb7182 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -31,7 +31,7 @@ use horizon_world::{ compression::Compression, heightmap::{Heightmap, HeightmapChunk, HeightmapType}, nbt::{self, Compound, JavaString, Limits, NamedTag, Tag}, - region::{ChunkRecord, RegionFile, RegionWriter}, + region::{ChunkRecord, RegionFile}, registry::{BlockRegistry, BlockStateId}, worldgen::terrain::TerrainBlockStates, }; @@ -1306,94 +1306,87 @@ pub fn load_light(tag: &Tag, chunk: &mut Chunk, sky_light: bool) { } type RegionCache = Mutex>>>>; -/// Rewrite every region file in `folder` touched by `records`, keeping its -/// other chunks. `None` deletes a chunk. +/// The region file at `path` through the cache, which readers and the writer +/// share, so a read never sees a header older than the sectors it points to. +/// `create` makes a missing file (for a write); otherwise a missing file is +/// `None`, until a write creates it. +fn cached_region( + cache: &RegionCache, + key: (i32, i32), + path: &Path, + create: bool, +) -> Result>>> { + let mut regions = cache.lock().map_err(|_| "region cache poisoned")?; + if let Some(Some(region)) = regions.get(&key) { + return Ok(Some(region.clone())); + } + if !create && regions.contains_key(&key) { + return Ok(None); + } + if create && let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + // A file this process can't write (a read-only world) is still read. + let region = match RegionFile::open_writable(path, create) { + Err(e) if !create && e.kind() == std::io::ErrorKind::PermissionDenied => { + RegionFile::open(path) + } + other => other, + }; + let region = match region { + Ok(r) => Some(Arc::new(Mutex::new(r))), + Err(_) if !create => None, + Err(e) => return Err(e.into()), + }; + regions.insert(key, region.clone()); + Ok(region) +} +/// Write `records` into the region files of `folder` in place, as vanilla's +/// `RegionFile.write` does: each chunk takes free sectors and the header +/// then points to it; the region's other chunks are not touched. `None` +/// clears a chunk. The chunks are compressed before their region is locked +/// for the writes, and each region is flushed to the disk once. fn write_region_records( root: &Path, cache: &RegionCache, folder: &str, records: Vec<((i32, i32), Option>)>, ) -> Result<()> { - let mut by_region: BTreeMap<(i32, i32), Vec<((i32, i32), Option>)>> = - BTreeMap::new(); - for (key, tag) in records { - by_region - .entry((key.0 >> 5, key.1 >> 5)) - .or_default() - .push((key, tag)); - } - if by_region.is_empty() { - return Ok(()); - } - let staging = root.join(folder).join(".staging"); - fs::create_dir_all(&staging)?; let timestamp = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map_or(0, |d| d.as_secs() as u32); + let mut by_region: BTreeMap<(i32, i32), Vec<(ChunkPos, Option)>> = BTreeMap::new(); + for ((x, z), tag) in records { + let record = tag + .map(|tag| ChunkRecord::from_nbt(&tag, Compression::Zlib, timestamp, Limits::default())) + .transpose()?; + by_region + .entry((x >> 5, z >> 5)) + .or_default() + .push((ChunkPos::new(x, z), record)); + } for ((rx, rz), list) in by_region { let path = region_path(root, folder, rx, rz); - let mut records: BTreeMap<(i32, i32), ChunkRecord> = BTreeMap::new(); - let only_deletes = list.iter().all(|(_, tag)| tag.is_none()); - if let Ok(mut existing) = RegionFile::open(&path) { - let positions: Vec = existing.chunks().map(|(p, _)| p).collect(); - if only_deletes - && !positions - .iter() - .any(|p| list.iter().any(|(k, _)| *k == (p.x, p.z))) - { - continue; - } - for p in positions { - if let Ok(Some(r)) = existing.read(p) { - records.insert((p.x, p.z), r); - } - } - } else if only_deletes { + // Clearing chunks of a region that doesn't exist changes nothing. + let create = list.iter().any(|(_, r)| r.is_some()); + let Some(region) = cached_region(cache, (rx, rz), &path, create)? else { continue; - } - // Deleting chunks the region doesn't hold changes nothing: leave - // the file alone (unloading empty entity chunks asks for that). + }; + let mut region = region.lock().map_err(|_| "region file poisoned")?; let mut changed = false; - for (key, tag) in list { - match tag { - Some(tag) => { + for (pos, record) in &list { + match record { + Some(record) => { + region.write(*pos, record)?; changed = true; - records.insert( - key, - ChunkRecord::from_nbt( - &tag, - Compression::Zlib, - timestamp, - Limits::default(), - )?, - ); - } - None => { - changed |= records.remove(&key).is_some(); } + None => changed |= region.clear(*pos, timestamp)?, } } - if !changed { - continue; + if changed { + region.sync()?; } - if records.is_empty() { - let _ = fs::remove_file(&path); - } else { - let temporary = staging.join(format!("r.{rx}.{rz}.mca")); - let _ = fs::remove_file(&temporary); - let mut writer = RegionWriter::create(&temporary)?; - for ((x, z), r) in &records { - writer.write(ChunkPos::new(*x, *z), r)?; - } - writer.finish()?; - fs::rename(&temporary, &path)?; - } - cache - .lock() - .map_err(|_| "region cache poisoned")? - .remove(&(rx, rz)); } - let _ = fs::remove_dir(&staging); Ok(()) } @@ -1436,7 +1429,7 @@ impl Record { type PendingWrites = Mutex>; type WriteBatch = (bool, Vec<((i32, i32), Record, u64)>); -/// The background thread that encodes chunks and rewrites region files, in +/// The background thread that encodes chunks and writes them into the region files, in /// submission order. struct Writer { send: mpsc::Sender, @@ -1498,17 +1491,8 @@ impl SaveDir { } else { &self.entity_regions }; - let region = { - let mut regions = cache.lock().ok()?; - regions - .entry(key) - .or_insert_with(|| { - RegionFile::open(self.region_path(folder, key.0, key.1)) - .ok() - .map(|r| Arc::new(Mutex::new(r))) - }) - .clone()? - }; + let path = self.region_path(folder, key.0, key.1); + let region = cached_region(cache, key, &path, false).ok()??; let record = region.lock().ok()?.read(ChunkPos::new(x, z)).ok()??; record.nbt(Limits::default()).ok() } @@ -1614,7 +1598,18 @@ impl SaveDir { } else { ("region", ®ions) }; - let records = batch.iter().map(|(k, r, _)| (*k, r.tag())).collect(); + // IOWorker.pendingWrites: a chunk queued again since is + // written once, by its newest batch. + let newest = |k: &(i32, i32), seq: u64| { + pending.lock().map_or(true, |p| { + p.get(&(entities, k.0, k.1)).is_none_or(|p| p.0 <= seq) + }) + }; + let records = batch + .iter() + .filter(|(k, _, seq)| newest(k, *seq)) + .map(|(k, r, _)| (*k, r.tag())) + .collect(); if let Err(e) = write_region_records(&root, cache, folder, records) { log::warn!("Horizon: failed to write {folder} files: {e}"); } @@ -1650,7 +1645,7 @@ impl SaveDir { }; } } - /// Rewrite every region touched by `chunks`, keeping their other chunks. + /// Write `chunks` into their region files and wait until they are on disk. pub fn save( &self, chunks: &[Arc], diff --git a/crates/horizon-world/src/region.rs b/crates/horizon-world/src/region.rs index 05bdc1a54..1e4ba176c 100644 --- a/crates/horizon-world/src/region.rs +++ b/crates/horizon-world/src/region.rs @@ -1,5 +1,7 @@ -//! Anvil region reading and export to a new region. Existing files are never -//! overwritten. Use a separate directory for offline edits and copied .mcc data. +//! Anvil region reading, export to a new region, and in-place chunk writes +//! as vanilla's `RegionFile` makes them (a file opened with `open_writable`). +//! Exports never overwrite a file: use a separate directory for offline +//! edits and copied .mcc data. use crate::{ compression::{Compression, invalid}, nbt::{self, Limits, NamedTag}, @@ -54,6 +56,9 @@ pub struct RegionFile { position: ChunkPos, offsets: [u32; 1024], timestamps: [u32; 1024], + /// The sectors in use (`RegionBitmap`): the header's two and every + /// chunk's. + used: Vec, } pub fn position_from_path(path: &Path) -> io::Result { @@ -94,6 +99,12 @@ fn position(region: ChunkPos, index: usize) -> ChunkPos { fn external_path(directory: &Path, chunk: ChunkPos) -> PathBuf { directory.join(format!("c.{}.{}.mcc", chunk.x, chunk.z)) } +fn remove_if_exists(path: &Path) -> io::Result<()> { + match fs::remove_file(path) { + Err(e) if e.kind() != io::ErrorKind::NotFound => Err(e), + _ => Ok(()), + } +} impl RegionFile { pub fn position(&self) -> ChunkPos { @@ -101,8 +112,25 @@ impl RegionFile { } pub fn open(path: impl AsRef) -> io::Result { let path = path.as_ref().to_owned(); + let file = File::open(&path)?; + Self::from_file(path, file) + } + + /// Open for reading and in-place writes (`write`, `clear`), creating an + /// empty region with `create`. + pub fn open_writable(path: impl AsRef, create: bool) -> io::Result { + let path = path.as_ref().to_owned(); + let file = OpenOptions::new() + .read(true) + .write(true) + .create(create) + .truncate(false) + .open(&path)?; + Self::from_file(path, file) + } + + fn from_file(path: PathBuf, mut file: File) -> io::Result { let position = position_from_path(&path)?; - let mut file = File::open(&path)?; let len = file.metadata()?.len(); // RegionFile's constructor: a short header (an empty or truncated file) // reads as far as it goes, the rest stays zero (no chunks). @@ -134,13 +162,140 @@ impl RegionFile { offsets[i] = 0; } } - Ok(Self { + let mut region = Self { file, path, position, offsets, timestamps, - }) + used: Vec::new(), + }; + region.force(0, 2); + for offset in offsets.into_iter().filter(|&o| o != 0) { + region.force((offset >> 8) as usize, (offset & 255) as usize); + } + Ok(region) + } + + /// `RegionBitmap.force`. + fn force(&mut self, start: usize, count: usize) { + if self.used.len() < start + count { + self.used.resize(start + count, false); + } + self.used[start..start + count].fill(true); + } + /// `RegionBitmap.free`. + fn free(&mut self, start: usize, count: usize) { + let end = (start + count).min(self.used.len()); + if start < end { + self.used[start..end].fill(false); + } + } + /// `RegionBitmap.allocate`: the first run of free sectors long enough + /// (the end of the file is free). + fn allocate(&mut self, count: usize) -> usize { + let mut current = 0; + loop { + let start = (current..self.used.len()) + .find(|&s| !self.used[s]) + .unwrap_or(self.used.len()); + let end = (start..self.used.len()).find(|&s| self.used[s]); + if end.is_none_or(|end| end - start >= count) { + self.force(start, count); + return start; + } + current = end.unwrap_or(start); + } + } + + /// `RegionFile.write`: the chunk goes to the first free run of sectors + /// that holds it (an oversized one, 256 sectors or more, to its external + /// `c.X.Z.mcc` file with a one-sector stub), the header points to it, + /// and only then are its old sectors freed. The other chunks stay where + /// they are. A chunk's last sector is padded with zeros. + pub fn write(&mut self, chunk: ChunkPos, record: &ChunkRecord) -> io::Result<()> { + let i = slot(self.position, chunk)?; + if record.compressed.len() > MAX_COMPRESSED { + return Err(invalid("compressed chunk limit exceeded")); + } + let required = (record.compressed.len() + 5).div_ceil(SECTOR); + let external = required >= 256; + let sectors = if external { 1 } else { required }; + let start = self.allocate(sectors); + if start + sectors > 0x100_0000 { + self.free(start, sectors); + return Err(invalid("region sector address limit exceeded")); + } + let directory = self.path.parent().unwrap_or(Path::new(".")).to_owned(); + let external_file = external_path(&directory, chunk); + let mut data = Vec::with_capacity(sectors * SECTOR); + if external { + let temporary = directory.join(format!(".c.{}.{}.mcc.tmp", chunk.x, chunk.z)); + let written = File::create(&temporary) + .and_then(|mut f| f.write_all(&record.compressed)) + .and_then(|_| fs::rename(&temporary, &external_file)); + if let Err(e) = written { + let _ = fs::remove_file(&temporary); + self.free(start, sectors); + return Err(e); + } + data.extend_from_slice(&1u32.to_be_bytes()); + data.push(record.compression as u8 | 128); + } else { + data.extend_from_slice(&(record.compressed.len() as u32 + 1).to_be_bytes()); + data.push(record.compression as u8); + data.extend_from_slice(&record.compressed); + } + data.resize(sectors * SECTOR, 0); + let written = self + .file + .seek(SeekFrom::Start((start * SECTOR) as u64)) + .and_then(|_| self.file.write_all(&data)); + if let Err(e) = written { + self.free(start, sectors); + return Err(e); + } + let old = self.offsets[i]; + self.offsets[i] = (start as u32) << 8 | sectors as u32; + self.timestamps[i] = record.timestamp; + self.write_header_entry(i)?; + if !external { + remove_if_exists(&external_file)?; + } + if old != 0 { + self.free((old >> 8) as usize, (old & 255) as usize); + } + Ok(()) + } + + /// `RegionFile.clear`: the chunk is gone; `timestamp` marks when. + /// Returns whether the region held it. + pub fn clear(&mut self, chunk: ChunkPos, timestamp: u32) -> io::Result { + let i = slot(self.position, chunk)?; + let old = self.offsets[i]; + if old == 0 { + return Ok(false); + } + self.offsets[i] = 0; + self.timestamps[i] = timestamp; + self.write_header_entry(i)?; + let directory = self.path.parent().unwrap_or(Path::new(".")); + remove_if_exists(&external_path(directory, chunk))?; + self.free((old >> 8) as usize, (old & 255) as usize); + Ok(true) + } + + /// Flush the writes to the disk. + pub fn sync(&self) -> io::Result<()> { + self.file.sync_data() + } + + /// The header's offset and timestamp of slot `i`. + fn write_header_entry(&mut self, i: usize) -> io::Result<()> { + self.file.seek(SeekFrom::Start((i * 4) as u64))?; + self.file.write_all(&self.offsets[i].to_be_bytes())?; + self.file.seek(SeekFrom::Start((SECTOR + i * 4) as u64))?; + self.file.write_all(&self.timestamps[i].to_be_bytes()) } pub fn chunks(&self) -> impl Iterator + '_ { diff --git a/crates/horizon-world/tests/world_io.rs b/crates/horizon-world/tests/world_io.rs index 65b5006a8..3dd897794 100644 --- a/crates/horizon-world/tests/world_io.rs +++ b/crates/horizon-world/tests/world_io.rs @@ -74,6 +74,70 @@ fn external_threshold_negative_coordinates_and_timestamps_roundtrip() { assert!(reader.read(ChunkPos::new(0, 0)).is_err()); } +#[test] +fn in_place_writes_reuse_freed_sectors_and_keep_other_chunks() { + let dir = Directory::new(); + let (a, b, c) = ( + ChunkPos::new(-32, 0), + ChunkPos::new(-31, 0), + ChunkPos::new(-1, 31), + ); + let mut region = RegionFile::open_writable(dir.region(), true).unwrap(); + // RegionBitmap: a at sectors 2..4, b at 4. + region.write(a, &record(2 * 4096 - 5)).unwrap(); + region.write(b, &record(100)).unwrap(); + let sectors = |path: &Path| fs::metadata(path).unwrap().len() / 4096; + assert_eq!(sectors(&dir.region()), 5); + // a grows: it moves past b, and its two old sectors are free again. + region.write(a, &record(3 * 4096 - 5)).unwrap(); + assert_eq!(sectors(&dir.region()), 8); + // c fits in a's old sectors; then an oversized c goes to its .mcc file + // (a one-sector stub, at the end: sector 8) and back inline (sector 2). + region.write(c, &record(2 * 4096 - 5)).unwrap(); + assert_eq!(sectors(&dir.region()), 8); + region.write(c, &record(256 * 4096)).unwrap(); + assert!(dir.0.join("c.-1.31.mcc").exists()); + region.write(c, &record(10)).unwrap(); + assert!(!dir.0.join("c.-1.31.mcc").exists()); + assert!(region.clear(b, 7).unwrap()); + assert!(!region.clear(b, 7).unwrap()); + region.sync().unwrap(); + drop(region); + let mut reader = RegionFile::open(dir.region()).unwrap(); + assert_eq!(reader.chunks().map(|(p, _)| p).collect::>(), [a, c]); + assert_eq!( + reader.read(a).unwrap().unwrap().compressed, + record(3 * 4096 - 5).compressed + ); + assert_eq!( + reader.read(c).unwrap().unwrap().compressed, + record(10).compressed + ); + assert!(reader.read(b).unwrap().is_none()); + // Reopened, the bitmap holds the chunks' sectors: new chunks take the + // first free runs (sectors 3 and 4), not theirs. + let mut region = RegionFile::open_writable(dir.region(), false).unwrap(); + region.write(b, &record(4096 - 5)).unwrap(); + region + .write(ChunkPos::new(-30, 0), &record(4096 - 5)) + .unwrap(); + drop(region); + let mut reader = RegionFile::open(dir.region()).unwrap(); + assert_eq!( + reader.read(a).unwrap().unwrap().compressed, + record(3 * 4096 - 5).compressed + ); + assert_eq!( + reader.read(c).unwrap().unwrap().compressed, + record(10).compressed + ); + assert_eq!( + reader.read(b).unwrap().unwrap().compressed, + record(4096 - 5).compressed + ); + assert_eq!(sectors(&dir.region()), 9); +} + #[test] fn failed_writer_cannot_commit_and_removes_its_own_external_files() { let dir = Directory::new(); @@ -206,3 +270,57 @@ fn chunk_storage_edit_preserves_unknown_fields_and_other_blocks() { let record = ChunkRecord::from_nbt(&root, Compression::Lz4, 17, Limits::default()).unwrap(); assert_eq!(record.nbt(Limits::default()).unwrap(), root); } + +/// Saving one chunk into a full region: the whole file rewritten (every +/// chunk read and written again, then the new file published), against +/// vanilla's in-place write. Run with --ignored --nocapture. +#[test] +#[ignore = "microbenchmark; run with --ignored --nocapture"] +fn benchmark_saving_into_a_full_region() { + let dir = Directory::new(); + let chunks: Vec = (0..1024) + .map(|i| ChunkPos::new(-32 + i % 32, i / 32)) + .collect(); + // About what a compressed overworld chunk weighs: two sectors. + let saved = record(6000); + let mut region = RegionFile::open_writable(dir.region(), true).unwrap(); + for &c in &chunks { + region.write(c, &saved).unwrap(); + } + region.sync().unwrap(); + drop(region); + let rounds = 100; + let start = std::time::Instant::now(); + for round in 0..rounds { + let changed = chunks[round * 7 % 1024]; + let mut existing = RegionFile::open(dir.region()).unwrap(); + let mut records = Vec::new(); + for (p, _) in existing.chunks().collect::>() { + let r = if p == changed { + saved.clone() + } else { + existing.read(p).unwrap().unwrap() + }; + records.push((p, r)); + } + let staging = dir.0.join("staging"); + fs::create_dir_all(&staging).unwrap(); + let temporary = staging.join("r.-1.0.mca"); + let _ = fs::remove_file(&temporary); + let mut writer = RegionWriter::create(&temporary).unwrap(); + for (p, r) in &records { + writer.write(*p, r).unwrap(); + } + writer.finish().unwrap(); + fs::rename(&temporary, dir.region()).unwrap(); + } + let rewrite = start.elapsed() / rounds as u32; + let mut region = RegionFile::open_writable(dir.region(), false).unwrap(); + let start = std::time::Instant::now(); + for round in 0..rounds { + region.write(chunks[round * 7 % 1024], &saved).unwrap(); + region.sync().unwrap(); + } + let in_place = start.elapsed() / rounds as u32; + println!("one chunk into a full region: rewrite {rewrite:?}, in place {in_place:?}"); +} From 9190d14b7fa88fb7e409d72b1be48d112f5a35c3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 07:35:27 +0200 Subject: [PATCH 54/67] Block registry: read facts.json as it streams The game and the assets step each parsed facts.json into a JSON tree, then copied each state into an NBT tree, before building the registry. With a pack's 214,000 states each step peaked at 816 MB for a 139 MB registry. The registry now deserializes the rows it needs straight from the file (each row's state and its air and fluid columns, and the defaults): registry peak 816 -> 153 MB, assets peak 816 -> 235 MB, RSS after the assets step 1,344 -> 442 MB. Vanilla: 138 -> 27 MB. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/assets.rs | 73 ++------- crates/horizon-world/src/level/data.rs | 40 +---- crates/horizon-world/src/registry.rs | 58 ++++++-- crates/horizon-world/src/registry/facts.rs | 164 +++++++++++++++++++++ 4 files changed, 228 insertions(+), 107 deletions(-) create mode 100644 crates/horizon-world/src/registry/facts.rs diff --git a/crates/horizon-client/src/assets.rs b/crates/horizon-client/src/assets.rs index f278740f5..c42f0aaf7 100644 --- a/crates/horizon-client/src/assets.rs +++ b/crates/horizon-client/src/assets.rs @@ -2,14 +2,13 @@ use crate::Result; use horizon_world::{ DATA_VERSION, heightmap::HeightmapState, - nbt::Tag, registry::{BlockRegistry, BlockStateId}, worldgen::{tags::TagDefinitions, terrain::TerrainBlockStates}, }; use serde_json::Value; use std::{ collections::BTreeSet, - fs::{self, File}, + fs::File, io::{self, BufReader}, path::Path, sync::Arc, @@ -74,24 +73,6 @@ pub struct Assets { fn invalid(message: &str) -> io::Error { io::Error::new(io::ErrorKind::InvalidData, message) } -fn tag(value: &Value) -> Result { - Ok(match value { - Value::String(value) => Tag::String(value.as_str().into()), - Value::Number(value) => Tag::Int(i32::try_from( - value - .as_i64() - .ok_or_else(|| invalid("invalid registry number"))?, - )?), - Value::Array(values) => Tag::List(values.iter().map(tag).collect::>()?), - Value::Object(values) => Tag::Compound( - values - .iter() - .map(|(name, value)| Ok((name.as_str().into(), tag(value)?))) - .collect::>()?, - ), - _ => return Err(invalid("unsupported registry value").into()), - }) -} impl Assets { pub fn load(root: &Path) -> Result { let manifest: Value = serde_json::from_reader(BufReader::new(File::open( @@ -100,23 +81,8 @@ impl Assets { if manifest["data_version"].as_i64() != Some(i64::from(DATA_VERSION)) { return Err(invalid("resources do not match Horizon's pinned data version").into()); } - let facts: Value = serde_json::from_slice(&fs::read(root.join("facts.json"))?)?; - let states = facts["states"] - .as_array() - .ok_or_else(|| invalid("missing registry state facts"))?; - let registry = BlockRegistry::from_nbt(&Tag::compound([ - ("DataVersion", Tag::Int(DATA_VERSION)), - ( - "states", - Tag::List( - states - .iter() - .map(|state| tag(&state[4])) - .collect::>()?, - ), - ), - ("defaults", tag(&facts["defaults"])?), - ]))?; + let (registry, flags) = + BlockRegistry::from_facts_json_with_flags(File::open(root.join("facts.json"))?)?; let bundle: Value = serde_json::from_reader(BufReader::new(File::open(root.join("block-tags.json"))?))?; let mut definitions = TagDefinitions::default(); @@ -132,7 +98,7 @@ impl Assets { )?; } let built = - definitions.build(|name, _| facts["defaults"].get(name).map(|_| name.to_owned()))?; + definitions.build(|name, _| registry.default_state(name).map(|_| name.to_owned()))?; if !built.missing.is_empty() { return Err(invalid("unresolved block tags").into()); } @@ -158,27 +124,20 @@ impl Assets { .cloned() .collect(); let properties = Properties(Arc::new( - states + flags .iter() - .map(|state| { + .enumerate() + .map(|(i, &(air, fluid))| { + let name = ®istry + .state(BlockStateId(i as u32)) + .ok_or_else(|| invalid("missing registry state"))? + .name; Ok(StateFacts { - air: state[1] - .as_bool() - .ok_or_else(|| invalid("missing air fact"))?, - fluid: state[2] - .as_bool() - .ok_or_else(|| invalid("missing fluid fact"))?, - air_block: state[0].as_str() == Some("minecraft:air"), - motion: motion.contains( - state[0] - .as_str() - .ok_or_else(|| invalid("missing state name"))?, - ), - no_leaves: no_leaves.contains( - state[0] - .as_str() - .ok_or_else(|| invalid("missing state name"))?, - ), + air, + fluid, + air_block: name == "minecraft:air", + motion: motion.contains(name.as_str()), + no_leaves: no_leaves.contains(name.as_str()), }) }) .collect::>()?, diff --git a/crates/horizon-world/src/level/data.rs b/crates/horizon-world/src/level/data.rs index 0862aa5b8..e0ef0dca1 100644 --- a/crates/horizon-world/src/level/data.rs +++ b/crates/horizon-world/src/level/data.rs @@ -613,45 +613,9 @@ fn set_type(value: &Value, cache: &mut HashMap>) -> Option io::Result { - use crate::{DATA_VERSION, nbt::Tag}; - fn tag(value: &Value) -> io::Result { - Ok(match value { - Value::String(value) => Tag::String(value.as_str().into()), - Value::Number(value) => Tag::Int( - value - .as_i64() - .and_then(|v| i32::try_from(v).ok()) - .ok_or_else(|| fail("invalid registry number"))?, - ), - Value::Array(values) => Tag::List(values.iter().map(tag).collect::>()?), - Value::Object(values) => Tag::Compound( - values - .iter() - .map(|(name, value)| Ok((name.as_str().into(), tag(value)?))) - .collect::>()?, - ), - _ => return Err(fail("unsupported registry value")), - }) - } - let facts = read_json(&root.join("facts.json"))?; - let states = facts["states"] - .as_array() - .ok_or_else(|| fail("missing registry state facts"))?; - BlockRegistry::from_nbt(&Tag::compound([ - ("DataVersion", Tag::Int(DATA_VERSION)), - ( - "states", - Tag::List( - states - .iter() - .map(|state| tag(&state[4])) - .collect::>()?, - ), - ), - ("defaults", tag(&facts["defaults"])?), - ])) + BlockRegistry::from_facts_json(File::open(root.join("facts.json"))?) } pub(super) fn load(registry: &BlockRegistry, root: &Path) -> io::Result { diff --git a/crates/horizon-world/src/registry.rs b/crates/horizon-world/src/registry.rs index 30cd5e83c..9ce9a6814 100644 --- a/crates/horizon-world/src/registry.rs +++ b/crates/horizon-world/src/registry.rs @@ -1,5 +1,6 @@ //! Runtime block-state IDs imported from a version-pinned registry data export. //! This supplies identities and default properties, not block behavior. +mod facts; mod json; use crate::{DATA_VERSION, chunk::BlockState, compression::invalid, nbt::Tag}; pub use json::BlockStateJsonCodec; @@ -45,17 +46,53 @@ impl BlockRegistry { let Some(Tag::Compound(defaults)) = tag.get("defaults") else { return Err(invalid("missing registry defaults")); }; - if entries.is_empty() || entries.len() > i32::MAX as usize { + let states = entries + .iter() + .map(BlockState::from_nbt) + .collect::>()?; + let defaults = defaults + .iter() + .map(|(name, id)| { + let name = name.to_string().map_err(|e| invalid(e.to_string()))?; + let Tag::Int(id) = id else { + return Err(invalid("default state ID is not an int")); + }; + Ok((name, i64::from(*id))) + }) + .collect::>()?; + Self::from_states(states, defaults) + } + + /// The registry of a resources `facts.json` (`{"states": [[..., ..., ..., + /// ..., state], ...], "defaults": {name: id}}`), read as it streams: no + /// JSON or NBT tree of the whole file is built. For a pack of 190,000 + /// states the two trees were several hundred megabytes at startup. + pub fn from_facts_json(reader: impl io::Read) -> io::Result { + Ok(Self::from_facts_json_with_flags(reader)?.0) + } + + /// [`Self::from_facts_json`], with each state's `(air, fluid)` columns. + pub fn from_facts_json_with_flags( + reader: impl io::Read, + ) -> io::Result<(Self, Vec<(bool, bool)>)> { + let facts: facts::Facts = serde_json::from_reader(io::BufReader::new(reader)) + .map_err(|e| invalid(e.to_string()))?; + let flags = facts.states.iter().map(|r| (r.air, r.fluid)).collect(); + let states = facts.states.into_iter().map(|r| r.state).collect(); + Ok((Self::from_states(states, facts.defaults)?, flags)) + } + + fn from_states(states: Vec, defaults: BTreeMap) -> io::Result { + if states.is_empty() || states.len() > i32::MAX as usize { return Err(invalid("invalid registry size")); } let mut result = Self { - states: Vec::with_capacity(entries.len()), - ids: HashMap::with_capacity(entries.len()), + states: Vec::with_capacity(states.len()), + ids: HashMap::with_capacity(states.len()), collided: BTreeMap::new(), defaults: BTreeMap::new(), }; - for (i, entry) in entries.iter().enumerate() { - let state = BlockState::from_nbt(entry)?; + for (i, state) in states.into_iter().enumerate() { if result.id(&state).is_some() { return Err(invalid("duplicate block registry state")); } @@ -72,19 +109,16 @@ impl BlockRegistry { result.states.push(state); } for (name, id) in defaults { - let name = name.to_string().map_err(|e| invalid(e.to_string()))?; - let Tag::Int(id) = id else { - return Err(invalid("default state ID is not an int")); - }; - if *id < 0 + if id < 0 + || id > i64::from(i32::MAX) || result .states - .get(*id as usize) + .get(id as usize) .is_none_or(|s| s.name != name) { return Err(invalid("invalid default block state ID")); } - result.defaults.insert(name, BlockStateId(*id as u32)); + result.defaults.insert(name, BlockStateId(id as u32)); } let names: BTreeSet<_> = result.states.iter().map(|s| &s.name).collect(); if names.len() != result.defaults.len() diff --git a/crates/horizon-world/src/registry/facts.rs b/crates/horizon-world/src/registry/facts.rs new file mode 100644 index 000000000..3371a8283 --- /dev/null +++ b/crates/horizon-world/src/registry/facts.rs @@ -0,0 +1,164 @@ +//! The part of `facts.json` the block registry reads, deserialized as it +//! streams: each row's state (its fifth column) and the defaults. +use crate::chunk::BlockState; +use serde::{ + Deserialize, Deserializer, + de::{self, IgnoredAny, SeqAccess, Visitor}, +}; +use std::{collections::BTreeMap, fmt}; + +#[derive(Deserialize)] +pub(super) struct Facts { + pub states: Vec, + pub defaults: BTreeMap, +} + +/// One `states` row: `[name, air, fluid, description, state, ...]`, keeping +/// the two flags and the state. +pub(super) struct Row { + pub air: bool, + pub fluid: bool, + pub state: BlockState, +} + +impl<'de> Deserialize<'de> for Row { + fn deserialize>(deserializer: D) -> Result { + struct RowVisitor; + impl<'de> Visitor<'de> for RowVisitor { + type Value = Row; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("a registry state row") + } + fn visit_seq>(self, mut seq: A) -> Result { + let length = |i| de::Error::invalid_length(i, &self); + seq.next_element::()?.ok_or_else(|| length(0))?; + let air = seq.next_element::()?.ok_or_else(|| length(1))?; + let fluid = seq.next_element::()?.ok_or_else(|| length(2))?; + seq.next_element::()?.ok_or_else(|| length(3))?; + let state = seq.next_element::()?.ok_or_else(|| length(4))?; + while seq.next_element::()?.is_some() {} + Ok(Row { + air, + fluid, + state: state.0, + }) + } + } + deserializer.deserialize_seq(RowVisitor) + } +} + +/// A state as BlockState::from_nbt reads it: a name, or `{"id", "properties"}`. +struct State(BlockState); + +impl<'de> Deserialize<'de> for State { + fn deserialize>(deserializer: D) -> Result { + #[derive(Deserialize)] + #[serde(untagged)] + enum Shape { + Name(String), + Full { + id: String, + #[serde(default)] + properties: BTreeMap, + }, + } + Ok(State(match Shape::deserialize(deserializer)? { + Shape::Name(name) => BlockState { + name, + properties: BTreeMap::new(), + }, + Shape::Full { id, properties } => BlockState { + name: id, + properties, + }, + })) + } +} + +#[cfg(test)] +mod tests { + use crate::{ + DATA_VERSION, + chunk::BlockState, + nbt::Tag, + registry::{BlockRegistry, BlockStateId}, + }; + use serde_json::Value; + use std::path::Path; + + #[test] + fn reads_states_and_defaults() { + let json = br#"{"defaults": {"minecraft:air": 0, "minecraft:oak_slab": 2}, + "states": [ + ["minecraft:air", true, false, "Block{minecraft:air}", "minecraft:air"], + ["minecraft:oak_slab", false, false, "x", {"id": "minecraft:oak_slab", "properties": {"type": "top"}}, "extra"], + ["minecraft:oak_slab", false, true, "x", {"id": "minecraft:oak_slab", "properties": {"type": "bottom"}}] + ], "other": [1, 2]}"#; + let (registry, flags) = BlockRegistry::from_facts_json_with_flags(&json[..]).unwrap(); + assert_eq!(registry.len(), 3); + assert_eq!(flags, [(true, false), (false, false), (false, true)]); + assert_eq!( + registry.default_state("minecraft:oak_slab"), + Some(BlockStateId(2)) + ); + let top = BlockState { + name: "minecraft:oak_slab".into(), + properties: [("type".into(), "top".into())].into(), + }; + assert_eq!(registry.id(&top), Some(BlockStateId(1))); + // A default naming another block, a short row, a missing default. + for bad in [ + &br#"{"defaults": {"minecraft:air": 1}, "states": [["minecraft:air", true, false, "", "minecraft:air"], ["minecraft:stone", false, false, "", "minecraft:stone"]]}"#[..], + br#"{"defaults": {"minecraft:air": 0}, "states": [["minecraft:air", true, false, ""]]}"#, + br#"{"defaults": {}, "states": [["minecraft:air", true, false, "", "minecraft:air"]]}"#, + ] { + assert!(BlockRegistry::from_facts_json(bad).is_err()); + } + } + + /// The streamed registry is the one the JSON and NBT trees built. + #[test] + fn matches_the_tree_built_registry() { + let path = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3/facts.json"); + let Ok(bytes) = std::fs::read(&path) else { + eprintln!("skipped: no prepared facts.json"); + return; + }; + fn tag(v: &Value) -> Tag { + match v { + Value::String(s) => Tag::String(s.as_str().into()), + Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), + Value::Object(o) => { + Tag::Compound(o.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) + } + _ => unreachable!(), + } + } + let facts: Value = serde_json::from_slice(&bytes).unwrap(); + let rows = facts["states"].as_array().unwrap(); + let tree = BlockRegistry::from_nbt(&Tag::compound([ + ("DataVersion", Tag::Int(DATA_VERSION)), + ( + "states", + Tag::List(rows.iter().map(|r| tag(&r[4])).collect()), + ), + ("defaults", tag(&facts["defaults"])), + ])) + .unwrap(); + let (streamed, flags) = BlockRegistry::from_facts_json_with_flags(&bytes[..]).unwrap(); + assert_eq!(streamed.len(), tree.len()); + for (i, row) in rows.iter().enumerate() { + let id = BlockStateId(i as u32); + assert_eq!(streamed.state(id), tree.state(id)); + assert_eq!( + flags[i], + (row[1].as_bool().unwrap(), row[2].as_bool().unwrap()) + ); + } + for name in facts["defaults"].as_object().unwrap().keys() { + assert_eq!(streamed.default_state(name), tree.default_state(name)); + } + } +} From 7d27950e31983b0b1ac7f59f0b271a4f15028338 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 05:55:57 +0200 Subject: [PATCH 55/67] Carry: write worldgen block states in 26.3's shape 1.21.1 writes a block or fluid state in worldgen JSON as {"Name", "Properties"}; 26.3's codec reads {"id", "properties"} and refuses the old shape, so every processor list, feature or pool holding one failed to decode. Carry now rewrites them. On ATM10's carried mods: 1,005 files in 17 mods. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/worldgen.rs | 67 ++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index c44cc1108..c308f4159 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -1,6 +1,7 @@ -//! Structure mods' worldgen, carried over: the structure, placement and pool -//! element types of the common structure libraries rewritten as vanilla's, -//! and the loader's convention biome tags (`c:`) the structures name. +//! Structure mods' worldgen, carried over: block and fluid states in 26.3's +//! shape, the structure, placement and pool element types of the common +//! structure libraries rewritten as vanilla's, and the loader's convention +//! biome tags (`c:`) the structures name. //! //! The libraries' types add rules on top of vanilla's (a structure that may //! not start in liquid, a minimum distance from the world origin, a count @@ -44,6 +45,8 @@ const ELEMENTS: [(&str, &str); 3] = [ pub struct Upgrade { /// Files rewritten to vanilla types. pub mapped: usize, + /// Files whose block and fluid states were written in 26.3's shape. + pub block_states: usize, /// Types left as they are (Horizon skips what it cannot run), by type. pub unsupported: BTreeMap, } @@ -51,6 +54,41 @@ pub struct Upgrade { /// Rewrites one `data//worldgen//...` file in place; whether it /// changed. pub fn upgrade(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool { + let states = block_states(v); + report.block_states += usize::from(states); + upgrade_types(kind, v, report) || states +} + +/// 1.21.1 writes a block or fluid state as `{"Name": ..., "Properties": +/// {...}}`; 26.3's codec reads `{"id": ..., "properties": {...}}` and +/// refuses the old shape, which fails the processor list, feature or pool +/// holding it. Whether anything changed. +fn block_states(v: &mut Value) -> bool { + match v { + Value::Object(o) => { + let state = o.get("Name").is_some_and(Value::is_string) + && o.keys().all(|k| k == "Name" || k == "Properties"); + if state { + if let Some(name) = o.remove("Name") { + o.insert("id".into(), name); + } + if let Some(properties) = o.remove("Properties") { + o.insert("properties".into(), properties); + } + return true; + } + o.values_mut() + .fold(false, |changed, x| block_states(x) | changed) + } + Value::Array(a) => a + .iter_mut() + .fold(false, |changed, x| block_states(x) | changed), + _ => false, + } +} + +/// The library types of one file rewritten as vanilla's; whether it changed. +fn upgrade_types(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool { let mut unsupported = |t: &str| *report.unsupported.entry(t.to_owned()).or_default() += 1; let changed = match kind { "structure" => match v["type"].as_str() { @@ -223,6 +261,7 @@ mod tests { "minecraft:empty_pool_element" ); assert_eq!(report.mapped, 3); + assert_eq!(report.block_states, 0); assert_eq!( report.unsupported, BTreeMap::from([ @@ -235,6 +274,28 @@ mod tests { ); } + #[test] + fn writes_block_states_in_26_3_shape() { + let mut report = Upgrade::default(); + let mut list = json!({"processors": [{"processor_type": "minecraft:rule", "rules": [{ + "input_predicate": {"predicate_type": "minecraft:always_true"}, + "output_state": {"Name": "minecraft:oak_stairs", "Properties": {"facing": "east"}}}]}]}); + assert!(upgrade("processor_list", &mut list, &mut report)); + assert_eq!( + list["processors"][0]["rules"][0]["output_state"], + json!({"id": "minecraft:oak_stairs", "properties": {"facing": "east"}}) + ); + let mut spring = json!({"type": "minecraft:spring_feature", "config": { + "state": {"Name": "minecraft:water"}, "valid_blocks": ["minecraft:stone"]}}); + assert!(upgrade("configured_feature", &mut spring, &mut report)); + assert_eq!(spring["config"]["state"], json!({"id": "minecraft:water"})); + // An object with other keys is not a state; a 26.3 state is left as it is. + let mut other = json!({"Name": "x", "weight": 1, "state": {"id": "minecraft:stone"}}); + assert!(!upgrade("configured_feature", &mut other, &mut report)); + assert_eq!(report.block_states, 2); + assert_eq!(report.mapped, 0); + } + #[test] fn convention_tags_include_theirs() { let conventions = BTreeMap::from([ From f72c8674ad2d2ab616b028f814c9fa07132e2dfe Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 06:31:25 +0200 Subject: [PATCH 56/67] Carry: the start and placement rules of mapped jigsaws Repurposed Structures' and Moog's jigsaws and spreads add rules on top of vanilla's; mapped to vanilla they were lost, and Moog's spreads structures 1.65 times as far apart as its files say, so MVS alone started 9x as often as on NeoForge. Carry now writes the start rules under horizon:start_checks, the minimum distance and the super exclusion zone's sets (from the mod's structure set tags) under horizon: placement keys, and scales Moog's spacing. Layout rules (piece clipping, burying) are still lost and counted in the carry report's rules_lost. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/carry.rs | 4 +- crates/horizon-port-pack/src/worldgen.rs | 321 +++++++++++++++++++++-- 2 files changed, 306 insertions(+), 19 deletions(-) diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index 7b9bc8fee..c73570684 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -362,6 +362,7 @@ pub fn carry( let mut convention = BTreeSet::new(); let mut block_loot = BTreeSet::new(); let mut biome_conventions = BTreeSet::new(); + let set_tags = crate::worldgen::structure_set_tags(&mut zip); for i in 0..zip.len() { let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; let name = entry.name().to_owned(); @@ -421,7 +422,8 @@ pub fn carry( if kind == "structure" { crate::worldgen::convention_tags(&v["biomes"], &mut biome_conventions); } - if crate::worldgen::upgrade(kind, &mut v, &mut report.worldgen_types) { + if crate::worldgen::upgrade(kind, &mut v, &mut report.worldgen_types, &set_tags) + { bytes = serde_json::to_vec_pretty(&v).unwrap_or(bytes); } } diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index c308f4159..91ff1bfef 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -3,12 +3,16 @@ //! structure libraries rewritten as vanilla's, and the loader's convention //! biome tags (`c:`) the structures name. //! -//! The libraries' types add rules on top of vanilla's (a structure that may -//! not start in liquid, a minimum distance from the world origin, a count -//! cap per piece). Mapped to vanilla, those rules are lost: the structure -//! keeps its pieces and its spacing but can start where the mod would not. -//! Each rewrite is counted in the carry report, and every type left as it is -//! is listed, so the coverage report can say what generates and how closely. +//! The libraries' types add rules on top of vanilla's. The start and +//! placement rules of Repurposed Structures' and Moog's jigsaws (no start in +//! liquid, biomes and terrain height in a radius, a minimum distance from +//! the world origin, sets to keep away from) are written under `horizon:` +//! keys the game reads (`horizon_world::worldgen::structure::start_checks`), +//! so those structures start where the mod would. Rules that change how the +//! pieces are laid out (piece caps, burying, clipping) are lost and counted +//! by name. Each rewrite is counted in the carry report, and every type left +//! as it is is listed, so the coverage report can say what generates and how +//! closely. use serde_json::Value; use std::{ collections::{BTreeMap, BTreeSet}, @@ -27,6 +31,25 @@ const SPREADS: [&str; 2] = [ "moogs_structures:advanced_random_spread", "repurposed_structures:advanced_random_spread", ]; +/// Moog's Structure Lib spreads its structures 1.65 times as far apart as +/// its files say (AdvancedRandomSpread's constructor). +const MOOGS_SPACING: f64 = 1.65; +/// The jigsaws' start rules and the `horizon:start_checks` key each maps to. +const START_RULES: [(&str, &str); 3] = [ + ("valid_biome_radius_check", "biome_radius"), + ("terrain_height_radius_check", "terrain_radius"), + ("allowed_terrain_height_range", "terrain_range"), +]; +/// The jigsaws' rules on laying out pieces, which vanilla's jigsaw has no +/// place for. +const LAYOUT_RULES: [&str; 6] = [ + "y_allowance", + "burying_type", + "use_bounding_box_hack", + "pools_that_ignore_boundaries", + "enhanced_terrain_adaptation", + "vertical_distance_from_start_piece", +]; /// Pool elements that are vanilla's with extra fields: (mod type, vanilla type). const ELEMENTS: [(&str, &str); 3] = [ ( @@ -49,14 +72,23 @@ pub struct Upgrade { pub block_states: usize, /// Types left as they are (Horizon skips what it cannot run), by type. pub unsupported: BTreeMap, + /// Rules of mapped types the game does not run, by field. + pub rules_lost: BTreeMap, } /// Rewrites one `data//worldgen//...` file in place; whether it /// changed. -pub fn upgrade(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool { +/// `set_tags` are the mod's structure set tags, expanded +/// ([`structure_set_tags`]). +pub fn upgrade( + kind: &str, + v: &mut Value, + report: &mut Upgrade, + set_tags: &BTreeMap>, +) -> bool { let states = block_states(v); report.block_states += usize::from(states); - upgrade_types(kind, v, report) || states + upgrade_types(kind, v, report, set_tags) || states } /// 1.21.1 writes a block or fluid state as `{"Name": ..., "Properties": @@ -88,11 +120,22 @@ fn block_states(v: &mut Value) -> bool { } /// The library types of one file rewritten as vanilla's; whether it changed. -fn upgrade_types(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool { +fn upgrade_types( + kind: &str, + v: &mut Value, + report: &mut Upgrade, + set_tags: &BTreeMap>, +) -> bool { + let lost = &mut report.rules_lost; let mut unsupported = |t: &str| *report.unsupported.entry(t.to_owned()).or_default() += 1; let changed = match kind { "structure" => match v["type"].as_str() { Some(t) if JIGSAWS.contains(&t) => { + let liquid = match t.split_once(':').map(|(ns, _)| ns) { + Some("moogs_structures") => "top", + _ => "lowest", + }; + start_checks(v, liquid, lost); v["type"] = "minecraft:jigsaw".into(); true } @@ -104,7 +147,13 @@ fn upgrade_types(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool { }, "structure_set" => match v["placement"]["type"].as_str() { Some(t) if SPREADS.contains(&t) => { - v["placement"]["type"] = "minecraft:random_spread".into(); + let moogs = t.starts_with("moogs_structures:"); + let p = &mut v["placement"]; + if moogs { + moogs_spacing(p, lost); + } + placement_rules(p, set_tags, lost); + p["type"] = "minecraft:random_spread".into(); true } Some(t) if !t.starts_with("minecraft:") => { @@ -144,6 +193,144 @@ fn upgrade_types(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool { changed } +/// Moves a Repurposed Structures or Moog's jigsaw's start rules under +/// `horizon:start_checks`, and counts the layout rules it loses. `liquid` is +/// the block its no-start-in-liquid rule reads. +fn start_checks(v: &mut Value, liquid: &str, lost: &mut BTreeMap) { + let Some(o) = v.as_object_mut() else { + return; + }; + let mut checks = serde_json::Map::new(); + for (from, to) in START_RULES { + if let Some(n) = o.remove(from) { + checks.insert(to.into(), n); + } + } + if o.remove("cannot_spawn_in_liquid").and_then(|b| b.as_bool()) == Some(true) { + checks.insert("liquid".into(), liquid.into()); + } + // y_allowance bounds the terrain heights checked; it also clips pieces, + // which is lost. + for (from, to) in [("min_y_allowed", "min_y"), ("max_y_allowed", "max_y")] { + if let Some(n) = o.get("y_allowance").and_then(|y| y.get(from)) { + checks.insert(to.into(), n.clone()); + } + } + for rule in LAYOUT_RULES { + if o.remove(rule).is_some_and(|r| r != Value::Bool(false)) { + *lost.entry(rule.into()).or_default() += 1; + } + } + if !checks.is_empty() { + o.insert("horizon:start_checks".into(), checks.into()); + } +} + +/// AdvancedRandomSpread's spacing and separation, as Moog's computes them +/// with its default config: 1.65 times the file's, rounded, the separation +/// below the spacing. `when_replacing` (other numbers while a preset +/// replaces a vanilla structure) is lost. +fn moogs_spacing(p: &mut Value, lost: &mut BTreeMap) { + let scaled = |k: &str| (p[k].as_f64().unwrap_or(0.) * MOOGS_SPACING).round() as i64; + let spacing = scaled("spacing").max(1); + let separation = scaled("separation").min(spacing - 1); + p["spacing"] = spacing.into(); + p["separation"] = separation.into(); + if let Some(o) = p.as_object_mut() { + if o.remove("when_replacing").is_some() { + *lost.entry("when_replacing".into()).or_default() += 1; + } + } +} + +/// An advanced random spread's minimum distance from the world origin and +/// its super exclusion zone (sets named by a tag) under `horizon:` keys. +fn placement_rules( + p: &mut Value, + set_tags: &BTreeMap>, + lost: &mut BTreeMap, +) { + let Some(o) = p.as_object_mut() else { + return; + }; + if let Some(d) = o.remove("min_distance_from_world_origin") { + o.insert("horizon:min_distance_from_world_origin".into(), d); + } + if let Some(zone) = o.remove("super_exclusion_zone") { + let sets: Vec = match &zone["other_set"] { + Value::String(s) => match s.strip_prefix('#') { + Some(tag) => set_tags.get(tag).cloned().unwrap_or_default(), + None => vec![s.clone()], + }, + Value::Array(a) => a + .iter() + .filter_map(|s| Some(s.as_str()?.to_owned())) + .collect(), + _ => Vec::new(), + }; + if sets.is_empty() { + *lost.entry("super_exclusion_zone".into()).or_default() += 1; + } else { + o.insert( + "horizon:exclusion_zone".into(), + serde_json::json!({"other_sets": sets, "chunk_count": zone["chunk_count"]}), + ); + } + } +} + +/// The mod's structure set tags (`data//tags/worldgen/structure_set/`), +/// by tag id, with the tags they include expanded. Optional entries +/// (`{"id": ..., "required": false}`) count as their id. +pub fn structure_set_tags( + zip: &mut zip::ZipArchive, +) -> BTreeMap> { + let mut raw: BTreeMap> = BTreeMap::new(); + for i in 0..zip.len() { + let Ok(mut entry) = zip.by_index(i) else { + continue; + }; + let Some(id) = entry + .name() + .strip_prefix("data/") + .and_then(|r| r.split_once("/tags/worldgen/structure_set/")) + .and_then(|(ns, path)| Some(format!("{ns}:{}", path.strip_suffix(".json")?))) + else { + continue; + }; + let mut text = String::new(); + let Ok(v) = entry.read_to_string(&mut text).map_err(drop).and_then(|_| { + serde_json::from_str::(text.trim_start_matches('\u{feff}')).map_err(drop) + }) else { + continue; + }; + let values = v["values"].as_array().into_iter().flatten(); + raw.insert( + id, + values + .filter_map(|e| e.as_str().or_else(|| e["id"].as_str()).map(str::to_owned)) + .collect(), + ); + } + let expand = |tag: &str| { + let (mut out, mut seen, mut todo) = (Vec::new(), BTreeSet::new(), vec![tag.to_owned()]); + while let Some(t) = todo.pop() { + if !seen.insert(t.clone()) { + continue; + } + for e in raw.get(&t).into_iter().flatten() { + match e.strip_prefix('#') { + Some(inner) => todo.push(inner.to_owned()), + None if !out.contains(e) => out.push(e.clone()), + None => {} + } + } + } + out + }; + raw.keys().map(|t| (t.clone(), expand(t))).collect() +} + /// The `#c:` biome tags `v` names. pub fn convention_tags(v: &Value, out: &mut BTreeSet) { match v { @@ -231,27 +418,40 @@ mod tests { use super::*; use serde_json::json; + const NO_TAGS: BTreeMap> = BTreeMap::new(); + #[test] fn maps_library_types_to_vanillas() { let mut report = Upgrade::default(); let mut s = json!({"type": "lithostitched:jigsaw", "start_pool": "a:b"}); - assert!(upgrade("structure", &mut s, &mut report)); + assert!(upgrade("structure", &mut s, &mut report, &NO_TAGS)); assert_eq!(s["type"], "minecraft:jigsaw"); let mut nether = json!({"type": "moogs_structures:moogs_structures_generic_nether_jigsaw_structure"}); - assert!(!upgrade("structure", &mut nether, &mut report)); + assert!(!upgrade("structure", &mut nether, &mut report, &NO_TAGS)); let mut set = json!({"placement": {"type": "moogs_structures:advanced_random_spread", "spacing": 10, "separation": 5, "min_distance_from_world_origin": 250}}); - assert!(upgrade("structure_set", &mut set, &mut report)); + assert!(upgrade("structure_set", &mut set, &mut report, &NO_TAGS)); assert_eq!(set["placement"]["type"], "minecraft:random_spread"); - assert_eq!(set["placement"]["spacing"], 10); + // Moog's spreads 1.65 times as far as its file says. + assert_eq!(set["placement"]["spacing"], 17); + assert_eq!(set["placement"]["separation"], 8); + assert_eq!( + set["placement"]["horizon:min_distance_from_world_origin"], + 250 + ); + assert!( + set["placement"] + .get("min_distance_from_world_origin") + .is_none() + ); let mut pool = json!({"elements": [ {"weight": 1, "element": {"element_type": "moogs_structures:versioned_single_pool_element", "location": "m:x"}}, {"weight": 1, "element": {"element_type": "minecraft:list_pool_element", "elements": [ {"element_type": "empty_pool_element"}]}}, {"weight": 1, "element": {"element_type": "yungsapi:yung_single_element"}} ]}); - assert!(upgrade("template_pool", &mut pool, &mut report)); + assert!(upgrade("template_pool", &mut pool, &mut report, &NO_TAGS)); assert_eq!( pool["elements"][0]["element"]["element_type"], "minecraft:single_pool_element" @@ -274,24 +474,109 @@ mod tests { ); } + #[test] + fn keeps_the_jigsaws_start_rules() { + let mut report = Upgrade::default(); + let mut moogs = json!({"type": "moogs_structures:moogs_structures_generic_jigsaw_structure", + "cannot_spawn_in_liquid": true, "terrain_height_radius_check": 2, + "allowed_terrain_height_range": 12, "valid_biome_radius_check": 1, + "y_allowance": {"min_y_allowed": 60}, "burying_type": "lowest_corner", + "use_bounding_box_hack": false, "size": 7}); + assert!(upgrade("structure", &mut moogs, &mut report, &NO_TAGS)); + assert_eq!( + moogs["horizon:start_checks"], + json!({"liquid": "top", "terrain_radius": 2, "terrain_range": 12, + "biome_radius": 1, "min_y": 60}) + ); + for gone in [ + "cannot_spawn_in_liquid", + "y_allowance", + "burying_type", + "use_bounding_box_hack", + ] { + assert!(moogs.get(gone).is_none(), "{gone}"); + } + assert_eq!(moogs["size"], 7); + // Repurposed Structures reads the block above the bottom. + let mut rs = json!({"type": "repurposed_structures:generic_jigsaw_structure", + "cannot_spawn_in_liquid": true}); + assert!(upgrade("structure", &mut rs, &mut report, &NO_TAGS)); + assert_eq!(rs["horizon:start_checks"], json!({"liquid": "lowest"})); + // Nothing to check: no key. + let mut plain = json!({"type": "lithostitched:jigsaw", "cannot_spawn_in_liquid": false}); + assert!(upgrade("structure", &mut plain, &mut report, &NO_TAGS)); + assert!(plain.get("horizon:start_checks").is_none()); + assert_eq!( + report.rules_lost, + BTreeMap::from([("burying_type".into(), 1), ("y_allowance".into(), 1)]) + ); + } + + #[test] + fn super_exclusion_zones_name_their_sets() { + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default(); + for (name, text) in [ + ( + "data/rs/tags/worldgen/structure_set/avoid.json", + r##"{"values": ["minecraft:villages", "#rs:more", {"id": "x:y", "required": false}]}"##, + ), + ( + "data/rs/tags/worldgen/structure_set/more.json", + r##"{"values": ["minecraft:villages", "rs:cities", "#rs:avoid"]}"##, + ), + ] { + zip.start_file(name, opts).unwrap(); + std::io::Write::write_all(&mut zip, text.as_bytes()).unwrap(); + } + let mut zip = zip::ZipArchive::new(zip.finish().unwrap()).unwrap(); + let tags = structure_set_tags(&mut zip); + assert_eq!(tags["rs:avoid"], ["minecraft:villages", "x:y", "rs:cities"]); + let mut report = Upgrade::default(); + let mut set = json!({"placement": {"type": "repurposed_structures:advanced_random_spread", + "spacing": 30, "separation": 10, + "super_exclusion_zone": {"chunk_count": 4, "other_set": "#rs:avoid"}}}); + assert!(upgrade("structure_set", &mut set, &mut report, &tags)); + assert_eq!(set["placement"]["spacing"], 30); + assert_eq!( + set["placement"]["horizon:exclusion_zone"], + json!({"other_sets": ["minecraft:villages", "x:y", "rs:cities"], "chunk_count": 4}) + ); + let mut unknown = json!({"placement": {"type": "repurposed_structures:advanced_random_spread", + "spacing": 30, "separation": 10, + "super_exclusion_zone": {"chunk_count": 4, "other_set": "#rs:none"}}}); + assert!(upgrade("structure_set", &mut unknown, &mut report, &tags)); + assert_eq!(report.rules_lost["super_exclusion_zone"], 1); + } + #[test] fn writes_block_states_in_26_3_shape() { let mut report = Upgrade::default(); let mut list = json!({"processors": [{"processor_type": "minecraft:rule", "rules": [{ "input_predicate": {"predicate_type": "minecraft:always_true"}, "output_state": {"Name": "minecraft:oak_stairs", "Properties": {"facing": "east"}}}]}]}); - assert!(upgrade("processor_list", &mut list, &mut report)); + assert!(upgrade("processor_list", &mut list, &mut report, &NO_TAGS)); assert_eq!( list["processors"][0]["rules"][0]["output_state"], json!({"id": "minecraft:oak_stairs", "properties": {"facing": "east"}}) ); let mut spring = json!({"type": "minecraft:spring_feature", "config": { "state": {"Name": "minecraft:water"}, "valid_blocks": ["minecraft:stone"]}}); - assert!(upgrade("configured_feature", &mut spring, &mut report)); + assert!(upgrade( + "configured_feature", + &mut spring, + &mut report, + &NO_TAGS + )); assert_eq!(spring["config"]["state"], json!({"id": "minecraft:water"})); // An object with other keys is not a state; a 26.3 state is left as it is. let mut other = json!({"Name": "x", "weight": 1, "state": {"id": "minecraft:stone"}}); - assert!(!upgrade("configured_feature", &mut other, &mut report)); + assert!(!upgrade( + "configured_feature", + &mut other, + &mut report, + &NO_TAGS + )); assert_eq!(report.block_states, 2); assert_eq!(report.mapped, 0); } From 65732406fc428926985929b1367d42b6d57abde5 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 06:48:02 +0200 Subject: [PATCH 57/67] Carry: take processors written in Java out of their lists The game, like vanilla, refuses a processor list holding a type it does not know, and with it every pool naming the list, so those structures did not generate at all. Carry now takes the library types out of processor lists (files and inline ones in pool elements) and counts them by type in the carry report: the structures generate, undecorated. On ATM10's carried mods: 1,427 processors of 146 types; pool decode failures 31 -> 0. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/worldgen.rs | 96 +++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index 91ff1bfef..25e0dbbea 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -74,6 +74,8 @@ pub struct Upgrade { pub unsupported: BTreeMap, /// Rules of mapped types the game does not run, by field. pub rules_lost: BTreeMap, + /// Processors written in Java, taken out of their lists, by type. + pub processors_dropped: BTreeMap, } /// Rewrites one `data//worldgen//...` file in place; whether it @@ -88,7 +90,55 @@ pub fn upgrade( ) -> bool { let states = block_states(v); report.block_states += usize::from(states); - upgrade_types(kind, v, report, set_tags) || states + let dropped = match kind { + "processor_list" => drop_java_processors(v, &mut report.processors_dropped), + "template_pool" => { + let mut changed = false; + for e in v["elements"].as_array_mut().into_iter().flatten() { + let element = &mut e["element"]; + if let Some(list) = element.get_mut("elements").and_then(Value::as_array_mut) { + for inner in list { + if let Some(p) = inner.get_mut("processors") { + changed |= drop_java_processors(p, &mut report.processors_dropped); + } + } + } else if let Some(p) = element.get_mut("processors") { + changed |= drop_java_processors(p, &mut report.processors_dropped); + } + } + changed + } + _ => false, + }; + upgrade_types(kind, v, report, set_tags) || states || dropped +} + +/// Takes the processors written in Java (any type outside `minecraft:`) +/// out of a processor list: `{"processors": [...]}`, a bare list, or an id +/// (left as it is). The game, like vanilla, refuses a list holding a type it +/// does not know, and with it every pool that names the list, so without +/// this the structure does not generate at all. Without them it generates +/// undecorated: no pillars under it, spawners left as the template has +/// them, blocks not swapped. Whether anything was taken out. +fn drop_java_processors(list: &mut Value, dropped: &mut BTreeMap) -> bool { + let processors = match list { + Value::Array(a) => a, + Value::Object(o) => match o.get_mut("processors") { + Some(Value::Array(a)) => a, + _ => return false, + }, + _ => return false, + }; + let before = processors.len(); + processors.retain(|p| { + let t = p["processor_type"].as_str().unwrap_or_default(); + let java = !t.starts_with("minecraft:"); + if java { + *dropped.entry(t.to_owned()).or_default() += 1; + } + !java + }); + processors.len() != before } /// 1.21.1 writes a block or fluid state as `{"Name": ..., "Properties": @@ -549,6 +599,50 @@ mod tests { assert_eq!(report.rules_lost["super_exclusion_zone"], 1); } + #[test] + fn drops_processors_written_in_java() { + let mut report = Upgrade::default(); + let mut list = json!({"processors": [ + {"processor_type": "repurposed_structures:bottom_pillar_processor"}, + {"processor_type": "minecraft:gravity", "heightmap": "WORLD_SURFACE_WG", "offset": -1}, + {"processor_type": "moogs_structures:spawner_randomizing_processor"}]}); + assert!(upgrade("processor_list", &mut list, &mut report, &NO_TAGS)); + assert_eq!( + list["processors"], + json!([{"processor_type": "minecraft:gravity", + "heightmap": "WORLD_SURFACE_WG", "offset": -1}]) + ); + let mut pool = json!({"elements": [ + {"weight": 1, "element": {"element_type": "minecraft:single_pool_element", + "processors": {"processors": [{"processor_type": "lithostitched:block_swap"}]}}}, + {"weight": 1, "element": {"element_type": "minecraft:list_pool_element", "elements": [ + {"element_type": "minecraft:single_pool_element", "processors": "x:named"}]}}]}); + assert!(upgrade("template_pool", &mut pool, &mut report, &NO_TAGS)); + assert_eq!( + pool["elements"][0]["element"]["processors"], + json!({"processors": []}) + ); + assert_eq!( + pool["elements"][1]["element"]["elements"][0]["processors"], + "x:named" + ); + let mut vanilla = json!({"processors": [{"processor_type": "minecraft:nop"}]}); + assert!(!upgrade( + "processor_list", + &mut vanilla, + &mut report, + &NO_TAGS + )); + assert_eq!( + report.processors_dropped, + BTreeMap::from([ + ("lithostitched:block_swap".into(), 1), + ("moogs_structures:spawner_randomizing_processor".into(), 1), + ("repurposed_structures:bottom_pillar_processor".into(), 1), + ]) + ); + } + #[test] fn writes_block_states_in_26_3_shape() { let mut report = Upgrade::default(); From d0d5e79f1afaeec9107b88fa6a12c4f4f093936d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 06:31:33 +0200 Subject: [PATCH 58/67] Structures: honour a carried mod's start and placement rules Repurposed Structures' and Moog's jigsaws check biomes and terrain height in a radius and refuse to start in liquid; their spreads keep a minimum distance from the world origin and away from several other sets. Carry maps them to vanilla's jigsaw and random spread and writes those rules under horizon: keys; the game now runs them (start_checks.rs, the spread's min distance and exclusion sets). Vanilla data has none of the keys. Co-Authored-By: Claude Opus 5.5 --- .../horizon-world/src/worldgen/structure.rs | 1 + .../src/worldgen/structure/start.rs | 76 ++++- .../src/worldgen/structure/start_checks.rs | 277 ++++++++++++++++++ 3 files changed, 341 insertions(+), 13 deletions(-) create mode 100644 crates/horizon-world/src/worldgen/structure/start_checks.rs diff --git a/crates/horizon-world/src/worldgen/structure.rs b/crates/horizon-world/src/worldgen/structure.rs index f15c37dc2..361b2c1d0 100644 --- a/crates/horizon-world/src/worldgen/structure.rs +++ b/crates/horizon-world/src/worldgen/structure.rs @@ -14,6 +14,7 @@ pub mod processor; pub mod ruined_portal; pub mod simple; pub mod start; +pub mod start_checks; pub mod stronghold; pub mod template; pub mod temple; diff --git a/crates/horizon-world/src/worldgen/structure/start.rs b/crates/horizon-world/src/worldgen/structure/start.rs index 1b443eba5..a66f27509 100644 --- a/crates/horizon-world/src/worldgen/structure/start.rs +++ b/crates/horizon-world/src/worldgen/structure/start.rs @@ -55,6 +55,8 @@ pub struct JigsawDef { aliases: Vec, padding: [i32; 2], waterlogging: bool, + /// A carried mod's start rules (`"horizon:start_checks"`). + checks: super::start_checks::StartChecks, } #[derive(Debug)] pub enum StructureKind { @@ -129,6 +131,12 @@ pub struct StructureSet { frequency: f32, reduction: Reduction, exclusion: Option<(String, i32)>, + /// A carried mod's placement rules: no start nearer the world origin + /// than this many blocks (`"horizon:min_distance_from_world_origin"`), + /// and none within `chunk_count` chunks of any of several sets + /// (`"horizon:exclusion_zone"`: `other_sets`, `chunk_count`). + min_distance: Option, + avoid: Option<(Vec, i32)>, } fn heightmap(name: &str) -> Option { @@ -193,6 +201,7 @@ impl StructureRegistry { aliases: v["pool_aliases"].as_array().cloned().unwrap_or_default(), padding: pair(v.get("dimension_padding"), "bottom", "top", 0), waterlogging: v["liquid_settings"].as_str() != Some("ignore_waterlogging"), + checks: super::start_checks::StartChecks::decode(v.get("horizon:start_checks")), }), "minecraft:buried_treasure" => StructureKind::BuriedTreasure, "minecraft:igloo" => StructureKind::Igloo, @@ -291,6 +300,20 @@ impl StructureRegistry { z["chunk_count"].as_i64().unwrap_or(0) as i32, ) }), + min_distance: p + .get("horizon:min_distance_from_world_origin") + .and_then(Value::as_i64), + avoid: p.get("horizon:exclusion_zone").map(|z| { + ( + z["other_sets"] + .as_array() + .into_iter() + .flatten() + .filter_map(|s| Some(s.as_str()?.to_owned())) + .collect(), + z["chunk_count"].as_i64().unwrap_or(0) as i32, + ) + }), }); } Ok(Self { @@ -397,26 +420,47 @@ impl StructureRegistry { if !placed { return false; } + // AdvancedRandomSpread.isPlacementChunk, in blocks from the chunk's corner. + if let Some(d) = set.min_distance { + let (bx, bz) = (i64::from(x) * 16, i64::from(z) * 16); + if bx * bx + bz * bz < d * d { + return false; + } + } + if let Some((others, range)) = &set.avoid { + if others + .iter() + .any(|other| self.has_start_in_range(other, seed, x, z, *range, rings)) + { + return false; + } + } if set.frequency < 1. && !reduce(set.reduction, seed, set.salt, x, z, set.frequency) { return false; } match &set.exclusion { - Some((other, range)) => { - let Some(other) = self.sets.iter().find(|s| s.name == *other) else { - return true; - }; - for tx in x - range..=x + range { - for tz in z - range..=z + range { - if self.is_structure_chunk(other, seed, tx, tz, rings) { - return false; - } - } - } - true - } + Some((other, range)) => !self.has_start_in_range(other, seed, x, z, *range, rings), None => true, } } + /// ChunkGeneratorStructureState.hasStructureChunkInRange: whether set + /// `other` places a structure within `range` chunks of (`x`, `z`). + fn has_start_in_range( + &self, + other: &str, + seed: i64, + x: i32, + z: i32, + range: i32, + rings: &Rings, + ) -> bool { + let Some(other) = self.sets.iter().find(|s| s.name == other) else { + return false; + }; + (x - range..=x + range).any(|tx| { + (z - range..=z + range).any(|tz| self.is_structure_chunk(other, seed, tx, tz, rings)) + }) + } } impl StructureSet { /// The random spread's spacing in chunks (`None` for rings). @@ -606,6 +650,12 @@ fn try_generate( .start_height .sample(&mut random, context.generation()) .ok()?; + if !jigsaw + .checks + .pass(context, &def.biomes, chunk, height, jigsaw.project_to) + { + return None; + } let start = [chunk.x * 16, height, chunk.z * 16]; let aliases = resolve_aliases(&jigsaw.aliases, start, seed); let config = JigsawConfig { diff --git a/crates/horizon-world/src/worldgen/structure/start_checks.rs b/crates/horizon-world/src/worldgen/structure/start_checks.rs new file mode 100644 index 000000000..45b991531 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/start_checks.rs @@ -0,0 +1,277 @@ +//! Start rules a carried structure mod adds on top of vanilla's jigsaw. +//! +//! Repurposed Structures and Moog's Structure Lib give their jigsaw +//! structures checks that run before the pieces are laid out: every biome +//! in a radius must be one the structure allows, the start column may not +//! end in liquid, and the terrain in a radius must lie within a height range. +//! Carry (`horizon-port-pack`) maps those structures to `minecraft:jigsaw` +//! and writes the checks under `"horizon:start_checks"`; vanilla data never +//! has the key, so vanilla structures are unchanged. +//! +//! ```json +//! "horizon:start_checks": { +//! "biome_radius": 2, // chunks; every noise biome must be allowed +//! "liquid": "top", // "top": the column's highest block; "lowest": the block above min_y +//! "terrain_radius": 1, // chunks, with terrain_range and/or min_y, max_y +//! "terrain_range": 15, +//! "min_y": 60, +//! "max_y": 200 +//! } +//! ``` +use crate::{heightmap::HeightmapType, worldgen::biome::BiomeId}; +use horizon_core::position::ChunkPos; +use serde_json::Value; +use std::collections::BTreeSet; + +/// Which block of the start column must not be a liquid. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum LiquidCheck { + /// The highest non-air block of the noise column (Moog's). + Top, + /// The block above the column's bottom (Repurposed Structures: its loop + /// keeps the last block it reads, not the first solid one). + Lowest, +} + +/// The checks of one structure; all empty for vanilla's. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct StartChecks { + biome_radius: Option, + liquid: Option, + terrain_radius: Option, + terrain_range: Option, + min_y: Option, + max_y: Option, +} + +/// The column and biome queries the checks need. +pub trait CheckQueries { + /// ChunkGenerator.getFirstFreeHeight. + fn first_free_height(&mut self, x: i32, z: i32, kind: HeightmapType) -> i32; + /// The noise column at (`x`, `z`) as (not air, blocks motion) per block + /// from the noise's bottom up: in a noise column a block that is not air + /// and does not block motion is a liquid. + fn liquid_column(&mut self, x: i32, z: i32) -> (Vec, Vec); + fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId; +} + +impl CheckQueries for T { + fn first_free_height(&mut self, x: i32, z: i32, kind: HeightmapType) -> i32 { + self.base_height(x, z, kind) + } + fn liquid_column(&mut self, x: i32, z: i32) -> (Vec, Vec) { + let mut columns = self.base_columns( + x, + z, + &[HeightmapType::WorldSurfaceWg, HeightmapType::OceanFloorWg], + ); + let solid = columns.pop().unwrap_or_default(); + (columns.pop().unwrap_or_default(), solid) + } + fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId { + super::start::StartContext::noise_biome(self, quart) + } +} + +impl StartChecks { + /// `"horizon:start_checks"` of a structure, or none. + pub fn decode(value: Option<&Value>) -> Self { + let Some(v) = value else { + return Self::default(); + }; + let int = |k: &str| v.get(k).and_then(Value::as_i64).map(|n| n as i32); + Self { + biome_radius: int("biome_radius"), + liquid: match v.get("liquid").and_then(Value::as_str) { + Some("top") => Some(LiquidCheck::Top), + Some("lowest") => Some(LiquidCheck::Lowest), + _ => None, + }, + terrain_radius: int("terrain_radius"), + terrain_range: int("terrain_range"), + min_y: int("min_y"), + max_y: int("max_y"), + } + } + + /// GenericJigsawStructure.extraSpawningChecks: whether the structure may + /// start in `chunk` at `y` (the sampled start height). + pub fn pass( + &self, + queries: &mut impl CheckQueries, + biomes: &BTreeSet, + chunk: ChunkPos, + y: i32, + project_to: Option, + ) -> bool { + let (x0, z0) = (chunk.x * 16, chunk.z * 16); + if let Some(radius) = self.biome_radius { + let mut section_y = y; + if let Some(kind) = project_to { + section_y += queries.first_free_height(x0 + 7, z0 + 7, kind) - 1; + } + for cx in chunk.x - radius..=chunk.x + radius { + for cz in chunk.z - radius..=chunk.z + radius { + if !biomes.contains(&queries.noise_biome([cx << 2, section_y >> 2, cz << 2])) { + return false; + } + } + } + } + if let Some(check) = self.liquid { + let (filled, solid) = queries.liquid_column(x0 + 7, z0 + 7); + // From the top down to the block above the bottom, as the mods loop. + let index = match check { + LiquidCheck::Top => (1..filled.len()).rev().find(|&i| filled[i]), + LiquidCheck::Lowest => Some(1).filter(|&i| i < filled.len()), + }; + if index.is_some_and(|i| filled[i] && !solid[i]) { + return false; + } + } + if let Some(radius) = self + .terrain_radius + .filter(|_| self.terrain_range.is_some() || self.min_y.is_some()) + { + let kind = project_to.unwrap_or(HeightmapType::WorldSurfaceWg); + let (mut highest, mut lowest) = (i32::MIN, i32::MAX); + for cx in chunk.x - radius..=chunk.x + radius { + for cz in chunk.z - radius..=chunk.z + radius { + let height = queries.first_free_height((cx << 4) + 7, (cz << 4) + 7, kind); + highest = highest.max(height); + lowest = lowest.min(height); + // Both bounds test the lowest height so far, as the mods do. + if self.min_y.is_some_and(|m| lowest < m) + || self.max_y.is_some_and(|m| lowest > m) + { + return false; + } + } + } + if self + .terrain_range + .is_some_and(|range| highest - lowest > range) + { + return false; + } + } + true + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + use std::collections::BTreeMap; + + /// Heights per (x, z), a liquid column, one biome per quart column. + #[derive(Default)] + struct Fake { + heights: BTreeMap<(i32, i32), i32>, + column: Vec, + biomes: BTreeMap<(i32, i32), u32>, + asked: Vec<[i32; 3]>, + } + impl CheckQueries for Fake { + fn first_free_height(&mut self, x: i32, z: i32, _: HeightmapType) -> i32 { + self.heights.get(&(x, z)).copied().unwrap_or(64) + } + fn liquid_column(&mut self, _: i32, _: i32) -> (Vec, Vec) { + // '.' air, '#' stone, '~' water, from the bottom up. + ( + self.column.iter().map(|&c| c != '.').collect(), + self.column.iter().map(|&c| c == '#').collect(), + ) + } + fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId { + self.asked.push(quart); + BiomeId(self.biomes.get(&(quart[0], quart[2])).copied().unwrap_or(1)) + } + } + fn checks(v: Value) -> StartChecks { + StartChecks::decode(Some(&v)) + } + const CHUNK: ChunkPos = ChunkPos { x: 2, z: -1 }; + + #[test] + fn vanilla_structures_pass_everything() { + let mut fake = Fake { + column: "#~~.".chars().collect(), + ..Fake::default() + }; + let none = StartChecks::decode(None); + assert_eq!(none, StartChecks::default()); + assert!(none.pass(&mut fake, &BTreeSet::new(), CHUNK, 0, None)); + assert!(fake.asked.is_empty()); + } + + #[test] + fn every_biome_in_the_radius_must_be_allowed() { + let allowed = BTreeSet::from([BiomeId(1)]); + let mut fake = Fake::default(); + fake.heights.insert((39, -9), 70); + let c = checks(json!({"biome_radius": 1})); + assert!(c.pass( + &mut fake, + &allowed, + CHUNK, + 10, + Some(HeightmapType::WorldSurfaceWg) + )); + // Section y: start height plus the projected height minus one, in quarts. + assert_eq!(fake.asked.len(), 9); + assert!(fake.asked.iter().all(|q| q[1] == (10 + 70 - 1) >> 2)); + assert_eq!(fake.asked[0], [4, 19, -8]); + fake.biomes.insert((12, 0), 2); // chunk (3, 0), the radius' corner + assert!(!c.pass(&mut fake, &allowed, CHUNK, 10, None)); + } + + #[test] + fn liquid_checks_read_the_mods_block() { + let top = checks(json!({"liquid": "top"})); + let lowest = checks(json!({"liquid": "lowest"})); + let none = BTreeSet::new(); + let mut ocean = Fake { + column: "##~~~..".chars().collect(), + ..Fake::default() + }; + assert!(!top.pass(&mut ocean, &none, CHUNK, 0, None)); + assert!(lowest.pass(&mut ocean, &none, CHUNK, 0, None)); + let mut land = Fake { + column: "#~###..".chars().collect(), + ..Fake::default() + }; + assert!(top.pass(&mut land, &none, CHUNK, 0, None)); + assert!(!lowest.pass(&mut land, &none, CHUNK, 0, None)); + // The bottom block itself is never read; an empty column passes. + let mut empty = Fake { + column: "~....".chars().collect(), + ..Fake::default() + }; + assert!(top.pass(&mut empty, &none, CHUNK, 0, None)); + } + + #[test] + fn terrain_must_lie_within_its_range() { + let none = BTreeSet::new(); + let mut fake = Fake::default(); + // Chunk centres around (2, -1): x 23, 39, 55; z -25, -9, 7. + fake.heights.insert((23, -25), 60); + fake.heights.insert((55, 7), 75); + let range = |r: i32| checks(json!({"terrain_radius": 1, "terrain_range": r})); + assert!(range(15).pass(&mut fake, &none, CHUNK, 0, None)); + assert!(!range(14).pass(&mut fake, &none, CHUNK, 0, None)); + // The radius alone, or with only max_y, checks nothing. + assert!( + checks(json!({"terrain_radius": 1, "max_y": 0})).pass(&mut fake, &none, CHUNK, 0, None) + ); + let bounds = + |min: i32, max: i32| checks(json!({"terrain_radius": 1, "min_y": min, "max_y": max})); + assert!(bounds(60, 64).pass(&mut fake, &none, CHUNK, 0, None)); + assert!(!bounds(61, 100).pass(&mut fake, &none, CHUNK, 0, None)); + // max_y is compared with the lowest height so far: 75 never fails it. + assert!(bounds(0, 64).pass(&mut fake, &none, CHUNK, 0, None)); + assert!(!bounds(0, 59).pass(&mut fake, &none, CHUNK, 0, None)); + } +} From 05517a167fd71b4c160b999f765a053c505072af Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 07:40:41 +0200 Subject: [PATCH 59/67] docs/modpacks.md: structure carry-over now decodes and starts where the mod would; facts.json streams Co-Authored-By: Claude Opus 5.5 --- docs/modpacks.md | 59 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/docs/modpacks.md b/docs/modpacks.md index adf9a5318..1ecaed8bb 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -420,21 +420,52 @@ Two cases the oracle fills in: - their `advanced_random_spread` becomes `minecraft:random_spread`; - their versioned, legacy and empty pool elements become vanilla's. - The rules these libraries add on top of vanilla's (no start in liquid, a minimum distance, - caps per piece) are lost, so a mapped structure can start where the mod would not. NeoForge's - convention biome tags (`#c:is_forest`, ...) come from the loader jar - (`/loader/neoforge--universal.jar`, fetched dev-only and SHA-1 checked) and - are written into the carry. Each mod's `carry-report.json` lists what was mapped and the - types left as they are. The game skips the structures it cannot decode. + Three more rewrites make the mapped structures decode and start where their mod would: + - **Block states.** 1.21.1 writes a block or fluid state as `{"Name", "Properties"}`, 26.3 + as `{"id", "properties"}` and refuses the old shape, which failed the whole processor + list, feature or pool. Carry rewrites them (#816; 1,005 files in 17 of ATM10's mods). + - **Start and placement rules** (#820 in the game, #821 in carry). Repurposed Structures' + and Moog's jigsaws refuse to start in liquid and check biomes and terrain height in a + radius; their spreads keep a minimum distance from the world origin and away from other + sets (named by a structure set tag). Carry writes these under `horizon:start_checks`, + `horizon:min_distance_from_world_origin` and `horizon:exclusion_zone`; the game runs them + (`horizon-world`'s `structure/start_checks.rs`). Vanilla data has none of the keys. Moog's + also spreads its structures 1.65 times as far apart as its files say, which carry now + applies. MVS went from 829 starts to 90 in a 128×128-chunk square (seed 1234): 294 from the + spacing, the rest from the start checks. + - **Processors written in Java** are taken out of their lists (#822). The game, like + vanilla, refuses a list holding a type it does not know, and with it every pool naming the + list. The structures now generate undecorated (no pillars under them, spawners as the + template has them); 1,427 processors of 146 types in ATM10's mods, the most common + `lithostitched:block_swap`, Formations' `biome_replacements` and Repurposed Structures' + pillar, fluid and void processors. Porting the common ones is next. - On ATM10's 40 data-tier mods: + Each mod's `carry-report.json` lists what was mapped, the rules and processors lost by name, + and the types left as they are. NeoForge's convention biome tags (`#c:is_forest`, ...) come + from the loader jar (`/loader/neoforge--universal.jar`, fetched dev-only and + SHA-1 checked) and are written into the carry. The game skips the structures it cannot + decode. + + On ATM10's 42 data-tier mods with a jar (seed 1234, chunks -64..63, `chunkgen_bench + --starts` over the carried worldgen): 286 starts with 15,097 pieces, 150 of them vanilla's + (unchanged), and no pool that fails to decode (65 before #816 in the 64×64 centre). - 1,061 files were mapped (MVS 413, MNS 210, MSS 157, MES 110, Repurposed Structures 104, ChoiceTheorem's Overhauled Village 66), and 103 convention biome tags written. - When Dungeons Arise, Structory and Towns and Towers use vanilla's types already. - - Not generated yet, because their types are Java code: YUNG's API (491 elements and - jigsaws: every "YUNG's Better" mod), Formations (179), the nether jigsaw variants of Moog's - and Repurposed (69), MO Structures (38), and Repurposed's mineshafts, mansions, monuments - and cities. + - Still lost, written in Java: + - YUNG's API: its own jigsaw assembler (priority pieces, per-piece caps, conditions, + enhanced terrain adaptation) and 491 elements and jigsaws, behind every "YUNG's Better" + mod; + - Formations' structure and element types (179); + - the nether jigsaw variants of Moog's and Repurposed (69), which search the column for + land after the layout; + - Moog's and Repurposed's layout rules: they centre the pieces on the start, bury or lift + the structure to the terrain and cap pieces per pool; + - Repurposed's mineshafts, mansions, monuments and cities, and MO Structures (38; its + library is only on CurseForge). + - CTOV's villages and outposts are added to vanilla's `villages` and `pillager_outposts` + sets by Java code, as lithostitched's `add_structure_set_entries` worldgen modifiers do for + many packs; a content mod has no way to add to a vanilla set yet, so none start. In a world with the 40 mods, seed 1234, `/locate structure mvs:azelea_house` finds one 27 blocks from spawn and it stands there as the template builds it; When Dungeons Arise's @@ -682,8 +713,10 @@ ATM10 mods (153,511 states), the peak of each step falls by about 210 MB: The fluid tables no longer decode the 211 MB `gameplay-facts.json` a second time (#754). The content root is kept between starts (#756). What is left at startup: -- `facts.json` goes through a JSON tree, then an NBT tree, in both the registry and the assets - step; +- `facts.json` went through a JSON tree, then an NBT tree, in both the registry and the assets + step. It now streams (#825): at ATM10's size (214,338 states) each step's peak fell from + 816 MB to 153 MB (registry) and 235 MB (assets), and the RSS after the assets step from + 1,344 MB to 442 MB; - on a first start, the content-root build and `Behaviours`' decode of `gameplay-facts.json` (cached afterwards in the behaviour snapshot). From 454b3842bb9fe9fec0b75fcfa599f14d78a3dd8d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 07:28:35 +0200 Subject: [PATCH 60/67] feat(server): secure chat, as vanilla 26.3's server Chat session updates with their profile key checked against the services' keys, signed chat and the message chain, last-seen acknowledgements, the signature cache, signed say/me/msg, enforce-secure-profile, and vanilla's log lines. Offline servers and players without a chat session chat unsigned, as on vanilla. tools/test_secure_chat_parity.py compares both servers with test keys and a mock services server. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 2 + crates/horizon-client/examples/lan_flat.rs | 4 +- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/dedicated.rs | 15 +- crates/horizon-client/src/game/lan.rs | 33 +- crates/horizon-client/src/game/lan_actions.rs | 22 +- crates/horizon-client/src/game/lan_chat.rs | 194 ++++++ crates/horizon-client/src/lan/guest.rs | 9 + crates/horizon-client/src/lan/hub.rs | 79 +-- crates/horizon-client/src/lan/hub/chat.rs | 500 ++++++++++++++ crates/horizon-client/src/lan/mod.rs | 52 +- crates/horizon-protocol/Cargo.toml | 2 + crates/horizon-protocol/src/buf.rs | 10 + crates/horizon-protocol/src/lib.rs | 1 + crates/horizon-protocol/src/packets/play.rs | 63 +- crates/horizon-protocol/src/rsa.rs | 92 ++- crates/horizon-protocol/src/secure_chat.rs | 491 ++++++++++++++ crates/horizon-server-files/Cargo.toml | 2 + crates/horizon-server-files/src/profiles.rs | 68 ++ docs/hosting.md | 5 +- docs/server.md | 4 +- .../horizonlab/parity/SecureChatClient.java | 625 ++++++++++++++++++ tools/test_secure_chat_parity.py | 317 +++++++++ 23 files changed, 2505 insertions(+), 86 deletions(-) create mode 100644 crates/horizon-client/src/game/lan_chat.rs create mode 100644 crates/horizon-client/src/lan/hub/chat.rs create mode 100644 crates/horizon-protocol/src/secure_chat.rs create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java create mode 100644 tools/test_secure_chat_parity.py diff --git a/Cargo.lock b/Cargo.lock index 2f389dfef..b4081e7af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1539,6 +1539,7 @@ dependencies = [ "horizon-world", "serde_json", "sha1", + "sha2", ] [[package]] @@ -1569,6 +1570,7 @@ dependencies = [ name = "horizon-server-files" version = "0.1.0" dependencies = [ + "base64", "horizon-core", "horizon-protocol", "horizon-world", diff --git a/crates/horizon-client/examples/lan_flat.rs b/crates/horizon-client/examples/lan_flat.rs index 4800f05fa..20d87e03c 100644 --- a/crates/horizon-client/examples/lan_flat.rs +++ b/crates/horizon-client/examples/lan_flat.rs @@ -128,7 +128,9 @@ fn main() -> Result<(), Box> { eprintln!("[lan] {packet:?}"); } } - HostEvent::Chat(line) | HostEvent::Warn(line) => println!("{line}"), + HostEvent::Chat { line, .. } | HostEvent::Warn(line) | HostEvent::Error(line) => { + println!("{line}") + } HostEvent::Left(_) => {} } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index cfbb526ef..e30ef829e 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -64,6 +64,7 @@ mod item_particles; mod jukebox; mod lan; mod lan_actions; +mod lan_chat; mod lan_crafting; mod lan_entities; mod lan_mobs; diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 7348f3f9e..8a8ed63f9 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -473,6 +473,18 @@ impl Game<'_> { // DedicatedServer.initServer: names nobody has resolve offline only without authentication. cache.resolve_offline_users = !props.online_mode; *USER_CACHE.lock().unwrap() = Some(cache); + // Services.create: the services' public keys, fetched in the + // background (they check players' chat keys). + let secure_chat = Arc::new(horizon_client::lan::SecureChat::new( + props.enforce_secure_profile && props.online_mode, + || { + let discovery = profiles::discovery_url(); + profiles::fetch_profile_key_keys(&discovery).unwrap_or_else(|e| { + ::log::error!("{e}"); + Vec::new() + }) + }, + )); log("INFO", "Starting minecraft server version 26.3"); log("INFO", "Loading properties"); let game_mode = props.game_mode; @@ -571,8 +583,7 @@ impl Game<'_> { enable_status: props.enable_status, hide_online_players: props.hide_online_players, favicon, - // DedicatedServer.enforceSecureProfile needs online mode. - enforces_secure_chat: false, + secure_chat: Some(secure_chat), accepts_transfers: props.accepts_transfers, announce: false, listen: true, diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 30cd89b3d..114330e51 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -855,6 +855,9 @@ impl Game<'_> { } for event in events { match event { + event @ (HostEvent::Chat { .. } | HostEvent::Warn(_) | HostEvent::Error(_)) => { + self.lan_log_event(event); + } HostEvent::Joined(conn) => { self.lan_guest_joined(&mut host, conn, state.as_ref(), &entities, spawn); if self.options.dedicated.is_some() @@ -911,15 +914,6 @@ impl Game<'_> { ); } } - // MinecraftServer.logChatMessage of an unsigned message. - HostEvent::Chat(line) if self.options.dedicated.is_some() => { - super::dedicated::log("INFO", &format!("[Not Secure] {line}")); - } - HostEvent::Chat(line) => self.system_message(Text::literal(line)), - HostEvent::Warn(line) if self.options.dedicated.is_some() => { - super::dedicated::log("WARN", &line); - } - HostEvent::Warn(line) => log::warn!("{line}"), HostEvent::Packet(conn, packet) => { self.lan_guest_packet(&mut host, conn, *packet); } @@ -1209,3 +1203,24 @@ impl Game<'_> { } } } + +impl Game<'_> { + /// A hub's chat and log lines: a dedicated server logs them + /// (MinecraftServer.logChatMessage, `[Not Secure]` for unsigned or + /// expired messages); a LAN host shows the chat. + pub(super) fn lan_log_event(&mut self, event: HostEvent) { + let dedicated = self.options.dedicated.is_some(); + match event { + HostEvent::Chat { line, secure } if dedicated => { + let tag = if secure { "" } else { "[Not Secure] " }; + super::dedicated::log("INFO", &format!("{tag}{line}")); + } + HostEvent::Chat { line, .. } => self.system_message(Text::literal(line)), + HostEvent::Warn(line) if dedicated => super::dedicated::log("WARN", &line), + HostEvent::Warn(line) => log::warn!("{line}"), + HostEvent::Error(line) if dedicated => super::dedicated::log("ERROR", &line), + HostEvent::Error(line) => log::error!("{line}"), + _ => {} + } + } +} diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 59d71e7b2..ced435d8d 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -639,7 +639,8 @@ impl Game<'_> { self.drop_guest_items(host, gi, vec![c]); } } - S::ChatCommand(c) => self.guest_command(host, conn, &c.command), + S::ChatCommand(c) => self.guest_chat_command(host, conn, &c.command), + S::ChatCommandSigned(p) => self.guest_signed_command(host, conn, p), S::ChangeDifficulty(p) if host.hub.guests[gi].permission >= 2 => { if !self.level.difficulty_locked && (0..=3).contains(&p.difficulty) { self.ui.difficulty = if self.level.hardcore { @@ -731,12 +732,6 @@ impl Game<'_> { self.send_admin_feedback(host, Some(conn)); } } - S::Unknown(8, payload) => { - // A signed command: the text comes first. - if let Ok(text) = horizon_protocol::buf::Reader::new(&payload).string(32767) { - self.guest_command(host, conn, &text); - } - } S::Unknown(id, payload) if id == horizon_protocol::ids::play::serverbound::COMMAND_SUGGESTION => { @@ -861,7 +856,18 @@ impl Game<'_> { } /// Commands.performCommand for a guest with command access. - fn guest_command(&mut self, host: &mut LanHost, conn: u32, command: &str) { + /// Commands.performCommand for a guest, with the message arguments it + /// signed (none after an unsigned command packet). + pub(super) fn guest_command( + &mut self, + host: &mut LanHost, + conn: u32, + command: &str, + signed: Option>, + ) { + if self.guest_message_command(host, conn, command, signed.as_ref()) { + return; + } // ListPlayersCommand needs no permission and lists every player. let words: Vec<&str> = command.trim_start_matches('/').split(' ').collect(); if words[0] == "list" && (words.len() == 1 || words[1..] == ["uuids"]) { diff --git a/crates/horizon-client/src/game/lan_chat.rs b/crates/horizon-client/src/game/lan_chat.rs new file mode 100644 index 000000000..f678480ab --- /dev/null +++ b/crates/horizon-client/src/game/lan_chat.rs @@ -0,0 +1,194 @@ +//! A guest's commands that carry chat: the unsigned and signed command +//! packets (ServerGamePacketListenerImpl.handleChatCommand and +//! handleSignedChatCommand), and the message commands whose text they sign +//! (SayCommand, EmoteCommands, MsgCommand), relayed by the hub as chat. +use super::{Game, lan::LanHost}; +use horizon_client::{ + command::{Arg, ArgType, NodeKind, ParseResults, vanilla}, + lan::{ + data::component_nbt, + hub::{HostEvent, chat::ChatMessage}, + }, + text::{self, Text}, +}; +use horizon_protocol::packets::play::{self, Clientbound as C}; + +/// The signing context of a command: each message argument as the chain +/// let it through, by argument name (CommandSigningContext.SignedArguments). +type Signed = Vec<(String, ChatMessage)>; + +impl Game<'_> { + /// CommandDispatcher.parse with the guest's permission level. + fn parse_as(&mut self, permission: u8, command: &str) -> ParseResults { + let level = std::mem::replace(&mut self.ui.registries.permission, permission); + let parse = vanilla::game_dispatcher().parse(command, &self.ui.registries); + self.ui.registries.permission = level; + parse + } + + /// SignableCommand.of: the command's message arguments, by name, with + /// their text, not past a redirect to the root (`execute ... run`). + fn signable_arguments(&mut self, permission: u8, command: &str) -> Vec<(String, String)> { + let parse = self.parse_as(permission, command); + let nodes = &vanilla::game_dispatcher().nodes; + let root = parse.context.root; + let mut out = Vec::new(); + let mut context = Some(&parse.context); + while let Some(c) = context { + for &(node, (start, end)) in &c.nodes { + if let NodeKind::Argument { + ty: ArgType::Message, + name, + } = &nodes[node].kind + && let Some(text) = command.get(start..end) + { + out.push((name.clone(), text.to_owned())); + } + } + context = c.child.as_deref().filter(|child| child.root != root); + } + out + } + + /// handleChatCommand (performUnsignedChatCommand): refused when the + /// server enforces secure chat and the command has a message to sign. + pub(super) fn guest_chat_command(&mut self, host: &mut LanHost, conn: u32, command: &str) { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return; + }; + if !host.hub.try_handle_chat(i, command, true) { + return; + } + let permission = host.hub.guests[i].permission; + if host.hub.enforces_secure_chat() + && !self.signable_arguments(permission, command).is_empty() + { + let name = host.hub.guests[i].name.clone(); + self.lan_log_event(HostEvent::Error(format!( + "Received unsigned command packet from {name}, but the command requires signable arguments: {command}" + ))); + let failure = Text::translate("chat.disabled.invalid_command_signature", &[]) + .with_color(text::RED); + host.hub.guests[i].send(&C::SystemChat(play::SystemChat { + content: component_nbt(&failure.component()), + overlay: false, + })); + return; + } + self.guest_command(host, conn, command, None); + } + + /// handleSignedChatCommand: the acknowledgements, then each message + /// argument through the sender's chain, then the command with them. + pub(super) fn guest_signed_command( + &mut self, + host: &mut LanHost, + conn: u32, + p: play::ChatCommandSigned, + ) { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return; + }; + let mut events = Vec::new(); + let mut signed = None; + if let Some(last_seen) = host.hub.apply_last_seen(i, &p.last_seen, &mut events) + && host.hub.try_handle_chat(i, &p.command, true) + { + let permission = host.hub.guests[i].permission; + let parsed = self.signable_arguments(permission, &p.command); + match host + .hub + .signed_arguments(i, &p, &parsed, &last_seen, &mut events) + { + Ok(arguments) => signed = Some(arguments), + Err(e) => host.hub.decode_failure(i, e, &mut events), + } + } + for event in events { + self.lan_log_event(event); + } + if let Some(signed) = signed { + self.guest_command(host, conn, &p.command, Some(signed)); + } + } + + /// SayCommand, EmoteCommands and MsgCommand for a guest: chat to the + /// players, signed when the guest signed it (a disguised chat after an + /// unsigned command packet). False when the command is another one, or + /// one this can't run (selectors other than `@s` and `@a`), for the + /// game's dispatcher to handle. + pub(super) fn guest_message_command( + &mut self, + host: &mut LanHost, + conn: u32, + command: &str, + signed: Option<&Signed>, + ) -> bool { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return false; + }; + let parse = self.parse_as(host.hub.guests[i].permission, command); + let Ok((context, id)) = parse.executable() else { + return false; + }; + let argument = if id == "me" { "action" } else { "message" }; + let (Some(Arg::Str(text)), true) = + (context.get(argument), matches!(id, "say" | "me" | "msg")) + else { + return false; + }; + let message = signed.and_then(|s| { + s.iter() + .find(|(n, _)| n == argument) + .map(|(_, m)| m.clone()) + }); + let mut events = Vec::new(); + if id == "msg" { + let Some(Arg::Entity(selector)) = context.get("targets") else { + return false; + }; + let raw = context + .args + .iter() + .rev() + .find(|a| a.name == "targets") + .and_then(|a| command.get(a.range.0..a.range.1)); + let guests = &host.hub.guests; + let placed = |k: &usize| guests[*k].placed; + let targets: Vec = match (raw, &selector.player_name, selector.uuid) { + (Some("@s"), ..) => vec![i], + (Some("@a"), ..) => (0..guests.len()).filter(placed).collect(), + (_, Some(name), _) => (0..guests.len()) + .filter(|&k| placed(&k) && guests[k].name.eq_ignore_ascii_case(name)) + .collect(), + (_, None, Some(uuid)) => (0..guests.len()) + .filter(|&k| placed(&k) && guests[k].uuid == uuid) + .collect(), + _ => return false, + }; + if targets.is_empty() { + let failure = Text::empty() + .append(Text::translate("argument.entity.notfound.player", &[])) + .with_color(text::RED); + host.hub.guests[i].send(&C::SystemChat(play::SystemChat { + content: component_nbt(&failure.component()), + overlay: false, + })); + return true; + } + host.hub.whisper(i, &targets, message, text); + } else { + let chat_type = if id == "say" { + "say_command" + } else { + "emote_command" + }; + host.hub + .broadcast_command_chat(i, chat_type, message, text, &mut events); + } + for event in events { + self.lan_log_event(event); + } + true + } +} diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 780be4a33..1a699bc45 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -37,6 +37,12 @@ pub struct Guest { pub last_action_tick: u64, /// The next ClientboundPlayerChatPacket.globalIndex for this connection. pub chat_index: i32, + /// Secure chat (ServerGamePacketListenerImpl): the player's message + /// chain, keyed once it sends a chat session; the messages its + /// acknowledgements cover; the signatures its client caches. + pub chain: horizon_protocol::secure_chat::Chain, + pub last_seen: horizon_protocol::secure_chat::LastSeenValidator, + pub signatures: horizon_protocol::secure_chat::SignatureCache, /// The hub tick the player was placed on: chunks go out from the tick /// after next, behind the first doTick's packets, as vanilla's do. pub placed_at: u64, @@ -188,6 +194,9 @@ impl Guest { address: j.address, last_action_tick: 0, chat_index: 0, + chain: Default::default(), + last_seen: Default::default(), + signatures: Default::default(), placed_at: 0, center_sent: None, left_reason: String::new(), diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index e877fdd64..2d754dc24 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -33,6 +33,8 @@ use horizon_world::{ use std::sync::{Arc, atomic::Ordering}; /// GameEvent types (ClientboundGameEventPacket). +pub mod chat; + pub mod game_event { pub const START_RAINING: u8 = 1; pub const STOP_RAINING: u8 = 2; @@ -108,10 +110,16 @@ pub enum HostEvent { /// A guest left (state to save, entities to drop). Left(Box), Packet(u32, Box), - /// A chat line to show on the host (already broadcast to the guests). - Chat(String), + /// A chat line to show on the host (already broadcast to the guests); + /// a dedicated server logs it, `[Not Secure]` unless signed and fresh. + Chat { + line: String, + secure: bool, + }, /// A server log warning (ServerGamePacketListenerImpl's LOGGER.warn). Warn(String), + /// A server log error (LOGGER.error). + Error(String), } pub struct Hub { @@ -411,56 +419,23 @@ impl Hub { }, })); } - /// PlayerList.broadcastChatMessage of a guest's unsigned message - /// (PlayerChatMessage.unsigned): a ClientboundPlayerChatPacket to every - /// player, each connection numbering its messages. - pub fn player_chat(&mut self, from: usize, text: &str) { - let (uuid, name) = (self.guests[from].uuid, self.guests[from].name.clone()); - let timestamp = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map_or(0, |d| d.as_millis() as i64); - let bound = play::ChatTypeBound { - chat_type: self.chat_type, - name: display_name(uuid, &name), - target: None, - }; - for g in self.guests.iter_mut().filter(|g| g.placed) { - let packet = C::PlayerChat(play::PlayerChat { - global_index: g.chat_index, - sender: uuid, - index: 0, - signature: None, - content: text.to_owned(), - timestamp, - salt: 0, - last_seen: Vec::new(), - unsigned_content: None, - filter: (0, Vec::new()), - chat_type: bound.clone(), - }); - g.chat_index += 1; - g.send(&packet); - } - } - /// Disconnect a guest with a reason (ServerGamePacketListenerImpl.disconnect). pub fn kick(&mut self, conn: u32, reason: Tag) { - if let Some(g) = self.guest(conn) { + // The log's "lost connection" line gives the reason, as vanilla's. + let text = self.english(&reason); + if let Some(g) = self.guest_mut(conn) { + g.left_reason = text; g.sender.disconnect(reason); } } fn player_info(&self, uuid: u128, name: &str, mode: i32, latency: i32) -> play::PlayerInfo { + let guest = self.guests.iter().find(|g| g.uuid == uuid); play::PlayerInfo { uuid, name: Some(name.to_owned()), - properties: self - .guests - .iter() - .find(|g| g.uuid == uuid) - .map(|g| g.properties.clone()) - .unwrap_or_default(), - chat_session: None, + properties: guest.map(|g| g.properties.clone()).unwrap_or_default(), + chat_session: guest.and_then(|g| g.chain.session()).map(|s| s.data()), game_mode: Some(mode), listed: Some(true), latency: Some(latency), @@ -553,8 +528,9 @@ impl Hub { show_death_screen: true, limited_crafting: false, spawn: self.spawn_info(mode), - online_mode: false, - enforces_secure_chat: false, + // MinecraftServer.usesAuthentication, enforceSecureProfile. + online_mode: self.lan.shared.config.online.is_some(), + enforces_secure_chat: self.enforces_secure_chat(), })); g.send(&C::ChangeDifficulty(play::ChangeDifficulty { difficulty: ctx.difficulty, @@ -1056,7 +1032,9 @@ impl Hub { fn remove_guest(&mut self, i: usize, reason: &str) -> Guest { let mut g = self.guests.remove(i); - g.left_reason = reason.to_owned(); + if g.left_reason.is_empty() { + g.left_reason = reason.to_owned(); + } if std::env::var_os("HORIZON_NET_DEBUG").is_some() { log::info!("[lan] {} left: {reason}", g.name); } @@ -1807,14 +1785,9 @@ impl Hub { 0 => self.wake(i), _ => {} }, - S::Chat(c) => { - let text: String = c.message.chars().filter(|c| !c.is_control()).collect(); - if !text.is_empty() { - let name = self.guests[i].name.clone(); - self.player_chat(i, &text); - events.push(HostEvent::Chat(format!("<{name}> {text}"))); - } - } + S::Chat(c) => self.handle_chat(i, c, events), + S::ChatAck(p) => self.chat_ack(i, p.offset, events), + S::ChatSessionUpdate(p) => self.chat_session_update(i, p, events), other => events.push(HostEvent::Packet(conn, Box::new(other))), } } diff --git a/crates/horizon-client/src/lan/hub/chat.rs b/crates/horizon-client/src/lan/hub/chat.rs new file mode 100644 index 000000000..7e75c84c5 --- /dev/null +++ b/crates/horizon-client/src/lan/hub/chat.rs @@ -0,0 +1,500 @@ +//! Chat on the server side, as ServerGamePacketListenerImpl and PlayerList +//! handle it: chat sessions validated against the services' keys, signed +//! messages checked link by link, the acknowledged window, and messages +//! relayed with each connection's numbering and signature cache. +use super::{HostEvent, Hub, display_name}; +use horizon_protocol::{ + packets::play::{self, Clientbound as C}, + secure_chat::{self, Body, Chain, ChatSession, DecodeError, ProfileKey, Signature, Unpacked}, + text::{literal, translatable}, +}; +use horizon_world::nbt::{JavaString, Tag}; + +/// Now, in epoch milliseconds (Instant.now()). +pub fn now_millis() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as i64) +} + +/// StringUtil.isAllowedChatCharacter. +fn allowed(c: char) -> bool { + c != '\u{a7}' && c >= ' ' && c != '\u{7f}' +} + +/// A translatable message in red. +fn red(key: &str) -> Tag { + let mut t = translatable(key, &[]); + if let Tag::Compound(map) = &mut t { + map.insert( + JavaString::from("color"), + Tag::String(JavaString::from("red")), + ); + } + t +} + +/// PlayerChatMessage as the server relays it. +#[derive(Clone, Debug, PartialEq)] +pub struct ChatMessage { + pub sender: u128, + /// The link's index in the sender's chain (0 when unsigned). + pub index: i32, + pub signature: Option, + pub content: String, + /// Epoch milliseconds. + pub timestamp: i64, + pub salt: i64, + pub last_seen: Vec, + /// What clients show instead of the signed text (withUnsignedContent). + pub unsigned_content: Option, +} + +impl ChatMessage { + /// The message a chain let through: a signed one keeps its body; + /// PlayerChatMessage.unsigned takes the time now, no salt, nothing seen. + pub fn new(sender: u128, unpacked: Unpacked, body: &Body, now: i64) -> Self { + let (index, signature, timestamp, salt, last_seen) = match unpacked { + Unpacked::Unsigned => (0, None, now, 0, Vec::new()), + Unpacked::Signed { index, signature } => ( + index, + Some(signature), + body.timestamp, + body.salt, + body.last_seen.to_vec(), + ), + }; + Self { + sender, + index, + signature, + content: body.content.to_owned(), + timestamp, + salt, + last_seen, + unsigned_content: None, + } + } + /// hasExpiredServer. + pub fn expired(&self, now: i64) -> bool { + now > self.timestamp + secure_chat::EXPIRES_AFTER_SERVER + } + /// PlayerList.verifyChatTrusted: signed and not expired. + pub fn trusted(&self, now: i64) -> bool { + self.signature.is_some() && !self.expired(now) + } +} + +impl Hub { + /// MinecraftServer.enforceSecureProfile. + pub fn enforces_secure_chat(&self) -> bool { + self.lan.shared.config.enforces_secure_chat() + } + + /// Component.getString in the server's language, for the log. + pub(crate) fn english(&self, tag: &Tag) -> String { + match &self.lan.shared.config.log { + Some(log) => horizon_protocol::text::plain(tag, &log.lang), + None => horizon_protocol::text::plain(tag, &Default::default()), + } + } + + /// tryHandleChat: an illegal character disconnects; with chat hidden a + /// message (not a command) is refused. Whether to go on. + pub fn try_handle_chat(&mut self, i: usize, message: &str, command: bool) -> bool { + let conn = self.guests[i].conn; + if !message.chars().all(allowed) { + self.kick( + conn, + translatable("multiplayer.disconnect.illegal_characters", &[]), + ); + return false; + } + // ChatVisiblity.HIDDEN. + if !command && self.guests[i].info.chat_visibility == 2 { + self.guests[i].send(&C::SystemChat(play::SystemChat { + content: red("chat.disabled.options"), + overlay: false, + })); + return false; + } + true + } + + /// unpackAndApplyLastSeen: the acknowledged messages, or a disconnect. + pub fn apply_last_seen( + &mut self, + i: usize, + update: &play::LastSeenUpdate, + events: &mut Vec, + ) -> Option> { + let g = &mut self.guests[i]; + match g + .last_seen + .apply_update(update.offset, update.acknowledged, update.checksum) + { + Ok(seen) => Some(seen), + // Vanilla logs the ValidationException's stack trace after the line. + Err(_) => { + events.push(HostEvent::Error(format!( + "Failed to validate message acknowledgements from {}", + g.name + ))); + let conn = g.conn; + self.kick( + conn, + translatable("multiplayer.disconnect.chat_validation_failed", &[]), + ); + None + } + } + } + + /// handleChatAck. + pub(super) fn chat_ack(&mut self, i: usize, offset: i32, events: &mut Vec) { + let g = &mut self.guests[i]; + if g.last_seen.apply_offset(offset).is_err() { + events.push(HostEvent::Error(format!( + "Failed to validate message acknowledgement offset from {}", + g.name + ))); + let conn = g.conn; + self.kick( + conn, + translatable("multiplayer.disconnect.chat_validation_failed", &[]), + ); + } + } + + /// handleMessageDecodeFailure: logged, and the reason in red for the player. + pub fn decode_failure(&mut self, i: usize, e: DecodeError, events: &mut Vec) { + let reason = red(e.key()); + events.push(HostEvent::Warn(format!( + "Failed to update secure chat state for {}: '{}'", + self.guests[i].name, + self.english(&reason) + ))); + self.guests[i].send(&C::SystemChat(play::SystemChat { + content: reason, + overlay: false, + })); + } + + /// handleChat: the acknowledgements, then the message through the + /// sender's chain, then to everyone. + pub(super) fn handle_chat(&mut self, i: usize, c: play::Chat, events: &mut Vec) { + let Some(last_seen) = self.apply_last_seen(i, &c.last_seen, events) else { + return; + }; + if !self.try_handle_chat(i, &c.message, false) { + return; + } + let (enforce, now) = (self.enforces_secure_chat(), now_millis()); + let body = Body { + content: &c.message, + timestamp: c.timestamp, + salt: c.salt, + last_seen: &last_seen, + }; + let signature = c.signature.as_deref().and_then(secure_chat::signature); + let g = &mut self.guests[i]; + let (uuid, name) = (g.uuid, g.name.clone()); + let message = match g + .chain + .unpack(uuid, signature.as_ref(), &body, enforce, now) + { + Ok(unpacked) => ChatMessage::new(uuid, unpacked, &body, now), + Err(e) => return self.decode_failure(i, e, events), + }; + if message.signature.is_some() && message.expired(now) { + events.push(HostEvent::Warn(format!( + "Received expired chat: '{}'. Is the client/server system time unsynchronized?", + message.content + ))); + } + let bound = play::ChatTypeBound { + chat_type: self.chat_type, + name: display_name(uuid, &name), + target: None, + }; + let line = format!("<{name}> {}", message.content); + self.broadcast_chat(&message, &bound, line, events); + } + + /// handleChatSessionUpdate: a new profile key, validated against the + /// services' keys, starts a new chain and is announced to everyone. + pub(super) fn chat_session_update( + &mut self, + i: usize, + p: play::ChatSessionUpdate, + events: &mut Vec, + ) { + let key = ProfileKey { + expires_at: p.expires_at, + key: p.key, + signature: p.key_signature, + }; + let g = &self.guests[i]; + let (conn, uuid) = (g.conn, g.uuid); + let old = g.chain.session().map(|s| s.profile_key.expires_at); + if g.chain.session().is_some_and(|s| s.profile_key == key) { + return; + } + if old.is_some_and(|old| key.expires_at < old) { + self.kick( + conn, + translatable("multiplayer.disconnect.expired_public_key", &[]), + ); + return; + } + let secure = self.lan.shared.config.secure_chat.clone(); + let keys = secure.as_deref().map_or(&[][..], |s| s.keys()); + if keys.is_empty() { + events.push(HostEvent::Warn(format!( + "Ignoring chat session from {} due to missing Services public key", + g.name + ))); + return; + } + match ChatSession::validate(p.session, key, keys, uuid) { + Ok(session) => { + let data = session.data(); + self.guests[i].chain = Chain::keyed(session); + // resetPlayerChatState: INITIALIZE_CHAT to every player. + self.broadcast(&C::PlayerInfoUpdate(play::PlayerInfoUpdate { + actions: 2, + entries: vec![play::PlayerInfo { + uuid, + chat_session: Some(data), + ..Default::default() + }], + })); + } + Err(reason) => { + events.push(HostEvent::Error( + "Failed to validate profile key".to_owned(), + )); + self.kick(conn, translatable(reason, &[])); + } + } + } + + /// PlayerList.broadcastChatMessage: logged (`[Not Secure]` unless signed + /// and fresh), then sent to every player. + pub fn broadcast_chat( + &mut self, + message: &ChatMessage, + bound: &play::ChatTypeBound, + line: String, + events: &mut Vec, + ) { + let secure = message.trusted(now_millis()); + events.push(HostEvent::Chat { line, secure }); + for k in 0..self.guests.len() { + if self.guests[k].placed { + self.send_chat(k, message, bound); + } + } + } + + /// ServerPlayer.sendChatMessage (players who show chat) and + /// sendPlayerChatMessage: numbered for the connection, the last seen + /// packed against its cache; a signed message joins the cache and the + /// messages the player must acknowledge. + pub fn send_chat(&mut self, k: usize, message: &ChatMessage, bound: &play::ChatTypeBound) { + let g = &mut self.guests[k]; + if g.info.chat_visibility != 0 { + return; + } + let last_seen = message + .last_seen + .iter() + .map(|s| g.signatures.pack(s)) + .collect(); + g.send(&C::PlayerChat(play::PlayerChat { + global_index: g.chat_index, + sender: message.sender, + index: message.index, + signature: message.signature.map(|s| s.to_vec()), + content: message.content.clone(), + timestamp: message.timestamp, + salt: message.salt, + last_seen, + unsigned_content: message.unsigned_content.clone(), + filter: (0, Vec::new()), + chat_type: bound.clone(), + })); + g.chat_index += 1; + if let Some(signature) = &message.signature { + g.signatures.push(&message.last_seen, Some(signature)); + g.last_seen.add_pending(signature); + if g.last_seen.tracked_count() > 4096 { + let conn = g.conn; + self.kick( + conn, + translatable("multiplayer.disconnect.too_many_pending_chats", &[]), + ); + } + } + } + + /// sendChatMessage of a system message: a disguised chat, to players + /// who show chat. + fn send_disguised(&self, k: usize, message: Tag, chat_type: play::ChatTypeBound) { + let g = &self.guests[k]; + if g.info.chat_visibility == 0 { + g.send(&C::DisguisedChat(play::DisguisedChat { + message, + chat_type, + })); + } + } + + /// collectSignedArguments, or collectUnsignedArguments when the client + /// signed none: each of the command's message arguments (`parsed`, by + /// name) through the sender's chain. + pub fn signed_arguments( + &mut self, + i: usize, + p: &play::ChatCommandSigned, + parsed: &[(String, String)], + last_seen: &[Signature], + events: &mut Vec, + ) -> Result, DecodeError> { + let (enforce, now) = (self.enforces_secure_chat(), now_millis()); + let g = &mut self.guests[i]; + let uuid = g.uuid; + let mut out = Vec::new(); + if p.arguments.is_empty() { + for (name, value) in parsed { + // SignedMessageBody.unsigned. + let body = Body { + content: value, + timestamp: now, + salt: 0, + last_seen: &[], + }; + let unpacked = g.chain.unpack(uuid, None, &body, enforce, now)?; + out.push((name.clone(), ChatMessage::new(uuid, unpacked, &body, now))); + } + return Ok(out); + } + let mismatch = |events: &mut Vec| { + let names = |l: &mut dyn Iterator| { + l.map(String::as_str).collect::>().join(", ") + }; + events.push(HostEvent::Error(format!( + "Signed command mismatch between server and client ('{}'): got [{}] from client, but expected [{}]", + p.command, + names(&mut p.arguments.iter().map(|a| &a.0)), + names(&mut parsed.iter().map(|a| &a.0)), + ))); + DecodeError::InvalidCommandSignature + }; + for (name, signature) in &p.arguments { + let Some((_, value)) = parsed.iter().find(|(n, _)| n == name) else { + g.chain.set_broken(); + return Err(mismatch(events)); + }; + let body = Body { + content: value, + timestamp: p.timestamp, + salt: p.salt, + last_seen, + }; + let signature = secure_chat::signature(signature); + let unpacked = g + .chain + .unpack(uuid, signature.as_ref(), &body, enforce, now)?; + out.push((name.clone(), ChatMessage::new(uuid, unpacked, &body, now))); + } + if parsed.iter().any(|(n, _)| !out.iter().any(|(o, _)| o == n)) { + return Err(mismatch(events)); + } + Ok(out) + } + + /// A message command's chat (SayCommand, EmoteCommands): to everyone, + /// bound to `chat_type` and the sender's name. + pub fn broadcast_command_chat( + &mut self, + i: usize, + chat_type: &str, + message: Option, + text: &str, + events: &mut Vec, + ) { + let (uuid, name) = (self.guests[i].uuid, self.guests[i].name.clone()); + let bound = play::ChatTypeBound { + chat_type: self.chat_type_id(chat_type), + name: display_name(uuid, &name), + target: None, + }; + let line = match chat_type { + "say_command" => format!("[{name}] {text}"), + _ => format!("* {name} {text}"), + }; + match message { + Some(message) => self.broadcast_chat(&message, &bound, line, events), + // An unsigned command packet (no signing context): + // PlayerChatMessage.system, a disguised chat. + None => { + events.push(HostEvent::Chat { + line, + secure: false, + }); + for k in 0..self.guests.len() { + if self.guests[k].placed { + self.send_disguised(k, literal(text), bound.clone()); + } + } + } + } + } + + /// MsgCommand.sendMessage to each target: the outgoing line for the + /// sender, the incoming one for the target. Nothing is logged. + pub fn whisper( + &mut self, + i: usize, + targets: &[usize], + message: Option, + text: &str, + ) { + let (uuid, name) = (self.guests[i].uuid, self.guests[i].name.clone()); + let (outgoing, incoming) = ( + self.chat_type_id("msg_command_outgoing"), + self.chat_type_id("msg_command_incoming"), + ); + for &t in targets { + let target = display_name(self.guests[t].uuid, &self.guests[t].name); + let out = play::ChatTypeBound { + chat_type: outgoing, + name: display_name(uuid, &name), + target: Some(target), + }; + let inc = play::ChatTypeBound { + chat_type: incoming, + name: display_name(uuid, &name), + target: None, + }; + match &message { + Some(m) => { + self.send_chat(i, m, &out); + self.send_chat(t, m, &inc); + } + None => { + self.send_disguised(i, literal(text), out); + self.send_disguised(t, literal(text), inc); + } + } + } + } + + /// A chat type's registry id. + fn chat_type_id(&self, name: &str) -> i32 { + self.sync + .id("minecraft:chat_type", &format!("minecraft:{name}")) + .unwrap_or(self.chat_type) + } +} diff --git a/crates/horizon-client/src/lan/mod.rs b/crates/horizon-client/src/lan/mod.rs index 7a1d921a4..afe188699 100644 --- a/crates/horizon-client/src/lan/mod.rs +++ b/crates/horizon-client/src/lan/mod.rs @@ -122,8 +122,8 @@ pub struct NetConfig { pub hide_online_players: bool, /// server-icon.png (64x64 PNG bytes). pub favicon: Option>, - /// MinecraftServer.enforceSecureProfile. - pub enforces_secure_chat: bool, + /// Secure chat (a dedicated server; none on a LAN world). + pub secure_chat: Option>, pub accepts_transfers: bool, /// LanServerPinger: announce the world on the local network. pub announce: bool, @@ -138,6 +138,10 @@ pub struct NetConfig { pub log: Option, } impl NetConfig { + /// MinecraftServer.enforceSecureProfile. + pub fn enforces_secure_chat(&self) -> bool { + self.secure_chat.as_ref().is_some_and(|s| s.enforced()) + } /// IntegratedServer.publishServer. pub fn lan(port: u16, motd: String, max_players: usize) -> Self { Self { @@ -149,7 +153,7 @@ impl NetConfig { enable_status: true, hide_online_players: false, favicon: None, - enforces_secure_chat: false, + secure_chat: None, accepts_transfers: false, announce: true, listen: true, @@ -161,6 +165,46 @@ impl NetConfig { } } +/// A dedicated server's secure chat (DedicatedServer.enforceSecureProfile, +/// Services.profileKeySignatureValidator): the services' profile-key keys, +/// fetched in the background from startup and waited for by their first +/// use, as authlib's RetryableFetch does. +pub struct SecureChat { + /// enforce-secure-profile, in online mode. + pub enforce: bool, + keys: std::sync::OnceLock>, + fetch: Mutex>>>, +} + +impl SecureChat { + pub fn new( + enforce: bool, + fetch: impl FnOnce() -> Vec + Send + 'static, + ) -> Self { + let handle = std::thread::Builder::new() + .name("Services keys".into()) + .spawn(fetch) + .ok(); + Self { + enforce, + keys: std::sync::OnceLock::new(), + fetch: Mutex::new(handle), + } + } + /// The keys players' chat keys must be signed by (none when the + /// services couldn't be reached). + pub fn keys(&self) -> &[horizon_protocol::rsa::RsaPublicKey] { + self.keys.get_or_init(|| { + let handle = self.fetch.lock().ok().and_then(|mut h| h.take()); + handle.and_then(|h| h.join().ok()).unwrap_or_default() + }) + } + /// enforceSecureProfile: enforced, and able to check keys. + pub fn enforced(&self) -> bool { + self.enforce && !self.keys().is_empty() + } +} + /// State shared with the connection threads. pub struct Shared { pub sync: Arc, @@ -301,7 +345,7 @@ impl Shared { Json::String(format!("data:image/png;base64,{}", base64(icon))), )); } - if self.config.enforces_secure_chat { + if self.config.enforces_secure_chat() { status.push(("enforcesSecureChat".to_owned(), Json::Bool(true))); } Json::Object(status).compact() diff --git a/crates/horizon-protocol/Cargo.toml b/crates/horizon-protocol/Cargo.toml index aa1f60f89..66a0af162 100644 --- a/crates/horizon-protocol/Cargo.toml +++ b/crates/horizon-protocol/Cargo.toml @@ -13,6 +13,8 @@ serde_json = "1.0.151" # The shared secret and RSA padding (OS randomness) and the server hash. getrandom = "0.3" sha1 = "0.10" +# Secure chat: SHA256withRSA message signatures. +sha2 = "0.10" [lints] workspace = true diff --git a/crates/horizon-protocol/src/buf.rs b/crates/horizon-protocol/src/buf.rs index e761aec3f..349ec74cf 100644 --- a/crates/horizon-protocol/src/buf.rs +++ b/crates/horizon-protocol/src/buf.rs @@ -311,6 +311,16 @@ impl Codec> for Bytes { w.byte_array(v) } } +/// ByteBufCodecs.byteArray(N): VarInt-prefixed bytes, at most N. +pub struct BytesN; +impl Codec> for BytesN { + fn read(r: &mut Reader) -> Result> { + r.byte_array(N) + } + fn write(v: &Vec, w: &mut Writer) { + w.byte_array(v) + } +} /// Everything left in the packet (custom payloads). pub struct Rest; impl Codec> for Rest { diff --git a/crates/horizon-protocol/src/lib.rs b/crates/horizon-protocol/src/lib.rs index e49031b77..1d0f081f7 100644 --- a/crates/horizon-protocol/src/lib.rs +++ b/crates/horizon-protocol/src/lib.rs @@ -13,6 +13,7 @@ pub mod item; pub mod packets; pub mod registry; pub mod rsa; +pub mod secure_chat; pub mod servers; pub mod text; diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 20a25d078..895db9755 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -2,9 +2,9 @@ use crate::{ Error, Result, buf::{ - Bool, Byte, ChunkPos, Codec, Double, Float, Ident, Int, List, Long, LpVec3, Nbt, Opt, - OptVarInt, Pos, Reader, SectionPos, Short, Str, StrN, UByte, Uuid, VarInt, VarLong, Vec3d, - Writer, + Bool, Byte, BytesN, ChunkPos, Codec, Double, Float, Ident, Int, List, Long, LpVec3, Nbt, + Opt, OptVarInt, Pos, Reader, SectionPos, Short, Str, StrN, UByte, Uuid, VarInt, VarLong, + Vec3d, Writer, }, chunk::{ChunkData, LightData}, entity_data::{EntityData, Value}, @@ -1604,6 +1604,42 @@ packet!(Chat { last_seen: LastSeenUpdate => LastSeenUpdate, }); packet!(ChatCommand { command: String => Str }); +packet!(ChatAck { offset: i32 => VarInt }); +/// ArgumentSignatures: each signed argument's name (at most 16 chars) and +/// signature, at most 8. +pub struct ArgumentSignatures; +impl Codec)>> for ArgumentSignatures { + fn read(r: &mut Reader) -> Result)>> { + let n = r.count(8)?; + (0..n) + .map(|_| Ok((r.string(16)?, r.bytes(256)?.to_vec()))) + .collect() + } + fn write(v: &Vec<(String, Vec)>, w: &mut Writer) { + w.varint(v.len() as i32); + for (name, signature) in v { + w.string(name); + w.bytes(signature); + } + } +} +packet!(ChatCommandSigned { + command: String => Str, + timestamp: i64 => Long, + salt: i64 => Long, + arguments: Vec<(String, Vec)> => ArgumentSignatures, + last_seen: LastSeenUpdate => LastSeenUpdate, +}); +packet!( + /// RemoteChatSession.Data: the session id, then ProfilePublicKey.Data + /// (expiry in epoch milliseconds, X.509 key, the services' signature). + ChatSessionUpdate { + session: u128 => Uuid, + expires_at: i64 => Long, + key: Vec => BytesN<512>, + key_signature: Vec => BytesN<4096>, + } +); packet!(ChangeDifficultyRequest { difficulty: i32 => VarInt }); packet!(LockDifficulty { locked: bool => Bool }); packet!(SetGameRule { entries: Vec => List }); @@ -1697,7 +1733,10 @@ packet_enum!(Serverbound, crate::ids::play::serverbound { Attack(Attack) = ATTACK, ChangeGameMode(ChangeGameMode) = CHANGE_GAME_MODE, Chat(Chat) = CHAT, + ChatAck(ChatAck) = CHAT_ACK, ChatCommand(ChatCommand) = CHAT_COMMAND, + ChatCommandSigned(ChatCommandSigned) = CHAT_COMMAND_SIGNED, + ChatSessionUpdate(ChatSessionUpdate) = CHAT_SESSION_UPDATE, PlaceRecipe(super::recipes::PlaceRecipe) = PLACE_RECIPE, ChunkBatchReceived(ChunkBatchReceived) = CHUNK_BATCH_RECEIVED, ClientCommand(ClientCommand) = CLIENT_COMMAND, @@ -2184,6 +2223,24 @@ mod tests { Serverbound::ChatCommand(ChatCommand { command: "time set day".into(), }), + Serverbound::ChatAck(ChatAck { offset: 3 }), + Serverbound::ChatCommandSigned(ChatCommandSigned { + command: "say hi".into(), + timestamp: 1_700_000_000_000, + salt: -5, + arguments: vec![("message".into(), vec![9; 256])], + last_seen: LastSeenUpdate { + offset: 1, + acknowledged: [1, 0, 8], + checksum: -3, + }, + }), + Serverbound::ChatSessionUpdate(ChatSessionUpdate { + session: 0x1234, + expires_at: 1_800_000_000_000, + key: vec![0x30; 294], + key_signature: vec![7; 512], + }), Serverbound::ChunkBatchReceived(ChunkBatchReceived { chunks_per_tick: 9.5, }), diff --git a/crates/horizon-protocol/src/rsa.rs b/crates/horizon-protocol/src/rsa.rs index fe31e425e..d34489fb4 100644 --- a/crates/horizon-protocol/src/rsa.rs +++ b/crates/horizon-protocol/src/rsa.rs @@ -3,8 +3,10 @@ //! and challenge (Crypt.encryptUsingKey, Java's "RSA" cipher = //! RSA/ECB/PKCS1Padding), and on the server's side the key pair it makes at //! startup (Crypt.generateKeyPair: 1024 bits) and the decryption -//! (Crypt.decryptUsingKey). Modular exponentiation uses Montgomery -//! multiplication on 32-bit limbs; RSA moduli are always odd. +//! (Crypt.decryptUsingKey); and the PKCS#1 v1.5 signature checks of secure +//! chat (SHA256withRSA for messages, SHA1withRSA for the services' profile +//! key signatures). Modular exponentiation uses Montgomery multiplication on +//! 32-bit limbs; RSA moduli are always odd. use crate::{Error, Result}; /// A non-negative integer, little-endian 32-bit limbs. @@ -291,6 +293,63 @@ impl RsaPublicKey { } } +/// The digest a PKCS#1 v1.5 signature covers: Java's `SHA1withRSA` and +/// `SHA256withRSA`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SignatureHash { + Sha1, + Sha256, +} + +impl SignatureHash { + /// The DER DigestInfo before the digest (RFC 8017 9.2, note 1). + fn prefix(self) -> &'static [u8] { + match self { + Self::Sha1 => &[ + 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, + 0x14, + ], + Self::Sha256 => &[ + 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, + 0x01, 0x05, 0x00, 0x04, 0x20, + ], + } + } + fn digest(self, message: &[u8]) -> Vec { + use sha1::Digest; + match self { + Self::Sha1 => sha1::Sha1::digest(message).to_vec(), + Self::Sha256 => sha2::Sha256::digest(message).to_vec(), + } + } +} + +impl RsaPublicKey { + /// `Signature.verify` for RSASSA-PKCS1-v1_5 (RFC 8017 8.2.2): the + /// signature is exactly the modulus' length and below it, and opens to + /// `00 01 FF.. 00 DigestInfo`. Anything else is a failed check, as + /// SignatureValidator reports Java's exceptions. + pub fn verify(&self, hash: SignatureHash, message: &[u8], signature: &[u8]) -> bool { + let k = self.size(); + // Equal lengths compare as big-endian numbers. + if signature.len() != k || signature >= self.modulus.as_slice() { + return false; + } + let (prefix, digest) = (hash.prefix(), hash.digest(message)); + let t = prefix.len() + digest.len(); + if k < t + 11 { + return false; + } + let mut expected = vec![0xff; k]; + expected[0] = 0; + expected[1] = 1; + expected[k - t - 1] = 0; + expected[k - t..k - digest.len()].copy_from_slice(prefix); + expected[k - digest.len()..].copy_from_slice(&digest); + self.apply(signature, &self.exponent) == expected + } +} + /// Small odd primes for sieving prime candidates. const SMALL_PRIMES: [u32; 53] = [ 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, @@ -604,5 +663,34 @@ pub(crate) mod tests { assert!(block.ends_with(&hex( "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" ))); + let signature = hex(TEST_SIG_HELLO); + assert!(key.verify(SignatureHash::Sha256, b"hello", &signature)); + assert!(!key.verify(SignatureHash::Sha256, b"hellO", &signature)); + assert!(!key.verify(SignatureHash::Sha1, b"hello", &signature)); + let mut flipped = signature.clone(); + flipped[40] ^= 1; + assert!(!key.verify(SignatureHash::Sha256, b"hello", &flipped)); + assert!(!key.verify(SignatureHash::Sha256, b"hello", &signature[1..])); + assert!(!key.verify(SignatureHash::Sha256, b"hello", &key.modulus)); + } + + /// Signatures made here with the private exponent (EMSA-PKCS1-v1_5 by + /// hand) check out for both digests. + #[test] + fn verifies_both_digests() { + let key = RsaPublicKey::from_der(&hex(TEST_SPKI)).unwrap(); + for hash in [SignatureHash::Sha1, SignatureHash::Sha256] { + let (prefix, digest) = (hash.prefix(), hash.digest(b"payload")); + let mut block = vec![0xff; 128]; + block[0] = 0; + block[1] = 1; + let t = prefix.len() + digest.len(); + block[127 - t] = 0; + block[128 - t..128 - digest.len()].copy_from_slice(prefix); + block[128 - digest.len()..].copy_from_slice(&digest); + let signature = key.apply(&block, &hex(TEST_D)); + assert!(key.verify(hash, b"payload", &signature)); + assert!(!key.verify(hash, b"payloaD", &signature)); + } } } diff --git a/crates/horizon-protocol/src/secure_chat.rs b/crates/horizon-protocol/src/secure_chat.rs new file mode 100644 index 000000000..534c132f6 --- /dev/null +++ b/crates/horizon-protocol/src/secure_chat.rs @@ -0,0 +1,491 @@ +//! Secure chat as vanilla 26.3's server keeps it: a player's profile key, +//! validated against the services' keys (ProfilePublicKey, +//! RemoteChatSession), the message chain each player signs +//! (SignedMessageChain), what a signature covers (PlayerChatMessage, +//! SignedMessageLink, SignedMessageBody), the window of messages a client +//! acknowledges (LastSeenMessagesValidator) and the cache a connection packs +//! signatures against (MessageSignatureCache). +use crate::rsa::{RsaPublicKey, SignatureHash}; +use std::collections::{HashSet, VecDeque}; + +/// MessageSignature.BYTES. +pub const SIGNATURE_BYTES: usize = 256; +/// A message signature (MessageSignature). +pub type Signature = [u8; SIGNATURE_BYTES]; +/// LastSeenMessages.LAST_SEEN_MESSAGES_MAX_LENGTH: the window a client acknowledges. +pub const LAST_SEEN: usize = 20; +/// PlayerChatMessage.MESSAGE_EXPIRES_AFTER_SERVER, in milliseconds. +pub const EXPIRES_AFTER_SERVER: i64 = 5 * 60 * 1000; + +/// A signature from its packet bytes (always 256 on the wire). +pub fn signature(bytes: &[u8]) -> Option { + bytes.try_into().ok() +} + +/// ProfilePublicKey.Data: the player's chat key (X.509 DER), when it +/// expires (epoch milliseconds), and the services' signature over both. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ProfileKey { + pub expires_at: i64, + pub key: Vec, + pub signature: Vec, +} + +impl ProfileKey { + /// Data.signedPayload: the profile id, the expiry and the key. + fn signed_payload(&self, profile: u128) -> Vec { + let mut out = Vec::with_capacity(24 + self.key.len()); + out.extend_from_slice(&profile.to_be_bytes()); + out.extend_from_slice(&self.expires_at.to_be_bytes()); + out.extend_from_slice(&self.key); + out + } + /// Data.validateSignature with the services' profile-key keys + /// (SignatureValidator.from(keySet, PROFILE_KEY): any of them, SHA1withRSA). + pub fn signed_by(&self, services: &[RsaPublicKey], profile: u128) -> bool { + let payload = self.signed_payload(profile); + services + .iter() + .any(|k| k.verify(SignatureHash::Sha1, &payload, &self.signature)) + } + /// Data.hasExpired. + pub fn has_expired(&self, now: i64) -> bool { + self.expires_at < now + } +} + +/// A validated RemoteChatSession: its id and the player's profile key. +#[derive(Clone, Debug)] +pub struct ChatSession { + pub id: u128, + pub profile_key: ProfileKey, + key: RsaPublicKey, +} + +impl ChatSession { + /// RemoteChatSession.Data.validate (ProfilePublicKey.createValidated); + /// the error is the disconnect message's translation key. + pub fn validate( + id: u128, + profile_key: ProfileKey, + services: &[RsaPublicKey], + profile: u128, + ) -> Result { + const INVALID: &str = "multiplayer.disconnect.invalid_public_key_signature"; + let key = RsaPublicKey::from_der(&profile_key.key).map_err(|_| INVALID)?; + if !profile_key.signed_by(services, profile) { + return Err(INVALID); + } + Ok(Self { + id, + profile_key, + key, + }) + } + /// RemoteChatSession.Data, as ClientboundPlayerInfoUpdatePacket's + /// INITIALIZE_CHAT carries it. + pub fn data(&self) -> (u128, i64, Vec, Vec) { + let k = &self.profile_key; + (self.id, k.expires_at, k.key.clone(), k.signature.clone()) + } +} + +/// SignedMessageBody: what a player signed besides the chain link. +#[derive(Clone, Copy, Debug)] +pub struct Body<'a> { + pub content: &'a str, + /// Epoch milliseconds. + pub timestamp: i64, + pub salt: i64, + pub last_seen: &'a [Signature], +} + +/// PlayerChatMessage.updateSignature: the version, the link (sender, +/// session, index) and the body (salt, time in seconds, content, last seen). +pub fn signed_payload(sender: u128, session: u128, index: i32, body: &Body) -> Vec { + let content = body.content.as_bytes(); + let mut out = Vec::with_capacity(64 + content.len() + body.last_seen.len() * SIGNATURE_BYTES); + out.extend_from_slice(&1i32.to_be_bytes()); + out.extend_from_slice(&sender.to_be_bytes()); + out.extend_from_slice(&session.to_be_bytes()); + out.extend_from_slice(&index.to_be_bytes()); + out.extend_from_slice(&body.salt.to_be_bytes()); + out.extend_from_slice(&body.timestamp.div_euclid(1000).to_be_bytes()); + out.extend_from_slice(&(content.len() as i32).to_be_bytes()); + out.extend_from_slice(content); + out.extend_from_slice(&(body.last_seen.len() as i32).to_be_bytes()); + for s in body.last_seen { + out.extend_from_slice(s); + } + out +} + +/// SignedMessageChain.DecodeException's messages, and the signed command +/// mismatch (ServerGamePacketListenerImpl.INVALID_COMMAND_SIGNATURE). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum DecodeError { + MissingProfileKey, + ChainBroken, + ExpiredProfileKey, + InvalidSignature, + OutOfOrderChat, + InvalidCommandSignature, +} + +impl DecodeError { + pub fn key(self) -> &'static str { + match self { + Self::MissingProfileKey => "chat.disabled.missingProfileKey", + Self::ChainBroken => "chat.disabled.chain_broken", + Self::ExpiredProfileKey => "chat.disabled.expiredProfileKey", + Self::InvalidSignature => "chat.disabled.invalid_signature", + Self::OutOfOrderChat => "chat.disabled.out_of_order_chat", + Self::InvalidCommandSignature => "chat.disabled.invalid_command_signature", + } + } +} + +/// A message the chain let through: unsigned (PlayerChatMessage.unsigned) +/// or signed at a link of the sender's chain. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Unpacked { + Unsigned, + Signed { index: i32, signature: Signature }, +} + +/// A player's SignedMessageChain.Decoder: unsigned until the player sends a +/// chat session, then keyed to it. +#[derive(Clone, Debug, Default)] +pub struct Chain { + session: Option, + /// The next link's index; None once the chain is broken. + next: Option, + /// The last message's time (lastTimeStamp, from Instant.EPOCH). + last_timestamp: i64, +} + +impl Chain { + /// RemoteChatSession.createMessageDecoder: a new chain from link 0. + pub fn keyed(session: ChatSession) -> Self { + Self { + session: Some(session), + next: Some(0), + last_timestamp: 0, + } + } + pub fn session(&self) -> Option<&ChatSession> { + self.session.as_ref() + } + /// Decoder.unpack for `sender`'s message. Without a session the message + /// is unsigned unless the server enforces secure chat; with one it must + /// be the next link, in order, signed by the player's key. + pub fn unpack( + &mut self, + sender: u128, + signature: Option<&Signature>, + body: &Body, + enforce: bool, + now: i64, + ) -> Result { + let Some(session) = &self.session else { + return if enforce { + Err(DecodeError::MissingProfileKey) + } else { + Ok(Unpacked::Unsigned) + }; + }; + let signature = signature.ok_or(DecodeError::MissingProfileKey)?; + if session.profile_key.has_expired(now) { + return Err(DecodeError::ExpiredProfileKey); + } + let index = self.next.ok_or(DecodeError::ChainBroken)?; + if body.timestamp < self.last_timestamp { + self.next = None; + return Err(DecodeError::OutOfOrderChat); + } + self.last_timestamp = body.timestamp; + let payload = signed_payload(sender, session.id, index, body); + if !session + .key + .verify(SignatureHash::Sha256, &payload, signature) + { + self.next = None; + return Err(DecodeError::InvalidSignature); + } + self.next = index.checked_add(1); + Ok(Unpacked::Signed { + index, + signature: *signature, + }) + } + /// Decoder.setChainBroken (nothing for the unsigned decoder). + pub fn set_broken(&mut self) { + if self.session.is_some() { + self.next = None; + } + } +} + +/// Arrays.hashCode(byte[]). +fn java_hash(bytes: &[u8]) -> i32 { + bytes.iter().fold(1i32, |h, &b| { + h.wrapping_mul(31).wrapping_add(i32::from(b as i8)) + }) +} + +/// LastSeenMessages.computeChecksum: never 0, which means "not checked". +pub fn checksum(entries: &[Signature]) -> i8 { + let c = entries + .iter() + .fold(1i32, |c, e| c.wrapping_mul(31).wrapping_add(java_hash(e))); + match c as i8 { + 0 => 1, + b => b, + } +} + +/// LastSeenMessagesValidator: the messages sent to a player that its next +/// acknowledgement may name, each pending until it does. +#[derive(Clone, Debug)] +pub struct LastSeenValidator { + tracked: VecDeque>, + last_pending: Option, +} + +impl Default for LastSeenValidator { + fn default() -> Self { + Self { + tracked: std::iter::repeat_n(None, LAST_SEEN).collect(), + last_pending: None, + } + } +} + +impl LastSeenValidator { + pub fn add_pending(&mut self, signature: &Signature) { + if self.last_pending.as_ref() != Some(signature) { + self.tracked.push_back(Some((*signature, true))); + self.last_pending = Some(*signature); + } + } + pub fn tracked_count(&self) -> usize { + self.tracked.len() + } + /// applyOffset: drop the `offset` oldest messages. + pub fn apply_offset(&mut self, offset: i32) -> Result<(), String> { + let max = self.tracked.len() as i64 - LAST_SEEN as i64; + if offset >= 0 && i64::from(offset) <= max { + self.tracked.drain(..offset as usize); + Ok(()) + } else { + Err(format!( + "Advanced last seen window by {offset} messages, but expected at most {max}" + )) + } + } + /// applyUpdate: the offset, then the acknowledged messages of the + /// window (a 20-bit set, little-endian bytes), checked against the + /// client's checksum unless it sent 0. + pub fn apply_update( + &mut self, + offset: i32, + acknowledged: [u8; 3], + checksum: i8, + ) -> Result, String> { + self.apply_offset(offset)?; + let bit = |i: usize| acknowledged[i / 8] >> (i % 8) & 1 != 0; + let length = (0..24).rev().find(|&i| bit(i)).map_or(0, |i| i + 1); + if length > LAST_SEEN { + return Err(format!( + "Last seen update contained {length} messages, but maximum window size is {LAST_SEEN}" + )); + } + let mut seen = Vec::with_capacity(LAST_SEEN); + for i in 0..LAST_SEEN { + let entry = self.tracked[i]; + if bit(i) { + let Some((s, _)) = entry else { + return Err(format!( + "Last seen update acknowledged unknown or previously ignored message at index {i}" + )); + }; + self.tracked[i] = Some((s, false)); + seen.push(s); + } else { + if let Some((_, false)) = entry { + return Err(format!( + "Last seen update ignored previously acknowledged message at index {i}" + )); + } + self.tracked[i] = None; + } + } + if checksum != 0 && checksum != self::checksum(&seen) { + return Err( + "Checksum mismatch on last seen update: the client and server must have desynced" + .to_owned(), + ); + } + Ok(seen) + } +} + +/// MessageSignatureCache: the 128 signatures a connection's client also +/// holds, so a packed signature can name one by index. +#[derive(Clone, Debug)] +pub struct SignatureCache { + entries: Vec>, +} + +impl Default for SignatureCache { + fn default() -> Self { + Self { + entries: vec![None; 128], + } + } +} + +impl SignatureCache { + /// MessageSignature.pack: the cache index, or the full signature. + pub fn pack(&self, signature: &Signature) -> (i32, Option>) { + match self + .entries + .iter() + .position(|e| e.as_ref() == Some(signature)) + { + Some(i) => (i as i32, None), + None => (-1, Some(signature.to_vec())), + } + } + /// push(body, signature): the message's last seen and its signature go + /// to the front, most recent first; what they displace moves back. + pub fn push(&mut self, last_seen: &[Signature], signature: Option<&Signature>) { + let mut queue: VecDeque = last_seen.iter().chain(signature).copied().collect(); + let new: HashSet = queue.iter().copied().collect(); + for slot in &mut self.entries { + let Some(next) = queue.pop_back() else { break }; + if let Some(old) = slot.replace(next) + && !new.contains(&old) + { + queue.push_front(old); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sig(b: u8) -> Signature { + [b; SIGNATURE_BYTES] + } + + #[test] + fn payload_layout_matches_update_signature() { + let body = Body { + content: "hé", + timestamp: 1_700_000_123_456, + salt: -2, + last_seen: &[sig(7)], + }; + let p = signed_payload(1, 2, 3, &body); + assert_eq!(&p[..4], &[0, 0, 0, 1]); + assert_eq!(&p[4..20], &1u128.to_be_bytes()); + assert_eq!(&p[20..36], &2u128.to_be_bytes()); + assert_eq!(&p[36..40], &3i32.to_be_bytes()); + assert_eq!(&p[40..48], &(-2i64).to_be_bytes()); + assert_eq!(&p[48..56], &1_700_000_123i64.to_be_bytes()); + assert_eq!(&p[56..60], &3i32.to_be_bytes()); + assert_eq!(&p[60..63], "hé".as_bytes()); + assert_eq!(&p[63..67], &1i32.to_be_bytes()); + assert_eq!(p.len(), 67 + 256); + // Instant.getEpochSecond floors. + let before = Body { + timestamp: -1, + ..body + }; + assert_eq!( + &signed_payload(1, 2, 3, &before)[48..56], + &(-1i64).to_be_bytes() + ); + } + + #[test] + fn unsigned_chain_follows_enforcement() { + let mut chain = Chain::default(); + let body = Body { + content: "hi", + timestamp: 5, + salt: 0, + last_seen: &[], + }; + assert_eq!( + chain.unpack(1, None, &body, false, 0), + Ok(Unpacked::Unsigned) + ); + assert_eq!( + chain.unpack(1, Some(&sig(1)), &body, false, 0), + Ok(Unpacked::Unsigned) + ); + assert_eq!( + chain.unpack(1, None, &body, true, 0), + Err(DecodeError::MissingProfileKey) + ); + } + + #[test] + fn checksums_follow_java_hash_codes() { + // Arrays.hashCode(new byte[]{-1}) = 31 + (-1) = 30. + assert_eq!(java_hash(&[0xff]), 30); + assert_eq!(checksum(&[]), 1); + let one = [sig(0)]; + let expected = 31i32.wrapping_add(java_hash(&sig(0))) as i8; + assert_eq!(checksum(&one), if expected == 0 { 1 } else { expected }); + } + + #[test] + fn last_seen_window() { + let mut v = LastSeenValidator::default(); + assert_eq!(v.tracked_count(), 20); + v.add_pending(&sig(1)); + v.add_pending(&sig(1)); + v.add_pending(&sig(2)); + assert_eq!(v.tracked_count(), 22); + assert!(v.apply_offset(3).is_err()); + // Slide by two: the two messages are the window's last entries. + let mut ack = [0u8; 3]; + ack[18 / 8] |= 1 << (18 % 8); + ack[19 / 8] |= 1 << (19 % 8); + let seen = v.apply_update(2, ack, checksum(&[sig(1), sig(2)])).unwrap(); + assert_eq!(seen, vec![sig(1), sig(2)]); + // Once acknowledged, a message can't be ignored. + assert!(v.apply_update(0, [0; 3], 0).is_err()); + let mut v = LastSeenValidator::default(); + v.add_pending(&sig(1)); + assert!(v.apply_update(1, [0, 0, 0x10], 0).is_err(), "bit 20"); + let mut v = LastSeenValidator::default(); + assert!(v.apply_update(0, [1, 0, 0], 0).is_err(), "unknown"); + let mut v = LastSeenValidator::default(); + v.add_pending(&sig(1)); + let mut ack = [0u8; 3]; + ack[2] |= 1 << 3; + assert!(v.apply_update(1, ack, 99).is_err(), "checksum"); + } + + #[test] + fn cache_moves_recent_signatures_first() { + let mut c = SignatureCache::default(); + assert_eq!(c.pack(&sig(1)), (-1, Some(sig(1).to_vec()))); + c.push(&[], Some(&sig(1))); + assert_eq!(c.pack(&sig(1)), (0, None)); + c.push(&[sig(1)], Some(&sig(2))); + assert_eq!(c.pack(&sig(2)), (0, None)); + assert_eq!(c.pack(&sig(1)), (1, None)); + c.push(&[], Some(&sig(3))); + assert_eq!( + [c.pack(&sig(3)).0, c.pack(&sig(2)).0, c.pack(&sig(1)).0], + [0, 1, 2] + ); + } +} diff --git a/crates/horizon-server-files/Cargo.toml b/crates/horizon-server-files/Cargo.toml index 184dadc90..2643fc726 100644 --- a/crates/horizon-server-files/Cargo.toml +++ b/crates/horizon-server-files/Cargo.toml @@ -12,6 +12,8 @@ horizon-protocol = { path = "../horizon-protocol" } horizon-world = { path = "../horizon-world" } log = "0.4" # Online mode: finding accounts by name (the services' profiles endpoint). +# Secure chat: the services' public keys arrive base64-encoded. +base64 = "0.22" ureq = { version = "2", default-features = false, features = ["tls"] } [lints] diff --git a/crates/horizon-server-files/src/profiles.rs b/crates/horizon-server-files/src/profiles.rs index a093fbfb1..3e16e2a41 100644 --- a/crates/horizon-server-files/src/profiles.rs +++ b/crates/horizon-server-files/src/profiles.rs @@ -6,6 +6,7 @@ use crate::{ json::{self, Json}, user_lists::NameAndId, }; +use horizon_protocol::rsa::RsaPublicKey; use std::time::Duration; /// Mojang's services discovery document. @@ -51,6 +52,57 @@ pub fn find_by_name(discovery: &str, name: &str) -> Option { found(&agent.get(&url).call().ok()?.into_string().ok()?, name) } +/// MinecraftServicesKeyInfo.get: the services' public keys from the +/// discovery document's `authentication` / `getPublicKeys` endpoint. The +/// profile-key keys (`playerCertificateKeys`, ServicesKeyType.PROFILE_KEY) +/// sign the players' chat keys; secure chat checks them. The error is the +/// line vanilla logs, after which it has no keys. +pub fn fetch_profile_key_keys(discovery: &str) -> Result, &'static str> { + const FAILED: &str = "Failed to request Minecraft Services public key"; + let agent = ureq::AgentBuilder::new() + .timeout(Duration::from_secs(15)) + .user_agent(concat!("Horizon/", env!("CARGO_PKG_VERSION"))) + .build(); + let get = |url: &str| agent.get(url).call().ok()?.into_string().ok(); + let doc = get(discovery) + .and_then(|d| json::parse(&d, true).ok()) + .ok_or(FAILED)?; + let url = endpoint(&doc, "authentication", "getPublicKeys").ok_or(FAILED)?; + profile_key_keys(&get(&url).ok_or(FAILED)?) +} + +/// The key set answer: `{"profilePropertyKeys": [...], "playerCertificateKeys": +/// [...]}`, each `{"publicKey": base64 X.509}`. One bad key makes the whole +/// answer malformed (parseList throws). +pub fn profile_key_keys(answer: &str) -> Result, &'static str> { + use base64::Engine; + const MALFORMED: &str = "Received malformed Minecraft Services public key data"; + let doc = json::parse(answer, true).map_err(|_| MALFORMED)?; + let list = |name: &str| -> Result, &'static str> { + let Some(entries) = doc.get(name) else { + return Ok(Vec::new()); + }; + let Json::Array(entries) = entries else { + return Err(MALFORMED); + }; + entries + .iter() + .map(|e| { + let text = e + .get("publicKey") + .and_then(Json::as_string) + .ok_or(MALFORMED)?; + let der = base64::engine::general_purpose::STANDARD + .decode(text) + .map_err(|_| MALFORMED)?; + RsaPublicKey::from_der(&der).map_err(|_| MALFORMED) + }) + .collect() + }; + list("profilePropertyKeys")?; + list("playerCertificateKeys") +} + /// MinecraftServicesDiscoveryService.getUrl(service, endpoint). fn endpoint(doc: &Json, service: &str, name: &str) -> Option { doc.get("discovery")? @@ -97,4 +149,20 @@ mod tests { assert_eq!(found("{}", "Notch"), None); assert_eq!(found("", "Notch"), None); } + + #[test] + fn services_key_sets() { + // The 1024-bit test key of horizon-protocol's RSA tests, base64. + let key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrAmB0+SRQqPcm84lbsRF2yo8GXzEAzUFDusT302dO3P+VtBoFJdI2WlScEG7GaFiBs5IXdCD9Mf9cFE/xG6JDEedRzSxrXRgXtq9mhqQ8ldYSQ2EwBeaUW2CTzZUiPOxeiFaCbanS7hsyM2KsHOvqs2WcvEUxmptUDdqBakeY/wIDAQAB"; + let answer = format!( + r#"{{"profilePropertyKeys":[{{"publicKey":"{key}"}}],"playerCertificateKeys":[{{"publicKey":"{key}"}},{{"publicKey":"{key}"}}]}}"# + ); + let keys = profile_key_keys(&answer).unwrap(); + assert_eq!(keys.len(), 2); + assert_eq!(keys[0].size(), 128); + assert_eq!(profile_key_keys("{}").unwrap().len(), 0); + let bad = r#"{"profilePropertyKeys":[{"publicKey":"AAAA"}],"playerCertificateKeys":[]}"#; + assert!(profile_key_keys(bad).is_err()); + assert!(profile_key_keys("not json").is_err()); + } } diff --git a/docs/hosting.md b/docs/hosting.md index bb5d6db4d..346eb7c8b 100644 --- a/docs/hosting.md +++ b/docs/hosting.md @@ -20,8 +20,9 @@ plan are in [docs/server.md](https://github.com/ludovic111/horizon/blob/main/doc Until then Horizon players can join offline servers only (`online_mode = false`). An offline server never checks accounts: anyone who reaches it can join under any name. Keep it on your LAN or a tailnet (Tailscale, ZeroTier), or share it only with friends you trust. -- **No secure chat.** Chat isn't signed, and `enforce-secure-profile` has no effect. Vanilla clients show - chat from the server as not secure. +- **Secure chat** works as on vanilla's server: in online mode, vanilla clients sign their chat, and with + `enforce-secure-profile=true` (the default) unsigned chat is refused. On an offline server, or for a player + without a chat session (Horizon's own client today), chat stays unsigned and shows as not secure. - **Nether and End:** each player travels on their own through portals, and operators with `execute in run tp`. The players in the level they leave stop seeing them, and the ones in the level they arrive in start to, as on vanilla (checked against vanilla with two players, by command and through an End diff --git a/docs/server.md b/docs/server.md index 30b81f64a..941a4341b 100644 --- a/docs/server.md +++ b/docs/server.md @@ -82,7 +82,8 @@ not tick cost, so it is in scope here (see section 9). | Area | Status | |---|---| -| `online-mode=true` | **Works** (ServerLoginPacketListenerImpl): a 1024-bit RSA key pair at start ("Generating keypair"), the encryption request, the shared secret and challenge, AES/CFB8, and the session service's `hasJoined` check (with the address when `prevent-proxy-connections` is on), with vanilla's log lines and messages. `test_online_mode_parity.py` runs the same logins against vanilla's server and Horizon's with a mock session service. Not yet: secure chat (chat is unsigned, as on a server that doesn't enforce it). | +| `online-mode=true` | **Works** (ServerLoginPacketListenerImpl): a 1024-bit RSA key pair at start ("Generating keypair"), the encryption request, the shared secret and challenge, AES/CFB8, and the session service's `hasJoined` check (with the address when `prevent-proxy-connections` is on), with vanilla's log lines and messages. `test_online_mode_parity.py` runs the same logins against vanilla's server and Horizon's with a mock session service. | +| Secure chat | **Works** (`SignedMessageChain`, `LastSeenMessagesValidator`, `MessageSignatureCache`): the chat session update and its profile key, checked against the services' `playerCertificateKeys`; signed chat, `say`, `me` and `msg`, relayed with their signatures and last-seen lists; `enforce-secure-profile` (with `online-mode`) refuses unsigned chat; and vanilla's log lines (`[Not Secure]`, the decoder's warnings, the kicks). Offline servers and players without a chat session chat unsigned, as on vanilla. `test_secure_chat_parity.py` runs the same signed and unsigned players against vanilla's server and Horizon's, with test keys and a mock services server. | | Nether and End | **Players can't change dimension.** The engine keeps one dimension loaded (`Game::enter_dimension` moves only the local single-player player), and the hub serves one `dimension` to everyone. | | Refused logins | Logged as vanilla does: `Disconnecting (/address): `, then ` (/address) lost connection: `, and the session errors (`Username '' tried to join with an invalid session`, `Couldn't verify username because servers are unavailable`). | | Gameplay for remote players | The LAN guest leftovers apply: an item used on a block is used twice, the recipe book doesn't work, status effects don't apply to guests, arrows don't record their owner, team friendly fire is ignored (#484). Block desync and short freezes are reported on a same-machine server (#500). | @@ -507,7 +508,6 @@ machine, then published in `benchmarks/`; and item 5. **Out (next):** -- secure chat on the server (online mode itself works); - the Nether and End for remote players; - the headless engine and its RAM win; - a server-only prepare without a JDK; diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java new file mode 100644 index 000000000..4d08dafd2 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java @@ -0,0 +1,625 @@ +package dev.horizonlab.parity; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.mojang.serialization.JsonOps; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.EOFException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.math.BigInteger; +import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.URI; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.KeyFactory; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Base64; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.zip.Deflater; +import java.util.zip.Inflater; +import javax.crypto.Cipher; +import javax.crypto.CipherInputStream; +import javax.crypto.CipherOutputStream; +import javax.crypto.SecretKey; +import net.minecraft.SharedConstants; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.core.UUIDUtil; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.ProtocolInfo; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.ComponentSerialization; +import net.minecraft.network.chat.LastSeenMessages; +import net.minecraft.network.chat.LastSeenMessagesTracker; +import net.minecraft.network.chat.LocalChatSession; +import net.minecraft.network.chat.MessageSignature; +import net.minecraft.network.chat.MessageSignatureCache; +import net.minecraft.network.chat.PlayerChatMessage; +import net.minecraft.network.chat.RemoteChatSession; +import net.minecraft.network.chat.SignedMessageBody; +import net.minecraft.network.chat.SignedMessageChain; +import net.minecraft.network.chat.SignedMessageLink; +import net.minecraft.network.chat.SignedMessageValidator; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.common.ClientboundDisconnectPacket; +import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ClientboundPingPacket; +import net.minecraft.network.protocol.common.ServerboundClientInformationPacket; +import net.minecraft.network.protocol.common.ServerboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ServerboundPongPacket; +import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ClientboundSelectKnownPacks; +import net.minecraft.network.protocol.configuration.ConfigurationProtocols; +import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ServerboundSelectKnownPacks; +import net.minecraft.network.protocol.game.ClientboundChunkBatchFinishedPacket; +import net.minecraft.network.protocol.game.ClientboundDisguisedChatPacket; +import net.minecraft.network.protocol.game.ClientboundLoginPacket; +import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket; +import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket; +import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket; +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; +import net.minecraft.network.protocol.game.GameProtocols; +import net.minecraft.network.protocol.game.ServerboundAcceptTeleportationPacket; +import net.minecraft.network.protocol.game.ServerboundChatAckPacket; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraft.network.protocol.game.ServerboundChatCommandSignedPacket; +import net.minecraft.network.protocol.game.ServerboundChatPacket; +import net.minecraft.network.protocol.game.ServerboundChatSessionUpdatePacket; +import net.minecraft.network.protocol.game.ServerboundChunkBatchReceivedPacket; +import net.minecraft.network.protocol.game.ServerboundPlayerLoadedPacket; +import net.minecraft.network.protocol.handshake.ClientIntent; +import net.minecraft.network.protocol.handshake.ClientIntentionPacket; +import net.minecraft.network.protocol.handshake.HandshakeProtocols; +import net.minecraft.network.protocol.login.ClientboundHelloPacket; +import net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket; +import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket; +import net.minecraft.network.protocol.login.ClientboundLoginFinishedPacket; +import net.minecraft.network.protocol.login.LoginProtocols; +import net.minecraft.network.protocol.login.ServerboundHelloPacket; +import net.minecraft.network.protocol.login.ServerboundKeyPacket; +import net.minecraft.network.protocol.login.ServerboundLoginAcknowledgedPacket; +import net.minecraft.server.level.ClientInformation; +import net.minecraft.util.Crypt; +import net.minecraft.util.Signer; +import net.minecraft.world.entity.player.ProfileKeyPair; +import net.minecraft.world.entity.player.ProfilePublicKey; + +/** + * Secure chat seen by vanilla-codec clients: several players on one server, + * signing their chat with profile keys that a mock of the Minecraft services + * signed (the harness serves its public key as the services' key set). + * + *

{@code keys

} writes the mock services' key pair: the private key + * (PKCS#8) and {@code publickeys.json} as the services answer it. With a + * host, a port, the private key, an output file and a scenario, it plays the + * scenario. Each client signs and acknowledges exactly as + * ClientPacketListener does (LocalChatSession, SignedMessageChain's encoder, + * LastSeenMessagesTracker, MessageSignatureCache), and checks every player + * chat it receives with SignedMessageValidator against the sender's session + * from the player list. The transcript holds what each player received and + * whether its signature checked out; keys, salts, times and signatures + * differ between runs and are left out. + */ +public final class SecureChatClient { + private static final List LINES = new ArrayList<>(); + private static PrivateKey services; + private static String sessionUrl; + + public static void main(String[] args) throws Exception { + ParityData.init(); + if (args[0].equals("keys")) { + keys(Path.of(args[1])); + return; + } + String host = args[0]; + int port = Integer.parseInt(args[1]); + services = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Files.readAllBytes(Path.of(args[2])))); + Path output = Path.of(args[3]); + String scenario = args[4]; + sessionUrl = args.length > 5 ? args[5] : null; + try { + if (scenario.equals("offline")) offline(host, port); + else online(host, port); + } catch (Exception e) { + StringBuilder line = new StringBuilder("error " + e); + for (Throwable c = e.getCause(); c != null; c = c.getCause()) line.append(" / caused by ").append(c); + LINES.add(line.toString()); + } + try (PrintWriter w = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) { + for (String line : LINES) w.println(line); + } + System.exit(0); + } + + private static void keys(Path dir) throws Exception { + KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); + generator.initialize(2048); + KeyPair pair = generator.generateKeyPair(); + Files.write(dir.resolve("services-private.der"), pair.getPrivate().getEncoded()); + JsonObject key = new JsonObject(); + key.addProperty("publicKey", Base64.getEncoder().encodeToString(pair.getPublic().getEncoded())); + JsonArray list = new JsonArray(); + list.add(key); + JsonObject set = new JsonObject(); + set.add("profilePropertyKeys", list); + set.add("playerCertificateKeys", list); + Files.writeString(dir.resolve("publickeys.json"), set.toString()); + } + + // ---- scenarios ---------------------------------------------------- + + /** Offline mode: the server checks chat sessions but enforces nothing. */ + private static void offline(String host, int port) throws Exception { + Client alice = Client.join(host, port, "Alice", true, null); + Client bob = Client.join(host, port, "Bob", true, null); + Client carol = Client.join(host, port, "Carol", false, null); + List all = List.of(alice, bob, carol); + settle(all, "joined"); + alice.chat("hello, signed"); + settle(all, "alice chats"); + bob.chat("seen it"); + settle(all, "bob chats with alice's message seen"); + carol.chat("no key here"); + settle(all, "carol chats unsigned"); + alice.signedCommand("say signed announcement", "message", "signed announcement"); + settle(all, "alice says"); + alice.signedCommand("me waves", "action", "waves"); + settle(all, "alice emotes"); + alice.signedCommand("msg Bob just between us", "message", "just between us"); + settle(all, "alice whispers to bob"); + carol.signedCommand("me shrugs", null, null); + settle(all, "carol emotes, unsigned arguments"); + carol.command("say plain announcement"); + settle(all, "carol says through the unsigned packet"); + bob.signedCommand("say mismatch", "nothing", "mismatch"); + settle(all, "bob signs the wrong argument"); + bob.chat("after the mismatch"); + settle(all, "bob's chain after the mismatch"); + alice.tamperedChat("forged"); + settle(all, "alice's signature fails"); + alice.chat("after the forgery"); + settle(all, "alice's chain is broken"); + bob.send(new ServerboundChatAckPacket(50)); + settle(all, "bob acknowledges too much"); + for (Client c : all) c.close(); + } + + /** Online mode with enforce-secure-profile: unsigned chat is refused. */ + private static void online(String host, int port) throws Exception { + Client alice = Client.join(host, port, "Alice", true, null); + Client carol = Client.join(host, port, "Carol", false, null); + List all = List.of(alice, carol); + settle(all, "joined"); + alice.chat("signed and enforced"); + settle(all, "alice chats"); + carol.chat("unsigned under enforcement"); + settle(all, "carol chats without a key"); + carol.command("me tries the unsigned packet"); + settle(all, "carol's unsigned command"); + carol.signedCommand("me tries without signatures", null, null); + settle(all, "carol's command without signatures"); + // A key the services signed for another player. + Client mallory = Client.join(host, port, "Mallory", true, UUID.nameUUIDFromBytes("someone else".getBytes())); + settle(List.of(alice, carol, mallory), "mallory's borrowed key"); + for (Client c : all) c.close(); + mallory.close(); + } + + /** Lets the server answer, then records what each player received, in player order. */ + private static void settle(List clients, String step) throws Exception { + Thread.sleep(1500); + LINES.add("step " + step); + for (Client c : clients) c.drain(); + } + + private static String json(Component c) { + return ComponentSerialization.CODEC.encodeStart(ParityData.access.createSerializationContext(JsonOps.INSTANCE), c).getOrThrow().toString(); + } + + /** A player: one connection, its chat session and what its client tracks. */ + private static final class Client { + final String name; + UUID id; + Socket socket; + DataInputStream in; + OutputStream out; + int threshold = -1; + ProtocolInfo inbound; + ProtocolInfo outbound; + final BlockingQueue> queue = new LinkedBlockingQueue<>(); + volatile boolean closed; + LocalChatSession session; + SignedMessageChain.Encoder encoder = SignedMessageChain.Encoder.UNSIGNED; + final LastSeenMessagesTracker lastSeen = new LastSeenMessagesTracker(20); + final MessageSignatureCache cache = MessageSignatureCache.createDefault(); + int nextChatIndex; + final Map names = new LinkedHashMap<>(); + final Map validators = new HashMap<>(); + boolean enforced; + boolean loaded; + net.minecraft.world.entity.PositionMoveRotation position = new net.minecraft.world.entity.PositionMoveRotation( + net.minecraft.world.phys.Vec3.ZERO, net.minecraft.world.phys.Vec3.ZERO, 0, 0); + + Client(String name) { + this.name = name; + } + + void record(String line) { + LINES.add(name + " " + line); + } + + static Client join(String host, int port, String name, boolean signs, UUID keyOwner) throws Exception { + Client c = new Client(name); + c.connect(host, port, signs, keyOwner); + return c; + } + + void connect(String host, int port, boolean signs, UUID keyOwner) throws Exception { + socket = new Socket(); + socket.connect(new InetSocketAddress(host, port), 10_000); + socket.setSoTimeout(30_000); + socket.setTcpNoDelay(true); + in = new DataInputStream(socket.getInputStream()); + out = socket.getOutputStream(); + outbound = HandshakeProtocols.SERVERBOUND; + send(new ClientIntentionPacket(SharedConstants.getProtocolVersion(), host, port, ClientIntent.LOGIN)); + outbound = LoginProtocols.SERVERBOUND; + inbound = LoginProtocols.CLIENTBOUND; + send(new ServerboundHelloPacket(name, UUIDUtil.createOfflinePlayerUUID(name))); + while (true) { + Packet p = read(); + if (p instanceof ClientboundHelloPacket hello) { + // ClientHandshakePacketListenerImpl.handleHello: join through the session service. + SecretKey secret = Crypt.generateSecretKey(); + String hash = new BigInteger(Crypt.digestData(hello.getServerId(), hello.getPublicKey(), secret)).toString(16); + joinSession(hash); + send(new ServerboundKeyPacket(secret, hello.getPublicKey(), hello.getChallenge())); + in = new DataInputStream(new CipherInputStream(socket.getInputStream(), Crypt.getCipher(Cipher.DECRYPT_MODE, secret))); + out = new CipherOutputStream(socket.getOutputStream(), Crypt.getCipher(Cipher.ENCRYPT_MODE, secret)); + } else if (p instanceof ClientboundLoginCompressionPacket compression) { + threshold = compression.getCompressionThreshold(); + } else if (p instanceof ClientboundLoginFinishedPacket finished) { + id = finished.gameProfile().id(); + record("logged in"); + send(ServerboundLoginAcknowledgedPacket.INSTANCE); + break; + } else if (p instanceof ClientboundLoginDisconnectPacket d) { + record("refused " + json(d.reason())); + closed = true; + return; + } + } + outbound = ConfigurationProtocols.SERVERBOUND; + inbound = ConfigurationProtocols.CLIENTBOUND; + send(new ServerboundClientInformationPacket(ClientInformation.createDefault())); + while (true) { + Packet p = read(); + if (p instanceof ClientboundSelectKnownPacks packs) { + send(new ServerboundSelectKnownPacks(packs.knownPacks())); + } else if (p instanceof ClientboundKeepAlivePacket k) { + send(new ServerboundKeepAlivePacket(k.getId())); + } else if (p instanceof ClientboundPingPacket ping) { + send(new ServerboundPongPacket(ping.getId())); + } else if (p instanceof ClientboundFinishConfigurationPacket) { + send(ServerboundFinishConfigurationPacket.INSTANCE); + break; + } else if (p instanceof ClientboundDisconnectPacket d) { + record("disconnected " + json(d.reason())); + closed = true; + return; + } + } + GameProtocols.Context context = new GameProtocols.Context() { + @Override + public boolean hasInfiniteMaterials() { + return false; + } + + @Override + public boolean canUseCommandBlocks() { + return false; + } + }; + outbound = GameProtocols.SERVERBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access), context); + inbound = GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access)); + // ClientPacketListener.handleLogin: the session goes out once the + // login packet is in (prepareKeyPair, setKeyPair). + while (true) { + Packet p = read(); + if (p instanceof ClientboundLoginPacket login) { + enforced = login.enforcesSecureChat(); + record("login online " + login.onlineMode() + " enforces " + login.enforcesSecureChat()); + if (signs) startSession(keyOwner == null ? id : keyOwner); + break; + } + } + Thread reader = new Thread(() -> { + try { + while (!closed) { + Packet p = read(); + queue.put(p); + // Nothing follows a disconnect but the closed socket. + if (p instanceof ClientboundDisconnectPacket) return; + } + } catch (Exception e) { + if (!closed) queue.add(new ClientboundDisconnectPacket(Component.literal("connection lost: " + e))); + } + }, name + " reader"); + reader.setDaemon(true); + reader.start(); + } + + /** The session service's join, as the launcher's account would. */ + void joinSession(String serverHash) throws Exception { + HttpURLConnection http = (HttpURLConnection) URI.create(sessionUrl + "/session/minecraft/join").toURL().openConnection(); + http.setRequestMethod("POST"); + http.setDoOutput(true); + http.setRequestProperty("Content-Type", "application/json"); + JsonObject body = new JsonObject(); + body.addProperty("accessToken", "mock-token"); + body.addProperty("selectedProfile", name); + body.addProperty("serverId", serverHash); + http.getOutputStream().write(body.toString().getBytes(StandardCharsets.UTF_8)); + http.getResponseCode(); + http.disconnect(); + } + + /** A profile key pair the mock services signed for {@code owner}, and its session. */ + void startSession(UUID owner) throws Exception { + KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); + generator.initialize(2048); + KeyPair pair = generator.generateKeyPair(); + Instant expires = Instant.now().plusSeconds(3600).truncatedTo(java.time.temporal.ChronoUnit.MILLIS); + byte[] key = pair.getPublic().getEncoded(); + byte[] payload = ByteBuffer.allocate(24 + key.length).putLong(owner.getMostSignificantBits()) + .putLong(owner.getLeastSignificantBits()).putLong(expires.toEpochMilli()).put(key).array(); + byte[] signature = Signer.from(services, "SHA1withRSA").sign(payload); + ProfilePublicKey publicKey = new ProfilePublicKey(new ProfilePublicKey.Data(expires, pair.getPublic(), signature)); + session = LocalChatSession.create(new ProfileKeyPair(pair.getPrivate(), publicKey, expires)); + encoder = session.createMessageEncoder(id); + send(new ServerboundChatSessionUpdatePacket(session.asRemote().asData())); + } + + /** ClientPacketListener.sendChat. */ + void chat(String content) throws Exception { + Instant now = Instant.now(); + long salt = Crypt.SaltSupplier.getLong(); + LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate(); + MessageSignature signature = encoder.pack(new SignedMessageBody(content, now, salt, update.lastSeen())); + send(new ServerboundChatPacket(content, now, salt, Optional.ofNullable(signature), update.update())); + } + + /** A chat whose signature doesn't match its text. */ + void tamperedChat(String content) throws Exception { + Instant now = Instant.now(); + long salt = Crypt.SaltSupplier.getLong(); + LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate(); + MessageSignature signature = encoder.pack(new SignedMessageBody(content + "!", now, salt, update.lastSeen())); + send(new ServerboundChatPacket(content, now, salt, Optional.ofNullable(signature), update.update())); + } + + /** ClientPacketListener.sendCommand for a command with one message argument (none: no signatures). */ + void signedCommand(String command, String argument, String value) throws Exception { + Instant now = Instant.now(); + long salt = Crypt.SaltSupplier.getLong(); + LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate(); + List entries = new ArrayList<>(); + if (argument != null) { + MessageSignature signature = encoder.pack(new SignedMessageBody(value, now, salt, update.lastSeen())); + if (signature != null) entries.add(new ArgumentSignatures.Entry(argument, signature)); + } + send(new ServerboundChatCommandSignedPacket(command, now, salt, new ArgumentSignatures(entries), update.update())); + } + + void command(String command) throws Exception { + send(new ServerboundChatCommandPacket(command)); + } + + /** Handles and records what arrived. */ + void drain() throws Exception { + Packet p; + while ((p = queue.poll(50, TimeUnit.MILLISECONDS)) != null) handle(p); + } + + void handle(Packet packet) throws Exception { + if (packet instanceof ClientboundKeepAlivePacket k) { + send(new ServerboundKeepAlivePacket(k.getId())); + } else if (packet instanceof ClientboundPingPacket ping) { + send(new ServerboundPongPacket(ping.getId())); + } else if (packet instanceof ClientboundPlayerPositionPacket pos) { + position = net.minecraft.world.entity.PositionMoveRotation.calculateAbsolute(position, pos.change(), pos.relatives()); + send(new ServerboundAcceptTeleportationPacket(pos.id(), position.position().x, position.position().y, + position.position().z, position.yRot(), position.xRot())); + if (!loaded) { + send(new ServerboundPlayerLoadedPacket()); + loaded = true; + } + } else if (packet instanceof ClientboundChunkBatchFinishedPacket) { + send(new ServerboundChunkBatchReceivedPacket(64.0F)); + } else if (packet instanceof ClientboundPlayerInfoUpdatePacket info) { + for (ClientboundPlayerInfoUpdatePacket.Entry e : info.entries()) { + if (info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) { + names.put(e.profileId(), e.profile().name()); + } + if (info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.INITIALIZE_CHAT)) { + RemoteChatSession.Data data = e.chatSession(); + String who = names.getOrDefault(e.profileId(), "?"); + if (data != null) { + RemoteChatSession remote = new RemoteChatSession(data.sessionId(), new ProfilePublicKey(data.profilePublicKey())); + validators.put(e.profileId(), remote.createMessageValidator(ProfilePublicKey.EXPIRY_GRACE_PERIOD)); + sessions.put(e.profileId(), data.sessionId()); + record("session of " + who); + } else if (!info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) { + record("session cleared for " + who); + } + } + } + } else if (packet instanceof ClientboundPlayerChatPacket chat) { + playerChat(chat); + } else if (packet instanceof ClientboundDisguisedChatPacket d) { + record("disguised " + d.chatType().chatType().getRegisteredName() + " " + json(d.chatType().name()) + " " + + d.chatType().targetName().map(SecureChatClient::json) + " " + json(d.message())); + } else if (packet instanceof ClientboundSystemChatPacket s) { + record("system " + json(s.content())); + } else if (packet instanceof ClientboundDisconnectPacket d) { + record("disconnected " + json(d.reason())); + closed = true; + } + } + + /** ClientPacketListener.handlePlayerChat, then markMessageAsProcessed. */ + void playerChat(ClientboundPlayerChatPacket p) { + int expected = nextChatIndex++; + String sender = names.getOrDefault(p.sender(), p.sender().toString()); + StringBuilder lastSeenPacked = new StringBuilder(); + for (MessageSignature.Packed packed : p.body().lastSeen().entries()) { + lastSeenPacked.append(packed.fullSignature() != null ? "full" : "cached:" + packed.id()).append(','); + } + Optional body = p.body().unpack(cache); + if (p.globalIndex() != expected || body.isEmpty()) { + record("chat out of order or unknown signature: index " + p.globalIndex() + " expected " + expected); + return; + } + MessageSignature signature = p.signature().orElse(null); + cache.push(body.get(), signature); + SignedMessageValidator validator = validators.get(p.sender()); + String verdict; + if (validator != null) { + PlayerChatMessage message = new PlayerChatMessage( + new SignedMessageLink(p.index(), p.sender(), sessionId(p.sender())), signature, body.get(), + p.unsignedContent().orElse(null), p.filterMask()); + verdict = validator.updateAndValidate(message) != null ? "verified" : "rejected"; + } else { + verdict = signature == null ? (enforced ? "unsigned-rejected" : "unsigned") : "no-session"; + } + record("chat #" + p.globalIndex() + " from " + sender + " link " + p.index() + " " + verdict + " '" + body.get().content() + + "' seen [" + lastSeenPacked + "] " + body.get().lastSeen().entries().size() + " unsigned " + + p.unsignedContent().map(SecureChatClient::json) + " " + p.chatType().chatType().getRegisteredName() + " " + + json(p.chatType().name()) + " " + p.chatType().targetName().map(SecureChatClient::json)); + if (signature != null) lastSeen.addPending(signature, true); + } + + private final Map sessions = new HashMap<>(); + + UUID sessionId(UUID sender) { + return sessions.getOrDefault(sender, new UUID(0, 0)); + } + + // ---- framing ------------------------------------------------------ + + synchronized void send(Packet packet) throws Exception { + ByteBuf body = Unpooled.buffer(); + encode(body, packet); + byte[] raw = new byte[body.readableBytes()]; + body.readBytes(raw); + ByteBuf frame = Unpooled.buffer(); + if (threshold >= 0) { + ByteBuf inner = Unpooled.buffer(); + if (raw.length >= threshold) { + writeVarInt(inner, raw.length); + Deflater d = new Deflater(); + d.setInput(raw); + d.finish(); + byte[] tmp = new byte[raw.length + 64]; + while (!d.finished()) inner.writeBytes(tmp, 0, d.deflate(tmp)); + d.end(); + } else { + writeVarInt(inner, 0); + inner.writeBytes(raw); + } + writeVarInt(frame, inner.readableBytes()); + frame.writeBytes(inner); + } else { + writeVarInt(frame, raw.length); + frame.writeBytes(raw); + } + byte[] bytes = new byte[frame.readableBytes()]; + frame.readBytes(bytes); + // A server that disconnected the player may close before this + // write: what it sent last is the disconnect, which the reader + // records, not the broken pipe. + if (broken) return; + try { + out.write(bytes); + out.flush(); + } catch (java.io.IOException e) { + broken = true; + } + } + + private boolean broken; + + @SuppressWarnings({"unchecked", "rawtypes"}) + private void encode(ByteBuf body, Packet packet) { + ((ProtocolInfo) outbound).codec().encode(body, packet); + } + + Packet read() throws Exception { + int length = readVarInt(in); + byte[] frame = new byte[length]; + in.readFully(frame); + byte[] raw = frame; + if (threshold >= 0) { + DataInputStream f = new DataInputStream(new ByteArrayInputStream(frame)); + int dataLength = readVarInt(f); + byte[] rest = f.readAllBytes(); + if (dataLength == 0) { + raw = rest; + } else { + Inflater inflater = new Inflater(); + inflater.setInput(rest); + raw = new byte[dataLength]; + inflater.inflate(raw); + inflater.end(); + } + } + return inbound.codec().decode(Unpooled.wrappedBuffer(raw)); + } + + void close() throws Exception { + closed = true; + socket.close(); + } + } + + private static int readVarInt(InputStream in) throws Exception { + int value = 0; + for (int i = 0; i < 5; i++) { + int b = in.read(); + if (b < 0) throw new EOFException(); + value |= (b & 0x7F) << (7 * i); + if ((b & 0x80) == 0) return value; + } + throw new IllegalStateException("VarInt too big"); + } + + private static void writeVarInt(ByteBuf buf, int value) { + while ((value & ~0x7F) != 0) { + buf.writeByte((value & 0x7F) | 0x80); + value >>>= 7; + } + buf.writeByte(value); + } +} diff --git a/tools/test_secure_chat_parity.py b/tools/test_secure_chat_parity.py new file mode 100644 index 000000000..13bf494dd --- /dev/null +++ b/tools/test_secure_chat_parity.py @@ -0,0 +1,317 @@ +#!/usr/bin/env python3 +"""Secure chat on vanilla's dedicated server and Horizon's, with no real account. + +A mock of the Minecraft services stands in for Mojang's: its key set (the +`authentication` / `getPublicKeys` endpoint) holds a key pair the harness +makes, so the profile keys it signs are as good as Mojang's to both servers. +Vanilla finds the mock through authlib's `-Dminecraft.api.discovery.host`, +Horizon through `HORIZON_API_DISCOVERY`. SecureChatClient (vanilla's own +codecs and chat classes) plays several players at once: each signs its chat +with a profile key the mock signed, acknowledges what it saw, and checks the +signature of every chat it receives against the sender's session from the +player list, as vanilla's client does. + +Two servers, each run on vanilla then on Horizon: +- offline mode (enforce-secure-profile is then off): signed chat with the + last-seen window and the signature cache, unsigned chat beside it, /say, + /me and /msg with signed and unsigned arguments, a signed argument under + the wrong name, a forged signature and the broken chain after it, and an + acknowledgement past the window; +- online mode with enforce-secure-profile: the status and the login packet + say so, unsigned chat and unsigned commands with a message are refused, and + a key signed for another player is refused. + +Compared: every player's transcript, the status' `enforcesSecureChat`, and +the servers' log lines about chat. +""" +import hashlib +import json +import os +from pathlib import Path +import platform +import re +import shutil +import socket +import subprocess +import sys +import time +import threading +import urllib.parse +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +try: + from . import lab, oracle_harness as oh, test_rust_parity as base + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import lab + import oracle_harness as oh + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'secure-chat-parity' +SCENARIOS = { + 'offline': {'online-mode': 'false'}, + 'online': {'online-mode': 'true', 'enforce-secure-profile': 'true'}, +} +PROPERTIES = { + 'white-list': 'false', + 'level-seed': '12345', + 'view-distance': '3', + 'simulation-distance': '3', + 'spawn-protection': '0', + 'max-players': '8', + 'pause-when-empty-seconds': '-1', +} + + +def account(name): + """The mock services' account id for a name.""" + return hashlib.md5(f'account:{name}'.encode()).hexdigest() + + +class Services: + """Discovery, the key set, the session service's join and hasJoined, profiles by name.""" + + def __init__(self, key_set): + self.joined = {} + self.lock = threading.Lock() + services = self + + class Handler(BaseHTTPRequestHandler): + def log_message(self, *args): + pass + + def reply(self, status, body=None): + data = b'' if body is None else json.dumps(body).encode() + self.send_response(status) + if body is not None: + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(data))) + self.end_headers() + self.wfile.write(data) + + def do_GET(self): + url = urllib.parse.urlparse(self.path) + query = dict(urllib.parse.parse_qsl(url.query)) + if url.path == '/minecraft/client': + return self.reply(200, services.discovery()) + if url.path == '/publickeys': + return self.reply(200, key_set) + if url.path.startswith('/users/profiles/minecraft/'): + name = urllib.parse.unquote(url.path.rsplit('/', 1)[1]) + return self.reply(200, {'id': account(name), 'name': name}) + if url.path == '/session/minecraft/hasJoined': + name = query.get('username') + with services.lock: + ok = services.joined.get(name) == query.get('serverId') + if ok: + return self.reply(200, {'id': account(name), 'name': name, 'properties': []}) + return self.reply(204) + self.reply(404, {'error': 'NotFound'}) + + def do_POST(self): + body = json.loads(self.rfile.read(int(self.headers.get('Content-Length', 0))) or b'{}') + if self.path != '/session/minecraft/join': + return self.reply(404, {'error': 'NotFound'}) + # The test client names its account instead of its id. + with services.lock: + services.joined[body.get('selectedProfile')] = body.get('serverId') + self.reply(204) + + self.server = ThreadingHTTPServer(('127.0.0.1', 0), Handler) + self.url = f'http://127.0.0.1:{self.server.server_address[1]}' + threading.Thread(target=self.server.serve_forever, daemon=True).start() + + def discovery(self): + def endpoints(**names): + return {'endpoints': {k: {'uri': self.url + v} for k, v in names.items()}} + return {'environment': 'mock', 'product': 'minecraft', 'discovery': { + 'authentication': endpoints(getPublicKeys='/publickeys'), + 'session': endpoints(join='/session/minecraft/join', verify='/session/minecraft/hasJoined', + getProfileById='/session/minecraft/profile/{profileId}'), + 'player': {'endpoints': {}}, + 'profiles': {'endpoints': {'getTexture': {'validUris': [self.url + '/texture/{textureId}']}, + 'getByName': {'uri': self.url + '/users/profiles/minecraft/{name}'}}}, + 'telemetry': {'endpoints': {}}, + }} + + def stop(self): + self.server.shutdown() + + +def player_id(name, scenario): + """The player's id: the account's online, UUIDUtil.createOfflinePlayerUUID offline.""" + if scenario == 'online': + return account(name) + digest = bytearray(hashlib.md5(f'OfflinePlayer:{name}'.encode()).digest()) + digest[6] = digest[6] & 0x0F | 0x30 + digest[8] = digest[8] & 0x3F | 0x80 + return digest.hex() + + +def dashed(u): + return f'{u[:8]}-{u[8:12]}-{u[12:16]}-{u[16:20]}-{u[20:]}' + + +def varint(n): + out = b'' + while True: + b = n & 0x7F + n >>= 7 + out += bytes([b | (0x80 if n else 0)]) + if not n: + return out + + +def status(port): + """The status response's JSON (ServerStatus).""" + def packet(body): + return varint(len(body)) + body + + def read_varint(s): + value = 0 + for i in range(5): + b = s.recv(1)[0] + value |= (b & 0x7F) << (7 * i) + if not b & 0x80: + return value + with socket.create_connection(('127.0.0.1', port), timeout=10) as s: + host = b'127.0.0.1' + s.sendall(packet(b'\x00' + varint(777) + varint(len(host)) + host + port.to_bytes(2, 'big') + b'\x01')) + s.sendall(packet(b'\x00')) + length = read_varint(s) + data = b'' + while len(data) < length: + data += s.recv(length - len(data)) + text = data[1:] + n, shift, i = 0, 0, 0 + while True: + b = text[i] + n |= (b & 0x7F) << shift + shift += 7 + i += 1 + if not b & 0x80: + break + return json.loads(text[i:i + n]) + + +# The servers' lines about chat, with their level. The thread is left out: +# vanilla handles acknowledgements on its network threads, named per platform. +CHAT_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*(?:<\w+>|\[Not Secure\]|\[\w+\] |\* \w+ |secure chat|' + r'signable|Signed command|acknowledgement|profile key|chat session|expired chat|lost connection|' + r'Disconnecting).*)$') + + +def chat_lines(lines): + out = [] + for line in lines: + if m := CHAT_LINE.match(line): + text = re.sub(r'/127\.0\.0\.1:\d+', '/127.0.0.1:_', m.group(2)) + out.append(f'[{m.group(1).rsplit("/", 1)[-1]}] {text}') + return out + + +def run_side(label, scenario, command, directory, client): + port = free_port() + shutil.rmtree(directory, ignore_errors=True) + directory.mkdir(parents=True) + props = dict(PROPERTIES, **SCENARIOS[scenario], **{'server-port': str(port)}) + (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items())) + (directory / 'eula.txt').write_text('eula=true\n') + # Alice and Carol are operators (/say needs it), by their ids. + ops = [{'uuid': dashed(player_id(name, scenario)), 'name': name, 'level': 4, 'bypassesPlayerLimit': False} + for name in ('Alice', 'Carol')] + (directory / 'ops.json').write_text(json.dumps(ops, indent=2)) + server = Server(command, directory, f'{label}-{scenario}') + try: + server.wait_for(r'Done \(', timeout=600) + # A server can drop a ping in its first moments (the network thread still starting): try again. + for attempt in range(5): + try: + enforces = status(port).get('enforcesSecureChat', False) + break + except (OSError, IndexError): + if attempt == 4: + raise + time.sleep(2) + out = directory.parent / f'{label}-{scenario}.txt' + client(port, scenario, out) + transcript = [f'status enforcesSecureChat {enforces}'] + out.read_text(encoding='utf-8').splitlines() + finally: + server.stop() + return transcript, chat_lines(server.lines) + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java' + oh.run_logged(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + build, 'compile-java') + oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build, + 'compile-rust') + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + java = ['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies, + 'dev.horizonlab.parity.SecureChatClient'] + oh.run_logged([*java, 'keys', build], build, 'keys') + services = Services(json.loads((build / 'publickeys.json').read_text())) + discovery = services.url + '/minecraft/client' + os.environ.update(HORIZON_NO_AUDIO='1', HORIZON_API_DISCOVERY=discovery) + servers = { + 'vanilla': ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}', + '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'], + 'horizon': [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'], + } + + def client(port, scenario, out): + oh.run_logged([*java, '127.0.0.1', port, build / 'services-private.der', out, scenario, services.url], + build, out.stem, timeout=300) + + differences = [] + try: + for scenario in SCENARIOS: + results = {} + for label, command in servers.items(): + results[label] = run_side(label, scenario, command, build / f'{label}-{scenario}', client) + transcript, log = results[label] + (build / f'{label}-{scenario}-all.txt').write_text('\n'.join(transcript + ['--- log'] + log) + '\n', + encoding='utf-8') + import difflib + for part, index in (('transcript', 0), ('log', 1)): + diff = [l for l in difflib.unified_diff(results['vanilla'][index], results['horizon'][index], + 'vanilla', 'horizon', n=0, lineterm='') + if l[:1] in '+-' and l[:3] not in ('+++', '---')] + if diff: + differences.append(f'{scenario} {part}:\n ' + '\n '.join(diff[:60])) + errors = [l for l in results['vanilla'][0] if l.startswith('error')] + if errors: + differences.append(f'{scenario}: the vanilla run failed: {errors}') + finally: + services.stop() + if differences: + raise AssertionError('\n'.join(differences) + f'\n(transcripts in {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + scope='Secure chat against vanilla\'s dedicated server: chat sessions validated against mock services keys, ' + 'signed chat with last seen and the signature cache, unsigned chat, /say /me /msg with signed and ' + 'unsigned arguments, mismatched and forged signatures, the broken chain, a bad acknowledgement; ' + 'enforce-secure-profile in online mode (status, login, refusals, a borrowed key); the chat log lines.')) + print('secure chat: transcripts and chat log lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') From 8f3d35ab6df5a000bd0be437d8b060eae8ff4960 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 07:57:10 +0200 Subject: [PATCH 61/67] Block entity animations: read the drawn block entities only when they change tick_block_entity_anims rebuilt a map of every drawn block entity, sorted it and swept the animation states against it every tick. The renderer now keeps a revision that changes when its block entities do, and the sorted ticker list is rebuilt only then. The animation states use FxHashMap. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/block_entity_anim.rs | 55 +++++++++++-------- .../src/game/block_entity_anims.rs | 25 ++++++--- crates/horizon-client/src/gfx.rs | 28 ++++++++-- 3 files changed, 73 insertions(+), 35 deletions(-) diff --git a/crates/horizon-client/src/block_entity_anim.rs b/crates/horizon-client/src/block_entity_anim.rs index eefd1ba63..2a35c9835 100644 --- a/crates/horizon-client/src/block_entity_anim.rs +++ b/crates/horizon-client/src/block_entity_anim.rs @@ -12,11 +12,11 @@ //! (blocks, entities, game time) as closures, so the parity fixture can drive //! them with scripted inputs (`tools/test_block_entity_anim_parity.py`). use crate::block_entity_render::BlockAnim; +use crate::fast_hash::FxHashMap; use horizon_core::{ geometry::trig::{self, Mth}, random::{LegacyRandom, RandomSource}, }; -use std::collections::HashMap; /// `Direction` names by `get3DDataValue`. pub const DIRECTIONS: [&str; 6] = ["down", "up", "north", "south", "west", "east"]; @@ -832,38 +832,44 @@ pub fn ticker(name: &str, prop: impl Fn(&str) -> Option) -> Option, - pub books: HashMap<[i32; 3], EnchantingBook>, + pub chests: FxHashMap<[i32; 3], ChestLid>, + pub books: FxHashMap<[i32; 3], EnchantingBook>, /// `EnchantingTableBlockEntity.RANDOM`, shared by every table. pub book_random: LegacyRandom, - pub shulkers: HashMap<[i32; 3], Shulker>, - pub bells: HashMap<[i32; 3], Bell>, - pub conduits: HashMap<[i32; 3], Conduit>, - pub skulls: HashMap<[i32; 3], Skull>, - pub pots: HashMap<[i32; 3], PotWobble>, - pub gateways: HashMap<[i32; 3], EndGateway>, + pub shulkers: FxHashMap<[i32; 3], Shulker>, + pub bells: FxHashMap<[i32; 3], Bell>, + pub conduits: FxHashMap<[i32; 3], Conduit>, + pub skulls: FxHashMap<[i32; 3], Skull>, + pub pots: FxHashMap<[i32; 3], PotWobble>, + pub gateways: FxHashMap<[i32; 3], EndGateway>, /// The integrated server's copies of the gateways (`portalTick`). - pub gateway_servers: HashMap<[i32; 3], EndGateway>, + pub gateway_servers: FxHashMap<[i32; 3], EndGateway>, /// The client level's random (conduit particles). pub random: LegacyRandom, /// [`Ticker`] by block state id (see [`BlockEntityAnims::classify`]). pub kinds: Vec>, + /// The drawn block entities with a ticker, sorted by position, and the + /// renderer's block entity revision they were read at. + pub present: Vec<([i32; 3], Ticker)>, + pub present_revision: Option, } impl Default for BlockEntityAnims { fn default() -> Self { Self { - chests: HashMap::new(), - books: HashMap::new(), + chests: FxHashMap::default(), + books: FxHashMap::default(), book_random: LegacyRandom::new(0x626f6f6b), - shulkers: HashMap::new(), - bells: HashMap::new(), - conduits: HashMap::new(), - skulls: HashMap::new(), - pots: HashMap::new(), - gateways: HashMap::new(), - gateway_servers: HashMap::new(), + shulkers: FxHashMap::default(), + bells: FxHashMap::default(), + conduits: FxHashMap::default(), + skulls: FxHashMap::default(), + pots: FxHashMap::default(), + gateways: FxHashMap::default(), + gateway_servers: FxHashMap::default(), random: LegacyRandom::new(0x6c6576656c), kinds: Vec::new(), + present: Vec::new(), + present_revision: None, } } } @@ -877,15 +883,20 @@ impl BlockEntityAnims { .and_then(|s| ticker(&s.name, |k| s.properties.get(k).cloned())) }) .collect(); + self.present_revision = None; } /// The ticker of a block state. pub fn ticker(&self, state: u32) -> Option { self.kinds.get(state as usize).copied().flatten() } /// Drops the state of block entities that are gone or changed kind: - /// `present` maps the loaded block entities to their tickers. - pub fn retain_present(&mut self, present: &HashMap<[i32; 3], Ticker>) { - let is = |p: &[i32; 3], f: fn(&Ticker) -> bool| present.get(p).is_some_and(f); + /// `present` holds the loaded block entities' tickers, sorted by position. + pub fn retain_present(&mut self, present: &[([i32; 3], Ticker)]) { + let is = |p: &[i32; 3], f: fn(&Ticker) -> bool| { + present + .binary_search_by_key(p, |(q, _)| *q) + .is_ok_and(|i| f(&present[i].1)) + }; self.chests.retain(|p, _| is(p, |t| *t == Ticker::Lid)); self.books.retain(|p, _| is(p, |t| *t == Ticker::Book)); self.shulkers diff --git a/crates/horizon-client/src/game/block_entity_anims.rs b/crates/horizon-client/src/game/block_entity_anims.rs index d377aeb45..102b46063 100644 --- a/crates/horizon-client/src/game/block_entity_anims.rs +++ b/crates/horizon-client/src/game/block_entity_anims.rs @@ -13,7 +13,6 @@ use horizon_client::{ }; use horizon_core::geometry::{Aabb, Vec3}; use horizon_world::nbt::Tag; -use std::collections::HashMap; /// The handle of the local player among a bell's gathered entities. const PLAYER: u64 = u64::MAX; @@ -174,11 +173,20 @@ impl Game<'_> { let Some(r) = &self.renderer else { return; }; - let present: HashMap<[i32; 3], Ticker> = r - .block_entities() - .filter_map(|(p, s)| Some((*p, self.be_anims.ticker(*s)?))) - .collect(); - self.be_anims.retain_present(&present); + // Sorted by position: the order they tick in, and a quick lookup. + // Read again only when the drawn block entities changed. + let revision = r.block_entity_revision(); + if self.be_anims.present_revision != Some(revision) { + let mut present: Vec<([i32; 3], Ticker)> = r + .block_entities() + .filter_map(|(p, s)| Some((*p, self.be_anims.ticker(*s)?))) + .collect(); + present.sort_unstable_by_key(|(p, _)| *p); + self.be_anims.retain_present(&present); + self.be_anims.present = present; + self.be_anims.present_revision = Some(revision); + } + let positions = std::mem::take(&mut self.be_anims.present); let players: Vec<[f64; 3]> = match &self.player { Some(p) if !self.spectator() => vec![[p.position.x, p.position.y, p.position.z]], _ => Vec::new(), @@ -198,9 +206,7 @@ impl Game<'_> { .get("minecraft:conduit_effect_block") .cloned() .unwrap_or_default(); - let mut positions: Vec<([i32; 3], Ticker)> = present.into_iter().collect(); - positions.sort_unstable_by_key(|(p, _)| *p); - for (pos, ticker) in positions { + for &(pos, ticker) in &positions { match ticker { Ticker::Lid => self.be_anims.chests.entry(pos).or_default().tick(), Ticker::Book => { @@ -322,6 +328,7 @@ impl Game<'_> { } } } + self.be_anims.present = positions; } /// `EndGatewayBlock.entityInside` on the server: whether the gateway at /// `pos` let the entity in (`triggerCooldown`, with its block event). diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index be842c3c6..0ee935d75 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -69,6 +69,12 @@ pub mod translucency; pub(crate) mod upload; mod world_post; +/// A new value for `Renderer::block_entity_revision`. +fn next_block_entity_revision() -> u64 { + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) +} + #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct Globals { @@ -348,6 +354,8 @@ pub struct Renderer { draw_pages: Vec<(u32, Option)>, /// Block-entity blocks per meshed section (world position, state id). block_entities: HashMap>, + /// Changes whenever `block_entities` does (unique across renderers). + block_entity_revision: u64, /// VisGraph face connectivity per meshed section (cave culling). occlusion: occlusion::Occlusion, /// Resolution scale and FXAA (Horizon settings). @@ -1821,6 +1829,7 @@ impl Renderer { draws: Vec::new(), draw_pages: Vec::new(), block_entities: HashMap::new(), + block_entity_revision: next_block_entity_revision(), occlusion: occlusion::Occlusion::new(crate::chunk::HEIGHT / 16), world_post, pack: None, @@ -1987,10 +1996,14 @@ impl Renderer { } pub fn upload(&mut self, key: SectionKey, mesh: &SectionMesh) { self.occlusion.set(key, mesh.visibility); - if mesh.block_entities.is_empty() { - self.block_entities.remove(&key); + let changed = if mesh.block_entities.is_empty() { + self.block_entities.remove(&key).is_some() } else { - self.block_entities.insert(key, mesh.block_entities.clone()); + let old = self.block_entities.insert(key, mesh.block_entities.clone()); + old.as_deref() != Some(&mesh.block_entities[..]) + }; + if changed { + self.block_entity_revision = next_block_entity_revision(); } let solid = (mesh.solid.len() / 4) as u32; let translucent = (mesh.translucent.len() / 4) as u32; @@ -2099,8 +2112,13 @@ impl Renderer { pub fn block_entities(&self) -> impl Iterator { self.block_entities.values().flatten() } + /// A value that changes whenever `block_entities` gives something else. + pub fn block_entity_revision(&self) -> u64 { + self.block_entity_revision + } pub fn clear_sections(&mut self) { self.block_entities.clear(); + self.block_entity_revision = next_block_entity_revision(); self.sections.clear(); self.arena.clear(); if let Some(flat) = &mut self.flat { @@ -2111,7 +2129,9 @@ impl Renderer { self.occlusion.clear(); } pub fn remove(&mut self, key: SectionKey) { - self.block_entities.remove(&key); + if self.block_entities.remove(&key).is_some() { + self.block_entity_revision = next_block_entity_revision(); + } self.occlusion.remove(key); self.release_section(key); } From a2363a3bf635f7035052f97dfea488c2d16f878c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 08:09:49 +0200 Subject: [PATCH 62/67] fix(server-thread): the integrated server follows the render distance, past 32 too IntegratedServer.tickServer: the server takes the window's render and simulation distances when they change ("Changing view distance to N, from M"), tells every player (SetChunkCacheRadius, SetSimulationDistance), and each player's chunks follow, up to what they asked for. The integrated server no longer clamps the view distance to 32, and a remote client sends its render distance again when it changes (Options.broadcastOptions), past 32 too. Fixes #804. lan_e2e: a distance check (--server-thread), and the guest is found by name in lan status, where the server thread lists the owner too: the hazards check read the owner's food (the "no hunger from moving" gap). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 4 ++ crates/horizon-client/src/game/integrated.rs | 48 ++++++++++++++++++-- crates/horizon-client/src/game/remote.rs | 30 ++++++++++-- crates/horizon-client/src/lan/hub.rs | 16 +++++++ tools/lan_e2e.py | 43 +++++++++++++++--- 5 files changed, 127 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index cfbb526ef..bb9d8f0a7 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9979,6 +9979,10 @@ impl ApplicationHandler for Game<'_> { self.pause = self.pauses_game(); if let Some(server) = &self.integrated { server.set_paused(self.pause); + server.set_distances( + self.settings.render_distance, + self.settings.simulation_distance, + ); } self.clock.end_frame(self.pause); let finished = self diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index 412c41693..b2c0a2e7f 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -28,7 +28,7 @@ use std::{ path::Path, sync::{ Arc, - atomic::{AtomicBool, AtomicU16, AtomicU32, Ordering}, + atomic::{AtomicBool, AtomicI32, AtomicU16, AtomicU32, Ordering}, mpsc::{Receiver, Sender, channel, sync_channel}, }, thread::JoinHandle, @@ -45,6 +45,10 @@ struct Flags { /// while it is not. published: AtomicU16, smoothed_tick_ms: AtomicU32, + /// `options.renderDistance` and `options.simulationDistance`, which the + /// server follows while it runs (IntegratedServer.tickServer). + render_distance: AtomicI32, + simulation_distance: AtomicI32, } /// Work the window hands its server thread (`MinecraftServer.execute`). @@ -66,6 +70,13 @@ impl Integrated { pub fn set_paused(&self, paused: bool) { self.flags.paused.store(paused, Ordering::Relaxed); } + /// The window's render and simulation distances, for the server to follow. + pub fn set_distances(&self, render: i32, simulation: i32) { + self.flags.render_distance.store(render, Ordering::Relaxed); + self.flags + .simulation_distance + .store(simulation, Ordering::Relaxed); + } /// `IntegratedServer.getPort` while `isPublished`. pub fn published(&self) -> Option { Some(self.flags.published.load(Ordering::Relaxed)).filter(|&p| p != 0) @@ -192,6 +203,12 @@ impl Game<'_> { options.dev_mods.clear(); let (res, pack, settings) = (self.res.clone(), self.pack.clone(), self.settings.clone()); let flags = Arc::new(Flags::default()); + flags + .render_distance + .store(settings.render_distance, Ordering::Relaxed); + flags + .simulation_distance + .store(settings.simulation_distance, Ordering::Relaxed); let (ready_tx, ready) = channel(); let (tasks, server_tasks) = channel(); let server_flags = flags.clone(); @@ -249,7 +266,9 @@ impl Game<'_> { launch.create.as_ref(), )?; let saved = Box::new(self.level.clone()); - let view_distance = self.settings.render_distance.clamp(2, 32); + // Math.max(2, options.renderDistance): past vanilla's 32 when the + // window's render distance is (Horizon's options go to 64). + let view_distance = self.settings.render_distance.max(2); let config = HubConfig { default_game_mode: self.level.mode(), // Only the owner until it is opened to the LAN. @@ -257,7 +276,7 @@ impl Game<'_> { force_game_mode: false, hardcore: self.level.hardcore, view_distance, - simulation_distance: self.settings.simulation_distance.clamp(2, 32), + simulation_distance: self.settings.simulation_distance.max(2), dimension: self.dim.id().to_owned(), seed: self.level.seed, max_players: MAX_PLAYERS, @@ -318,6 +337,7 @@ impl Game<'_> { // Only the connections run. self.tick_server(true)?; } else { + self.follow_distances(&flags); let started = Instant::now(); self.tick_server(false)?; self.debug_charts.log_tick(started, started.elapsed()); @@ -438,6 +458,28 @@ impl Game<'_> { } /// Publish or unpublish for real, once the hub is in its place (a /// player's `/publish` runs while it is out). + /// IntegratedServer.tickServer: the server follows the window's render + /// and simulation distances when they change. + fn follow_distances(&mut self, flags: &Flags) { + let Some(host) = self.lan.as_mut() else { + return; + }; + let view = flags.render_distance.load(Ordering::Relaxed).max(2); + let old = host.hub.config.view_distance; + if view != old { + log::info!("Changing view distance to {view}, from {old}"); + host.hub.set_view_distance(view); + self.settings.render_distance = view; + } + let simulation = flags.simulation_distance.load(Ordering::Relaxed).max(2); + let old = host.hub.config.simulation_distance; + if simulation != old { + log::info!("Changing simulation distance to {simulation}, from {old}"); + host.hub.set_simulation_distance(simulation); + self.settings.simulation_distance = simulation; + } + } + fn apply_publish(&mut self) { if self.lan.is_none() { return; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index e5769acde..474e943c0 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -100,6 +100,8 @@ pub(super) struct Remote { was_sprinting: bool, carried: Option, loaded_sent: bool, + /// The view distance last sent in ClientInformation. + sent_view: i8, /// BlockStatePredictionHandler: blocks changed ahead of the server (a predicted break), /// with the sequence of the last action that changed them and the state the server /// last said they have. Until the server acknowledges that sequence its block updates @@ -209,7 +211,7 @@ impl Game<'_> { .and_then(|p| Builtin::load(&p).ok()) .ok_or("Resources are incomplete: registries.json is missing or invalid. Click \"Prepare again\" in the launcher (or run horizon-prepare), then try again.")?; let lang = Arc::new(horizon_client::net::load_language(&root)); - let view = self.settings.render_distance.clamp(2, 32) as i8; + let view = client_view_distance(self.settings.render_distance); if local.is_none() { horizon_client::crash::set_detail( "Server", @@ -296,6 +298,7 @@ impl Game<'_> { was_sprinting: false, carried: None, loaded_sent: false, + sent_view: view, predictions: HashMap::new(), menu: None, menu_title: None, @@ -1153,10 +1156,12 @@ impl Game<'_> { self.streaming.radius = self.settings.render_distance.min(p.chunk_radius + 1).max(2); self.enter_remote_dimension(&p.spawn, false); + let view = client_view_distance(self.settings.render_distance); + if let Some(r) = self.remote.as_mut() { + r.sent_view = view; + } self.send(S::ClientInformation( - horizon_protocol::packets::configuration::ClientInformation::new( - self.settings.render_distance.clamp(2, 32) as i8, - ), + horizon_protocol::packets::configuration::ClientInformation::new(view), )); } C::Respawn(p) => { @@ -2303,6 +2308,17 @@ impl Game<'_> { /// One client tick while connected (Minecraft.tick with a ClientLevel). pub(super) fn tick_remote(&mut self) -> Result<()> { self.ticks += 1; + // Options.broadcastOptions: a changed render distance is sent again. + let view = client_view_distance(self.settings.render_distance); + if let Some(r) = self.remote.as_mut() + && r.joined + && r.sent_view != view + { + r.sent_view = view; + self.send(S::ClientInformation( + horizon_protocol::packets::configuration::ClientInformation::new(view), + )); + } // Minecraft.tick: TickRateManager.tick; ClientLevel.tick advances // the time only while the level runs normally. let running = self.clock.world_runs(); @@ -3749,6 +3765,12 @@ pub(super) fn input_ordinal(input: Input) -> i32 { } } +/// The view distance a client asks for: its render distance, which Horizon +/// lets go past vanilla's 32 (a vanilla server still caps it at its own). +fn client_view_distance(render_distance: i32) -> i8 { + render_distance.clamp(2, i32::from(i8::MAX)) as i8 +} + #[cfg(test)] mod tests { use super::Game; diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index e877fdd64..54b3ba63b 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -299,6 +299,22 @@ impl Hub { g.sender.send(body.clone()); } } + /// PlayerList.setViewDistance: every player is told the new radius, + /// and each one's chunks follow it, up to what they asked for + /// (ChunkMap.getPlayerViewDistance). + pub fn set_view_distance(&mut self, distance: i32) { + self.config.view_distance = distance; + self.broadcast(&C::SetChunkCacheRadius(play::Distance { distance })); + for g in &mut self.guests { + g.view_distance = i32::from(g.info.view_distance).clamp(2, distance); + g.center = None; + } + } + /// PlayerList.setSimulationDistance. + pub fn set_simulation_distance(&mut self, distance: i32) { + self.config.simulation_distance = distance; + self.broadcast(&C::SetSimulationDistance(play::Distance { distance })); + } /// Whether a guest is placed in the level in place (`config.dimension`). pub fn here(&self, g: &Guest) -> bool { g.placed && g.dimension == self.config.dimension diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 7dc8b2c01..c306504ab 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -23,6 +23,8 @@ address. The script then checks, from both sides, what a guest's game used to ge opens as 9x6 with the right half first; - feedback: the guest's command feedback names the guest and shows the numbers; - effects: a beacon's power reaches every player in range, the host and the guest; +- distance: (--server-thread) the server follows the host's render distance, past vanilla's 32 too + (IntegratedServer.tickServer), and the guest's chunks follow the server's (#804) - portals: (--server-thread) the server holds a level per dimension: the guest goes to the Nether through a portal while the host stays, then the host goes to the End while the guest is in the Nether, and both come back. @@ -79,7 +81,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'effects', 'portals'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'effects', 'portals', 'distance'] class Game: @@ -192,7 +194,7 @@ def check_armour(host, guest, out): face_each_other(host, guest) host.shot(out, 'armour') guest.shot(out, 'armour') - seen = host.raw('lan', 'status')['guests'][0].get('model_armor') + seen = lan_guest(host).get('model_armor') assert seen == [None, 'minecraft:diamond_chestplate', 'minecraft:diamond_leggings', None], \ f'the host draws the guest in {seen}' host_gear = [e for e in remote(guest).get('player_equipment', [])] @@ -355,6 +357,33 @@ def nether_portal(game, x, y, z): game('command', command=f'fill {x} {y} {z} {x + 1} {y + 2} {z} minecraft:nether_portal[axis=x]') +def lan_guest(host): + """The guest's entry in the host's `lan status` (on the server thread the owner is listed too).""" + guests = host.raw('lan', 'status')['guests'] + return next(g for g in guests if g['name'] == GUEST_NAME) + +def check_distance(host, guest, out): + """IntegratedServer.tickServer: the server takes the host's render distance as its view distance + (past 32, which Horizon's options allow) and sends it to every player (SetChunkCacheRadius).""" + if not SERVER_THREAD: + return 'skipped (needs --server-thread)' + seen = {} + try: + for distance in (48, 6): + host.raw('render-distance', distance) + # The pause screen doesn't hold the server here: it follows on its next tick. + host.wait(20) + guest.wait(20) + seen[distance] = (host('status').get('render_distance'), guest('status').get('render_distance')) + finally: + host.raw('render-distance', 12) + # A client streams out to min(its render distance, the server's view distance + 1). + assert seen[48][0] == 48, f'the host at 48 streams {seen[48][0]} chunks out (all: {seen})' + assert seen[6][0] == 6, f'the host at 6 streams {seen[6][0]} chunks out (all: {seen})' + assert seen[6][1] == 7, f'the guest streams {seen[6][1]} with the server at 6 (all: {seen})' + return seen + + def check_portals(host, guest, out): if not SERVER_THREAD: return 'skipped (needs --server-thread)' @@ -470,14 +499,14 @@ def check_hazards(host, guest, out): guest.wait(10) x, y, z = pos_of(guest) guest.raw('look-at', f'{x + 60:.2f}', f'{y + 1.6:.2f}', f'{z:.2f}') - food = host.raw('lan', 'status')['guests'][0]['food'] + food = lan_guest(host)['food'] for k in ('w', 'ctrl', 'space'): guest.raw('key', k, 'down') guest.wait(160) for k in ('space', 'ctrl', 'w'): guest.raw('key', k, 'up') guest.wait(10) - fed = host.raw('lan', 'status')['guests'][0]['food'] + fed = lan_guest(host)['food'] hunger = {'before': food, 'after': fed} hurt = {} hx, hy, hz = (int(v // 1) for v in pos_of(host)) @@ -865,7 +894,7 @@ def changed_pixels(a, b): def sneaking_on_host(host, want, ticks=40): """Whether the host sees the guest crouching, once it matches `want` or after `ticks`.""" for _ in range(ticks // 5): - sneaking = host.raw('lan', 'status')['guests'][0]['sneaking'] + sneaking = lan_guest(host)['sneaking'] if sneaking == want: return sneaking host.wait(5) @@ -988,7 +1017,7 @@ def check_sleep(host, guest, out): guest.raw('interact', *guest_bed) guest.wait(10) asleep = {'guest': guest('state').get('sleeping'), 'host': host('state').get('sleeping'), - 'server': host.raw('lan', 'status')['guests'][0].get('sleeping')} + 'server': lan_guest(host).get('sleeping')} guest.shot(out, 'sleep-guest') # Alone in bed: still night 120 ticks later (everyone must sleep). guest.wait(120) @@ -1178,7 +1207,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'containers': check_containers, 'feedback': check_feedback, 'titles': check_titles, 'commands': check_commands, 'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls, - 'effects': check_effects, 'portals': check_portals, 'clicks': check_clicks, + 'effects': check_effects, 'portals': check_portals, 'distance': check_distance, 'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push, 'eat': check_eat, 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles} From 84d3d2a9a025df673abefe6d815f4d8da8191e40 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 08:14:28 +0200 Subject: [PATCH 63/67] Rerun CI (flaky options_ui test, #802) Co-Authored-By: Claude Opus 5.5 From 817a71ffa5c57fada9ee5a19a7f4e751486df187 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 06:09:46 +0200 Subject: [PATCH 64/67] Clients keep the light the server sends; the server sends light updates A remote client (a LAN guest, or the game on its server thread) threw away the light in each LevelChunkWithLight and relit the column and its neighbours itself. It now applies the sky and block layers as ClientPacketListener.applyLightData does, and relights only block changes, as vanilla's client does. The hub now sends ClientboundLightUpdatePacket for the light sections that changed in a column to the players for whom it is on the tracked border (ChunkHolder.broadcastChanges), so light from blocks a guest doesn't track reaches it. horizon-ctl gains 'light X1 Y1 Z1 X2 Y2 Z2', and lan_e2e a 'light' check comparing the guest's light with the host's. Co-Authored-By: Claude Opus 5.5 --- .../src/bin/horizon-ctl/commands.rs | 4 + crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/agent.rs | 38 +++++- crates/horizon-client/src/game/lan.rs | 2 + crates/horizon-client/src/game/remote.rs | 60 ++++++++++ crates/horizon-client/src/lan/chunks.rs | 27 +++++ crates/horizon-client/src/lan/hub.rs | 41 +++++++ crates/horizon-client/src/light.rs | 113 ++++++++++++++++++ crates/horizon-client/src/save.rs | 27 +---- crates/horizon-client/src/streaming.rs | 33 ++++- crates/horizon-client/src/world.rs | 5 + docs/native-client.md | 2 +- tools/lan_e2e.py | 73 ++++++++++- 13 files changed, 394 insertions(+), 32 deletions(-) diff --git a/crates/horizon-client/src/bin/horizon-ctl/commands.rs b/crates/horizon-client/src/bin/horizon-ctl/commands.rs index c5e716a03..3e47a0545 100644 --- a/crates/horizon-client/src/bin/horizon-ctl/commands.rs +++ b/crates/horizon-client/src/bin/horizon-ctl/commands.rs @@ -187,6 +187,10 @@ pub const COMMANDS: &[Cmd] = &[ &[opt("radius", Ty::Num, "search radius in blocks (default 32)"), opt("type", Ty::Str, "entity type filter")], mcp: true, local: false), cmd!("block", Observe, "block X Y Z", "the block state at a position", "", ["horizon-ctl block 10 64 -3"], &BLOCK_XYZ, mcp: true, local: false), + cmd!("light", Observe, "light X1 Y1 Z1 X2 Y2 Z2", "the stored sky and block light of a box", + "At most 32768 blocks per request. One hex digit per block in `sky` and `block`, x fastest, then z, then y; `.` where no chunk is loaded.", + ["horizon-ctl light 0 64 0 15 70 15"], + &[p("x1", Ty::Int, "corner X"), p("y1", Ty::Int, "corner Y"), p("z1", Ty::Int, "corner Z"), p("x2", Ty::Int, "opposite X"), p("y2", Ty::Int, "opposite Y"), p("z2", Ty::Int, "opposite Z")], mcp: true, local: false), cmd!("blocks", Observe, "blocks X1 Y1 Z1 X2 Y2 Z2", "the non-air blocks of a box, with counts per block", "At most 32768 blocks per request.", ["horizon-ctl blocks 0 60 0 15 70 15"], &[p("x1", Ty::Int, "corner X"), p("y1", Ty::Int, "corner Y"), p("z1", Ty::Int, "corner Z"), p("x2", Ty::Int, "opposite X"), p("y2", Ty::Int, "opposite Y"), p("z2", Ty::Int, "opposite Z")], mcp: true, local: false), diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 54d032d20..9bf2096aa 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6634,6 +6634,7 @@ impl Game<'_> { world.weather = self.world.weather; // An open hub keeps hearing the new level's block changes. world.block_log = self.world.block_log.as_ref().map(|_| Vec::new()); + world.light_log = self.world.light_log.as_ref().map(|_| Default::default()); // The sculk tables' watched blocks are built once per game. world.watched = std::mem::take(&mut self.world.watched); self.streaming = streaming; diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index 8a6b54650..b11fdd552 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -4,7 +4,13 @@ //! take time run as a task advanced every frame and reply when they finish or //! time out, so `horizon-ctl break 10 64 3` returns what the block dropped. use super::{Game, Screen}; -use horizon_client::{Result, block_entity_anim::Ticker, items::ItemStack, widgets}; +use horizon_client::{ + Result, + block_entity_anim::Ticker, + chunk::{MIN_Y, SECTIONS}, + items::ItemStack, + widgets, +}; use horizon_core::geometry::Vec3; use horizon_world::registry::BlockStateId; use serde_json::{Value, json}; @@ -27,6 +33,7 @@ pub(super) const HELP: &[&str] = &[ "entities [RADIUS] [TYPE] mobs, dropped items and orbs near the player, nearest first", "block X Y Z the block state at a position", "blocks X1 Y1 Z1 X2 Y2 Z2 the non-air blocks of a box (at most 32768 blocks)", + "light X1 Y1 Z1 X2 Y2 Z2 the stored sky and block light of a box (at most 32768 blocks)", "find-block BLOCK [RADIUS] the nearest loaded blocks of a kind (default radius 32)", "inventory hotbar, main inventory, armor, offhand and carried stack", "screen the open screen or menu, its slots and its buttons", @@ -962,6 +969,35 @@ impl Game<'_> { } ok(json!({ "from": lo, "to": hi, "counts": census, "blocks": list })) } + "light" => { + self.need_world()?; + let (p, q) = (a.block_pos(0)?, a.block_pos(3)?); + let lo = [0, 1, 2].map(|i| p[i].min(q[i])); + let hi = [0, 1, 2].map(|i| p[i].max(q[i])); + let volume: i64 = (0..3).map(|i| i64::from(hi[i] - lo[i] + 1)).product(); + if volume > 32768 { + return Err(format!("{volume} blocks; at most 32768 per request").into()); + } + // One hex digit per block, x fastest, then z, then y; `.` + // outside the loaded chunks. + let (mut sky, mut block) = (String::new(), String::new()); + for y in lo[1]..=hi[1] { + for z in lo[2]..=hi[2] { + for x in lo[0]..=hi[0] { + let packed = self + .world + .chunks + .get(&(x >> 4, z >> 4)) + .filter(|_| (MIN_Y..MIN_Y + SECTIONS as i32 * 16).contains(&y)) + .map(|c| c.light(x & 15, y, z & 15)); + let digit = |v: u8| char::from_digit(u32::from(v), 16).unwrap_or('.'); + sky.push(packed.map_or('.', |v| digit(v >> 4))); + block.push(packed.map_or('.', |v| digit(v & 15))); + } + } + } + ok(json!({ "from": lo, "to": hi, "sky": sky, "block": block })) + } "find-block" => { self.need_world()?; let want = namespaced(a.get(0, "BLOCK")?); diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 114330e51..d35bf787a 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -504,6 +504,7 @@ impl Game<'_> { let hub = Hub::new(lan, sync, encoder, config); let port = hub.lan.port; self.world.block_log = Some(Vec::new()); + self.world.light_log = Some(Default::default()); self.lan = Some(LanHost { hub, settings: LanSettings { port, ..settings }, @@ -553,6 +554,7 @@ impl Game<'_> { // Give the writer threads a moment to flush the disconnect packets. std::thread::sleep(std::time::Duration::from_millis(100)); self.world.block_log = None; + self.world.light_log = None; self.audio.capture = None; self.lan_events.clear(); self.streaming.extra.clear(); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 474e943c0..680360c93 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -49,6 +49,9 @@ fn remote_text(tag: &Tag) -> horizon_client::text::Text { comp_text(&tag_to_json(tag)) } +/// A section's sky and block light layers from the wire. +type LightLayers = (Option>, Option>); + /// Streaming needs a generator; a server world never generates. struct NoTerrain; impl ChunkGenerator for NoTerrain { @@ -771,8 +774,64 @@ impl Game<'_> { PalettedContainer::from_values(BIOME_STRATEGY, &cells); } } + // ClientPacketListener.applyLightData: the column keeps the light + // the server sent; nothing is relit. + let (layers, above) = self.wire_light(&p.light); + let sky = r.dimension.has_skylight; + horizon_client::light::light_from_layers(&mut chunk, &layers, above, sky); Ok(chunk) } + /// `ClientPacketListener.readSectionList`: the sky and block layers of a + /// light packet, by section of the client's column, and the sky light + /// above the column's top section (from the light section just above + /// it, else open sky). + fn wire_light( + &self, + light: &horizon_protocol::chunk::LightData, + ) -> (Vec, [u8; 256]) { + let mut layers: Vec = vec![(None, None); SECTIONS]; + let Some(r) = self.remote.as_ref() else { + return (layers, [0; 256]); + }; + let sky = r.dimension.has_skylight; + let mut above = [if sky { 15 } else { 0 }; 256]; + // Light section b is the dimension's section b - 1. + let first = (r.dimension.min_y - MIN_Y).div_euclid(16) - 1; + for (b, layer) in light + .layers(r.dimension.sections() + 2) + .into_iter() + .enumerate() + { + let si = first + b as i32; + if (0..SECTIONS as i32).contains(&si) { + layers[si as usize] = layer; + } else if si == SECTIONS as i32 + && sky + && let Some(l) = &layer.0 + { + for (i, a) in above.iter_mut().enumerate() { + *a = (l[i >> 1] >> ((i & 1) * 4)) & 15; + } + } + } + (layers, above) + } + /// `handleLightUpdatePacket`: the sections it names take its light. + fn remote_light_update(&mut self, p: &play::LightUpdate) { + let Some(sky) = self.remote.as_ref().map(|r| r.dimension.has_skylight) else { + return; + }; + if !self.world.chunks.contains_key(&(p.x, p.z)) { + return; + } + let (layers, _) = self.wire_light(&p.light); + let Some(chunk) = self.world.chunks.get_mut(&(p.x, p.z)) else { + return; + }; + let changed = + horizon_client::light::update_light_layers(Arc::make_mut(chunk), &layers, sky); + Streaming::light_updated(&mut self.world, p.x, p.z, &changed); + } /// `ClientChunkCache.Storage.inRange`: within the storage around the /// view centre, `calculateStorageRange` (the server's radius, at least /// two, plus three) chunks each way. @@ -1219,6 +1278,7 @@ impl Game<'_> { } Err(e) => log::warn!("Horizon: chunk {} {}: {e}", p.x, p.z), }, + C::LightUpdate(p) => self.remote_light_update(&p), C::ForgetLevelChunk(p) => { let [x, z] = p.pos; self.streaming.forget(&mut self.world, x, z); diff --git a/crates/horizon-client/src/lan/chunks.rs b/crates/horizon-client/src/lan/chunks.rs index d6a7dc3a1..a9cf57a7d 100644 --- a/crates/horizon-client/src/lan/chunks.rs +++ b/crates/horizon-client/src/lan/chunks.rs @@ -480,6 +480,33 @@ impl ChunkEncoder { w.data } + /// ClientboundLightUpdatePacket for the light sections over `changed` (a + /// bit per section of the column, `sectionLightChanged`), as vanilla's + /// `ClientboundLightUpdatePacketData` writes them, with both kinds of + /// each section (Horizon's light engine doesn't tell them apart; the + /// client keeps the same light either way). A section changed as its + /// last block went (light dimming around a removed block) is sent too: + /// a client not tracking the block would otherwise keep its old light. + pub fn light_update(&self, chunk: &Chunk, changed: u32) -> Vec { + let first = self.sections.start; + let lights: Vec<(usize, SectionLight)> = self + .light_sections(chunk, u64::MAX) + .into_iter() + .filter(|&(b, _)| { + // Light section b holds the column's section first + b - 1. + (1..=self.sections.len()).contains(&b) && changed >> (first + b - 1) & 1 != 0 + }) + .collect(); + horizon_protocol::packets::play::Clientbound::LightUpdate( + horizon_protocol::packets::play::LightUpdate { + x: chunk.x, + z: chunk.z, + light: light_data_of(&lights, self.sky), + }, + ) + .encode() + } + pub fn encode( &self, chunk: &Chunk, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index faff066ab..c88292787 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -988,18 +988,23 @@ impl Hub { .any(|g| g.dimension == self.config.dimension) { let _ = ctx.world.block_log.take(); + let _ = ctx.world.light_log.take(); } return events; } if ctx.world.block_log.is_none() { ctx.world.block_log = Some(Vec::new()); } + if ctx.world.light_log.is_none() { + ctx.world.light_log = Some(Default::default()); + } self.sync_world(ctx); for i in 0..self.guests.len() { if self.here(&self.guests[i]) { self.update_chunks(i, ctx); } } + self.broadcast_light_changes(ctx.world); self.broadcast_block_changes(ctx.world); // ServerGamePacketListenerImpl.tick: the actions handled since the last tick are // acknowledged after the block changes they made were sent, so a client's predicted @@ -1234,6 +1239,42 @@ impl Hub { } } + /// `ChunkHolder.broadcastChanges`' light part: the light sections that + /// changed in a column go to the players for whom it is on the border of + /// what they track (`ChunkMap.isChunkOnTrackedBorder`); the others light + /// the blocks they are sent themselves. + fn broadcast_light_changes(&mut self, world: &mut World) { + let Some(log) = world.light_log.as_mut() else { + return; + }; + if log.is_empty() { + return; + } + let mut changes: Vec<((i32, i32), u32)> = log.drain().collect(); + changes.sort_unstable(); + for ((x, z), sections) in changes { + let tracked = |g: &Guest, x: i32, z: i32| g.sent_chunks.contains(&(x, z)); + let viewers: Vec = (0..self.guests.len()) + .filter(|&i| { + let g = &self.guests[i]; + self.here(g) + && tracked(g, x, z) + && (-1..=1).any(|dz| (-1..=1).any(|dx| !tracked(g, x + dx, z + dz))) + }) + .collect(); + if viewers.is_empty() { + continue; + } + let Some(chunk) = world.chunks.get(&(x, z)) else { + continue; + }; + let body = self.encoder.light_update(chunk, sections); + for &i in &viewers { + self.guests[i].sender.send(body.clone()); + } + } + } + fn broadcast_block_changes(&mut self, world: &mut World) { let Some(log) = world.block_log.as_mut() else { return; diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index dcf4633a9..7a939b308 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -381,6 +381,81 @@ pub fn apply_relighting(chunk: &mut Chunk, lights: Vec) -> Vec<(i32, i32, changed } +/// `DataLayer.get`: the nibble at index `i` (`y << 8 | z << 4 | x`), low first. +#[inline] +fn nibble(layer: &[u8], i: usize) -> u8 { + (layer[i >> 1] >> ((i & 1) * 4)) & 15 +} + +/// A column's light as vanilla stores it, two DataLayers per section (sky, +/// block; `layers[i]` for section `i`, `None` where no layer is stored), into +/// the sections' packed light, which then needs no light job. A missing sky +/// layer repeats the bottom layer of the nearest stored one above it, and +/// `above` (per `z << 4 | x`) above the highest +/// (`SkyLightSectionStorage.getLightValue`); a missing block layer is dark. +pub fn light_from_layers>( + chunk: &mut Chunk, + layers: &[(Option, Option)], + mut above: [u8; 256], + sky_light: bool, +) { + for (si, (sky, block)) in layers.iter().enumerate().take(SECTIONS).rev() { + let sky = sky.as_ref().filter(|_| sky_light); + let section = Arc::make_mut(&mut chunk.sections[si]); + if sky.is_none() && block.is_none() && above.iter().all(|&v| v == above[0]) { + section.light = Light::Uniform(above[0] << 4); + section.compact(); + continue; + } + let mut data = Box::new([0u8; 4096]); + for (i, packed) in data.iter_mut().enumerate() { + let s = sky.map_or(above[i & 255], |a| nibble(a.as_ref(), i)); + let b = block.as_ref().map_or(0, |a| nibble(a.as_ref(), i)); + *packed = s << 4 | b; + } + for (i, a) in above.iter_mut().enumerate() { + *a = data[i] >> 4; + } + section.light = Light::Full(data); + section.compact(); + } + chunk.lit = true; + chunk.light_loaded = true; +} + +/// A light update's layers (`LevelLightEngine.queueSectionData`): each one +/// given replaces that kind of light in its section, the other kind stays. +/// Returns the sections whose light changed. +pub fn update_light_layers>( + chunk: &mut Chunk, + layers: &[(Option, Option)], + sky_light: bool, +) -> Vec { + let mut changed = Vec::new(); + for (si, (sky, block)) in layers.iter().enumerate().take(SECTIONS) { + let sky = sky.as_ref().filter(|_| sky_light); + if sky.is_none() && block.is_none() { + continue; + } + let old = &chunk.sections[si].light; + let mut data = Box::new([0u8; 4096]); + for (i, packed) in data.iter_mut().enumerate() { + let was = old.get(i); + let s = sky.map_or(was >> 4, |a| nibble(a.as_ref(), i)); + let b = block.as_ref().map_or(was & 15, |a| nibble(a.as_ref(), i)); + *packed = s << 4 | b; + } + if (0..4096).all(|i| old.get(i) == data[i]) { + continue; + } + let section = Arc::make_mut(&mut chunk.sections[si]); + section.light = Light::Full(data); + section.compact(); + changed.push(si); + } + changed +} + /// Resident chunks the incremental relighter reads and writes. pub trait LightWorld { fn state(&self, x: i32, y: i32, z: i32) -> Option; @@ -809,6 +884,44 @@ mod tests { assert!(copied <= 3, "{copied} sections copied"); } + #[test] + fn stored_layers_fill_the_column_as_vanilla_reads_them() { + // Section 10's sky layer is 7 on its bottom row and 3 above; no + // layers anywhere else; a block layer of 5 in section 2. + let mut sky = vec![0x33u8; 2048]; + sky[..128].fill(0x77); + let mut layers: Vec<(Option>, Option>)> = vec![(None, None); SECTIONS]; + layers[10].0 = Some(sky); + layers[2].1 = Some(vec![0x55; 2048]); + let mut chunk = Chunk::empty(0, 0); + light_from_layers(&mut chunk, &layers, [15; 256], true); + let at = |c: &Chunk, si: usize, i: usize| c.sections[si].light(i); + assert!(chunk.lit && chunk.light_loaded); + assert!( + matches!(chunk.sections[11].light, Light::Uniform(0xF0)), + "above: open sky" + ); + assert_eq!((at(&chunk, 10, 0), at(&chunk, 10, 4095)), (0x70, 0x30)); + // Below, each section repeats the bottom row of the stored one above. + assert!(matches!(chunk.sections[9].light, Light::Uniform(0x70))); + assert_eq!(at(&chunk, 2, 100), 0x75, "block light only where stored"); + assert!(matches!(chunk.sections[0].light, Light::Uniform(0x70))); + + // A light update replaces one kind of light in the sections it names. + let mut update: Vec<(Option>, Option>)> = vec![(None, None); SECTIONS]; + update[2].1 = Some(vec![0; 2048]); + update[3].0 = Some(vec![0x77; 2048]); + let changed = update_light_layers(&mut chunk, &update, true); + assert_eq!(changed, vec![2]); + assert!(matches!(chunk.sections[2].light, Light::Uniform(0x70))); + update[3].0 = Some(vec![0xFF; 2048]); + assert_eq!( + update_light_layers(&mut chunk, &update, false), + Vec::::new(), + "no sky storage" + ); + } + #[test] fn empty_chunk_missing_neighbours_and_no_sky() { let mut chunk = Chunk::empty(0, 0); diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index c60da2caa..ccef3e567 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -1278,31 +1278,8 @@ pub fn load_light(tag: &Tag, chunk: &mut Chunk, sky_light: bool) { }; layers[si as usize] = (layer("SkyLight"), layer("BlockLight")); } - let sky_default = if sky_light { 15u8 } else { 0 }; - // Sky nibbles of the section above's bottom layer, going down. - let mut above: [u8; 256] = [sky_default; 256]; - for si in (0..SECTIONS).rev() { - let (sky, block) = layers[si]; - let mut data = Box::new([0u8; 4096]); - let nibble = |a: &[u8], i: usize| (a[i >> 1] >> ((i & 1) * 4)) & 15; - for (i, packed) in data.iter_mut().enumerate() { - let s = match sky { - Some(a) if sky_light => nibble(a, i), - _ => above[i & 255], - }; - let b = block.map_or(0, |a| nibble(a, i)); - *packed = s << 4 | b; - } - for (i, a) in above.iter_mut().enumerate() { - *a = data[i] >> 4; - } - let mut section = (*chunk.sections[si]).clone(); - section.light = Light::Full(data); - section.compact(); - chunk.sections[si] = Arc::new(section); - } - chunk.lit = true; - chunk.light_loaded = true; + let above = [if sky_light { 15 } else { 0 }; 256]; + crate::light::light_from_layers(chunk, &layers, above, sky_light); } type RegionCache = Mutex>>>>; diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index bff0a8fd6..6acdcafd2 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -502,16 +502,38 @@ impl Streaming { } } } - /// A column received from a server: store it and relight it and its neighbours. + /// A column received from a server, with the light it sent + /// (`ClientPacketListener.handleLevelChunkWithLight`): nothing is + /// relit, and it and its neighbours remesh (`enableChunkLight`). pub fn insert_remote(&mut self, world: &mut World, chunk: Chunk) { let key = (chunk.x, chunk.z); - self.lit.remove(&key); self.lighting.remove(&key); + self.light_snapshots.remove(&key); world.chunks.insert(key, Arc::new(chunk)); + self.lit.insert(key); self.generated_total += 1; for dz in -1..=1 { for dx in -1..=1 { - world.dirty_light.insert((key.0 + dx, key.1 + dz)); + for sy in 0..SECTIONS as i32 { + world.dirty_sections.insert((key.0 + dx, sy, key.1 + dz)); + } + } + } + } + /// A server's light update for a resident column: the sections whose + /// light changed and their neighbours remesh + /// (`setSectionDirtyWithNeighbors`). + pub fn light_updated(world: &mut World, x: i32, z: i32, sections: &[usize]) { + for &sy in sections { + for dz in -1..=1 { + for dy in -1..=1 { + for dx in -1..=1 { + let s = sy as i32 + dy; + if (0..SECTIONS as i32).contains(&s) { + world.dirty_sections.insert((x + dx, s, z + dz)); + } + } + } } } } @@ -1148,6 +1170,11 @@ impl Streaming { /// Sections whose meshes sample a changed light value (faces and corners /// read light from neighbouring blocks). fn mark_light_changes(world: &mut World, changed: &[(i32, i32, i32)]) { + if let Some(log) = &mut world.light_log { + for &(x, y, z) in changed { + *log.entry((x >> 4, z >> 4)).or_default() |= 1 << ((y - crate::chunk::MIN_Y) >> 4); + } + } let mut last = None; for &(x, y, z) in changed { for dz in -1..=1 { diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index f6fbcd04d..efcbf9591 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -47,6 +47,10 @@ pub struct World { /// Every block position written since the log was last drained, kept /// only while the world is open to the network (see `lan`). pub block_log: Option>, + /// The sections whose light changed since the log was last drained, as + /// a bit per section of each column, kept while `block_log` is + /// (`ChunkHolder.sectionLightChanged`). + pub light_log: Option>, /// The villager POIs of each section (`PoiManager`'s sections). pub pois: crate::poi_sections::PoiSections, } @@ -133,6 +137,7 @@ impl World { day_time: 1000, weather: Weather::default(), block_log: None, + light_log: None, pois: Default::default(), } } diff --git a/docs/native-client.md b/docs/native-client.md index 4911c8197..fcf28246a 100644 --- a/docs/native-client.md +++ b/docs/native-client.md @@ -109,7 +109,7 @@ game accepts, test hooks included. - **Observe:** `state` (position, rotation, velocity, health, food, saturation, XP, effects, game mode, dimension, time, weather, the screen and what the crosshair points at), `looking-at`, `entities [RADIUS] [TYPE]` (mobs with - ids and health, dropped items, orbs), `block X Y Z`, `blocks X1 Y1 Z1 X2 Y2 Z2`, + ids and health, dropped items, orbs), `block X Y Z`, `blocks X1 Y1 Z1 X2 Y2 Z2`, `light X1 Y1 Z1 X2 Y2 Z2`, `find-block BLOCK [RADIUS]`, `inventory` (stacks with their components as SNBT), `screen` (the open screen or title menu, its buttons and slots) and `messages` (chat, title, subtitle and action bar). diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index c306504ab..4d13b1c22 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -49,6 +49,9 @@ address. The script then checks, from both sides, what a guest's game used to ge guest as the host has them (kind, place, rotation, what they hold or wear, the variant). - projectiles: primed TNT (one falling), an end crystal and an arrow that lands reach the guest where the host has them; a falling block lands on both sides. +- light: the guest's light matches the host's: around a torch placed near it, under a roof and + once a block of the roof is removed under the sky, and in the last chunk it tracks next to + glowstone in a chunk it doesn't (only the server's light updates bring that one). Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -81,7 +84,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'effects', 'portals', 'distance'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'light', 'effects', 'portals', 'distance'] class Game: @@ -1201,6 +1204,71 @@ def check_projectiles(host, guest, out): return f'{len(theirs)} identical, fuses (host, guest) {fuses}' +def light_differences(host, guest, lo, hi): + """Positions of the box loaded on both sides, and those whose sky or block light differ.""" + h, g = host.raw('light', *lo, *hi), guest.raw('light', *lo, *hi) + both = [i for i in range(len(h['sky'])) if h['sky'][i] != '.' and g['sky'][i] != '.'] + differ = [i for i in both if (h['sky'][i], h['block'][i]) != (g['sky'][i], g['block'][i])] + return h, g, both, differ + + +def same_light(host, guest, lo, hi, what, ticks=100): + """Wait until the guest's light over the box is the host's; the number of positions compared.""" + for _ in range(ticks // 10): + h, g, both, differ = light_differences(host, guest, lo, hi) + if not differ: + assert both, f'{what}: no position of the box is loaded on both sides' + return len(both) + host.wait(10) + i = differ[0] + nx, nz = hi[0] - lo[0] + 1, hi[2] - lo[2] + 1 + pos = (lo[0] + i % nx, lo[1] + i // (nx * nz), lo[2] + i // nx % nz) + raise AssertionError(f'{what}: {len(differ)} of {len(both)} positions differ, first {pos}: host sky ' + f'{h["sky"][i]} block {h["block"][i]}, guest sky {g["sky"][i]} block {g["block"][i]}') + + +def check_light(host, guest, out): + gx, gy, gz = (int(v // 1) for v in pos_of(guest)) + results = {} + # A torch near the guest: the guest lights it itself, as vanilla's client does. + torch = (gx + 3, gy, gz + 3) + host('command', command=f'setblock {torch[0]} {torch[1]} {torch[2]} minecraft:torch') + try: + results['torch'] = same_light(host, guest, (gx - 12, gy - 1, gz - 12), (gx + 16, gy + 3, gz + 16), 'torch') + finally: + host('command', command=f'setblock {torch[0]} {torch[1]} {torch[2]} minecraft:air') + # A roof over the guest, then a hole in it: sky light falls through. + roof = f'{gx - 3} {gy + 4} {gz - 3} {gx + 3} {gy + 4} {gz + 3}' + box = (gx - 6, gy - 1, gz - 6), (gx + 6, gy + 5, gz + 6) + host('command', command=f'fill {roof} minecraft:stone') + try: + results['roof'] = same_light(host, guest, *box, 'roof') + host('command', command=f'setblock {gx} {gy + 4} {gz} minecraft:air') + results['hole'] = same_light(host, guest, *box, 'hole in the roof') + finally: + host('command', command=f'fill {roof} minecraft:air') + # The last chunk the guest tracks eastwards, and glowstone high up just past it in a chunk it + # doesn't track: the guest never sees that block, only the light updates of its border chunk. + y = 200 + row = guest.raw('light', gx, y, gz, gx + 16 * 24, y, gz)['sky'] + assert '.' in row, 'the guest has every chunk 24 chunks east' + untracked = (gx + row.index('.')) >> 4 + near = untracked * 16 + 1 + # On its server thread the host's own game is a client too, sent the same light updates. + if not SERVER_THREAD: + assert host.raw('light', near, y, gz, near, y, gz)['sky'] != '.', 'the host lacks the chunk past the guest' + host('command', command=f'setblock {near} {y} {gz} minecraft:glowstone') + try: + lo, hi = (untracked * 16 - 16, y - 3, gz - 8), (untracked * 16 - 1, y + 3, gz + 8) + results['border'] = same_light(host, guest, lo, hi, 'border chunk') + lit = guest.raw('light', near - 2, y, gz, near - 2, y, gz)['block'] + assert lit == 'd', f'the border chunk by the glowstone has block light {lit}, not 13' + finally: + host('command', command=f'setblock {near} {y} {gz} minecraft:air') + results['border_cleared'] = same_light(host, guest, lo, hi, 'border chunk without the glowstone') + return results + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'sign': check_sign, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, @@ -1209,7 +1277,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls, 'effects': check_effects, 'portals': check_portals, 'distance': check_distance, 'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push, 'eat': check_eat, - 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles} + 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, + 'light': check_light} def run(ctl, out, resources, checks): From 8901a49d0b914f7812c4c1be204596f681d16593 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 09:40:55 +0200 Subject: [PATCH 65/67] Upload section meshes through a reused staging belt Queue::write_buffer creates a staging buffer for every call: on Metal a kernel call to make the buffer, then page faults while the mesh is copied into fresh memory. While exploring at render distance 32 the render thread made two such writes per uploaded section, plus one for its flat-draw ids. Section meshes and flat ids now go through their own StagingBelt (4 MiB mapped buffers, reused once the GPU has read them) into one encoder that the frame's submission runs ahead of its own commands. A page of the section arena that grows records its copy into that encoder too, so copies recorded before the grow reach the grown buffer. A frame that is not drawn still submits the copies, and more than 64 MiB of them are submitted early. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 71 ++++++++++++++++++--- crates/horizon-client/src/gfx/arena.rs | 84 ++++++++++++++++++------- crates/horizon-client/src/gfx/flat.rs | 12 +++- crates/horizon-client/src/gfx/upload.rs | 7 ++- 4 files changed, 141 insertions(+), 33 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index be842c3c6..ce47eb659 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -107,6 +107,12 @@ struct SectionGpu { } /// Line segments the hit outline can hold. const MAX_OUTLINE_EDGES: u64 = 256; +/// Staging buffer size for section meshes: a frame of uploads while +/// exploring fits in a few. +const SECTION_UPLOAD_CHUNK: u64 = 4 << 20; +/// Section copies are submitted early past this many staged bytes, which +/// bounds the staging memory frames that are not drawn can hold. +const SECTION_COPY_LIMIT: u64 = 64 << 20; pub struct Frame<'a> { pub camera: DVec3, pub yaw: f32, @@ -319,6 +325,13 @@ pub struct Renderer { lightmap_sampler: wgpu::Sampler, animations: AnimatedSprites, uploads: upload::Uploads, + /// Section meshes are staged through their own belt into + /// `section_copies`, which the next submission runs ahead of the frame: + /// `Queue::write_buffer` would make a staging buffer per write. + section_uploads: upload::Uploads, + section_copies: Option, + /// Bytes staged in `section_copies`. + section_copy_bytes: u64, /// Whether `lightmap` holds `lightmap_pixels` yet. lightmap_uploaded: bool, sprite_minimum_size: u32, @@ -1726,6 +1739,7 @@ impl Renderer { .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE); let world_post = world_post::Post::new(&device, config.format.remove_srgb_suffix()); let uploads = upload::Uploads::new(&device); + let section_uploads = upload::Uploads::with_chunk(&device, SECTION_UPLOAD_CHUNK); let panorama = panorama::Panorama::new( &device, &queue, @@ -1800,6 +1814,9 @@ impl Renderer { lightmap_sampler, animations: AnimatedSprites::new(animations, sprite_size, sprite_mips), uploads, + section_uploads, + section_copies: None, + section_copy_bytes: 0, lightmap_uploaded: false, sprite_minimum_size, indices, @@ -2004,14 +2021,20 @@ impl Renderer { self.indices = Self::quad_indices(&self.device, self.index_quads); } let vertices = (mesh.solid.len() + mesh.translucent.len()) as u64; + let encoder = self.section_copies.get_or_insert_with(|| { + self.device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("Section uploads"), + }) + }); // With video and system memory both full, the section is not drawn. - let Some((page, start)) = self.arena.alloc(&self.device, &self.queue, vertices) else { + let Some((page, start)) = self.arena.alloc(&self.device, encoder, vertices) else { return; }; let sorted_indices = if translucent > 0 { let indices = self.sorted_indices - .alloc(&self.device, &self.queue, u64::from(translucent) * 6); + .alloc(&self.device, encoder, u64::from(translucent) * 6); if indices.is_none() { self.arena.release(page, start, vertices); return; @@ -2020,22 +2043,35 @@ impl Renderer { } else { None }; - let flat_id = self - .flat - .as_mut() - .map(|f| f.add(&self.device, &self.queue, page, start, vertices)); + let flat_id = self.flat.as_mut().map(|f| { + f.add( + &self.device, + encoder, + &mut self.section_uploads, + page, + start, + vertices, + ) + }); let stride = std::mem::size_of::() as u64; for (offset, part) in [(0, &mesh.solid), (mesh.solid.len(), &mesh.translucent)] { if !part.is_empty() { let bytes: &[u8] = bytemuck::cast_slice(part); self.uploaded_bytes += bytes.len() as u64; - self.queue.write_buffer( + self.section_copy_bytes += bytes.len() as u64; + self.section_uploads.buffer( + encoder, self.arena.buffer(page), (start + offset as u64) * stride, bytes, ); } } + if self.section_copy_bytes >= SECTION_COPY_LIMIT + && let Some(copies) = self.take_section_copies() + { + self.queue.submit([copies]); + } self.sections.insert( key, SectionGpu { @@ -2057,6 +2093,13 @@ impl Renderer { }, ); } + /// The section copies recorded since the last submission, closed. + fn take_section_copies(&mut self) -> Option { + let encoder = self.section_copies.take()?; + self.section_copy_bytes = 0; + self.section_uploads.finish(&encoder); + Some(encoder.finish()) + } /// Draws `count` section draws from `first` with the arena and section /// origins bound: one indirect multi-draw, or a loop without it. /// Consecutive draws in one arena page share a vertex buffer binding, @@ -2286,6 +2329,14 @@ impl Renderer { ); } pub fn render(&mut self, frame: &Frame) -> Result { + let rendered = self.draw_frame(frame); + // A frame that was not drawn still submits the section copies. + if let Some(copies) = self.take_section_copies() { + self.queue.submit([copies]); + } + rendered + } + fn draw_frame(&mut self, frame: &Frame) -> Result { self.arena.trim(); self.sorted_indices.trim(); if let Some(flat) = &mut self.flat { @@ -3159,7 +3210,11 @@ impl Renderer { .map(|path| (path, self.copy_frame(&mut encoder, &target_texture))); self.uploads.finish(&encoder); let commands = crate::spans::time("encode", || encoder.finish()); - let submission = crate::spans::time("submit", || self.queue.submit([commands])); + // The section copies run first: this frame draws what they write. + let copies = self.take_section_copies(); + let submission = crate::spans::time("submit", || { + self.queue.submit(copies.into_iter().chain([commands])) + }); if let Some(timer) = &mut self.gpu_timer { timer.submitted(); timer.collect(&self.device, &mut self.gpu_times); diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs index 74d3b98e9..8da835aa2 100644 --- a/crates/horizon-client/src/gfx/arena.rs +++ b/crates/horizon-client/src/gfx/arena.rs @@ -155,12 +155,14 @@ impl Arena { .buffer } /// A range of `vertices`: (page, first vertex). Grows the newest page - /// (copying its contents on the GPU) or starts a new one when nothing - /// free is large enough; `None` when no memory is left for it. + /// (copying its contents on the GPU, in `encoder`) or starts a new one + /// when nothing free is large enough; `None` when no memory is left for + /// it. Writes to the arena belong in `encoder` too, so that those + /// recorded before a grow reach the grown page. pub fn alloc( &mut self, device: &wgpu::Device, - queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, vertices: u64, ) -> Option<(u32, u64)> { let len = Self::span(vertices).min(self.page_limit); @@ -178,7 +180,7 @@ impl Arena { let current = page.capacity; let grown = (current * 2).max(current + len).min(self.page_limit); if (grown - current >= len || grown == self.page_limit) - && self.grow(device, queue, last, grown) + && self.grow(device, encoder, last, grown) && let Some(start) = self.pages[last].as_mut().unwrap().free.take(len) { return Some((last as u32, start)); @@ -228,7 +230,7 @@ impl Arena { fn grow( &mut self, device: &wgpu::Device, - queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, page: usize, capacity: u64, ) -> bool { @@ -237,12 +239,8 @@ impl Arena { return false; }; let old = self.pages[page].as_mut().expect("live arena page"); - let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { - label: Some("Grow section arena"), - }); + // Copies recorded for the old buffer run before this one. encoder.copy_buffer_to_buffer(&old.buffer, 0, &buffer, 0, old.capacity * self.stride); - // Writes queued for the old buffer run before this copy. - queue.submit([encoder.finish()]); let previous = old.capacity; old.buffer = buffer; old.capacity = capacity; @@ -304,6 +302,50 @@ mod tests { .ok() } + /// `Arena::alloc`, with the copies of a grow submitted at once. + fn alloc( + arena: &mut Arena, + device: &wgpu::Device, + queue: &wgpu::Queue, + vertices: u64, + ) -> Option<(u32, u64)> { + let mut encoder = device.create_command_encoder(&Default::default()); + let range = arena.alloc(device, &mut encoder, vertices); + queue.submit([encoder.finish()]); + range + } + + #[test] + fn writes_recorded_before_a_grow_reach_the_grown_page() { + let Some((device, queue)) = device() else { + eprintln!("no GPU adapter: skipped"); + return; + }; + let mut arena = Arena::indices(&device, GRAIN, false); + let mut uploads = super::super::upload::Uploads::new(&device); + let mut encoder = device.create_command_encoder(&Default::default()); + let (page, start) = arena.alloc(&device, &mut encoder, GRAIN).unwrap(); + uploads.buffer( + &mut encoder, + arena.buffer(page), + start * 4, + &7u32.to_le_bytes(), + ); + // The page is full: the next range grows it, in the same encoder. + let (grown, next) = arena.alloc(&device, &mut encoder, GRAIN).unwrap(); + assert_eq!(grown, page); + uploads.buffer( + &mut encoder, + arena.buffer(grown), + next * 4, + &9u32.to_le_bytes(), + ); + uploads.finish(&encoder); + queue.submit([encoder.finish()]); + assert_eq!(read(&device, &queue, arena.buffer(page), start * 4), 7); + assert_eq!(read(&device, &queue, arena.buffer(page), next * 4), 9); + } + #[test] fn empty_pages_are_reclaimed_without_moving_live_ranges() { let Some((device, queue)) = device() else { @@ -313,9 +355,9 @@ mod tests { let mut arena = Arena::new(&device, 4, GRAIN); arena.page_limit = 2 * GRAIN; assert_eq!(arena.bytes(), 0); - let (first, a) = arena.alloc(&device, &queue, GRAIN).unwrap(); - let (_, b) = arena.alloc(&device, &queue, GRAIN).unwrap(); - let (live, c) = arena.alloc(&device, &queue, 2 * GRAIN).unwrap(); + let (first, a) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + let (_, b) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + let (live, c) = alloc(&mut arena, &device, &queue, 2 * GRAIN).unwrap(); let buffer = arena.buffer(live).clone(); let data = [1u8, 2, 3, 4]; queue.write_buffer(&buffer, c * 4, &data); @@ -327,7 +369,7 @@ mod tests { arena.trim(); assert_eq!(arena.bytes(), 2 * GRAIN * 4); assert_eq!(arena.buffer(live), &buffer); - let (reused, d) = arena.alloc(&device, &queue, GRAIN).unwrap(); + let (reused, d) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); assert_eq!(reused, first); let (tx, rx) = std::sync::mpsc::channel(); @@ -356,11 +398,11 @@ mod tests { Arena::new(&device, 28, GRAIN), Arena::indices(&device, GRAIN, false), ] { - arena.alloc(&device, &queue, GRAIN).unwrap(); + alloc(&mut arena, &device, &queue, GRAIN).unwrap(); assert_eq!(arena.bytes(), arena.stride * GRAIN); arena.clear(); assert_eq!(arena.bytes(), 0); - assert_eq!(arena.alloc(&device, &queue, GRAIN), Some((0, 0))); + assert_eq!(alloc(&mut arena, &device, &queue, GRAIN), Some((0, 0))); assert_eq!(arena.bytes(), arena.stride * GRAIN); } } @@ -414,12 +456,12 @@ mod tests { return; } let mut arena = Arena::indices(&device, GRAIN, false); - let (page, start) = arena.alloc(&device, &queue, GRAIN).unwrap(); + let (page, start) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); queue.write_buffer(arena.buffer(page), start * 4, &7u32.to_le_bytes()); arena.video_memory_full = true; // The full page can't grow in video memory: it moves to system // memory with its contents, and new ranges come from there. - let (grown, next) = arena.alloc(&device, &queue, GRAIN).unwrap(); + let (grown, next) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); assert!(arena.spilled); assert_eq!(grown, page); assert!( @@ -439,13 +481,13 @@ mod tests { return; }; let mut arena = Arena::indices(&device, GRAIN, false); - arena.alloc(&device, &queue, GRAIN).unwrap(); + alloc(&mut arena, &device, &queue, GRAIN).unwrap(); arena.video_memory_full = true; // Without mappable primary buffers nothing can spill: the request // fails, and the arena keeps what it has. - assert_eq!(arena.alloc(&device, &queue, GRAIN), None); + assert_eq!(alloc(&mut arena, &device, &queue, GRAIN), None); assert_eq!(arena.bytes(), GRAIN * 4); arena.release(0, 0, GRAIN); - assert_eq!(arena.alloc(&device, &queue, GRAIN), Some((0, 0))); + assert_eq!(alloc(&mut arena, &device, &queue, GRAIN), Some((0, 0))); } } diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs index b867a42c2..ff58e3ca6 100644 --- a/crates/horizon-client/src/gfx/flat.rs +++ b/crates/horizon-client/src/gfx/flat.rs @@ -262,11 +262,12 @@ impl Flat { } } /// A section uploaded to `vertices` vertices from `start` of `page`: its - /// id, which its blocks now name. + /// id, which its blocks now name (written through `uploads` in `encoder`). pub fn add( &mut self, device: &wgpu::Device, - queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + uploads: &mut super::upload::Uploads, page: u32, start: u64, vertices: u64, @@ -290,7 +291,12 @@ impl Flat { }); let first = start / GRAIN; let ids = vec![id; vertices.div_ceil(GRAIN) as usize]; - queue.write_buffer(&table.blocks, first * 4, bytemuck::cast_slice(&ids)); + uploads.buffer( + encoder, + &table.blocks, + first * 4, + bytemuck::cast_slice(&ids), + ); id } pub fn remove(&mut self, id: u32) { diff --git a/crates/horizon-client/src/gfx/upload.rs b/crates/horizon-client/src/gfx/upload.rs index d42958862..a60791d4c 100644 --- a/crates/horizon-client/src/gfx/upload.rs +++ b/crates/horizon-client/src/gfx/upload.rs @@ -14,8 +14,13 @@ pub struct Uploads { impl Uploads { pub fn new(device: &wgpu::Device) -> Self { + Self::with_chunk(device, CHUNK) + } + /// A belt whose staging buffers hold `chunk` bytes (larger writes get + /// a buffer of their own). + pub fn with_chunk(device: &wgpu::Device, chunk: u64) -> Self { Self { - belt: wgpu::util::StagingBelt::new(device.clone(), CHUNK), + belt: wgpu::util::StagingBelt::new(device.clone(), chunk), } } /// Copy tightly packed RGBA8 `pixels` (`width` by `height`) into one mip From 7ddb4ddae5433a0b474a3b55b72cab86cf8d96e1 Mon Sep 17 00:00:00 2001 From: LookImEcho-Rick <62575172+LookImEcho-Rick@users.noreply.github.com> Date: Sun, 18 Oct 2026 03:02:53 -0500 Subject: [PATCH 66/67] Fix falling block collisions with partial blocks --- crates/horizon-client/src/collisions.rs | 15 +- crates/horizon-client/src/game.rs | 208 ++++++++++++++++++------ tools/test_world_movement_parity.py | 16 +- 3 files changed, 189 insertions(+), 50 deletions(-) diff --git a/crates/horizon-client/src/collisions.rs b/crates/horizon-client/src/collisions.rs index 5d5f4c1bb..a52f25ce7 100644 --- a/crates/horizon-client/src/collisions.rs +++ b/crates/horizon-client/src/collisions.rs @@ -131,12 +131,25 @@ impl SceneCollisions<'_, '_> { movement: Vec3, max_step: f32, on_ground: bool, + ) -> Result { + self.resolve_with_context(bounds, movement, max_step, on_ground, self.context(bounds)) + } + /// Resolve movement with the entity's captured collision context. Most + /// callers only need the position context; entities such as falling blocks + /// also affect contextual block shapes (for example powder snow). + pub fn resolve_with_context( + &mut self, + bounds: Aabb, + movement: Vec3, + max_step: f32, + on_ground: bool, + context: CollisionContext<'_>, ) -> Result { Ok(resolve_world_movement( self, MovementRequest { entity: EntityIdentity(0), - context: self.context(bounds), + context, bounds, movement, max_step, diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2ac64c50a..2b4091821 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -193,6 +193,8 @@ use horizon_core::{ use horizon_core::{geometry::Vec3, random::RandomSource}; use horizon_world::{ block_collision::BlockCollisionRegistry, + collision_context::{CollisionContext, CollisionEntity, CollisionItem}, + entity_obstruction::{EntityIdentity, EntityObstructionWorld, ObstructionEntity}, level::{ Behaviours, Dimension as LevelDimension, EntityQuery, Landing, LevelAccess, LevelEvent, PlaceContext, PlaceOutcome, PlayerContext, PlayerEffect, Ticks, flags as level_flags, @@ -451,12 +453,14 @@ struct Hit { struct GameLevel<'w> { world: &'w mut World, facts: &'w BlockFacts, + collision: &'w BlockCollisionRegistry<'w>, events: &'w mut Vec, dimension: LevelDimension, darken: i32, raining: bool, thundering: bool, player_box: Option<([f64; 3], [f64; 3])>, + player_collision: Option, /// Minecart boxes (detector rails). cart_boxes: Vec<([f64; 3], [f64; 3])>, biomes: &'w Biomes, @@ -472,6 +476,32 @@ struct GameLevel<'w> { pending: &'w mut spill::Pending, spill: &'w mut spill::Spill, } +struct PlayerCollision { + y: f64, + descending: bool, + fall_distance: f64, + main_hand_item: Arc, + main_hand_count: i32, + leather_boots: bool, +} +struct PlayerObstruction(Aabb); +impl EntityObstructionWorld for PlayerObstruction { + type Error = std::convert::Infallible; + fn obstruction_candidates( + &mut self, + _excluded: Option, + _bounds: Aabb, + ) -> std::result::Result, Self::Error> { + Ok(vec![ObstructionEntity { + identity: EntityIdentity(0), + root_vehicle: EntityIdentity(0), + bounds: self.0, + removed: false, + spectator: false, + blocks_building: true, + }]) + } +} impl LevelAccess for GameLevel<'_> { fn precipitation(&self, pos: BlockPos) -> Option { self.behaviours.precipitation_info(self, pos) @@ -584,21 +614,63 @@ impl LevelAccess for GameLevel<'_> { fn dimension(&self) -> LevelDimension { self.dimension } - /// Level.isUnobstructed against the player for full-collision blocks. + /// `Level.isUnobstructed`: the placed state's collision shape against the player. fn is_unobstructed(&self, pos: BlockPos, state: BlockStateId) -> bool { let Some((lo, hi)) = self.player_box else { return true; }; - if !self.facts.get(state).full_collision { + let player = self.player_collision.as_ref(); + let context = player.map_or_else( + || CollisionContext::position(lo[1]), + |player| { + CollisionContext::for_entity( + CollisionEntity { + identity: EntityIdentity(0), + y: player.y, + descending: player.descending, + main_hand: Some(CollisionItem { + item: &player.main_hand_item, + count: player.main_hand_count, + }), + }, + false, + ) + }, + ); + let mut scene = collisions::SceneCollisions { + world: &*self.world, + binding: self.collision, + subject: player.map(|player| horizon_client::collisions::PowderSnowSubject { + fall_distance: player.fall_distance, + falling_block: false, + leather_boots: player.leather_boots, + walkable_type: false, + lava_walker: false, + }), + }; + let Ok(shape) = self + .collision + .collision_shape_with_context(&mut scene, state, pos, &context) + else { + return false; + }; + if shape.is_empty() { return true; } - let (x, y, z) = (f64::from(pos.x), f64::from(pos.y), f64::from(pos.z)); - !(x < hi[0] - 1e-7 - && x + 1.0 > lo[0] + 1e-7 - && y < hi[1] - 1e-7 - && y + 1.0 > lo[1] + 1e-7 - && z < hi[2] - 1e-7 - && z + 1.0 > lo[2] + 1e-7) + let shape = shape.moved(horizon_core::geometry::Vec3::new( + f64::from(pos.x), + f64::from(pos.y), + f64::from(pos.z), + )); + horizon_world::entity_obstruction::is_unobstructed( + &mut PlayerObstruction(Aabb::new( + horizon_core::geometry::Vec3::new(lo[0], lo[1], lo[2]), + horizon_core::geometry::Vec3::new(hi[0], hi[1], hi[2]), + )), + None, + &shape, + ) + .unwrap_or(false) } /// Entities that trigger pressure plates, tripwires and detector rails: /// the player (a mob) and the minecarts. @@ -1769,6 +1841,22 @@ impl Game<'_> { ], ) }); + let player_collision = self.player.as_ref().filter(|_| !spectator).map(|p| { + let main_hand = self.inventory.get(self.selected).and_then(Option::as_ref); + PlayerCollision { + y: p.position.y, + descending: p.velocity.y < 0.0, + fall_distance: p.fall_distance, + main_hand_item: main_hand.map_or_else( + || Arc::from("minecraft:air"), + |stack| Arc::clone(&stack.item), + ), + main_hand_count: main_hand.map_or(0, |stack| stack.count as i32), + leather_boots: self.armor[3] + .as_ref() + .is_some_and(|stack| stack.item.as_ref() == "minecraft:leather_boots"), + } + }); let cart_boxes = self .carts .iter() @@ -1790,12 +1878,14 @@ impl Game<'_> { cart_boxes, world: &mut self.world, facts: &self.res.facts, + collision: self.collision, events: &mut self.level_events, dimension, darken, raining: rain > 0.2, thundering: thunder > 0.9, player_box, + player_collision, biomes: &self.res.biomes, seed: self.level.seed, behaviours: &self.res.behaviours, @@ -2297,65 +2387,87 @@ impl Game<'_> { let mut falling = std::mem::take(&mut self.falling); let mut landed = Vec::new(); let mut dropped = Vec::new(); + let mut collision_error = None; let entity_drops = self.ui.rules.bool("entity_drops"); falling.retain_mut(|f| { + let original = f.clone(); f.previous = f.position; f.age = f.age.wrapping_add(1); if !f.no_gravity { f.velocity.y -= 0.04; } + let wanted = f.velocity; + let mut scene = collisions::SceneCollisions { + world: &self.world, + binding: self.collision, + subject: Some(collisions::PowderSnowSubject { + fall_distance: f.fall_distance, + falling_block: true, + ..Default::default() + }), + }; + let context = CollisionContext::for_entity( + CollisionEntity { + identity: EntityIdentity(0), + y: f.position.y, + descending: wanted.y < 0.0, + main_hand: None, + }, + false, + ); + let movement = match scene.resolve_with_context( + horizon_client::falling_block::bounds(f.position), + wanted, + 0.0, + false, + context, + ) { + Ok(movement) => movement, + Err(error) => { + *f = original; + collision_error.get_or_insert(error); + return true; + } + }; let next = Vec3::new( - f.position.x + f.velocity.x, - f.position.y + f.velocity.y, - f.position.z + f.velocity.z, + f.position.x + movement.x, + f.position.y + movement.y, + f.position.z + movement.z, ); - let below = self.world.block( - next.x.floor() as i32, - next.y.floor() as i32, - next.z.floor() as i32, - ); - let below_facts = self.res.facts.get(below); - // PowderSnowBlock.getCollisionShape for a FallingBlockEntity: the - // whole block, or 0.9 high after a fall of more than 2.5 blocks, - // where the block it rests in is the snow itself. - let powder_snow = &*below_facts.name == "minecraft:powder_snow"; - let sunk = powder_snow - && f.fall_distance > 2.5 - && next.y - next.y.floor() < f64::from(0.9_f32); - let solid = - (below_facts.full_collision || powder_snow && f.fall_distance <= 2.5 || sunk) - && f.velocity.y < 0.0; + let on_ground = wanted.y < 0.0 && movement.y != wanted.y; // FallingBlockEntity.tick: off the ground, it gives up after 600 // ticks (100 outside the world) and drops itself as an item. let y = next.y.floor() as i32; let outside = y <= horizon_client::chunk::MIN_Y || y >= horizon_client::chunk::MIN_Y + 384; - if !solid && (f.age > 100 && outside || f.age > 600) { + if !on_ground && (f.age > 100 && outside || f.age > 600) { if f.drop_item && entity_drops { dropped.push((f.state, next)); } return false; } - if solid { - let pos = [ - next.x.floor() as i32, - next.y.floor() as i32 + i32::from(!sunk), - next.z.floor() as i32, - ]; - let landing_y = if sunk { - next.y.floor() + f64::from(0.9_f32) - } else { - f64::from(pos[1]) - }; - f.fall_distance += (f.position.y - landing_y).max(0.0); - f.position = Vec3::new(next.x, landing_y, next.z); - landed.push((f.clone(), pos)); - return false; - } if next.y < f.position.y { f.fall_distance += f.position.y - next.y; } f.position = next; + if on_ground { + let pos = [ + f.position.x.floor() as i32, + f.position.y.floor() as i32, + f.position.z.floor() as i32, + ]; + landed.push((f.clone(), pos)); + return false; + } + if movement.x != wanted.x { + f.velocity.x = 0.0; + } + if movement.y != wanted.y { + f.velocity.y = 0.0; + } + if movement.z != wanted.z { + f.velocity.z = 0.0; + } f.velocity = Vec3::new( f.velocity.x * 0.98, f.velocity.y * 0.98, @@ -2386,7 +2498,11 @@ impl Game<'_> { for (block, pos) in landed { self.finish_falling_block(block, pos); } - self.process_level_events() + self.process_level_events()?; + if let Some(error) = collision_error { + return Err(error); + } + Ok(()) } fn break_block(&mut self, pos: [i32; 3]) -> Result<()> { self.with_player_actor(|game| game.break_block_as_player(pos)) diff --git a/tools/test_world_movement_parity.py b/tools/test_world_movement_parity.py index 8cffb456c..e01a989f5 100644 --- a/tools/test_world_movement_parity.py +++ b/tools/test_world_movement_parity.py @@ -18,18 +18,28 @@ except ImportError: def corpus(facts): def bits(v):return struct.pack('>d',float(v)).hex() - def row(state,delta=(1,-0.08,0),step=0.6,grounded=True,context=2,offset=0,fail=-1,extra=(),entities=(),border=None): + def row(state,delta=(1,-0.08,0),step=0.6,grounded=True,context=2,offset=0,fail=-1,extra=(),entities=(),border=None,box=None): blocks=[[x+offset,-1,z,facts['defaults']['minecraft:stone']] for x in range(-2,4) for z in range(-2,3)] blocks.append([offset,0,0,state]);blocks.extend(extra) return dict(chunks=[[x,z] for x in range((offset>>4)-1,(offset>>4)+2) for z in range(-1,2)], blocks=blocks,context=context,y=bits(0),entities=[0,0],fail=fail,limit=100000,suffocating=False, - box=list(map(bits,[-0.6+offset,0,0.2,-0.2+offset,1.8,0.8])), + box=list(map(bits,box if box is not None else [-0.6+offset,0,0.2,-0.2+offset,1.8,0.8])), movement=dict(delta=list(map(bits,delta)),step=bits(step),grounded=grounded, entities=[list(map(bits,b)) for b in entities],border=None if border is None else list(map(bits,border)))) states=facts['states'] for state in range(len(states)): yield row(state,context=state%4) defaults=facts['defaults'] + falling_surfaces=[('minecraft:stone_slab',0.5),('minecraft:oak_stairs',1.0),('minecraft:snow',0.125), + ('minecraft:oak_fence',1.5),('minecraft:iron_bars',1.0),('minecraft:soul_sand',0.875), + ('minecraft:farmland',0.9375)] + for block_id,top in falling_surfaces: + state=defaults[block_id] + size=struct.unpack('>f',struct.pack('>f',0.98))[0] + half=size/2 + bottom=top+0.1 + box=[0.5-half,bottom,0.5-half,0.5+half,bottom+size,0.5+half] + yield row(state,delta=(0,-0.25,0),step=0,grounded=False,context=2,box=box) obstacles=[i for i,s in enumerate(states) if s['id'] in ['minecraft:oak_stairs','minecraft:stone_slab', 'minecraft:snow','minecraft:scaffolding','minecraft:powder_snow','minecraft:water', 'minecraft:shulker_box','minecraft:moving_piston','minecraft:piston','minecraft:soul_sand', @@ -114,7 +124,7 @@ def main(): 'definition_counts':dict(collections.Counter(definitions.values())), 'native_chunk_storage':'ProtoChunk', 'rust_chunk_storage':'TerrainChunk', 'fixtures':count, 'sources_sha256':tested, 'fixtures_sha256':fixtures, 'rust_output_sha256':lab.digest(build/'rust.txt', 'sha256'), 'references':references, - 'scope':'Actual native Entity.collide executes initial collision and step-up decisions over BlockCollisions, registered block shapes and resident ProtoChunk sections. Rust combines owned/fallible step resolution with registry-bound TerrainChunk scanning. All registered states are used as obstacles, plus step heights, falling/grounded motion, zero and underflow motion, partial blocks, fluids, dynamic blocks, ceilings, chunk boundaries and query failures. Entity collider lists and world-border proximity/shape are controlled inputs; query order and reuse, shape collection and resulting displacement are compared. Contexts are deliberately supplied empty/position/placement snapshots in both worlds. This does not establish entity broad-phase selection, actual world-border proximity, travel/gravity/velocity or grounded side effects, experimental minecarts or a playable game.'} + 'scope':'Actual native Entity.collide executes initial collision and step-up decisions over BlockCollisions, registered block shapes and resident ProtoChunk sections. Rust combines owned/fallible step resolution with registry-bound TerrainChunk scanning. All registered states are used as obstacles, plus step heights, falling/grounded motion, zero and underflow motion, partial blocks including exact falling-block-sized boxes over slabs, stairs, snow, fences, iron bars, soul sand and farmland, fluids, dynamic blocks, ceilings, chunk boundaries and query failures. Entity collider lists and world-border proximity/shape are controlled inputs; query order and reuse, shape collection and resulting displacement are compared. Contexts are deliberately supplied empty/position/placement snapshots in both worlds. This does not establish entity broad-phase selection, actual world-border proximity, travel/gravity/velocity or grounded side effects, experimental minecarts or a playable game.'} lab.write_json(report_path, report);print(f'Scoped world movement parity passed: {report_path}', flush=True) From 9dd896bfc30d26c69c83adafb2cb018c2800bdec Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 10:10:57 +0200 Subject: [PATCH 67/67] tooltip_display harness: write the case flags in lowercase Rust's bool parser takes only true/false, so the fixture failed on Python's True/False; Java's Boolean.parseBoolean ignores case. Co-Authored-By: Claude Opus 5.5 --- tools/test_tooltip_display_parity.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/test_tooltip_display_parity.py b/tools/test_tooltip_display_parity.py index e98807d1b..22b485320 100644 --- a/tools/test_tooltip_display_parity.py +++ b/tools/test_tooltip_display_parity.py @@ -16,7 +16,8 @@ def cases(build): (True, True, True), ] path = build / 'cases.txt' - path.write_text('\n'.join('\t'.join(map(str, row)) for row in values) + '\n') + # Lowercase: Rust's bool parser takes only true/false (Java's parseBoolean ignores case). + path.write_text('\n'.join('\t'.join(str(v).lower() for v in row) for row in values) + '\n') return path, len(values)