LAN guests use crafters
A guest's use of a crafter now opens CrafterMenu (minecraft:crafter_3x3) over the block entity. The host runs its clicks on the menu engine's verified Kind::CrafterBlock (a disabled slot refuses items, filling one enables it), sends the disabled slots and triggered as data slots (CrafterBlockEntity.containerData), and handles ServerboundContainerSlotStateChangedPacket (new in horizon-protocol) as CrafterBlockEntity.setSlotState. The guest shows the server's crafter, predicts its clicks and sends a slot toggle before the click as CrafterScreen.slotClicked does. Part of #868. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -639,6 +639,13 @@ impl Game<'_> {
|
||||
_ => ("minecraft:furnace", "container.furnace"),
|
||||
};
|
||||
(menu, title, Slots::Block(pos))
|
||||
} else if let Some(BlockEntity::Crafter(_)) = self.world.block_entities.get(&pos) {
|
||||
// CrafterBlock.useWithoutItem: CrafterMenu over the block entity.
|
||||
(
|
||||
"minecraft:crafter_3x3",
|
||||
"container.crafter",
|
||||
Slots::Block(pos),
|
||||
)
|
||||
} else if let Some(BlockEntity::Brewing(_)) = self.world.block_entities.get(&pos) {
|
||||
// BrewingStandBlock.useWithoutItem: BrewingStandMenu.
|
||||
(
|
||||
@@ -921,6 +928,9 @@ impl Game<'_> {
|
||||
self.lan_station_button(host, conn, p.container, p.button);
|
||||
}
|
||||
S::RenameItem(p) => self.lan_station_rename(host, conn, &p.name),
|
||||
S::ContainerSlotStateChanged(p) => {
|
||||
self.lan_crafter_slot_state(host, conn, p.container, p.slot, p.enabled);
|
||||
}
|
||||
S::SetBeacon(p) => self.lan_station_beacon(host, conn, p.primary, p.secondary),
|
||||
S::SelectTrade(p) => self.lan_select_trade(host, conn, p.item),
|
||||
S::ContainerClose(p) if self.lan_merchant_closed(host, conn, Some(p.container)) => {
|
||||
|
||||
@@ -205,8 +205,14 @@ impl Game<'_> {
|
||||
(Kind::Furnace(recipe), f.items.to_vec())
|
||||
}
|
||||
Some(BlockEntity::Brewing(b)) => (Kind::Brewing, b.items.to_vec()),
|
||||
Some(BlockEntity::Crafter(c)) => (Kind::CrafterBlock, c.items.to_vec()),
|
||||
_ => return false,
|
||||
};
|
||||
// CrafterBlockEntity.containerData: the disabled slots, then triggered.
|
||||
let crafter_data = match self.world.block_entities.get(&pos) {
|
||||
Some(BlockEntity::Crafter(c)) => Some((c.disabled, c.triggered)),
|
||||
_ => None,
|
||||
};
|
||||
if host.hub.guests[gi].spectator() {
|
||||
return true;
|
||||
}
|
||||
@@ -223,6 +229,13 @@ impl Game<'_> {
|
||||
for (k, s) in items.iter().enumerate() {
|
||||
menu.set_slot(env, k, s.clone());
|
||||
}
|
||||
// After the slots: setting a disabled slot (CrafterBlockEntity.setItem) enables it.
|
||||
if let Some((disabled, triggered)) = crafter_data {
|
||||
for (k, d) in disabled.iter().enumerate() {
|
||||
menu.data[k] = i32::from(*d);
|
||||
}
|
||||
menu.data[9] = i32::from(triggered);
|
||||
}
|
||||
let before: Vec<_> = (0..menu.slot_count())
|
||||
.map(|s| menu.slot(s).cloned())
|
||||
.collect();
|
||||
@@ -245,6 +258,13 @@ impl Game<'_> {
|
||||
b.items[k] = menu.slot(k).cloned();
|
||||
}
|
||||
}
|
||||
// CrafterBlockEntity.setItem: filling a disabled slot enables it.
|
||||
Some(BlockEntity::Crafter(c)) => {
|
||||
for k in 0..9 {
|
||||
c.items[k] = menu.slot(k).cloned();
|
||||
c.disabled[k] = menu.data[k] == 1;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
host.play.entry(conn).or_default().crafting.machine = Some((p.container, menu));
|
||||
@@ -252,6 +272,39 @@ impl Game<'_> {
|
||||
true
|
||||
}
|
||||
|
||||
/// `ServerGamePacketListenerImpl.handleContainerSlotStateChanged`: a
|
||||
/// guest's crafter slot toggle (`CrafterBlockEntity.setSlotState`) in
|
||||
/// their open crafter window.
|
||||
pub(super) fn lan_crafter_slot_state(
|
||||
&mut self,
|
||||
host: &mut LanHost,
|
||||
conn: u32,
|
||||
window: i32,
|
||||
slot: i32,
|
||||
enabled: bool,
|
||||
) {
|
||||
let Some(pos) = host
|
||||
.hub
|
||||
.guest(conn)
|
||||
.filter(|g| !g.spectator())
|
||||
.and_then(|g| g.menu.as_ref())
|
||||
.filter(|m| window != 0 && m.window == window)
|
||||
.and_then(|m| match m.slots {
|
||||
Slots::Block(pos) => Some(pos),
|
||||
_ => None,
|
||||
})
|
||||
else {
|
||||
return;
|
||||
};
|
||||
if let (Some(BlockEntity::Crafter(c)), Ok(slot)) = (
|
||||
self.world.block_entities.get_mut(&pos),
|
||||
usize::try_from(slot),
|
||||
) {
|
||||
c.set_slot_state(slot, enabled);
|
||||
self.world.modified.insert((pos.0 >> 4, pos.2 >> 4));
|
||||
}
|
||||
}
|
||||
|
||||
/// ServerGamePacketListenerImpl.handlePlaceRecipe: the guest's recipe
|
||||
/// book places a known recipe in their inventory grid or crafting table
|
||||
/// (`ServerPlaceRecipe`), and answers with a ghost recipe when the items
|
||||
|
||||
@@ -8,6 +8,7 @@ use horizon_client::{
|
||||
containers::Crafter,
|
||||
hud::{HudBatch, rgba},
|
||||
};
|
||||
use horizon_protocol::packets::play::{self, Serverbound as S};
|
||||
use serde_json::Value;
|
||||
|
||||
/// `CrafterBlock`'s crafting animation length.
|
||||
@@ -323,6 +324,15 @@ impl Game<'_> {
|
||||
menu.set_slot_state(i, enabled);
|
||||
Some(true)
|
||||
});
|
||||
if let Some((container, _)) = self.remote.as_ref().and_then(|r| r.menu.clone()) {
|
||||
self.send(S::ContainerSlotStateChanged(
|
||||
play::ContainerSlotStateChanged {
|
||||
slot: i as i32,
|
||||
container,
|
||||
enabled,
|
||||
},
|
||||
));
|
||||
}
|
||||
let pitch = if enabled { 1.0 } else { 0.75 };
|
||||
self.audio
|
||||
.play("minecraft:ui.button.click", None, 0.4, pitch);
|
||||
|
||||
@@ -1251,6 +1251,9 @@ impl Game<'_> {
|
||||
"minecraft:hopper" => slots(5),
|
||||
"minecraft:generic_3x3" => slots(9),
|
||||
"minecraft:brewing_stand" => slots(5),
|
||||
// CrafterMenu: the grid, the inventory, then the result (the
|
||||
// client's own).
|
||||
"minecraft:crafter_3x3" => slots(9),
|
||||
// A workstation: its inputs, its result, then the inventory.
|
||||
m => {
|
||||
let kind = Self::remote_station_kind(m)?;
|
||||
@@ -1961,6 +1964,12 @@ impl Game<'_> {
|
||||
3 => f.cooking_total = v,
|
||||
_ => {}
|
||||
},
|
||||
// CrafterBlockEntity.containerData.
|
||||
Some(BlockEntity::Crafter(c)) => match usize::try_from(p.id) {
|
||||
Ok(k @ 0..9) => c.disabled[k] = v == 1,
|
||||
Ok(9) => c.triggered = v == 1,
|
||||
_ => {}
|
||||
},
|
||||
// BrewingStandBlockEntity.dataAccess.
|
||||
Some(BlockEntity::Brewing(b)) => match p.id {
|
||||
0 => b.brew_time = v,
|
||||
@@ -3799,6 +3808,10 @@ impl Game<'_> {
|
||||
/// 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 {
|
||||
// CrafterScreen.slotClicked: a slot toggle goes out before the click.
|
||||
if let [(Some(slot), button, input)] = ops {
|
||||
self.crafter_slot_clicked(*slot, *button, *input);
|
||||
}
|
||||
let (container, menu) = self.remote_menu();
|
||||
let mut clicks = Vec::with_capacity(ops.len());
|
||||
for &(slot, button, input) in ops {
|
||||
@@ -4078,6 +4091,8 @@ impl Game<'_> {
|
||||
horizon_client::brewing::BrewingStand::new(),
|
||||
))),
|
||||
"minecraft:generic_3x3" => Some(BlockEntity::Chest(vec![None; 9])),
|
||||
// CrafterMenu: the disabled slots and triggered come in ContainerSetData.
|
||||
"minecraft:crafter_3x3" => Some(BlockEntity::Crafter(Default::default())),
|
||||
m if m.starts_with("minecraft:generic_9x") || m == "minecraft:shulker_box" => {
|
||||
let rows = m
|
||||
.strip_prefix("minecraft:generic_9x")
|
||||
|
||||
@@ -455,18 +455,30 @@ impl Hub {
|
||||
let Slots::Block(pos) = m.slots else {
|
||||
return;
|
||||
};
|
||||
// AbstractFurnaceBlockEntity.dataAccess, BrewingStandBlockEntity.dataAccess.
|
||||
// AbstractFurnaceBlockEntity.dataAccess, BrewingStandBlockEntity.dataAccess,
|
||||
// CrafterBlockEntity.containerData.
|
||||
let data = match world.block_entities.get(&pos) {
|
||||
Some(BlockEntity::Furnace(f)) => [
|
||||
Some(BlockEntity::Furnace(f)) => vec![
|
||||
f.lit_remaining,
|
||||
f.lit_total,
|
||||
f.cooking_timer,
|
||||
f.cooking_total,
|
||||
],
|
||||
Some(BlockEntity::Brewing(b)) => [b.brew_time, b.fuel, b.total_brew_time, b.total_fuel],
|
||||
Some(BlockEntity::Brewing(b)) => {
|
||||
vec![b.brew_time, b.fuel, b.total_brew_time, b.total_fuel]
|
||||
}
|
||||
// CrafterBlockEntity.containerData: the disabled slots, then triggered.
|
||||
Some(BlockEntity::Crafter(c)) => c
|
||||
.disabled
|
||||
.iter()
|
||||
.chain([&c.triggered])
|
||||
.map(|&d| i32::from(d))
|
||||
.collect(),
|
||||
_ => return,
|
||||
}
|
||||
.map(|v| v as i16);
|
||||
.into_iter()
|
||||
.map(|v| v as i16)
|
||||
.collect::<Vec<i16>>();
|
||||
m.sent_data.resize(data.len(), i16::MIN);
|
||||
let window = m.window;
|
||||
let changed: Vec<(usize, i16)> = data
|
||||
|
||||
@@ -1715,6 +1715,11 @@ packet!(
|
||||
/// (the loom's patterns, the stonecutter's recipes, the enchanting table's offers).
|
||||
ContainerButtonClick { container: i32 => VarInt, button: i32 => VarInt }
|
||||
);
|
||||
packet!(
|
||||
/// ServerboundContainerSlotStateChangedPacket: the crafter's slot toggle
|
||||
/// (`CrafterBlockEntity.setSlotState`).
|
||||
ContainerSlotStateChanged { slot: i32 => VarInt, container: i32 => VarInt, enabled: bool => Bool }
|
||||
);
|
||||
packet!(
|
||||
/// ServerboundRenameItemPacket: `AnvilMenu.setItemName`.
|
||||
RenameItem { name: String => Str }
|
||||
@@ -1807,6 +1812,7 @@ packet_enum!(Serverbound, crate::ids::play::serverbound {
|
||||
PlayerLoaded(Empty) = PLAYER_LOADED,
|
||||
Pong(Ping) = PONG,
|
||||
RenameItem(RenameItem) = RENAME_ITEM,
|
||||
ContainerSlotStateChanged(ContainerSlotStateChanged) = CONTAINER_SLOT_STATE_CHANGED,
|
||||
Punch(Empty) = PUNCH,
|
||||
ResourcePack(ResourcePackResponse) = RESOURCE_PACK,
|
||||
SelectTrade(SelectTrade) = SELECT_TRADE,
|
||||
@@ -2390,6 +2396,14 @@ mod tests {
|
||||
});
|
||||
assert_eq!(&rename.encode()[1..], &[5, b'B', b'l', b'a', b'd', b'e']);
|
||||
round_trip(rename, Serverbound::encode, Serverbound::decode);
|
||||
// The slot, the container id (VarInts), then the new state.
|
||||
let toggle = Serverbound::ContainerSlotStateChanged(ContainerSlotStateChanged {
|
||||
slot: 4,
|
||||
container: 2,
|
||||
enabled: false,
|
||||
});
|
||||
assert_eq!(&toggle.encode()[1..], &[4, 2, 0]);
|
||||
round_trip(toggle, Serverbound::encode, Serverbound::decode);
|
||||
// Two optional mob effect ids: a presence byte, then the VarInt.
|
||||
let beacon = Serverbound::SetBeacon(SetBeacon {
|
||||
primary: Some(2),
|
||||
|
||||
+31
-1
@@ -147,7 +147,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', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth']
|
||||
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth']
|
||||
|
||||
|
||||
class Game:
|
||||
@@ -1182,6 +1182,35 @@ def check_beacon(host, guest, out):
|
||||
return seen
|
||||
|
||||
|
||||
def check_crafter(host, guest, out):
|
||||
guest('command', command='gamemode survival')
|
||||
guest('command', command='clear @s')
|
||||
guest('command', command='give @s minecraft:oak_planks 4')
|
||||
x, y, z = (int(v // 1) for v in pos_of(guest))
|
||||
at = (x, y, z + 2)
|
||||
guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:crafter')
|
||||
guest.wait(10)
|
||||
guest.raw('interact', *at)
|
||||
guest.raw('wait-for', 'screen', 'container', '--timeout', 10)
|
||||
guest.wait(10)
|
||||
# CrafterScreen.slotClicked: an empty-handed click disables the first slot
|
||||
# (ServerboundContainerSlotStateChangedPacket); the planks then skip it.
|
||||
guest.raw('click-slot', 300)
|
||||
guest.wait(5)
|
||||
guest.raw('click-slot', hotbar_slot(guest, 'minecraft:oak_planks'), 'shift')
|
||||
guest.wait(10)
|
||||
guest.shot(out, 'crafter')
|
||||
guest.raw('key', 'esc', 'tap')
|
||||
guest.wait(10)
|
||||
# Opening it again shows the server's crafter.
|
||||
items = open_container(guest, at, out, 'crafter-after')
|
||||
guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:air')
|
||||
seen = {'items': items[0] if isinstance(items, tuple) else items, 'planks': server_count(guest, 'oak_planks')}
|
||||
want = {'items': {1: 'minecraft:oak_planks'}, 'planks': 0}
|
||||
assert seen == want, f'the crafter on the guest: {seen}'
|
||||
return seen
|
||||
|
||||
|
||||
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']:
|
||||
@@ -2380,6 +2409,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name
|
||||
'smithing': check_smithing,
|
||||
'enchanting': check_enchanting,
|
||||
'beacon': check_beacon,
|
||||
'crafter': check_crafter,
|
||||
'hurt': check_hurt,
|
||||
'commands': check_commands,
|
||||
'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls,
|
||||
|
||||
Reference in New Issue
Block a user