Files
horizon-airdef/crates/horizon-client/src/recipe_book.rs
T
Ludovic Marie eec52f9055 The recipe book draws as vanilla's widgets do; test_recipe_book_render_parity
horizon_client::recipe_book_screen lays out RecipeBookComponent's drawing
(background, search EditBox, tab buttons, the filter CycleButton, the page
of RecipeButtons with its number and arrows, the alternatives overlay,
tooltips) and GhostSlots as vanilla's GUI calls with their poses, and the
game paints them. The new harness builds vanilla's widgets for 600 random
states (VanillaRecipeBookScreenOracle) and compares every call: 600/600.

Ingredients keep their HolderSet order (tags in TagLoader order), which is
how the overlay and ghost slots cycle; the book centres over the menu on
narrow screens as widthTooNarrow does.

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

819 lines
31 KiB
Rust

//! The recipe book: what the book shows for each menu (`RecipeBookType`
//! tabs, `RecipeBookCategories`, groups), which recipes the player can craft
//! with the items at hand (`StackedItemContents.canCraft` and the biggest
//! craftable stack), where a recipe goes in a grid (`PlaceRecipeHelper`) and
//! the book's saved settings.
use crate::{
crafting::{Ingredient, Placement, Recipe, Recipes},
items::{ItemStack, Items},
};
use horizon_world::nbt::Tag;
use std::collections::{BTreeSet, HashMap};
/// `RecipeBookType`: the menus with a recipe book and their own settings.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum BookType {
Crafting,
Furnace,
BlastFurnace,
Smoker,
}
impl BookType {
pub const ALL: [Self; 4] = [
Self::Crafting,
Self::Furnace,
Self::BlastFurnace,
Self::Smoker,
];
pub fn index(self) -> usize {
self as usize
}
/// `RecipeBookSettings` keys in the player's `recipeBook` compound.
fn keys(self) -> (&'static str, &'static str) {
match self {
Self::Crafting => ("isGuiOpen", "isFilteringCraftable"),
Self::Furnace => ("isFurnaceGuiOpen", "isFurnaceFilteringCraftable"),
Self::BlastFurnace => (
"isBlastingFurnaceGuiOpen",
"isBlastingFurnaceFilteringCraftable",
),
Self::Smoker => ("isSmokerGuiOpen", "isSmokerFilteringCraftable"),
}
}
/// The tab buttons of the book's screen, search first.
pub fn tabs(self) -> Vec<Tab> {
use Category::*;
let categories: &[Category] = match self {
Self::Crafting => &[
CraftingEquipment,
CraftingBuilding,
CraftingMisc,
CraftingRedstone,
],
Self::Furnace => &[FurnaceFood, FurnaceBlocks, FurnaceMisc],
Self::BlastFurnace => &[BlastBlocks, BlastMisc],
Self::Smoker => &[SmokerFood],
};
std::iter::once(Tab::Search)
.chain(categories.iter().map(|c| Tab::Category(*c)))
.collect()
}
/// The categories the search tab lists, in `SearchRecipeBookCategory` order.
fn search_categories(self) -> &'static [Category] {
use Category::*;
match self {
Self::Crafting => &[
CraftingEquipment,
CraftingBuilding,
CraftingMisc,
CraftingRedstone,
],
Self::Furnace => &[FurnaceFood, FurnaceBlocks, FurnaceMisc],
Self::BlastFurnace => &[BlastBlocks, BlastMisc],
Self::Smoker => &[SmokerFood],
}
}
}
/// `RecipeBookCategories` of the four menus' recipes.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Category {
CraftingBuilding,
CraftingRedstone,
CraftingEquipment,
CraftingMisc,
FurnaceFood,
FurnaceBlocks,
FurnaceMisc,
BlastBlocks,
BlastMisc,
SmokerFood,
}
impl Category {
/// The `RecipeBookCategories` registry path.
pub fn name(self) -> &'static str {
match self {
Self::CraftingBuilding => "crafting_building_blocks",
Self::CraftingRedstone => "crafting_redstone",
Self::CraftingEquipment => "crafting_equipment",
Self::CraftingMisc => "crafting_misc",
Self::FurnaceFood => "furnace_food",
Self::FurnaceBlocks => "furnace_blocks",
Self::FurnaceMisc => "furnace_misc",
Self::BlastBlocks => "blast_furnace_blocks",
Self::BlastMisc => "blast_furnace_misc",
Self::SmokerFood => "smoker_food",
}
}
}
/// A tab button: the search tab or one category.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Tab {
Search,
Category(Category),
}
impl Tab {
/// The tab's icon items (`TabInfo`).
pub fn icons(self) -> (&'static str, Option<&'static str>) {
use Category::*;
match self {
Self::Search => ("minecraft:compass", None),
Self::Category(c) => match c {
CraftingEquipment => ("minecraft:iron_axe", Some("minecraft:golden_sword")),
CraftingBuilding => ("minecraft:bricks", None),
CraftingMisc => ("minecraft:lava_bucket", Some("minecraft:apple")),
CraftingRedstone => ("minecraft:redstone", None),
FurnaceFood | SmokerFood => ("minecraft:porkchop", None),
FurnaceBlocks => ("minecraft:stone", None),
FurnaceMisc => ("minecraft:lava_bucket", Some("minecraft:emerald")),
BlastBlocks => ("minecraft:redstone_ore", None),
BlastMisc => ("minecraft:iron_shovel", Some("minecraft:golden_leggings")),
},
}
}
}
/// `RecipeDisplay`: how a recipe is laid out.
#[derive(Clone, Debug)]
pub enum Display {
Shaped {
width: usize,
height: usize,
cells: Vec<Option<Ingredient>>,
},
Shapeless(Vec<Ingredient>),
/// A smelting-type recipe: its input; the fuel is any fuel.
Furnace(Ingredient),
}
/// One recipe of the book (`RecipeDisplayEntry`).
#[derive(Clone, Debug)]
pub struct Entry {
/// The recipe id, `minecraft:name`.
pub id: String,
/// `RecipeDisplayId`: the display's index among every recipe's displays
/// (`Book::set_display_ids`; -1 until then).
pub display_id: i32,
pub category: Category,
pub group: Option<String>,
pub display: Display,
pub result: ItemStack,
}
impl Entry {
/// The ingredients that must be matched to craft it, in order.
pub fn requirements(&self) -> Vec<&Ingredient> {
match &self.display {
Display::Shaped { cells, .. } => cells.iter().flatten().collect(),
Display::Shapeless(list) => list.iter().collect(),
Display::Furnace(i) => vec![i],
}
}
/// `CraftingRecipeBookComponent.canDisplay` for a grid.
pub fn fits(&self, grid_width: usize, grid_height: usize) -> bool {
match &self.display {
Display::Shaped { width, height, .. } => grid_width >= *width && grid_height >= *height,
Display::Shapeless(list) => grid_width * grid_height >= list.len(),
Display::Furnace(_) => true,
}
}
/// The size the recipe takes in the grid (`PlaceRecipeHelper`).
pub fn size_in(&self, grid_width: usize, grid_height: usize) -> (usize, usize) {
match &self.display {
Display::Shaped { width, height, .. } => (*width, *height),
_ => (grid_width, grid_height),
}
}
/// The ingredient of each grid cell the recipe fills, with its grid index
/// (the ghost recipe and the placement use the same layout).
pub fn grid_cells(&self, grid_width: usize, grid_height: usize) -> Vec<(Option<usize>, usize)> {
let (w, h) = self.size_in(grid_width, grid_height);
// One entry per recipe cell: the index of its ingredient, if any.
let entries: Vec<Option<usize>> = match &self.display {
Display::Shaped { cells, .. } => {
let mut next = 0;
cells
.iter()
.map(|c| {
c.as_ref().map(|_| {
next += 1;
next - 1
})
})
.collect()
}
Display::Shapeless(list) => (0..list.len()).map(Some).collect(),
Display::Furnace(_) => vec![Some(0)],
};
place_cells(grid_width, grid_height, w, h, &entries)
}
}
/// `PlaceRecipeHelper.placeRecipe`: the grid index each entry goes to; a
/// recipe smaller than half the grid is centred.
pub fn place_cells<T: Clone>(
grid_width: usize,
grid_height: usize,
recipe_width: usize,
recipe_height: usize,
entries: &[T],
) -> Vec<(T, usize)> {
let mut out = Vec::new();
let mut iter = entries.iter().peekable();
let mut index = 0usize;
let mut y = 0;
while y < grid_height {
let center_rows = (recipe_height as f32) < grid_height as f32 / 2.0;
let start_row = (grid_height as f32 / 2.0 - recipe_height as f32 / 2.0).floor() as usize;
if center_rows && start_row > y {
index += grid_width;
y += 1;
}
for x in 0..grid_width {
if iter.peek().is_none() {
return out;
}
let center_cols = (recipe_width as f32) < grid_width as f32 / 2.0;
let start_col = (grid_width as f32 / 2.0 - recipe_width as f32 / 2.0).floor() as usize;
let mut total = recipe_width;
let mut add = x < recipe_width;
if center_cols {
total = start_col + recipe_width;
add = start_col <= x && x < start_col + recipe_width;
}
if add {
if let Some(item) = iter.next() {
out.push((item.clone(), index));
}
} else if total == x {
index += grid_width - x;
break;
}
index += 1;
}
y += 1;
}
out
}
/// A group of recipes shown as one button (`RecipeCollection`).
#[derive(Clone, Debug, Default)]
pub struct Collection {
/// Indices into `Book::entries`.
pub entries: Vec<usize>,
}
/// Every recipe the menus' books can show.
#[derive(Default)]
pub struct Book {
pub entries: Vec<Entry>,
/// `Recipe.placementInfo` by recipe id: what the book's craftable test
/// and the server's placement look for.
pub placements: HashMap<String, Placement>,
/// Item id -> registry position, to order ingredient choices.
pub order: HashMap<String, usize>,
}
fn crafting_category(name: &str) -> Category {
match name {
"building" => Category::CraftingBuilding,
"redstone" => Category::CraftingRedstone,
"equipment" => Category::CraftingEquipment,
_ => Category::CraftingMisc,
}
}
impl Book {
pub fn build(recipes: &Recipes, items: &Items) -> Self {
let mut entries = Vec::new();
let group = |s: &str| (!s.is_empty()).then(|| s.to_owned());
for (name, recipe) in &recipes.crafting {
let (category, grp) = recipes.meta.get(name).cloned().unwrap_or_default();
let (display, result) = match recipe {
Recipe::Shaped {
width,
height,
cells,
result,
} => (
Display::Shaped {
width: *width,
height: *height,
cells: cells.clone(),
},
result,
),
Recipe::Shapeless {
ingredients,
result,
} => (Display::Shapeless(ingredients.clone()), result),
// Custom recipes are never learnable; the others show one
// display per material count (TransmuteRecipe.display).
Recipe::Special(special) => {
for (display, result) in special.book_displays() {
entries.push(Entry {
id: format!("minecraft:{name}"),
display_id: -1,
category: crafting_category(&category),
group: group(&grp),
display,
result,
});
}
continue;
}
};
entries.push(Entry {
id: format!("minecraft:{name}"),
display_id: -1,
category: crafting_category(&category),
group: group(&grp),
display,
result: result.create(),
});
}
for c in &recipes.cooking {
let category = match (c.kind.as_str(), c.category.as_str()) {
("minecraft:smelting", "food") => Category::FurnaceFood,
("minecraft:smelting", "blocks") => Category::FurnaceBlocks,
("minecraft:smelting", _) => Category::FurnaceMisc,
("minecraft:blasting", "blocks") => Category::BlastBlocks,
("minecraft:blasting", _) => Category::BlastMisc,
("minecraft:smoking", _) => Category::SmokerFood,
_ => continue,
};
entries.push(Entry {
id: format!("minecraft:{}", c.id),
display_id: -1,
category,
group: group(&c.group),
display: Display::Furnace(c.ingredient.clone()),
result: c.result.clone(),
});
}
let order = items
.order
.iter()
.enumerate()
.map(|(i, id)| (id.to_string(), i))
.collect();
let placements = entries
.iter()
.filter_map(|e| Some((e.id.clone(), recipes.placement(&e.id)?)))
.collect();
Self {
entries,
placements,
order,
}
}
/// Which book a recipe id belongs to.
pub fn book_of(&self, id: &str) -> Option<BookType> {
let e = self.entries.iter().find(|e| e.id == id)?;
Some(match e.category {
Category::CraftingBuilding
| Category::CraftingRedstone
| Category::CraftingEquipment
| Category::CraftingMisc => BookType::Crafting,
Category::FurnaceFood | Category::FurnaceBlocks | Category::FurnaceMisc => {
BookType::Furnace
}
Category::BlastBlocks | Category::BlastMisc => BookType::BlastFurnace,
Category::SmokerFood => BookType::Smoker,
})
}
/// `RecipeDisplayEntry.canCraft`: the recipe's placement ingredients
/// (`craftingRequirements`) can all be found in `stock`.
pub fn can_craft(&self, entry: &Entry, stock: &crate::menu::place::Stacked) -> bool {
self.placements
.get(&entry.id)
.is_some_and(|p| stock.can_craft(&p.ingredients.iter().collect::<Vec<_>>()))
}
/// Numbers the entries as `RecipeManager.unpackRecipeInfo` numbers the
/// displays: every recipe in id order, each with its displays.
pub fn set_display_ids(&mut self, index: &RecipeIndex) {
let mut previous: Option<(String, i32)> = None;
for e in &mut self.entries {
let next = match &previous {
Some((id, last)) if *id == e.id => last + 1,
_ => index.first_display(&e.id).unwrap_or(-1),
};
e.display_id = next;
previous = Some((e.id.clone(), next));
}
}
/// The entry of a display id.
pub fn entry_of_display(&self, display: i32) -> Option<usize> {
self.entries.iter().position(|e| e.display_id == display)
}
pub fn entry(&self, id: &str) -> Option<&Entry> {
self.entries.iter().find(|e| e.id == id)
}
/// `ingredient`'s items as its display resolves them (the cycling order
/// of ghost slots and the overlay).
pub fn items_of(&self, ingredient: &Ingredient) -> Vec<String> {
ingredient.1.clone()
}
/// The recipe-book search tree (`SessionSearchTrees.updateRecipes`, a
/// `FullTextSearchTree`): without a colon the text must occur in a
/// tooltip line of a result item of the collection; `namespace:path`
/// needs the namespace in a result id and the path in an id or a line.
pub fn collection_matches(
&self,
entries: &[usize],
query: &str,
tooltip: &dyn Fn(&ItemStack) -> Vec<String>,
) -> bool {
let query = query.to_lowercase();
let results: Vec<&ItemStack> = entries.iter().map(|&i| &self.entries[i].result).collect();
let in_lines = |text: &str| {
results
.iter()
.any(|r| tooltip(r).iter().any(|l| l.to_lowercase().contains(text)))
};
let Some((namespace, path)) = query.split_once(':') else {
return in_lines(&query);
};
let (namespace, path) = (namespace.trim(), path.trim());
let id = |r: &&ItemStack| {
let (ns, p) = r.item.split_once(':').unwrap_or(("minecraft", &r.item));
(ns.to_lowercase(), p.to_lowercase())
};
results.iter().any(|r| id(r).0.contains(namespace))
&& (results.iter().any(|r| id(r).1.contains(path)) || in_lines(path))
}
}
/// A `RecipeDisplayId` as a `HashMap` key: a record of one int hashes as it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct DisplayKey(i32);
impl horizon_world::java_hash_map::JavaKey for DisplayKey {
fn java_hash(&self) -> i32 {
self.0
}
fn tie_break(&self, other: &Self) -> std::cmp::Ordering {
if self.0 < other.0 {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
}
}
/// `ClientRecipeBook`: the displays the client was sent, in a `HashMap` whose
/// iteration order (the display ids' buckets, then arrival order) decides the
/// order of the book's buttons.
#[derive(Default)]
pub struct ClientBook {
known: horizon_world::java_hash_map::JavaHashMap<DisplayKey, ()>,
pub highlight: std::collections::HashSet<i32>,
}
impl ClientBook {
pub fn add(&mut self, display: i32) {
self.known.put(DisplayKey(display), ());
}
pub fn remove(&mut self, display: i32) {
self.known.remove(&DisplayKey(display));
self.highlight.remove(&display);
}
/// `clear` (a replacing `ClientboundRecipeBookAddPacket`): the table keeps its size.
pub fn clear(&mut self) {
self.known.clear();
self.highlight.clear();
}
/// The known display ids in iteration order.
pub fn displays(&self) -> Vec<i32> {
self.known.entries().map(|(k, _)| k.0).collect()
}
/// `rebuildCollections` then `getCollection(tab)`: per category, a
/// collection per entry without a group or per group, in the order the
/// known displays iterate; the search tab lists its categories in turn.
pub fn collections(&self, book: &Book, book_type: BookType, tab: Tab) -> Vec<Collection> {
let by_display: HashMap<i32, usize> = book
.entries
.iter()
.enumerate()
.map(|(i, e)| (e.display_id, i))
.collect();
let mut by_category: HashMap<Category, Vec<Collection>> = HashMap::new();
let mut groups: HashMap<(Category, String), (Category, usize)> = HashMap::new();
for display in self.displays() {
let Some(&i) = by_display.get(&display) else {
continue;
};
let e = &book.entries[i];
let list = by_category.entry(e.category).or_default();
match &e.group {
None => list.push(Collection { entries: vec![i] }),
Some(g) => match groups.get(&(e.category, g.clone())) {
Some(&(_, at)) => list[at].entries.push(i),
None => {
groups.insert((e.category, g.clone()), (e.category, list.len()));
list.push(Collection { entries: vec![i] });
}
},
}
}
let categories: Vec<Category> = match tab {
Tab::Search => book_type.search_categories().to_vec(),
Tab::Category(c) => vec![c],
};
categories
.into_iter()
.flat_map(|c| by_category.remove(&c).unwrap_or_default())
.collect()
}
}
/// What the server knows of each recipe of the datapack: whether it is
/// special (`Recipe.isSpecial`: never learnable) and how many recipe book
/// displays it has (`RecipeManager.listDisplaysForRecipe`).
#[derive(Clone, Debug, Default)]
pub struct RecipeIndex {
/// Recipe id -> (special, display count, first display id).
recipes: std::collections::BTreeMap<String, (bool, usize, i32)>,
}
impl RecipeIndex {
/// Reads `data/minecraft/recipe/*.json` under `root`.
pub fn load(root: &std::path::Path) -> Self {
let mut recipes = std::collections::BTreeMap::new();
let dir = root.join("data/minecraft/recipe");
for entry in std::fs::read_dir(dir).into_iter().flatten().flatten() {
let path = entry.path();
if path.extension().is_none_or(|e| e != "json") {
continue;
}
let (Some(stem), Some(v)) = (
path.file_stem().and_then(|s| s.to_str()),
std::fs::read(&path)
.ok()
.and_then(|b| serde_json::from_slice::<serde_json::Value>(&b).ok()),
) else {
continue;
};
let kind = v["type"].as_str().unwrap_or_default();
let special = kind.starts_with("minecraft:crafting_special_")
|| kind == "minecraft:crafting_decorated_pot";
let displays = if special {
0
} else if kind == "minecraft:crafting_transmute" {
// TransmuteRecipe.display: one per material count.
match &v["material_count"] {
serde_json::Value::Object(o) => {
let min = o.get("min").and_then(|m| m.as_u64()).unwrap_or(1);
let max = o.get("max").and_then(|m| m.as_u64()).unwrap_or(8);
(max.saturating_sub(min) + 1) as usize
}
_ => 1,
}
} else {
1
};
recipes.insert(format!("minecraft:{stem}"), (special, displays, 0));
}
// RecipeManager keeps its recipes sorted by id and numbers the displays in that order.
let mut next = 0;
for r in recipes.values_mut() {
r.2 = next;
next += r.1 as i32;
}
Self { recipes }
}
pub fn contains(&self, id: &str) -> bool {
self.recipes.contains_key(id)
}
pub fn special(&self, id: &str) -> bool {
self.recipes.get(id).is_some_and(|r| r.0)
}
pub fn displays(&self, id: &str) -> usize {
self.recipes.get(id).map_or(0, |r| r.1)
}
/// Every recipe id, in id order.
pub fn ids(&self) -> impl Iterator<Item = &String> {
self.recipes.keys()
}
/// The display ids of a recipe (none for a special one).
pub fn display_ids(&self, id: &str) -> std::ops::Range<i32> {
self.recipes.get(id).map_or(0..0, |r| r.2..r.2 + r.1 as i32)
}
pub fn first_display(&self, id: &str) -> Option<i32> {
self.recipes.get(id).filter(|r| r.1 > 0).map(|r| r.2)
}
}
/// `ServerRecipeBook`: the player's `recipeBook` compound, the known recipes
/// and the ones still to highlight (`toBeDisplayed`) with the open and
/// filter settings of each `RecipeBookType`.
#[derive(Clone, Debug, Default)]
pub struct Settings {
/// `isGuiOpen`-style flags per `BookType`.
pub open: [bool; 4],
pub filtering: [bool; 4],
/// Known recipe ids (`recipes`).
pub recipes: BTreeSet<String>,
/// Recipes not yet shown in the book (`toBeDisplayed`): they animate once.
pub to_be_displayed: BTreeSet<String>,
}
impl Settings {
/// `ServerRecipeBook.addRecipes`: the displays added (the count
/// `/recipe give` reports) and the recipes now known, in order.
pub fn add_recipes(&mut self, index: &RecipeIndex, ids: &[String]) -> (usize, Vec<String>) {
let mut displays = 0;
let mut added = Vec::new();
for id in ids {
if !self.recipes.contains(id) && !index.special(id) {
self.recipes.insert(id.clone());
self.to_be_displayed.insert(id.clone());
displays += index.displays(id);
added.push(id.clone());
}
}
(displays, added)
}
/// `ServerRecipeBook.removeRecipes`: the displays removed.
pub fn remove_recipes(&mut self, index: &RecipeIndex, ids: &[String]) -> usize {
let mut displays = 0;
for id in ids {
if self.recipes.remove(id) {
self.to_be_displayed.remove(id);
displays += index.displays(id);
}
}
displays
}
/// `removeHighlight` (`ServerboundRecipeBookSeenRecipePacket`).
pub fn remove_highlight(&mut self, id: &str) {
self.to_be_displayed.remove(id);
}
pub fn knows(&self, id: &str) -> bool {
self.recipes.contains(id)
}
/// The compound as saved, every recipe kept.
pub fn read(tag: Option<&Tag>) -> Self {
Self::read_valid(tag, &|_| true)
}
/// `ServerRecipeBook.loadUntrusted`: unknown recipes are dropped. Without
/// both lists the compound does not decode and nothing is read.
pub fn read_valid(tag: Option<&Tag>, valid: &dyn Fn(&str) -> bool) -> Self {
let mut s = Self::default();
let Some(tag) = tag else { return s };
let ids = |key: &str| match tag.get(key) {
Some(Tag::List(list)) => Some(
list.iter()
.filter_map(|t| match t {
Tag::String(id) => Some(id.to_string_lossy()),
_ => None,
})
.map(|id| {
if id.contains(':') {
id
} else {
format!("minecraft:{id}")
}
})
.filter(|id| valid(id))
.collect::<BTreeSet<String>>(),
),
_ => None,
};
let (Some(recipes), Some(to_be_displayed)) = (ids("recipes"), ids("toBeDisplayed")) else {
return s;
};
let flag = |k: &str| matches!(tag.get(k), Some(Tag::Byte(b)) if *b != 0);
for b in BookType::ALL {
let (open, filter) = b.keys();
s.open[b.index()] = flag(open);
s.filtering[b.index()] = flag(filter);
}
s.recipes = recipes;
s.to_be_displayed = to_be_displayed;
s
}
/// The compound written to the player file (vanilla's lists follow an
/// identity hash order; these are sorted).
pub fn write(&self) -> Tag {
let mut entries: Vec<(&str, Tag)> = Vec::new();
for b in BookType::ALL {
let (open, filter) = b.keys();
// The codec omits settings at their default (false).
if self.open[b.index()] {
entries.push((open, Tag::Byte(1)));
}
if self.filtering[b.index()] {
entries.push((filter, Tag::Byte(1)));
}
}
let list = |ids: &BTreeSet<String>| {
Tag::List(ids.iter().map(|i| Tag::String(i.as_str().into())).collect())
};
entries.push(("recipes", list(&self.recipes)));
entries.push(("toBeDisplayed", list(&self.to_be_displayed)));
Tag::compound(entries)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ing(items: &[&str]) -> Ingredient {
Ingredient::of(items.iter().map(|s| (*s).to_owned()).collect())
}
#[test]
fn small_recipes_are_centred_in_the_grid() {
// One ingredient in a 3x3 grid goes to the middle cell.
assert_eq!(place_cells(3, 3, 1, 1, &['a']), vec![('a', 4)]);
// A 2x2 recipe fills the top-left corner.
let cells: Vec<usize> = place_cells(3, 3, 2, 2, &['a', 'b', 'c', 'd'])
.into_iter()
.map(|(_, i)| i)
.collect();
assert_eq!(cells, vec![0, 1, 3, 4]);
// A one-row recipe sits on the middle row.
let row: Vec<usize> = place_cells(3, 3, 3, 1, &['a', 'b', 'c'])
.into_iter()
.map(|(_, i)| i)
.collect();
assert_eq!(row, vec![3, 4, 5]);
// The 2x2 grid is never centred.
let small: Vec<usize> = place_cells(2, 2, 1, 1, &['a'])
.into_iter()
.map(|(_, i)| i)
.collect();
assert_eq!(small, vec![0]);
}
#[test]
fn collections_group_recipes_of_a_category() {
let entry = |id: &str, display_id, group: Option<&str>, category| Entry {
id: id.into(),
display_id,
category,
group: group.map(str::to_owned),
display: Display::Shapeless(vec![ing(&["minecraft:stick"])]),
result: ItemStack::new("minecraft:stick", 1),
};
let book = Book {
entries: vec![
entry("minecraft:a", 0, Some("planks"), Category::CraftingBuilding),
entry("minecraft:b", 1, Some("planks"), Category::CraftingBuilding),
entry("minecraft:c", 2, None, Category::CraftingBuilding),
entry("minecraft:d", 3, Some("planks"), Category::CraftingMisc),
],
placements: HashMap::new(),
order: HashMap::new(),
};
let mut client = ClientBook::default();
for d in 0..4 {
client.add(d);
}
let tab = Tab::Category(Category::CraftingBuilding);
let c = client.collections(&book, BookType::Crafting, tab);
assert_eq!(c.len(), 2);
assert_eq!(c[0].entries, vec![0, 1]);
// The search tab lists every category; the same group in another category is separate.
let all = client.collections(&book, BookType::Crafting, Tab::Search);
assert_eq!(all.len(), 3);
// Displays the client was not sent are not listed.
client.clear();
client.add(2);
assert_eq!(client.collections(&book, BookType::Crafting, tab).len(), 1);
}
#[test]
fn display_ids_collide_in_a_small_table() {
// A 16-bucket table: 17 and 1 share a bucket and keep arrival order.
let mut client = ClientBook::default();
for d in [17, 3, 1] {
client.add(d);
}
assert_eq!(client.displays(), vec![17, 1, 3]);
}
#[test]
fn search_reads_tooltip_lines_and_ids() {
let book = Book {
entries: vec![Entry {
id: "minecraft:stick".into(),
display_id: 0,
category: Category::CraftingMisc,
group: None,
display: Display::Shapeless(Vec::new()),
result: ItemStack::new("minecraft:stick", 4),
}],
placements: HashMap::new(),
order: HashMap::new(),
};
let lines = |s: &ItemStack| vec![format!("Item {}", s.item), "Stick".to_owned()];
assert!(book.collection_matches(&[0], "STI", &lines));
// Plain text searches tooltips only, not the registry path.
assert!(!book.collection_matches(&[0], "stick:", &lines));
assert!(book.collection_matches(&[0], "minecraft:stic", &lines));
assert!(!book.collection_matches(&[0], "other:stick", &lines));
}
#[test]
fn settings_round_trip() {
let mut s = Settings::default();
s.open[BookType::Furnace.index()] = true;
s.filtering[BookType::Crafting.index()] = true;
s.recipes = ["minecraft:stick".to_owned()].into();
let back = Settings::read(Some(&s.write()));
assert_eq!(back.open, s.open);
assert_eq!(back.filtering, s.filtering);
assert_eq!(back.recipes, s.recipes);
}
}