Files
horizon-airdef/crates/horizon-client/examples/cauldron_fixture.rs
T
Ludovic Marie 3e868683ba Anvil and smithing table on the menu engine; LAN guests use them
AnvilMenu and SmithingMenu (ItemCombinerMenu) now run on
horizon_client::menu: their slot rules, createResult (the anvil's cost
data slot, repair count and rename, the smithing table's hasRecipeError),
mayPickup with the player's levels, the results' onTake and
quickMoveStack, verified against the vanilla classes by
test_menu_click_parity with repairs, merges, renames (a new RENAME op,
AnvilMenu.setItemName) and the player's levels. The harness found the
anvil treating only enchanted books as books (vanilla: anything with
stored enchantments), now fixed, and the grindstone's and anvil's result
slots now count for a double click's pick-all as in vanilla.

Single player routes both screens' clicks, drags and double clicks
through the engine; the anvil's wear and level events 1030/1029, and the
smithing table's 1044, play with vanilla's random pitch.

The LAN host opens minecraft:anvil and minecraft:smithing for guests,
handles ServerboundRenameItemPacket (new in horizon-protocol), charges the
guest's levels and wears the anvil; guests send the rename box as
AnvilScreen.onNameChanged does and show the server's cost. The control
socket's `type` now reaches the anvil's rename box.

Part of #868.

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

156 lines
5.1 KiB
Rust

//! Cauldron interactions for tools/test_cauldron_parity.py: reads one JSON
//! case per line and prints "result TAB events TAB inventory".
//!
//! usage: cauldron_fixture RESOURCES INPUT OUTPUT
use horizon_client::{
cauldron::{Outcome, Player, interact, removes_dye},
crafting::Recipes,
item_data::{canon_stack, parse_stack},
items::{ItemStack, Items},
menu::{Env, Kind, Menu},
};
use horizon_world::chunk::BlockState;
use serde_json::Value;
use std::{fs, path::Path};
struct Stand<'a> {
menu: Menu,
env: &'a Env<'a>,
events: Vec<String>,
hand: Option<ItemStack>,
}
impl Player for Stand<'_> {
fn creative(&self) -> bool {
self.menu.creative
}
fn hand(&mut self) -> &mut Option<ItemStack> {
&mut self.hand
}
fn add(&mut self, stack: &mut ItemStack) -> bool {
// The held stack lives in inventory slot 0 for Inventory.add.
self.menu.set_player_item(0, self.hand.take());
let added = self.menu.give(self.env, stack);
self.hand = self.menu.player_item(0).cloned();
self.menu.set_player_item(0, None);
added
}
fn contains(&self, stack: &ItemStack) -> bool {
self.menu.player_contains(stack)
|| self
.hand
.as_ref()
.is_some_and(|h| h.same_item_same_components(stack))
}
fn drop_stack(&mut self, stack: ItemStack) {
self.events
.push(format!("drop {}", canon_stack(Some(&stack))));
}
}
fn parse(text: &str) -> BlockState {
let (name, props) = text.split_once('|').unwrap_or((text, ""));
BlockState {
name: name.to_owned(),
properties: props
.split(',')
.filter(|p| !p.is_empty())
.filter_map(|p| p.split_once('='))
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect(),
}
}
fn describe(state: &BlockState) -> String {
let props: Vec<String> = state
.properties
.iter()
.map(|(k, v)| format!("{k}={v}"))
.collect();
format!("{}|{}", state.name, props.join(","))
}
fn main() -> horizon_client::Result<()> {
let args: Vec<String> = std::env::args().collect();
let root = Path::new(
args.get(1)
.ok_or("usage: cauldron_fixture RESOURCES INPUT OUTPUT")?,
);
let input = fs::read_to_string(args.get(2).ok_or("missing INPUT")?)?;
let items = Items::catalog(root)?;
let recipes = Recipes::load(root, &items)?;
let brewing = horizon_client::brewing::Brewing::load(root, &items);
// The inventory menu never reads banner patterns.
let banners = horizon_client::banner::Banners::default();
let station_recipes = horizon_client::stations::StationRecipes::default();
let enchantments = horizon_client::enchant::Enchantments::default();
let names = horizon_client::creative::Names::default();
let env = Env {
items: &items,
recipes: &recipes,
brewing: &brewing,
banners: &banners,
station_recipes: &station_recipes,
enchantments: &enchantments,
names: &names,
trade: None,
};
let mut out = String::new();
for line in input.lines() {
let case: Value = serde_json::from_str(line)?;
let mut menu = Menu::new(Kind::Inventory);
menu.creative = case["creative"].as_bool().unwrap_or(false);
for pair in case["inventory"].as_array().ok_or("inventory")? {
menu.set_player_item(
pair[0].as_u64().unwrap_or(0) as usize,
parse_stack(pair[1].as_str().unwrap_or("-"))?,
);
}
let held = parse_stack(case["held"].as_str().unwrap_or("-"))?;
let mut stand = Stand {
menu,
env: &env,
events: Vec::new(),
hand: held,
};
let cauldron = parse(case["state"].as_str().unwrap_or_default());
let dye = stand
.hand
.as_ref()
.is_some_and(|h| removes_dye(&items, &h.item));
let result = interact(
&cauldron,
case["under_water"].as_bool().unwrap_or(false),
dye,
&mut stand,
);
if let Some(block) = &result.block {
stand.events.push(format!("set {}", describe(block)));
}
if let Some(sound) = result.sound {
stand.events.push(format!("sound {sound}"));
}
let kind = match result.outcome {
Outcome::Success => "success",
Outcome::Consume => "consume",
Outcome::Pass => "pass",
};
stand.menu.set_player_item(0, stand.hand.take());
let inventory: Vec<String> = (0..41)
.filter_map(|i| {
stand
.menu
.player_item(i)
.map(|s| format!("{i}={}", canon_stack(Some(s))))
})
.collect();
out += &format!(
"{kind}\t{}\t{}\n",
stand.events.join(";"),
inventory.join(" ")
);
}
fs::write(args.get(3).ok_or("missing OUTPUT")?, out)?;
Ok(())
}