Files
horizon-airdef/crates/horizon-client/src/game/remote_stations.rs
T
Ludovic Marie 4fecf43d28 Grindstone on the menu engine; LAN guests use grindstones
GrindstoneMenu now runs on horizon_client::menu (Kind::Grindstone): its
input filter, computeResult, the result's onTake (both inputs cleared,
the experience sum handed to the caller) and quickMoveStack, verified
against the vanilla class by test_menu_click_parity with enchanted,
damaged and cursed tools and books.

Single player routes the grindstone's clicks, drags and double clicks
through the engine and rolls the orbs and level event 1042 (now playing
block.grindstone.use) where the result is taken. The LAN host opens
minecraft:grindstone for guests on the same engine and awards the orbs at
the block; guests read enchantments, stored enchantments, repair cost and
custom names from the server's stacks, and the host now sends repair
cost.

Part of #868.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 08:43:29 +02:00

93 lines
3.4 KiB
Rust

//! A server's workstation menus on this client: the server opens the menu
//! (`ClientboundOpenScreenPacket` with the station's `MenuType`), the
//! station screen draws it, and its clicks and buttons are predicted on the
//! menu engine (`Game::menu_session`) and sent, as
//! `MultiPlayerGameMode.handleContainerInput` and `handleInventoryButtonClick`
//! do. The menu's client-side `slotsChanged` follows the server's slots, and
//! its data slots come in `ClientboundContainerSetDataPacket`.
use super::{
Game, Screen,
stations::{Kind, Station},
};
use horizon_protocol::packets::play::{self, Serverbound as S};
impl Game<'_> {
/// The server opened workstation menu `menu` in window `container`.
pub(super) fn remote_open_station(
&mut self,
container: i32,
menu: String,
kind: Kind,
title: String,
pos: [i32; 3],
) {
if let Some(r) = self.remote.as_mut() {
r.menu = Some((container, menu));
r.menu_title = Some(title);
}
self.craft_size = 2;
self.container = None;
self.station = Some(Station::new_kind(kind, pos));
self.screen = Screen::Inventory;
self.capture(false);
}
/// Window `container` is the server's workstation menu, open here.
pub(super) fn remote_station_open(&self, container: i32) -> bool {
self.station.is_some()
&& self
.remote
.as_ref()
.and_then(|r| r.menu.as_ref())
.is_some_and(|(id, name)| {
*id == container && Self::remote_station_kind(name).is_some()
})
}
/// The server set station slot `internal`: the client menu's
/// `Slot.set` runs its container's `setChanged`, so the loom lists the
/// patterns its inputs allow and the stonecutter the recipes of its input.
pub(super) fn remote_station_slot_changed(&mut self, internal: usize) {
if !self
.station
.as_ref()
.is_some_and(|s| matches!(s.kind, Kind::Loom | Kind::Stonecutter | Kind::Grindstone))
{
return;
}
self.menu_session(|menu, env, to_menu| {
let i = to_menu(internal)?;
let stack = menu.slot(i).cloned();
menu.set_slot(env, i, stack);
Some(true)
});
}
/// `AbstractContainerMenu.setData` from the server.
pub(super) fn remote_station_data(&mut self, id: usize, value: i16) {
let Some(s) = self.station.as_mut() else {
return;
};
match (s.kind, id) {
// LoomMenu.selectedBannerPatternIndex.
(Kind::Loom, 0) => s.loom.selected = usize::try_from(value).ok(),
// StonecutterMenu.selectedRecipeIndex.
(Kind::Stonecutter, 0) => s.selected = usize::try_from(value).ok(),
_ => {}
}
}
/// `MultiPlayerGameMode.handleInventoryButtonClick`: the screen already
/// ran `clickMenuButton` on its own menu.
pub(super) fn remote_station_button(&self, button: i32) {
if let Some((container, _)) = self.remote.as_ref().and_then(|r| r.menu.as_ref())
&& self.remote_station_open(*container)
{
self.send(S::ContainerButtonClick(play::ContainerButtonClick {
container: *container,
button,
}));
}
}
}