diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index f101f53cd..e59ffd867 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -708,6 +708,13 @@ impl Audio { let seed = self.random.next_long(); self.play_seeded(event, at, volume, pitch, seed); } + /// `play` for this client only: a sound a packet's handler plays + /// (`playLocalSound`), which the guests play from that packet themselves. + pub fn play_unshared(&mut self, event: &str, at: Option<[f64; 3]>, volume: f32, pitch: f32) { + let capture = self.capture.take(); + self.play(event, at, volume, pitch); + self.capture = capture; + } /// `LivingEntity.handleDamageEvent`'s pitch, `(r - r) * 0.2 + 1`, drawn on the /// client (the entity's own random there, never the server's world random). pub fn hurt_pitch(&mut self) -> f32 { diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 01621b35b..95aca4ecb 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3968,7 +3968,7 @@ impl Game<'_> { if self.use_delay > 0 { self.use_delay -= 1; } - self.apply_totem(); + self.tick_totem(); self.update_totem_ready(); self.tick_command_extras(); self.tick_mods(); @@ -4247,7 +4247,7 @@ impl Game<'_> { } } self.powder_snow_fire()?; - self.apply_totem(); + self.tick_totem(); let Some(player) = self.player.as_mut() else { return Ok(()); }; diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 19c3a9a6c..d3cfd3766 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -361,6 +361,7 @@ impl Game<'_> { self.apply_equipment(); play.equipped = Some(equipped); } + self.update_totem_ready(); Some(GuestSwap { conn, i, @@ -414,6 +415,8 @@ impl Game<'_> { /// The second half of `as_guest`: the guest's state back into their /// record, the game's back in place, and what the turn changed sent. pub(super) fn leave_guest(&mut self, host: &mut LanHost, swap: GuestSwap) { + // A totem that saved the guest this turn (its hurt set it off). + let totem = self.apply_totem(); let GuestSwap { conn, i, @@ -624,6 +627,16 @@ impl Game<'_> { let conn = g.conn; host.hub.set_game_mode(conn, new_mode); } + // LivingEntity.checkTotemDeathProtection's entity event, to every + // client around, the guest's own included; this window draws it too. + if let Some(body) = totem { + let id = host.hub.guests[i].entity_id; + host.hub + .entity_event(id, super::totem::TOTEM_EVENT, body.at); + if self.player.is_some() { + self.totem_event(body, false); + } + } host.play.insert(conn, play); self.stat_criteria_changed(&name, stat_events); if let Some(pos) = opened diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index dfde08d7f..156fbe13b 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2131,6 +2131,9 @@ impl Game<'_> { { m.health = 0.0; } + if p.event == super::totem::TOTEM_EVENT { + self.remote_totem(p.id); + } } C::Animate(p) => self.remote_animate(p.id, p.action), // ClientPacketListener.handleSwingAnimation: the entity swings diff --git a/crates/horizon-client/src/game/totem.rs b/crates/horizon-client/src/game/totem.rs index 47b67d9f8..e26d66a1a 100644 --- a/crates/horizon-client/src/game/totem.rs +++ b/crates/horizon-client/src/game/totem.rs @@ -8,8 +8,12 @@ use horizon_client::{ const TOTEM: &str = "minecraft:totem_of_undying"; +/// `EntityEvent.TOTEM_OF_UNDYING`. +pub(super) const TOTEM_EVENT: i8 = 35; + impl Game<'_> { - /// Whether either hand holds a totem, for the next hurt. + /// Whether either hand holds a totem, for the next hurt: the window's + /// player once a tick, a server's player for each of its turns. pub(super) fn update_totem_ready(&mut self) { let ready = self.held().is_some_and(|s| &*s.item == TOTEM) || self.offhand.as_ref().is_some_and(|s| &*s.item == TOTEM); @@ -17,19 +21,21 @@ impl Game<'_> { p.health.totem_ready = ready; } } - /// A totem fired: use it up (main hand first), set health to 1, clear and - /// apply its effects, then the sound and particles. - pub(super) fn apply_totem(&mut self) { - let Some(p) = self.player.as_mut() else { - return; - }; + /// The server's half once a totem fired: use it up (main hand first), + /// count it, set health to 1, then clear and apply its effects. Returns + /// the player's body, for entity event 35, which the caller broadcasts. + pub(super) fn apply_totem(&mut self) -> Option { + let p = self.player.as_mut()?; if !p.health.totem_used { - return; + return None; } p.health.totem_used = false; let _ = p.set_health(1.0, false); - let at = [p.position.x, p.position.y, p.position.z]; - let height = f64::from(p.dimensions.height); + let body = Body { + at: [p.position.x, p.position.y, p.position.z], + width: f64::from(p.dimensions.width), + height: f64::from(p.dimensions.height), + }; let slot = if self.held().is_some_and(|s| &*s.item == TOTEM) { Some(self.selected) } else { @@ -45,6 +51,14 @@ impl Game<'_> { *cell = None; } } + self.stats.add("used", TOTEM, 1); + self.trigger_item( + "minecraft:used_totem", + &horizon_client::items::ItemStack::new(TOTEM, 1), + ); + // ItemStack.causeUseVibration: the totem's use effects make one. + let ctx = self.player_context(None); + self.post_game_event("minecraft:item_interact_finish", body.at, ctx); // DeathProtection.TOTEM_EFFECTS: clear, then regeneration II, absorption II, fire resistance. self.potions.effects.clear(); for (id, ticks, amp) in [ @@ -56,19 +70,64 @@ impl Game<'_> { self.add_player_effect(EffectInstance::new(e.id, ticks, amp)); } } - self.audio - .play("minecraft:item.totem.use", Some(at), 1.0, 1.0); - self.trigger_item( - "minecraft:used_totem", - &horizon_client::items::ItemStack::new("minecraft:totem_of_undying", 1), - ); + Some(body) + } + /// The window's player's totem: the server's half, then the event to + /// the guests and to this window's own client. + pub(super) fn tick_totem(&mut self) { + let Some(body) = self.apply_totem() else { + return; + }; + if let Some(host) = &self.lan { + host.hub + .entity_event(host.host_entity, TOTEM_EVENT, body.at); + } + self.totem_event(body, true); + } + /// Entity event 35 from a server, for this client's own player or an + /// entity it draws. + pub(super) fn remote_totem(&mut self, id: i32) { + let own = self.remote.as_ref().is_some_and(|r| r.entity_id == id); + let body = if own { + self.player.as_ref().map(|p| Body { + at: [p.position.x, p.position.y, p.position.z], + width: f64::from(p.dimensions.width), + height: f64::from(p.dimensions.height), + }) + } else { + self.mobs.iter().find(|m| m.id == id as u32).map(|m| Body { + at: [m.position.x, m.position.y, m.position.z], + width: f64::from(m.width()), + height: f64::from(m.height()), + }) + }; + if let Some(body) = body { + self.totem_event(body, own); + } + } + /// `ClientPacketListener.handleEntityEvent` 35: the totem particles (a + /// 30-tick emitter) and its sound, heard here only (`playLocalSound`), + /// and the player's own rumble. + pub(super) fn totem_event(&mut self, body: Body, own: bool) { self.particles.tracking_emitter_for( ParticleOptions::simple(ParticleType::TotemOfUndying), - at, - 0.6, - height, + body.at, + body.width, + body.height, 30, ); - self.pad_rumble(horizon_client::gamepad::Rumble::Totem); + self.audio + .play_unshared("minecraft:item.totem.use", Some(body.at), 1.0, 1.0); + if own { + self.pad_rumble(horizon_client::gamepad::Rumble::Totem); + } } } + +/// Where an entity event's entity stands and its size. +#[derive(Clone, Copy)] +pub(super) struct Body { + pub at: [f64; 3], + width: f64, + height: f64, +} diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index b3dae9ef4..12be44657 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -173,6 +173,9 @@ address. The script then checks, from both sides, what a guest's game used to ge - 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). +- totem: the host and the guest each take a lethal hit holding a totem of undying in the off + hand, then the main hand: the server uses it up and keeps them alive at 1 health with its + effects, and each side hears the totem once (entity event 35) (#1292). 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). @@ -4019,5 +4022,55 @@ def main(): sys.exit(0 if results and all(r['pass'] for r in results.values()) else 1) + +# Checks added after the list above (kept apart so open branches that extend it don't conflict). + + +def heard(game, sound): + return game('status')['heard'].get(sound, 0) + + +def check_totem(host, guest, out): + """LivingEntity.checkTotemDeathProtection on the server for each player: 100 damage holding a + totem in the off hand, then in the main hand, uses the totem up, leaves 1 health with + Regeneration II, Absorption II and Fire Resistance, and broadcasts entity event 35, which both + clients play once (#1292).""" + sound = 'minecraft:item.totem.use' + seen = {} + for game in (host, guest): + for hand in ('offhand', 'mainhand'): + game('command', command='gamemode survival') + game('command', command='clear @s') + game('command', command='effect clear @s') + game('command', command='effect give @s minecraft:instant_health 1 5 true') + game('command', command=f'item replace entity @s weapon.{hand} with minecraft:totem_of_undying') + game.wait(10) + before = {g.name: heard(g, sound) for g in (host, guest)} + game('command', command='damage @s 100 minecraft:generic') + game.wait(10) + me = own(game) + case = f'{game.name}-{hand}' + seen[case] = { + 'screen': game('state').get('screen'), + 'health': me['health'], + 'server_health': server_player(host, game)['health'], + 'effects': sorted(effect_ids(game)), + 'totems': server_count(game, 'totem_of_undying'), + 'heard': {g.name: heard(g, sound) - before[g.name] for g in (host, guest)}, + } + game.shot(out, f'totem-{hand}') + game('command', command='effect clear @s') + for case, s in seen.items(): + # 1 health, and what Regeneration II and the full food bar gave back since. + assert s['screen'] == 'playing' and 0 < s['health'] < 5 and 0 < s['server_health'] < 5, f'{case}: {seen}' + assert s['effects'] == ['absorption', 'fire_resistance', 'regeneration'], f'{case}: {seen}' + assert s['totems'] == 0 and s['heard'] == {'host': 1, 'guest': 1}, f'{case}: {seen}' + return seen + + +CHECKS += ['totem'] +CHECK_FNS.update(totem=check_totem) + + if __name__ == '__main__': main()