diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a4e1fb7b4..cdee664fa 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3730,6 +3730,10 @@ impl Game<'_> { } self.audio .play("minecraft:entity.player.death", Some(at), 1.0, 1.0); + // LivingEntity.die: the LAN guests see the host's death animation. + if let Some(host) = &self.lan { + host.hub.entity_event(host.host_entity, 3, feet); + } self.screen = Screen::Dead; self.capture(false); } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 1af69959f..43610dd96 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -3773,6 +3773,13 @@ impl Game<'_> { .filter(|t| t.kind == "minecraft:player") .map(|t| t.position) .collect::>(), + // The other players this client draws dead (lying down, after EntityEvent DEATH). + "dead_players": self + .mobs + .iter() + .filter(|m| r.level_players.contains_key(&(m.id as i32)) && !m.alive()) + .map(|m| m.id) + .collect::>(), // The player's own model exists (third person and the inventory draw it). "player_model": self.player_model.is_some(), "health": r.health.0, diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index 365dd5992..b6f17d70e 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -288,6 +288,11 @@ impl Game<'_> { if hit == Some(0) && self.remote.is_none() { let keep = self.ui.rules.bool("keep_inventory") || self.spectator(); self.experience.respawn(keep); + // The guests are sent the respawned host as a new entity, not the dead body. + if let Some(host) = self.lan.as_mut() { + let id = host.host_entity; + host.hub.respawned(id); + } // Autosaves keep a login position; death must resolve the respawn point instead. self.level.position = None; } diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index 3ac18e708..6350cf6f9 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -469,6 +469,11 @@ impl Tracker { pub fn clear(&mut self) { self.seen.clear(); } + /// Stop tracking `id` without a packet: the caller removes it from the + /// client, and the next tick pairs it again if it is still in range. + pub fn forget(&mut self, id: i32) -> bool { + self.seen.remove(&id) + } pub fn len(&self) -> usize { self.seen.len() } @@ -1021,4 +1026,28 @@ mod tests { assert!(!t.is_tracking(7) && t.is_tracking(8) && t.is_tracking(9)); } } + + #[test] + fn a_forgotten_entity_is_paired_again_at_the_next_tick() { + // A respawned player: the hub removes it from the other clients and + // forgets it, and the next tick pairs it again as a new entity. + let mut level = Entities::default(); + let mut c = Clients::new(); + let mut player = state(0.0); + player.id = 1; + player.kind = "minecraft:player".into(); + c.tick(&mut level, &[&player]); + c.tick(&mut level, &[&player]); + assert!(c.trackers[1].forget(1)); + assert!(!c.trackers[1].forget(1)); + c.tick(&mut level, &[&player]); + assert!(c.trackers[1].is_tracking(1)); + let paired = c.out[1] + .first() + .and_then(|p| play::Clientbound::decode(i32::from(p[0]), &p[1..]).ok()); + assert!( + matches!(paired, Some(play::Clientbound::AddEntity(ref a)) if a.id == 1), + "{paired:?}" + ); + } } diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index a0b139e8b..9ec3eca2d 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -2144,6 +2144,18 @@ impl Hub { .map(str::to_owned) } + /// ServerLevel.removePlayerImmediately, then addRespawnedPlayer: the other players + /// lose the dead player's entity (ServerEntity.removePairing) and are sent the + /// respawned one as a new entity at the next tick. Their clients would otherwise keep + /// the dead body, lying down, wherever the player goes. + pub fn respawned(&mut self, id: i32) { + for g in &mut self.guests { + if g.entity_id != id && g.tracker.forget(id) { + g.send(&C::RemoveEntities(play::RemoveEntities { ids: vec![id] })); + } + } + } + /// PlayerList.respawn: back to `at` with a fresh start. The vanilla client waits on its /// level loading screen from the Respawn packet until the level info's "level chunks load /// start" event and the chunk under the player, so the packets follow vanilla's order. @@ -2170,6 +2182,9 @@ impl Hub { g.tracker.clear(); g.sent_chunks.clear(); g.chunk_queue.clear(); + let id = g.entity_id; + self.respawned(id); + let g = &mut self.guests[i]; // ServerPlayer.restoreFrom: the game mode again (onUpdateAbilities). g.send(&C::PlayerAbilities(play::PlayerAbilities { flags: g.ability_flags(), diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 4d13b1c22..44f974a0a 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -19,6 +19,9 @@ address. The script then checks, from both sides, what a guest's game used to ge - hazards: a guest in survival gets hungry sprint-jumping and is hurt by lava, a block at the eyes, drowning and the void (then respawns); - pvp: the guest and the host hit each other with a sword; with the pvp game rule off, nothing; +- respawn: a player killed is drawn dead by the other side, and alive again once it respawns (the + respawned player is a new entity to the others); the host dies, and on the server thread + the guest too; - 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; @@ -84,7 +87,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', 'light', 'effects', 'portals', 'distance'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'respawn', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'light', 'effects', 'portals', 'distance'] class Game: @@ -575,6 +578,45 @@ def check_pvp(host, guest, out): return hits +def dead_players(game, want, ticks=60): + """The other players `game`'s client draws dead, once that is `want` (empty or not) or after `ticks`.""" + for _ in range(ticks // 5): + dead = remote(game)['dead_players'] + if bool(dead) == want: + return dead + game.wait(5) + return dead + + +def check_respawn(host, guest, out): + # ServerLevel.removePlayerImmediately and addRespawnedPlayer: a respawned player is removed + # from the other players and paired again as a new entity, so their clients drop the body. + # The host window only has a client's view of the guest on the server thread. + for g in (host, guest): + back_to_game(g) + results = {} + sides = [('host', host, guest)] + ([('guest', guest, host)] if SERVER_THREAD else []) + for name, dying, watching in sides: + face_each_other(host, guest) + dying('command', command='kill @s') + dying.wait(30) + dead = dead_players(watching, True) + watching.shot(out, f'respawn-{name}-dead') + dying.raw('respawn') + dying.wait(40) + back_to_game(dying) + # Back within tracking range of the other player, wherever the respawn point is. + x, y, z = pos_of(dying) + watching('command', command=f'tp @s {x + 3:.2f} {y:.2f} {z:.2f}') + watching.wait(40) + alive = dead_players(watching, False) + watching.shot(out, f'respawn-{name}-alive') + results[name] = {'dead': dead, 'after_respawn': alive} + assert dead, f'the {name} was not drawn dead: {results}' + assert not alive, f'the {name} still lies dead after respawning: {results}' + return results + + def container_items(game): """The open container's stacks by slot (Horizon's container slots are 300 and up) and its size.""" slots = [s for s in game('screen').get('slots', []) if s.get('slot', 0) >= 300] @@ -1272,6 +1314,7 @@ def check_light(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, + 'respawn': check_respawn, 'containers': check_containers, 'feedback': check_feedback, 'titles': check_titles, 'commands': check_commands, 'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls,