Merge commit '123e8286ac55894a0e9f61c78b3844fd46241e2a' into bm2/combined

# Conflicts:
#	crates/horizon-client/src/game/remote.rs
#	tools/lan_e2e.py
This commit is contained in:
Ludovic Marie
2026-10-17 23:54:21 +02:00
5 changed files with 265 additions and 27 deletions
+23 -8
View File
@@ -4546,6 +4546,10 @@ impl Game<'_> {
/// Several clicks on one menu instance (a quick craft drag is three).
fn menu_ops(&mut self, ops: &[(Option<usize>, i32, horizon_client::menu::Input)]) -> bool {
use horizon_client::menu::Input;
if self.remote.is_some() && self.screen == Screen::Inventory {
// On a server the clicks are the server's: predicted and sent.
return self.remote_menu_ops(ops);
}
if let [(Some(slot), button, input)] = ops {
self.crafter_slot_clicked(*slot, *button, *input);
}
@@ -4720,7 +4724,7 @@ impl Game<'_> {
plain: false,
};
(kind, map)
} else if self.station.is_none() && !self.mount_active() && self.remote.is_none() {
} else if self.station.is_none() && !self.mount_active() {
let table = self.craft_size == 3;
let map = Map {
container: 0,
@@ -4833,7 +4837,12 @@ impl Game<'_> {
Some(false) => return true,
Some(true) => {}
}
self.register_exploration_maps(made);
// On a server the clicks only predict: the server drops, crafts and
// makes maps itself.
let predicting = self.remote.is_some();
if !predicting {
self.register_exploration_maps(made);
}
if let Some(s) = menu.merchant.take() {
self.merchant_sync_out(s);
}
@@ -4851,10 +4860,12 @@ impl Game<'_> {
if !offhand_mapped {
self.offhand = menu.player_item(40).cloned();
}
for stack in dropped {
self.player_drop(stack);
if !predicting {
for stack in dropped {
self.player_drop(stack);
}
self.award_crafted(&crafted);
}
self.award_crafted(&crafted);
if kind == Kind::Loom {
if let Some(s) = self.station.as_mut() {
s.loom.selected = usize::try_from(menu.data[0]).ok();
@@ -4986,9 +4997,13 @@ impl Game<'_> {
return;
}
if let Some(moved) = self.last_quick_moved.clone() {
self.menu_session(|menu, env, to_menu| {
Some(menu.quick_move_all(env, to_menu(slot)?, &moved).is_ok())
});
if self.remote.is_some() {
self.remote_quick_move_all(slot, &moved);
} else {
self.menu_session(|menu, env, to_menu| {
Some(menu.quick_move_all(env, to_menu(slot)?, &moved).is_ok())
});
}
}
self.last_inv_click = None;
self.inv_clicks.skip_release();
+136 -6
View File
@@ -14,6 +14,7 @@ use horizon_client::{
entities::{Experience, ItemEntity},
hud::{HudBatch, rgba},
items::ItemStack,
menu::Input,
mobs::{Mob, MobType},
motion::{MotionWorld, PlayerMotion},
net::{Event, Session},
@@ -2903,17 +2904,108 @@ impl Game<'_> {
let Some(internal) = self.slot_at(&batch, self.cursor.0, self.cursor.1) else {
return;
};
let input = if self.has_shift_down() { 1 } else { 0 };
self.remote_slot_click(internal, i8::from(right), input);
let input = if self.has_shift_down() {
Input::QuickMove
} else {
Input::Pickup
};
self.remote_menu_ops(&[(Some(internal), i32::from(right), input)]);
}
/// The server's open menu: its window id and menu type (0, "inventory"
/// when none is open).
fn remote_menu(&self) -> (i32, String) {
match self.remote.as_ref().and_then(|r| r.menu.clone()) {
Some((id, name)) => (id, name),
None => (0, "inventory".to_owned()),
}
}
/// `MultiPlayerGameMode.handleContainerInput` for each click of `ops` on
/// the server's open menu (a quick craft drag is several): the verified
/// menu engine predicts them on this client's copy, then each goes out as
/// a `ContainerClick` with the slots it changed and the carried stack
/// after it; the server corrects anything it sees differently. A menu the
/// engine doesn't model sends the clicks unpredicted. False when a click's
/// slot isn't one of the menu's.
pub(super) fn remote_menu_ops(&mut self, ops: &[(Option<usize>, i32, Input)]) -> bool {
let (container, menu) = self.remote_menu();
let mut clicks = Vec::with_capacity(ops.len());
for &(slot, button, input) in ops {
let slot = match slot {
None => -999,
Some(internal) => {
match (0..64).find(|&s| Self::menu_slot(&menu, s) == Some(internal)) {
Some(s) => s as i16,
None => return false,
}
}
};
clicks.push((slot, button, input));
}
// Unpredicted: nothing changed, the carried stack as it is.
let mut sent: Vec<Predicted> = clicks
.iter()
.map(|&(slot, button, input)| (slot, button, input, Vec::new(), self.carried.clone()))
.collect();
// The engine's slot indices are the menu's own, as on the network.
self.menu_session(|m, env, _| {
let mut predicted = Vec::with_capacity(clicks.len());
for &(slot, button, input) in &clicks {
let before = menu_items(m);
if m.click(env, i32::from(slot), button, input).is_err() {
// The server throws on it too: nothing is predicted.
return Some(false);
}
let changed = changed_slots(&before, m);
predicted.push((slot, button, input, changed, m.carried.clone()));
}
sent = predicted;
Some(true)
});
self.send_clicks(container, sent);
true
}
/// A Shift double click on a server (`AbstractContainerScreen.mouseReleased`):
/// a predicted QUICK_MOVE for every slot the engine moves, each sent.
pub(super) fn remote_quick_move_all(&mut self, slot: usize, moved: &ItemStack) {
let (container, _) = self.remote_menu();
let mut sent: Vec<Predicted> = Vec::new();
self.menu_session(|m, env, to_menu| {
let clicked = to_menu(slot)?;
let mut before = menu_items(m);
let mut each = |m: &horizon_client::menu::Menu, t: usize| {
let changed = changed_slots(&before, m);
sent.push((t as i16, 0, Input::QuickMove, changed, m.carried.clone()));
before = menu_items(m);
};
Some(
m.quick_move_all_each(env, clicked, moved, &mut each)
.is_ok(),
)
});
self.send_clicks(container, sent);
}
fn send_clicks(&mut self, container: i32, clicks: Vec<Predicted>) {
let state = self.remote.as_ref().map_or(0, |r| r.state_id);
let hashed = |g: &Self, s: &Option<ItemStack>| g.to_network(s).map(|s| Hashed::of(&s));
for (slot, button, input, changed, carried) in clicks {
let changed = changed.iter().map(|(i, s)| (*i, hashed(self, s))).collect();
let carried = hashed(self, &carried);
self.send(S::ContainerClick(play::ContainerClick {
container,
state,
slot,
button: button as i8,
input: input_ordinal(input),
changed,
carried,
}));
}
}
/// A `ContainerClick` on game slot `internal` of the server's open menu
/// (`input` is the ContainerInput ordinal). False when the menu has no
/// such slot.
pub(super) fn remote_slot_click(&mut self, internal: usize, button: i8, input: i32) -> bool {
let (container, menu) = match self.remote.as_ref().and_then(|r| r.menu.clone()) {
Some((id, name)) => (id, name),
None => (0, "inventory".to_owned()),
};
let (container, menu) = self.remote_menu();
let slot = (0..64).find(|&s| Self::menu_slot(&menu, s) == Some(internal));
let Some(slot) = slot else { return false };
let carried = self.to_network(&self.carried).map(|s| Hashed::of(&s));
@@ -3338,6 +3430,44 @@ fn remote_player_head_turn(m: &mut Mob) {
}
}
/// A click sent to a server: its slot, button and input, the slots it
/// changed and the carried stack after it.
type Predicted = (
i16,
i32,
Input,
Vec<(i16, Option<ItemStack>)>,
Option<ItemStack>,
);
fn menu_items(m: &horizon_client::menu::Menu) -> Vec<Option<ItemStack>> {
(0..m.slot_count()).map(|i| m.slot(i).cloned()).collect()
}
/// The slots whose stack is no longer `before`'s (`ItemStack.matches`).
fn changed_slots(
before: &[Option<ItemStack>],
m: &horizon_client::menu::Menu,
) -> Vec<(i16, Option<ItemStack>)> {
(0..m.slot_count())
.filter(|&i| m.slot(i) != before.get(i).and_then(Option::as_ref))
.map(|i| (i as i16, m.slot(i).cloned()))
.collect()
}
/// ServerboundContainerClickPacket's `ContainerInput` ordinal.
pub(super) fn input_ordinal(input: Input) -> i32 {
match input {
Input::Pickup => 0,
Input::QuickMove => 1,
Input::Swap => 2,
Input::Clone => 3,
Input::Throw => 4,
Input::QuickCraft => 5,
Input::PickupAll => 6,
}
}
#[cfg(test)]
mod tests {
use super::Game;
+1 -9
View File
@@ -70,15 +70,7 @@ impl Game<'_> {
/// One ordinary click on a game slot. False when nothing took it.
pub(super) fn tweak_click(&mut self, slot: usize, button: i32, input: Input) -> bool {
if self.remote.is_some() {
let ordinal = match input {
Input::Pickup => 0,
Input::QuickMove => 1,
Input::Swap => 2,
Input::Clone => 3,
Input::Throw => 4,
Input::QuickCraft => 5,
Input::PickupAll => 6,
};
let ordinal = super::remote::input_ordinal(input);
return self.remote_slot_click(slot, button as i8, ordinal);
}
if !self.menu_click(Some(slot), button, input) {
+13
View File
@@ -1668,6 +1668,18 @@ impl Menu {
env: &Env,
clicked: usize,
moved: &ItemStack,
) -> Result<(), &'static str> {
self.quick_move_all_each(env, clicked, moved, &mut |_, _| {})
}
/// `quick_move_all`, calling `each` with the menu and the slot after each
/// QUICK_MOVE (a server's client sends every one of them).
pub fn quick_move_all_each(
&mut self,
env: &Env,
clicked: usize,
moved: &ItemStack,
each: &mut dyn FnMut(&Self, usize),
) -> Result<(), &'static str> {
if clicked >= self.slots.len() || !self.can_take_for_pick_all(clicked) {
return Ok(());
@@ -1680,6 +1692,7 @@ impl Menu {
&& self.quick_replace(t, moved, env)
{
self.click(env, t as i32, 0, Input::QuickMove)?;
each(self, t);
}
}
Ok(())
+92 -4
View File
@@ -30,6 +30,10 @@ address. The script then checks, from both sides, what a guest's game used to ge
tree (Tab), and runs what it completed;
- recipes: the guest's recipe book lists the planks recipe the logs unlock, and clicking it
places a log in the inventory's crafting grid (the server places it).
- feedback: the guest's command feedback names the guest and shows the numbers.
- clicks: with real mouse presses (not click-slot, which skips the screen's press and drag
handling), the guest puts a stack into a chest and a furnace, drags planks across
its crafting grid and gathers a stack with a double click; the server has each.
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).
@@ -61,7 +65,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', 'effects', 'portals']
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'commands', 'recipes', 'trials', 'clicks', 'effects', 'portals']
class Game:
@@ -584,6 +588,91 @@ def check_containers(host, guest, out):
return {'ender': ender, 'double': double, 'taken': taken}
def slot_point(game, slot):
"""The GUI point in the middle of slot `slot` of the open screen (`ui`'s slot-N)."""
for w in game.raw('ui')['widgets']:
if w.get('id') == f'slot-{slot}':
x, y, width, height = w['bounds']
return x + width / 2, y + height / 2
raise Failed(f'no slot {slot} on the screen')
def mouse(game, *slots, right=False):
"""Press over the first slot, move over the others and release: one click, or a drag."""
points = [f'{v:.1f}' for s in slots for v in slot_point(game, s)]
game.raw('drag-at', *(['right'] if right else []), *points)
game.wait(4)
def hotbar_slot(game, item):
return next(s['slot'] for s in game('inventory')['hotbar'] if s and s.get('item') == item)
def check_clicks(host, guest, out):
guest('command', command='gamemode survival')
guest('command', command='clear @s')
guest('command', command='give @s minecraft:cobblestone 10')
guest('command', command='give @s minecraft:coal 3')
guest('command', command='give @s minecraft:raw_iron 2')
guest('command', command='give @s minecraft:oak_planks 2')
guest.wait(10)
x, y, z = (int(v // 1) for v in pos_of(guest))
chest, furnace = (x, y, z + 2), (x, y, z - 2)
guest('command', command=f'setblock {chest[0]} {chest[1]} {chest[2]} minecraft:chest')
guest('command', command=f'setblock {furnace[0]} {furnace[1]} {furnace[2]} minecraft:furnace')
guest.wait(10)
# Pick the cobblestone up and put it down in the chest's first slot.
guest.raw('interact', *chest)
guest.raw('wait-for', 'screen', 'container', '--timeout', 10)
guest.wait(10)
mouse(guest, hotbar_slot(guest, 'minecraft:cobblestone'))
mouse(guest, 300)
guest.wait(10)
guest.shot(out, 'clicks-chest')
guest.raw('key', 'esc', 'tap')
guest.wait(10)
in_chest, _ = open_container(guest, chest, out, 'clicks-chest-after')
# Coal into the fuel slot, raw iron into the input.
guest.raw('interact', *furnace)
guest.raw('wait-for', 'screen', 'container', '--timeout', 10)
guest.wait(10)
mouse(guest, hotbar_slot(guest, 'minecraft:coal'))
mouse(guest, 301)
mouse(guest, hotbar_slot(guest, 'minecraft:raw_iron'))
mouse(guest, 300)
guest.wait(10)
guest.shot(out, 'clicks-furnace')
guest.raw('key', 'esc', 'tap')
guest.wait(10)
# Opening the furnace again resends the whole menu: the server's own view.
in_furnace, _ = open_container(guest, furnace, out, 'clicks-furnace-after')
# Two planks dragged over the 2x2 grid's left column: one each, four sticks.
guest.raw('key', 'e', 'tap')
guest.raw('wait-for', 'screen', 'inventory')
guest.wait(5)
mouse(guest, hotbar_slot(guest, 'minecraft:oak_planks'))
mouse(guest, 100, 102)
guest.wait(10)
grid = {s['slot']: (s.get('item'), s.get('count')) for s in guest('screen').get('slots', [])
if s.get('slot') in (100, 102, 200) and s.get('item')}
guest.shot(out, 'clicks-drag')
guest.raw('click-slot', 200, 'shift')
guest.wait(10)
guest.raw('key', 'esc', 'tap')
guest.wait(10)
# The chest's menu brings the whole inventory as the server has it.
open_container(guest, chest, out, 'clicks-inventory')
counts = guest('inventory')['counts']
for at in (chest, furnace):
guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:air')
assert in_chest == {0: 'minecraft:cobblestone'}, f'the chest holds {in_chest} after the guest put cobblestone in'
assert in_furnace == {0: 'minecraft:raw_iron', 1: 'minecraft:coal'}, f'the furnace holds {in_furnace}'
assert grid.get(100) == ('minecraft:oak_planks', 1) and grid.get(102) == ('minecraft:oak_planks', 1), \
f'the drag left the grid as {grid}'
assert counts.get('minecraft:stick') == 4 and 'minecraft:cobblestone' not in counts, f'inventory after: {counts}'
return {'chest': in_chest, 'furnace': in_furnace, 'grid': grid, 'inventory': counts}
def check_feedback(host, guest, out):
x, y, z = (round(v, 1) for v in pos_of(guest))
guest('command', command='item replace entity @s weapon.mainhand with minecraft:stick')
@@ -828,9 +917,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name
'chest': check_chest, 'crafting': check_crafting, 'sign': check_sign, 'damage': check_damage,
'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp,
'containers': check_containers, 'feedback': check_feedback, 'commands': check_commands,
'recipes': check_recipes,
'trials': check_trials,
'walls': check_walls, 'effects': check_effects, 'portals': check_portals}
'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls,
'effects': check_effects, 'portals': check_portals, 'clicks': check_clicks}
def run(ctl, out, resources, checks):