From 74fafdd787df17ff751b3dea322592d50cb3f960 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 09:52:54 +0200 Subject: [PATCH] LAN: trial spawners and vaults see the guests PlayerDetector reads level.getPlayers(): the guests count for a trial spawner's detection and a vault's connected players, and a key a guest inserts rewards the guest's UUID. Part of #462 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 3 + crates/horizon-client/src/game/lan_actions.rs | 2 + crates/horizon-client/src/game/lan_mobs.rs | 41 ++++++++++++++ .../horizon-client/src/game/trial_spawner.rs | 21 +++++-- crates/horizon-client/src/game/vault.rs | 15 +++-- tools/lan_e2e.py | 55 ++++++++++++++++++- 6 files changed, 126 insertions(+), 11 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 005d40b86..f5d3d1435 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1032,6 +1032,8 @@ struct Game<'a> { lan_acting: Option, /// The name of the guest `lan_acting` acts for (`Player.getName`). lan_acting_name: Option, + /// Their UUID (`Player.getUUID`). + lan_acting_uuid: Option, /// Command feedback to forward to a guest. chat_capture: Option>, /// State of `/spectate`, `/posteffect`, `/debug`, `/waypoint` and friends. @@ -10182,6 +10184,7 @@ pub fn run() -> Result<()> { lid_openers: HashMap::new(), lan_acting: None, lan_acting_name: None, + lan_acting_uuid: None, chat_capture: None, cmds: Default::default(), ui: Default::default(), diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 53d4a5906..149e73791 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -155,10 +155,12 @@ impl Game<'_> { std::mem::swap(&mut self.ender_items, &mut g.ender_items); self.lan_acting = Some(conn); self.lan_acting_name = Some(g.name.clone()); + self.lan_acting_uuid = Some(g.uuid); let result = f(self); self.lan_acting = None; self.lan_acting_name = None; + self.lan_acting_uuid = None; // An ender chest the guest opened: the menu shows their inventory // itself (lan_open_container), not a stand-in block entity. self.close_ender_chest(); diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 67472bb1f..7cef7cda6 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -194,6 +194,47 @@ impl Game<'_> { } } +/// A guest as the blocks that look for players see them +/// (`PlayerDetector`, `level.getPlayers`: the dead ones too). +pub(super) struct Present { + pub uuid: [i32; 4], + pub position: [f64; 3], + pub eye: [f64; 3], + pub height: f32, + pub creative: bool, + pub spectator: bool, + pub alive: bool, +} + +impl Game<'_> { + /// Every guest in the world (`ServerLevel.getPlayers` past the host). + pub(super) fn lan_present(&self) -> Vec { + let guests = self.lan.iter().flat_map(|h| &h.hub.guests); + guests + .filter(|g| g.placed && g.loaded) + .map(|g| { + let [x, y, z] = g.position; + Present { + uuid: horizon_client::lan::playerdata::uuid_ints(g.uuid), + position: g.position, + eye: [x, y + g.eye_height(), z], + height: if g.sneaking { 1.5 } else { 1.8 }, + creative: g.creative(), + spectator: g.spectator(), + alive: !g.dead, + } + }) + .collect() + } + /// The UUID of the player acting: the guest `as_guest` runs, else the host. + pub(super) fn acting_uuid(&self) -> [i32; 4] { + self.lan_acting_uuid + .map(horizon_client::lan::playerdata::uuid_ints) + .or(self.level.player_uuid) + .unwrap_or_default() + } +} + /// A guest's bounding box (0.6 wide, 1.8 tall, 1.5 crouching). pub(super) fn guest_bounds(g: &horizon_client::lan::guest::Guest) -> Aabb { let [x, y, z] = g.position; diff --git a/crates/horizon-client/src/game/trial_spawner.rs b/crates/horizon-client/src/game/trial_spawner.rs index 7b0b9dcda..5f5289a5b 100644 --- a/crates/horizon-client/src/game/trial_spawner.rs +++ b/crates/horizon-client/src/game/trial_spawner.rs @@ -32,11 +32,23 @@ impl TrialSpawnerLevel for GameLevel<'_, '_> { self.game.ui.rules.bool("spawn_mobs") } fn players(&self) -> Vec { + let guests = self.game.lan_present().into_iter().map(|g| TrialPlayer { + uuid: g.uuid, + block: g.position.map(|v| v.floor() as i32), + eye: g.eye, + creative: g.creative, + spectator: g.spectator, + position: g.position, + height: g.height, + alive: g.alive, + // Guests carry no effects yet. + omen: None, + }); let (Some(p), Some(view)) = (self.game.player.as_ref(), self.game.player_view()) else { - return Vec::new(); + return guests.collect(); }; - vec![TrialPlayer { - uuid: self.game.level.player_uuid.unwrap_or_default(), + let host = TrialPlayer { + uuid: self.game.acting_uuid(), block: [p.position.x, p.position.y, p.position.z].map(|v| v.floor() as i32), eye: view.eye, creative: self.game.creative, @@ -49,7 +61,8 @@ impl TrialSpawnerLevel for GameLevel<'_, '_> { } else { self.game.potions.effects.get("bad_omen").map(|_| Omen::Bad) }, - }] + }; + std::iter::once(host).chain(guests).collect() } fn visual_hit(&self, from: [f64; 3], to: [f64; 3]) -> Option<[i32; 3]> { let v = |p: [f64; 3]| Vec3::new(p[0], p[1], p[2]); diff --git a/crates/horizon-client/src/game/vault.rs b/crates/horizon-client/src/game/vault.rs index 35330ab9b..dca31ddab 100644 --- a/crates/horizon-client/src/game/vault.rs +++ b/crates/horizon-client/src/game/vault.rs @@ -17,7 +17,7 @@ use horizon_core::random::LegacyRandom; type Pos = (i32, i32, i32); -/// What a vault reads: the game time and the player. +/// What a vault reads: the game time and the players. struct Players { time: i64, players: Vec, @@ -33,14 +33,19 @@ impl VaultLevel for Players { impl Game<'_> { fn vault_players(&self) -> Players { - let players = self.player.as_ref().map(|p| VaultPlayer { - uuid: self.level.player_uuid.unwrap_or_default(), + let host = self.player.as_ref().map(|p| VaultPlayer { + uuid: self.acting_uuid(), block: [p.position.x, p.position.y, p.position.z].map(|v| v.floor() as i32), spectator: self.spectator(), }); + let guests = self.lan_present().into_iter().map(|g| VaultPlayer { + uuid: g.uuid, + block: g.position.map(|v| v.floor() as i32), + spectator: g.spectator, + }); Players { time: self.world.game_time, - players: players.into_iter().collect(), + players: host.into_iter().chain(guests).collect(), } } /// The vault at `key` with its block's state and ominous flag. @@ -143,7 +148,7 @@ impl Game<'_> { let Some(stack) = held.cloned() else { return Ok(false); }; - let player = self.level.player_uuid.unwrap_or_default(); + let player = self.acting_uuid(); let used = self.with_vault( (pos[0], pos[1], pos[2]), |v, l, random, state, ominous, roll, events| { diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 6f0a79b81..2ed43fe01 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -34,6 +34,7 @@ macOS both games render offscreen. Games started here have no audio (HORIZON_NO_ import argparse import json import os +import re import shutil import subprocess import sys @@ -46,7 +47,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', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials'] class Game: @@ -473,10 +474,60 @@ def check_feedback(host, guest, out): return chat +def state_of(game, pos, key): + state = game.raw('block', *pos)['state'] + return dict(kv.split('=') for kv in state.partition('[')[2].rstrip(']').split(',') if kv).get(key) + + +def wait_state(game, pos, key, want, ticks): + for _ in range(ticks // 10): + if state_of(game, pos, key) == want: + return want + game.wait(10) + return state_of(game, pos, key) + + +def check_trials(host, guest, out): + # The host watches as a spectator, whom neither block detects: only the guest can wake them. + host('command', command='gamemode spectator') + host('command', command='difficulty normal') + host('command', command='gamerule spawn_mobs true') + guest('command', command='gamemode survival') + x, y, z = (int(v // 1) for v in pos_of(guest)) + spawner, vault = (x, y, z + 3), (x, y, z - 2) + try: + guest('command', command=f'setblock {spawner[0]} {spawner[1]} {spawner[2]} minecraft:trial_spawner' + '{spawn_data:{entity:{id:"minecraft:zombie"}},required_player_range:6}') + guest('command', command=f'setblock {vault[0]} {vault[1]} {vault[2]} minecraft:vault' + '{config:{key_item:{id:"minecraft:trial_key",count:1}}}') + trial = wait_state(host, spawner, 'trial_spawner_state', 'active', 100) + active = wait_state(host, vault, 'vault_state', 'active', 60) + host.shot(out, 'trials-active') + guest('command', command='item replace entity @s weapon.mainhand with minecraft:trial_key') + guest.wait(5) + guest.raw('interact', *vault) + unlocking = wait_state(host, vault, 'vault_state', 'unlocking', 20) + guest.shot(out, 'trials-unlocking') + rewarded = host('command', command=f'data get block {vault[0]} {vault[1]} {vault[2]} server_data.rewarded_players') + host_uuid = host('command', command='data get entity @s UUID') + finally: + host('command', command='gamerule spawn_mobs false') + host('command', command='kill @e[type=minecraft:zombie]') + host('command', command='gamemode survival') + guest('command', command=f'setblock {spawner[0]} {spawner[1]} {spawner[2]} minecraft:air') + guest('command', command=f'setblock {vault[0]} {vault[1]} {vault[2]} minecraft:air') + uuid = lambda reply: re.findall(r'-?\d+', json.dumps(reply).partition('[I;')[2].partition(']')[0]) + assert trial == 'active', f'the trial spawner never saw the guest: {trial}' + assert active == 'active', f'the vault never saw the guest: {active}' + assert unlocking == 'unlocking', f'the guest\'s key did not unlock the vault: {unlocking}' + assert uuid(rewarded) and uuid(rewarded) != uuid(host_uuid), f'rewarded {rewarded}, host {host_uuid}' + return {'spawner': trial, 'vault': active, 'rewarded': uuid(rewarded), 'host': uuid(host_uuid)} + + 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} + 'containers': check_containers, 'feedback': check_feedback, 'trials': check_trials} def run(ctl, out, resources, checks):