Open furnaces, blast furnaces and smokers on a server

Right-clicking a furnace as a guest did nothing: the host only handed
chests, barrels, hoppers, dispensers and shulker boxes over. It now opens
the furnace's own menu (minecraft:furnace, blast_furnace or smoker),
runs the guest's clicks through the host's AbstractFurnaceMenu (fuel slot,
take-only result, shift-clicks by recipe and fuel, a new input restarts
cooking) and keeps the client's progress arrows in step with
ContainerSetData. The dedicated server harness now opens a furnace and
smelts an ore in it on both servers.

Fixes #540

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-17 03:28:28 +02:00
parent 40970589be
commit e7f7236c34
6 changed files with 193 additions and 8 deletions
@@ -275,6 +275,14 @@ impl Game<'_> {
"container.enderchest",
Slots::Ender,
)
} else if let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get(&pos) {
// AbstractFurnaceBlock.openContainer: FurnaceMenu, BlastFurnaceMenu, SmokerMenu.
let (menu, title) = match f.kind.recipe_type() {
"minecraft:blasting" => ("minecraft:blast_furnace", "container.blast_furnace"),
"minecraft:smoking" => ("minecraft:smoker", "container.smoker"),
_ => ("minecraft:furnace", "container.furnace"),
};
(menu, title, Slots::Block(pos))
} else if let Some((other, right)) = self.chest_partner(pos) {
// ChestBlock.combine: the right half's slots come first.
let halves = if right {
@@ -475,6 +483,7 @@ impl Game<'_> {
S::ContainerClick(p) if self.lan_merchant_click(host, conn, &p) => {}
S::ContainerClick(p) if self.lan_inventory_click(host, conn, &p) => {}
S::ContainerClick(p) if self.lan_crafting_click(host, conn, &p) => {}
S::ContainerClick(p) if self.lan_furnace_click(host, conn, &p) => {}
S::SelectTrade(p) => self.lan_select_trade(host, conn, p.item),
S::ContainerClose(p) if self.lan_merchant_closed(host, conn, Some(p.container)) => {
let g = &mut host.hub.guests[gi];
+73 -1
View File
@@ -6,8 +6,9 @@
//! closes (`AbstractContainerMenu.clearContainer`).
use super::{Game, lan::LanHost};
use horizon_client::{
containers::BlockEntity,
items::ItemStack,
lan::guest::Guest,
lan::{guest::Guest, menu::Slots},
menu::{Env, Input, Kind, Menu},
};
use horizon_protocol::packets::play::{self, Clientbound as C};
@@ -20,6 +21,9 @@ pub(super) struct GuestCrafting {
inventory: Option<Box<Menu>>,
/// The open crafting table: its window, state id and menu.
table: Option<(i32, i32, Box<Menu>)>,
/// The open furnace's window and menu (`AbstractFurnaceMenu`); its slots live in
/// the furnace block entity and the hub's container menu keeps the client in sync.
furnace: Option<(i32, Box<Menu>)>,
}
/// ServerboundContainerClickPacket's input.
@@ -153,6 +157,74 @@ impl Game<'_> {
true
}
/// A click on the guest's open furnace, blast furnace or smoker: the host's
/// `AbstractFurnaceMenu` (fuel slot, take-only result, quick moves by recipe and
/// fuel) over the block entity's slots. False for other windows.
pub(super) fn lan_furnace_click(
&mut self,
host: &mut LanHost,
conn: u32,
p: &play::ContainerClick,
) -> bool {
let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else {
return false;
};
let Some(pos) = host.hub.guests[gi]
.menu
.as_ref()
.filter(|m| p.container != 0 && m.window == p.container)
.and_then(|m| match m.slots {
Slots::Block(pos) => Some(pos),
_ => None,
})
else {
return false;
};
let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get(&pos) else {
return false;
};
let recipe = match f.kind.recipe_type() {
"minecraft:blasting" => "blasting",
"minecraft:smoking" => "smoking",
_ => "smelting",
};
let items = f.items.clone();
if host.hub.guests[gi].spectator() {
return true;
}
let mut menu = host
.play
.entry(conn)
.or_default()
.crafting
.furnace
.take()
.filter(|(window, _)| *window == p.container)
.map_or_else(|| Box::new(Menu::new(Kind::Furnace(recipe))), |(_, m)| m);
let changed = self.guest_menu_op(host, gi, &mut menu, |menu, env| {
for (k, s) in items.iter().enumerate() {
menu.set_slot(env, k, s.clone());
}
let before: Vec<_> = (0..menu.slot_count())
.map(|s| menu.slot(s).cloned())
.collect();
let _ = menu.click(env, i32::from(p.slot), i32::from(p.button), input(p.input));
(0..menu.slot_count())
.filter(|&s| menu.slot(s) != before[s].as_ref())
.collect::<Vec<_>>()
});
if let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) {
for k in 0..3 {
f.items[k] = menu.slot(k).cloned();
}
// AbstractFurnaceBlockEntity.setItem: only a new input restarts cooking.
f.input_clicked(&self.res.recipes, items[0].clone());
}
host.play.entry(conn).or_default().crafting.furnace = Some((p.container, menu));
host.hub.inventory_clicked(conn, p, &self.world, &changed);
true
}
/// The guest opened a crafting table (`CraftingTableBlock.getMenuProvider`).
pub(super) fn lan_open_crafting(&mut self, host: &mut LanHost, conn: u32) {
self.lan_crafting_closed(host, conn, None);
+45 -1
View File
@@ -4,7 +4,7 @@ use super::{
hub::Hub,
menu::{self, Menu, Rules, Slots, View},
};
use crate::{items::ItemStack, world::World};
use crate::{containers::BlockEntity, items::ItemStack, world::World};
use horizon_protocol::{
item::Hashed,
packets::play::{self, Clientbound as C},
@@ -175,6 +175,7 @@ impl Hub {
state: 0,
sent: Vec::new(),
drag: None,
sent_data: Vec::new(),
});
self.send_menu_content(i, items);
}
@@ -416,6 +417,47 @@ impl Hub {
}
}
/// AbstractContainerMenu.broadcastChanges' data slots: a furnace's
/// AbstractFurnaceBlockEntity.dataAccess (lit time left, lit total, cooking
/// timer, cooking total), each sent when it changes.
fn sync_container_data(&mut self, i: usize, world: &World) {
let g = &mut self.guests[i];
let Some(m) = g.menu.as_mut() else {
return;
};
let Slots::Block(pos) = m.slots else {
return;
};
let Some(BlockEntity::Furnace(f)) = world.block_entities.get(&pos) else {
return;
};
let data = [
f.lit_remaining,
f.lit_total,
f.cooking_timer,
f.cooking_total,
]
.map(|v| v as i16);
m.sent_data.resize(data.len(), i16::MIN);
let window = m.window;
let changed: Vec<(usize, i16)> = data
.iter()
.enumerate()
.filter(|&(k, v)| m.sent_data[k] != *v)
.map(|(k, &v)| (k, v))
.collect();
for &(k, value) in &changed {
m.sent_data[k] = value;
}
for (k, value) in changed {
g.send(&C::ContainerSetData(play::ContainerSetData {
container: window,
id: k as i16,
value,
}));
}
}
/// Slot changes of open containers (AbstractContainerMenu.broadcastChanges).
pub fn sync_containers(&mut self, world: &World) {
for i in 0..self.guests.len() {
@@ -457,6 +499,8 @@ impl Hub {
}));
}
}
// Then the data slots, as broadcastChanges does.
self.sync_container_data(i, world);
}
}
}
+2
View File
@@ -28,6 +28,8 @@ pub struct Menu {
pub sent: Vec<Option<ItemStack>>,
/// An unfinished drag: (mode, slots).
pub drag: Option<(u8, Vec<usize>)>,
/// The data slots the client was last sent (ContainerSetData), a furnace's progress.
pub sent_data: Vec<i16>,
}
/// Where the slots of a container menu live.
@@ -125,7 +125,10 @@ public final class VanillaProtocolClient {
client.status(host, port);
client.join(host, port, name);
} catch (Exception e) {
client.lines.add("error " + client.state + " " + e);
// With its causes: a decoder error names the field it could not read there.
StringBuilder line = new StringBuilder("error " + client.state + " " + e);
for (Throwable c = e.getCause(); c != null; c = c.getCause()) line.append(" / caused by ").append(c);
client.lines.add(line.toString());
} finally {
if (client.socket != null) client.socket.close();
}
@@ -427,7 +430,7 @@ public final class VanillaProtocolClient {
boolean loaded = false;
PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0);
long lastChunk = 0;
long deadline = System.currentTimeMillis() + 90_000;
long deadline = System.currentTimeMillis() + 150_000;
int step = 0;
long stepAt = 0;
// The death the console deals and the respawn after it (PlayerList.respawn): from the
@@ -440,6 +443,10 @@ public final class VanillaProtocolClient {
String runType = null;
int run = 0;
PositionMoveRotation respawnAt = null;
// The furnace set beside the respawn point: its window and the menu's state id.
net.minecraft.core.BlockPos furnace = null;
int furnaceWindow = -1;
int furnaceState = 0;
// The villager the server's console summoned beside the spawn.
int villager = -1;
net.minecraft.world.phys.Vec3 villagerAt = null;
@@ -500,6 +507,18 @@ public final class VanillaProtocolClient {
int pz = (int) Math.floor(position.position().z) >> 4;
if (p.x() == px && p.z() == pz && !respawning) chunkParts(p);
if (p.x() == px && p.z() == pz && respawnAt != null) respawnChunk = true;
} else if (packet instanceof net.minecraft.network.protocol.game.ClientboundContainerSetContentPacket p
&& furnaceWindow > 0 && p.containerId() == furnaceWindow) {
furnaceState = p.stateId();
record("play-furnace", "content " + p.items() + " carried " + p.carriedItem());
} else if (packet instanceof net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket p
&& furnaceWindow > 0 && p.getContainerId() == furnaceWindow) {
furnaceState = p.getStateId();
record("play-furnace", "slot " + p.getSlot() + " " + p.getItem());
} else if (packet instanceof net.minecraft.network.protocol.game.ClientboundContainerSetDataPacket p
&& furnaceWindow > 0 && p.getContainerId() == furnaceWindow && (p.getId() == 1 || p.getId() == 3)) {
// Lit total and cooking total; the time left and the cooking timer follow the ticks.
record("play-furnace", "data " + p.getId() + " " + p.getValue());
} else if (packet instanceof net.minecraft.network.protocol.game.ClientboundEntityEventPacket p
&& step == 5 + REPLIES.length && (int) (byte) field(p, "eventId") == 28) {
// Operator (level 4, as the console made it): it may run /kill on itself.
@@ -524,6 +543,10 @@ public final class VanillaProtocolClient {
&& p.getType() == net.minecraft.world.entity.EntityTypes.VILLAGER) {
villager = p.getId();
villagerAt = new net.minecraft.world.phys.Vec3(p.getX(), p.getY(), p.getZ());
} else if (packet instanceof net.minecraft.network.protocol.game.ClientboundOpenScreenPacket p && step > 6 + REPLIES.length) {
furnaceWindow = p.getContainerId();
record("play-furnace", "open " + net.minecraft.core.registries.BuiltInRegistries.MENU.getKey(p.getType()) + " "
+ json(p.getTitle()));
} else if (packet instanceof net.minecraft.network.protocol.game.ClientboundOpenScreenPacket p && step > 0) {
record("play-trade", "open " + p.getContainerId() + " "
+ net.minecraft.core.registries.BuiltInRegistries.MENU.getKey(p.getType()) + " "
@@ -614,6 +637,36 @@ public final class VanillaProtocolClient {
if (runType != null) record("play-respawn", runLine(runType, run));
record("play-respawn", "level loaded at " + text(respawnAt.position()));
send(new ServerboundPlayerLoadedPacket());
// A furnace two blocks south, an ore and its fuel.
furnace = net.minecraft.core.BlockPos.containing(respawnAt.position()).south(2);
send(new ServerboundChatCommandPacket("setblock " + furnace.getX() + " " + furnace.getY() + " " + furnace.getZ()
+ " minecraft:furnace"));
send(new ServerboundChatCommandPacket("give @s minecraft:raw_iron"));
send(new ServerboundChatCommandPacket("give @s minecraft:coal"));
step++;
stepAt = now;
} else if (step == 7 + REPLIES.length && now - stepAt > 1500) {
// Right-click it with an empty hand (AbstractFurnaceBlock.useWithoutItem).
send(new net.minecraft.network.protocol.game.ServerboundUseItemOnPacket(net.minecraft.world.InteractionHand.MAIN_HAND,
new net.minecraft.world.phys.BlockHitResult(net.minecraft.world.phys.Vec3.atCenterOf(furnace),
net.minecraft.core.Direction.NORTH, furnace, false), 1));
step++;
stepAt = now;
} else if (step >= 8 + REPLIES.length && step <= 10 + REPLIES.length && furnaceWindow > 0
&& now - stepAt > (step == 10 + REPLIES.length ? 12_000 : 1500)) {
// Shift-click the ore (hotbar 0) into the input, the coal (hotbar 1) into the
// fuel slot, and the smelted ingot out of the result; nothing claimed, so the
// server sends every slot the click changed.
int slot = step == 8 + REPLIES.length ? 30 : step == 9 + REPLIES.length ? 31 : 2;
record("play-furnace", "quick move " + slot);
send(new net.minecraft.network.protocol.game.ServerboundContainerClickPacket(furnaceWindow, furnaceState, (short) slot,
(byte) 0, net.minecraft.world.inventory.ContainerInput.QUICK_MOVE,
new it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap<>(), net.minecraft.network.HashedStack.EMPTY));
step++;
stepAt = now;
} else if (step == 11 + REPLIES.length && now - stepAt > 1500) {
send(new net.minecraft.network.protocol.game.ServerboundContainerClosePacket(furnaceWindow));
record("play-furnace", "closed");
return;
}
}
+9 -4
View File
@@ -12,7 +12,9 @@ chunks around the spawn (their bytes are compared: the world is loaded from
the same files), chats, runs /list and commands that fail (`/random value
7..7`, `/random value x`, `/trigger nope`), trades, is made an operator by
the console, runs /kill, respawns (as vanilla's LevelLoadTracker waits: until the "level
chunks load start" event and the chunk under the player) and leaves. The
chunks load start" event and the chunk under the player), sets a furnace
beside itself, gives itself raw iron and coal, opens the furnace, shift-clicks
the ore and the coal in and the ingot out, and leaves. The
console then runs `list` and `say` and `stop`. Compared, all of them
exactly:
@@ -29,6 +31,8 @@ exactly:
inventory (PlayerList.respawn; chat, chunk data, light and keep-alives
left out, runs of a packet counted), then that the level loads where the
player respawned (guests' death messages aren't Horizon's yet: left out);
- the furnace menu: its type and title, its contents, the slots each
shift-click changes and its lit and cooking totals;
- the console lines about the player, the chat, `list`, `say` and `stop`;
- the files the servers leave: server.properties (but the date line),
eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json.
@@ -204,9 +208,10 @@ CONSOLE_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*)$')
KEPT = re.compile(r'Tester|\[Server\]|\[Not Secure\]|There are|Stopping|Saving|ThreadedAnvilChunkStorage|All dimensions|Unknown or incomplete')
# Guests' death messages and the op's command feedback broadcast to the console aren't
# Horizon's yet (the client's own transcript leaves the death message out as well).
NOT_YET = re.compile(r'Tester was killed|Killed Tester|^\[Tester: ')
# Guests' death messages, the op's command feedback broadcast to the console and guests'
# advancement announcements aren't Horizon's yet (the client's own transcript leaves the
# death message out as well).
NOT_YET = re.compile(r'Tester was killed|Killed Tester|System chat: \[Tester: |Tester has made the advancement')
def console_lines(lines):