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):