Add the recipe book

The inventory, crafting table, furnace, blast furnace and smoker screens get the green recipe book button, which opens the book beside the menu and moves the menu over. The book has the search tab and one tab per category (hidden until it holds a recipe), a search box matching recipe names (or @namespace), the craftable-only toggle, pages of 20 recipe buttons that cycle through the recipes of a group, right-click for the alternatives overlay, and tooltips. Clicking a recipe moves the items from the inventory into the grid (shift fills the biggest stack, clicking again adds one more set), and when items are missing the grid is cleared and the recipe is shown as a ghost over the slots.

Which recipes appear comes from the advancement rewards already unlocked; the open and craftable-only settings of each book and the unlocked recipes are saved in the player's recipeBook compound. Recipes now keep their category and group from the data pack.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-09 13:08:12 +02:00
parent f9eaae5a46
commit 6b80f9c1aa
8 changed files with 1965 additions and 3 deletions
+18 -1
View File
@@ -146,6 +146,11 @@ impl Special {
}
#[derive(Clone, Debug)]
pub struct Cooking {
/// The recipe's file name (its id without namespace).
pub id: String,
/// `category` (`food`, `blocks`, `misc`) and `group` of the recipe book.
pub category: String,
pub group: String,
pub kind: String,
pub ingredient: Ingredient,
pub result: ItemStack,
@@ -155,6 +160,8 @@ pub struct Cooking {
pub struct Recipes {
pub crafting: Vec<(String, Recipe)>,
pub cooking: Vec<Cooking>,
/// `category` and `group` of each crafting recipe, by file name.
pub meta: std::collections::HashMap<String, (String, String)>,
}
fn result(v: &Value) -> Option<ItemStack> {
Some(ItemStack {
@@ -168,6 +175,7 @@ impl Recipes {
pub fn load(root: &Path, items: &Items) -> Result<Self> {
let mut crafting = Vec::new();
let mut cooking = Vec::new();
let mut meta = std::collections::HashMap::new();
let mut files: Vec<_> = fs::read_dir(root.join("data/minecraft/recipe"))?
.flatten()
.map(|e| e.path())
@@ -181,6 +189,8 @@ impl Recipes {
.unwrap_or("")
.to_owned();
let v: Value = serde_json::from_slice(&fs::read(&path)?)?;
let text = |k: &str| v[k].as_str().unwrap_or("").to_owned();
meta.insert(name.clone(), (text("category"), text("group")));
match v["type"].as_str().unwrap_or("") {
"minecraft:crafting_shaped" => {
let pattern: Vec<Vec<char>> = v["pattern"]
@@ -274,6 +284,9 @@ impl Recipes {
result(&v["result"]),
) {
cooking.push(Cooking {
id: name,
category: text("category"),
group: text("group"),
kind: kind.to_owned(),
ingredient,
result,
@@ -285,7 +298,11 @@ impl Recipes {
_ => {}
}
}
Ok(Self { crafting, cooking })
Ok(Self {
crafting,
cooking,
meta,
})
}
/// Match a `size`x`size` grid (row-major) against the crafting recipes.
pub fn craft(&self, grid: &[Option<ItemStack>], size: usize) -> Option<ItemStack> {
+20 -1
View File
@@ -44,6 +44,7 @@ mod perspective;
mod piglins;
mod postprocess;
mod potions;
mod recipe_book;
mod remote;
mod render_control;
mod render_tick;
@@ -611,6 +612,8 @@ struct Game<'a> {
pending_shulker: HashMap<[i32; 3], Vec<(u8, ItemStack)>>,
/// Layers of broken banners waiting for their dropped item.
pending_banner: HashMap<[i32; 3], horizon_client::containers::Banner>,
/// The recipe book of the open screen (game/recipe_book.rs).
recipe_ui: recipe_book::RecipeUi,
/// The player's fishing hook.
hook: Option<fishing::Hook>,
tridents: Vec<trident::ThrownTrident>,
@@ -3028,7 +3031,7 @@ impl Game<'_> {
}
fn inventory_origin(&self, batch: &HudBatch) -> (f32, f32) {
(
(batch.width - 176.0) / 2.0,
(batch.width - 176.0) / 2.0 + self.recipe_book_shift(batch.width),
(batch.height - self.screen_height()) / 2.0,
)
}
@@ -3344,6 +3347,7 @@ impl Game<'_> {
self.draw_item(batch, &stack, ox + sx, oy + sy);
}
}
self.draw_recipe_book(batch);
if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) {
let (sx, sy) = self.slot_position(slot);
batch.fill(
@@ -3362,6 +3366,7 @@ impl Game<'_> {
}
pub(super) fn close_container(&mut self) {
self.close_station();
self.recipe_ui.reset_screen();
if self.remote.is_some() {
return self.remote_close_container();
}
@@ -3411,6 +3416,9 @@ impl Game<'_> {
self.close_cart_container();
}
fn click_inventory(&mut self, right: bool) {
if self.recipe_book_click(right) {
return;
}
if self.station_click(right) {
return;
}
@@ -6609,6 +6617,7 @@ impl Game<'_> {
self.ender_items = items.clone();
}
level.ender_items = self.ender_items.clone();
level.recipe_book.recipes = self.adv.recipes.iter().cloned().collect();
level.active_effects = self
.potions
.effects
@@ -6843,6 +6852,7 @@ impl Game<'_> {
Screen::Advancements => self.close_advancements(),
Screen::Chat => self.close_chat(),
},
KeyCode::KeyT if self.screen == Screen::Inventory => self.recipe_focus_search(),
KeyCode::KeyT if self.screen == Screen::Playing => self.open_chat(""),
// key.advancements.
KeyCode::KeyL if self.screen == Screen::Playing => self.open_advancements(false),
@@ -7277,6 +7287,14 @@ impl ApplicationHandler for Game<'_> {
}
self.creative_screen.scroll = 0.0;
}
WindowEvent::KeyboardInput { event, .. }
if self.recipe_search_focused()
&& event.state == ElementState::Pressed
&& !matches!(event.physical_key, PhysicalKey::Code(KeyCode::Escape)) =>
{
// The recipe book's search box takes every key but Escape.
self.recipe_key(&event);
}
WindowEvent::KeyboardInput { event, .. }
if self.station_typing()
&& event.state == ElementState::Pressed
@@ -7718,6 +7736,7 @@ pub fn run() -> Result<()> {
ender_open: None,
pending_shulker: HashMap::new(),
pending_banner: HashMap::new(),
recipe_ui: Default::default(),
hook: None,
tridents: Vec::new(),
rockets: Vec::new(),
@@ -293,6 +293,9 @@ impl Game<'_> {
match self.screen {
Screen::Chat => self.chat_type(text),
Screen::Options => self.ui.options.type_text(text),
Screen::Inventory if self.recipe_search_focused() => {
self.recipe_text(Some(text), false)
}
_ => {
return Err(
"type needs the chat screen (key t tap) or the language search".into(),
File diff suppressed because it is too large Load Diff
@@ -1116,6 +1116,11 @@ impl Game<'_> {
.is_some_and(|s| s.kind == Kind::Anvil && s.inputs[0].is_some())
}
pub(super) fn station_text(&mut self, text: Option<&str>, backspace: bool) {
// The recipe book's search box takes scripted typing too.
if self.recipe_search_focused() {
self.recipe_text(text, backspace);
return;
}
let Some(s) = self.station.as_mut() else {
return;
};
+1
View File
@@ -54,6 +54,7 @@ pub mod packs;
pub mod particles;
pub mod pose;
pub mod projectiles;
pub mod recipe_book;
pub mod save;
pub mod screen_effects;
pub mod settings;
+685
View File
@@ -0,0 +1,685 @@
//! 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, 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,
}
/// 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,
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>,
/// 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),
Recipe::Special(_) => continue,
};
entries.push(Entry {
id: format!("minecraft:{name}"),
category: crafting_category(&category),
group: group(&grp),
display,
result: result.clone(),
});
}
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),
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();
Self { entries, order }
}
/// The unlocked recipes of one tab as collections (`ClientRecipeBook.rebuildCollections`).
pub fn collections(
&self,
unlocked: &BTreeSet<String>,
book: BookType,
tab: Tab,
) -> Vec<Collection> {
let categories: Vec<Category> = match tab {
Tab::Search => book.search_categories().to_vec(),
Tab::Category(c) => vec![c],
};
let mut out = Vec::new();
for category in categories {
let mut by_group: HashMap<&str, usize> = HashMap::new();
let start = out.len();
for (i, e) in self.entries.iter().enumerate() {
if e.category != category || !unlocked.contains(&e.id) {
continue;
}
match e.group.as_deref().and_then(|g| by_group.get(g).copied()) {
Some(c) => {
let c: usize = c;
let collection: &mut Collection = &mut out[start + c];
collection.entries.push(i);
}
None => {
if let Some(g) = e.group.as_deref() {
by_group.insert(g, out.len() - start);
}
out.push(Collection { entries: vec![i] });
}
}
}
}
out
}
/// 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,
})
}
pub fn entry(&self, id: &str) -> Option<&Entry> {
self.entries.iter().find(|e| e.id == id)
}
/// `ingredient`'s items in registry order (the cycling order of ghost slots).
pub fn items_of(&self, ingredient: &Ingredient) -> Vec<String> {
let mut items: Vec<String> = ingredient.0.iter().cloned().collect();
items.sort_by_key(|i| (self.order.get(i).copied().unwrap_or(usize::MAX), i.clone()));
items
}
/// `RecipeCollection` search: the result's name or registry path, or
/// `@namespace`.
pub fn matches_search(&self, entry: &Entry, query: &str, name: &str) -> bool {
let query = query.to_lowercase();
if let Some(ns) = query.strip_prefix('@') {
return entry
.result
.item
.split(':')
.next()
.is_some_and(|n| n.starts_with(ns));
}
name.to_lowercase().contains(&query)
|| entry
.result
.item
.trim_start_matches("minecraft:")
.contains(&query)
}
}
/// `StackedItemContents`: simple stacks (undamaged, unenchanted, unnamed) by item.
#[derive(Clone, Debug, Default)]
pub struct Available {
pub counts: HashMap<String, u32>,
}
impl Available {
/// `accountSimpleStack`.
pub fn add(&mut self, stack: &ItemStack) {
if stack.count > 0
&& stack.damage == 0
&& stack.enchantments().is_empty()
&& stack.custom_name().is_none()
{
*self.counts.entry(stack.item.to_string()).or_default() += stack.count;
}
}
pub fn from_stacks<'a>(stacks: impl IntoIterator<Item = &'a ItemStack>) -> Self {
let mut a = Self::default();
for s in stacks {
a.add(s);
}
a
}
/// `StackedItemContents.canCraft(ingredients, null)`.
pub fn can_craft(&self, requirements: &[&Ingredient]) -> bool {
!requirements.is_empty() && pick(requirements, &self.counts, 1, &HashMap::new()).is_some()
}
/// `canCraft(recipe, amount, output)`: the item each ingredient uses when
/// `amount` sets can be made, preferring earlier registry items.
pub fn pick(
&self,
requirements: &[&Ingredient],
amount: u32,
order: &HashMap<String, usize>,
) -> Option<Vec<String>> {
pick(requirements, &self.counts, amount, order)
}
/// `getBiggestCraftableStack`: how many sets can be made, up to 99.
pub fn biggest_stack(&self, requirements: &[&Ingredient]) -> u32 {
if !self.can_craft(requirements) {
return 0;
}
let none = HashMap::new();
let (mut lo, mut hi) = (1u32, 99u32);
while lo < hi {
let mid = (lo + hi).div_ceil(2);
if pick(requirements, &self.counts, mid, &none).is_some() {
lo = mid;
} else {
hi = mid - 1;
}
}
lo
}
}
/// Bipartite assignment of one item to each ingredient where an item can
/// serve `count / amount` ingredients.
fn pick(
requirements: &[&Ingredient],
counts: &HashMap<String, u32>,
amount: u32,
order: &HashMap<String, usize>,
) -> Option<Vec<String>> {
let amount = amount.max(1);
let capacity = |item: &str| counts.get(item).copied().unwrap_or(0) / amount;
let candidates: Vec<Vec<String>> = requirements
.iter()
.map(|ing| {
let mut v: Vec<String> = ing.0.iter().filter(|i| capacity(i) > 0).cloned().collect();
v.sort_by_key(|i| (order.get(i).copied().unwrap_or(usize::MAX), i.clone()));
v
})
.collect();
let mut used: HashMap<String, Vec<usize>> = HashMap::new();
fn assign(
i: usize,
candidates: &[Vec<String>],
used: &mut HashMap<String, Vec<usize>>,
visited: &mut Vec<String>,
capacity: &dyn Fn(&str) -> u32,
) -> bool {
for item in &candidates[i] {
if visited.contains(item) {
continue;
}
visited.push(item.clone());
let holders = used.get(item).cloned().unwrap_or_default();
if (holders.len() as u32) < capacity(item) {
used.entry(item.clone()).or_default().push(i);
return true;
}
for j in holders {
if assign(j, candidates, used, visited, capacity) {
// j moved elsewhere: drop it from this item and take its place.
if let Some(list) = used.get_mut(item) {
list.retain(|x| *x != j);
list.push(i);
}
return true;
}
}
}
false
}
for i in 0..requirements.len() {
let mut visited = Vec::new();
if !assign(i, &candidates, &mut used, &mut visited, &capacity) {
return None;
}
}
let mut result = vec![String::new(); requirements.len()];
for (item, holders) in used {
for i in holders {
result[i] = item.clone();
}
}
Some(result)
}
/// The recipe book's saved state: the `recipeBook` compound of the player.
#[derive(Clone, Debug, Default)]
pub struct Settings {
/// `isGuiOpen`-style flags per `BookType`.
pub open: [bool; 4],
pub filtering: [bool; 4],
/// Unlocked recipe ids (`recipes`).
pub recipes: Vec<String>,
}
impl Settings {
pub fn read(tag: Option<&Tag>) -> Self {
let mut s = Self::default();
let Some(tag) = tag 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);
}
if let Some(Tag::List(list)) = tag.get("recipes") {
s.recipes = list
.iter()
.filter_map(|t| match t {
Tag::String(id) => Some(id.to_string_lossy()),
_ => None,
})
.collect();
}
s
}
/// The compound written to the player file; `toBeDisplayed` stays empty.
pub fn write(&self) -> Tag {
let mut entries: Vec<(&str, Tag)> = Vec::new();
for b in BookType::ALL {
let (open, filter) = b.keys();
entries.push((open, Tag::Byte(i8::from(self.open[b.index()]))));
entries.push((filter, Tag::Byte(i8::from(self.filtering[b.index()]))));
}
let list = |ids: &[String]| {
Tag::List(ids.iter().map(|i| Tag::String(i.as_str().into())).collect())
};
entries.push(("recipes", list(&self.recipes)));
entries.push(("toBeDisplayed", Tag::List(Vec::new())));
Tag::compound(entries)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ing(items: &[&str]) -> Ingredient {
Ingredient(items.iter().map(|s| (*s).to_owned()).collect())
}
fn have(items: &[(&str, u32)]) -> Available {
Available {
counts: items.iter().map(|(i, n)| ((*i).to_owned(), *n)).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 a_single_item_can_fill_several_ingredients() {
let planks = ing(&["minecraft:oak_planks", "minecraft:birch_planks"]);
let reqs = [&planks, &planks, &planks, &planks];
let a = have(&[("minecraft:oak_planks", 2), ("minecraft:birch_planks", 2)]);
assert!(a.can_craft(&reqs));
let none = HashMap::new();
// Two sets need 8 planks, but only 4 are there.
assert!(a.pick(&reqs, 2, &none).is_none());
assert!(a.pick(&reqs, 1, &none).is_some());
assert_eq!(a.biggest_stack(&reqs), 1);
assert_eq!(
have(&[("minecraft:oak_planks", 64)]).biggest_stack(&reqs),
16
);
}
#[test]
fn the_matching_reassigns_flexible_ingredients() {
// The first ingredient takes either item, the second only the stick:
// one stick must not be wasted on the first.
let any = ing(&["minecraft:stick", "minecraft:bone"]);
let stick = ing(&["minecraft:stick"]);
let a = have(&[("minecraft:stick", 1), ("minecraft:bone", 1)]);
let mut order = HashMap::new();
order.insert("minecraft:stick".to_owned(), 0);
order.insert("minecraft:bone".to_owned(), 1);
let picked = a.pick(&[&any, &stick], 1, &order).expect("matching");
assert_eq!(picked, vec!["minecraft:bone", "minecraft:stick"]);
assert!(!have(&[("minecraft:stick", 1)]).can_craft(&[&any, &stick]));
}
#[test]
fn damaged_or_named_stacks_are_not_counted() {
let mut named = ItemStack::new("minecraft:stick", 4);
named.data_mut().custom_name = Some("x".into());
let mut damaged = ItemStack::new("minecraft:iron_sword", 1);
damaged.damage = 3;
let a = Available::from_stacks([&named, &damaged, &ItemStack::new("minecraft:stick", 2)]);
assert_eq!(a.counts.get("minecraft:stick"), Some(&2));
assert!(!a.counts.contains_key("minecraft:iron_sword"));
}
#[test]
fn collections_group_recipes_of_a_category() {
let entry = |id: &str, group: Option<&str>, category| Entry {
id: id.into(),
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", Some("planks"), Category::CraftingBuilding),
entry("minecraft:b", Some("planks"), Category::CraftingBuilding),
entry("minecraft:c", None, Category::CraftingBuilding),
entry("minecraft:d", Some("planks"), Category::CraftingMisc),
],
order: HashMap::new(),
};
let unlocked: BTreeSet<String> = ["a", "b", "c", "d"]
.iter()
.map(|n| format!("minecraft:{n}"))
.collect();
let tab = Tab::Category(Category::CraftingBuilding);
let c = book.collections(&unlocked, 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 = book.collections(&unlocked, BookType::Crafting, Tab::Search);
assert_eq!(all.len(), 3);
// Locked recipes are hidden.
let some: BTreeSet<String> = ["minecraft:c".to_owned()].into();
assert_eq!(book.collections(&some, BookType::Crafting, tab).len(), 1);
}
#[test]
fn settings_round_trip() {
let mut s = Settings::default();
s.open[BookType::Furnace.index()] = true;
s.filtering[BookType::Crafting.index()] = true;
s.recipes = vec!["minecraft:stick".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);
}
}
+12 -1
View File
@@ -1358,6 +1358,8 @@ pub struct Level {
pub active_effects: Vec<(String, i32, i32, bool, bool)>,
/// Player.EnderItems: the ender chest inventory (27 slots).
pub ender_items: Vec<Option<crate::items::ItemStack>>,
/// The `recipeBook` compound: open/filter settings and unlocked recipes.
pub recipe_book: crate::recipe_book::Settings,
pub xp_level: i32,
pub xp_progress: f32,
pub xp_total: i32,
@@ -1570,6 +1572,7 @@ impl Level {
}
self.inventory = inventory;
self.ender_items = decode_items(p.get("EnderItems"), 27);
self.recipe_book = crate::recipe_book::Settings::read(p.get("recipeBook"));
self.inventory_components = inventory_components;
self.xp_seed = n("XpSeed").unwrap_or(0) as i32;
self.active_effects = match p.get("active_effects") {
@@ -1595,7 +1598,14 @@ impl Level {
let mut p = compound_or_empty(self.player_extra.clone());
// Keys rewritten from scratch.
if let Tag::Compound(map) = &mut p {
for key in ["Inventory", "EnderItems", "equipment", "Pos", "respawn"] {
for key in [
"Inventory",
"EnderItems",
"equipment",
"Pos",
"respawn",
"recipeBook",
] {
map.remove(&JavaString::from(key));
}
}
@@ -1690,6 +1700,7 @@ impl Level {
("playerGameType", Tag::Int(self.mode())),
("Inventory", Tag::List(inventory)),
("EnderItems", encode_items(&self.ender_items)),
("recipeBook", self.recipe_book.write()),
("abilities", abilities),
(
"Dimension",