diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs index c20f0bce9..aa434a556 100644 --- a/crates/horizon-client/src/crafting.rs +++ b/crates/horizon-client/src/crafting.rs @@ -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, + /// `category` and `group` of each crafting recipe, by file name. + pub meta: std::collections::HashMap, } fn result(v: &Value) -> Option { Some(ItemStack { @@ -168,6 +175,7 @@ impl Recipes { pub fn load(root: &Path, items: &Items) -> Result { 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> = 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], size: usize) -> Option { diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 02a649329..a252164e2 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -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, tridents: Vec, @@ -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(), diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 7c07af665..18d0e6b64 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -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(), diff --git a/crates/horizon-client/src/game/recipe_book.rs b/crates/horizon-client/src/game/recipe_book.rs new file mode 100644 index 000000000..d66b23e97 --- /dev/null +++ b/crates/horizon-client/src/game/recipe_book.rs @@ -0,0 +1,1221 @@ +//! The recipe book of the inventory, crafting table and furnace screens +//! (`RecipeBookComponent`): the green book button, the tabs by category, the +//! search box, the craftable filter, the pages of recipe buttons with the +//! alternatives overlay, ghost recipes, and placing a recipe in the grid +//! (`ServerPlaceRecipe`). Which recipes are known comes from the advancement +//! rewards; the open and filter settings are saved with the player. +use super::{Game, Screen}; +use horizon_client::{ + containers::{BlockEntity, FurnaceKind}, + hud::{HudBatch, Rect, rgba}, + items::ItemStack, + recipe_book::{Available, Book, BookType, Collection, Display, Entry, Tab}, + ui::EditBox, +}; +use std::{ + collections::BTreeSet, + hash::{Hash, Hasher}, +}; + +const WIDTH: f32 = 147.0; +const HEIGHT: f32 = 166.0; +/// `RecipeBookComponent.TICKS_TO_SWAP_SLOT`. +const SWAP_TICKS: f32 = 30.0; +/// Widest GUI at which the book sits next to the menu (`widthTooNarrow`). +const NARROW: f32 = 379.0; + +/// A ghost recipe slot: the items it cycles through. +struct Ghost { + slot: usize, + items: Vec, + result: bool, +} +/// The alternatives overlay of a recipe button (`OverlayRecipeComponent`). +struct Overlay { + x: f32, + y: f32, + /// (entry index, craftable) + entries: Vec<(usize, bool)>, +} +/// One recipe button: the collection's recipes shown in the current view. +#[derive(Clone, Default)] +struct Button { + /// (entry index, craftable) + entries: Vec<(usize, bool)>, +} +impl Button { + fn craftable(&self) -> bool { + self.entries.iter().any(|e| e.1) + } +} +#[derive(Default)] +struct View { + /// Visible tabs. + tabs: Vec, + buttons: Vec