diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index 0a6f6cc95..8a6b54650 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -468,6 +468,8 @@ impl Game<'_> { "gamemode": format!("{:?}", self.mode).to_lowercase(), // The bed (its head) the player sleeps in. "sleeping": self.sleeping, + // 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 d31cd4773..940824bcd 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2330,10 +2330,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)); @@ -2800,8 +2802,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(); @@ -2834,7 +2844,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, @@ -2842,6 +2852,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 64472e57c..df565e55f 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -38,6 +38,8 @@ address. The script then checks, from both sides, what a guest's game used to ge gets up with Escape, and once the host sleeps too the night ends and both wake. - 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. +- 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). @@ -58,6 +60,7 @@ import shutil import subprocess import sys import tempfile +import threading import time import zlib from pathlib import Path @@ -69,7 +72,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', 'effects', 'portals'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'effects', 'portals'] class Game: @@ -978,6 +981,33 @@ def check_push(host, guest, out): 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}' +def check_eat(host, guest, out): + 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() + 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 @@ -987,7 +1017,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'containers': check_containers, 'feedback': check_feedback, '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} + 'sleep': check_sleep, 'push': check_push, 'eat': check_eat} def run(ctl, out, resources, checks):