diff --git a/crates/horizon-client/examples/probe/menu_fixture.rs b/crates/horizon-client/examples/probe/menu_fixture.rs index 9ec558de3..2a8d70e23 100644 --- a/crates/horizon-client/examples/probe/menu_fixture.rs +++ b/crates/horizon-client/examples/probe/menu_fixture.rs @@ -7,6 +7,7 @@ use horizon_client::{ containers::{Furnace, FurnaceKind}, crafting::Recipes, + furnace::FurnaceCooking, item_data::{canon_stack, parse_stack}, items::Items, menu::{Env, Input, Kind, Menu}, diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 6210d2f7e..4908ed1ee 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -5,11 +5,8 @@ pub use crate::chunk::block_entity::{ Banner, Beacon, BlockEntity, Campfire, ChiseledBookshelf, Crafter, CreakingHeart, EndGateway, Furnace, FurnaceKind, Jukebox, Lectern, Skull, SkullSkin, }; -use crate::{ - crafting::Recipes, - items::{ItemStack, Items}, -}; -use std::{collections::HashMap, sync::Arc}; +use crate::items::ItemStack; +use std::collections::HashMap; impl FurnaceKind { pub fn recipe_type(self) -> &'static str { @@ -380,167 +377,6 @@ impl BlockEntity { } } pub use crate::providers::Providers; -fn remainder(item: &str) -> Option<&'static str> { - match item { - "minecraft:lava_bucket" | "minecraft:water_bucket" | "minecraft:milk_bucket" => { - Some("minecraft:bucket") - } - _ => None, - } -} -impl Furnace { - /// `AbstractFurnaceBlockEntity.setItem` on the input slot: anything but - /// the same item with the same components restarts cooking, with the - /// total time of the new input's recipe (200 ticks without one). - pub fn set_input(&mut self, recipes: &Recipes, stack: Option) { - let same = match (&self.items[0], &stack) { - (Some(old), Some(new)) => old.same_item_same_components(new), - _ => false, - }; - self.items[0] = stack; - if !same { - self.cooking_total = self.items[0] - .as_ref() - .and_then(|input| recipes.cook(self.kind.recipe_type(), input)) - .map_or(200, |recipe| total_time(recipe.time, self.speed)); - self.cooking_timer = 0; - } - } - /// `recipesUsed`, each recipe with its `experience` (0 for one this data pack does - /// not have), in the order `getRecipesToAwardAndPopExperience` pays them. - pub fn recipe_experience(&self, recipes: &Recipes) -> Vec<(u32, f32)> { - self.recipes_used - .iter() - .map(|(id, count)| (*count, recipes.cooking_experience(id))) - .collect() - } - /// A menu click wrote its slots into `items`, and `before` was the input - /// slot until then: the menu called `setItem` on it only if it changed - /// (`Slot.setByPlayer`; partial takes and merges keep the stack). - pub fn input_clicked(&mut self, recipes: &Recipes, before: Option) { - if self.items[0] != before { - let after = std::mem::replace(&mut self.items[0], before); - self.set_input(recipes, after); - } - } - /// One server tick. Returns `Some(lit)` when the LIT block state must change. - pub fn tick( - &mut self, - recipes: &Recipes, - items: &Items, - providers: &Providers, - ) -> Option { - let was_lit = self.lit_remaining > 0; - let mut lit = false; - if self.lit_remaining > 0 { - self.lit_remaining -= 1; - lit = self.lit_remaining > 0; - } - let fast = self.kind.fast(); - let has_ingredient = self.items[0].is_some(); - let has_fuel = self.items[1].is_some(); - if lit || has_fuel && has_ingredient { - if let Some(ingredient) = self.items[0].clone() { - if let Some(recipe) = recipes.cook(self.kind.recipe_type(), &ingredient) { - if self.cooking_timer == 0 { - self.cooking_total = total_time(recipe.time, self.speed); - } - let max = items.get(&recipe.result.item).map_or(64, |i| i.max_stack); - let can_burn = match &self.items[2] { - None => true, - Some(out) => { - out.item == recipe.result.item && out.count + recipe.result.count <= max - } - }; - if can_burn { - if !lit { - let fuel = self.items[1].clone(); - let time = fuel - .as_ref() - .map_or(0, |f| providers.burn_time(items, f, fast)); - let speed = fuel - .as_ref() - .map_or(1.0, |f| providers.speed(items, f, fast)); - self.lit_remaining = time; - self.lit_total = time; - self.speed = speed; - if self.cooking_total > 0 && self.cooking_timer < self.cooking_total { - let ratio = self.cooking_timer as f32 / self.cooking_total as f32; - self.cooking_total = total_time(recipe.time, self.speed); - self.cooking_timer = - (ratio * self.cooking_total as f32).ceil() as i32; - } - if time > 0 { - if let Some(f) = self.items[1].as_mut() { - let item = f.item.clone(); - f.count -= 1; - if f.count == 0 { - self.items[1] = remainder(&item).map(|r| ItemStack { - item: Arc::from(r), - count: 1, - damage: 0, - extra: None, - }); - } - } - lit = true; - } - } - if lit { - self.cooking_timer += 1; - if self.cooking_timer >= self.cooking_total { - self.cooking_timer = 0; - self.cooking_total = total_time(recipe.time, self.speed); - match &mut self.items[2] { - Some(out) => out.count += recipe.result.count, - None => self.items[2] = Some(recipe.result.clone()), - } - *self - .recipes_used - .entry(recipe_used_key(&recipe.id)) - .or_insert(0) += 1; - // AbstractFurnaceBlockEntity.burn: drying a wet sponge fills - // the bucket in the fuel slot. - if self.items[0] - .as_ref() - .is_some_and(|i| &*i.item == "minecraft:wet_sponge") - && self.items[1] - .as_ref() - .is_some_and(|f| &*f.item == "minecraft:bucket") - { - self.items[1] = - Some(ItemStack::new("minecraft:water_bucket", 1)); - } - if let Some(i) = self.items[0].as_mut() { - i.count -= 1; - if i.count == 0 { - self.items[0] = None; - } - } - } - } else { - self.cooking_timer = 0; - } - } else { - self.cooking_timer = 0; - } - } - } else { - self.cooking_timer = 0; - } - } else if self.cooking_timer > 0 { - self.cooking_timer = (self.cooking_timer - 2).clamp(0, self.cooking_total); - } - (was_lit != lit).then_some(lit) - } -} -fn total_time(time: u32, speed: f32) -> i32 { - if speed > 0.0 { - (time as f32 / speed).ceil() as i32 - } else { - time as i32 - } -} /// `AbstractFurnaceBlockEntity.createExperience`: `count * experience`, floored, and one /// more with the chance of what is left over (a level random draw only when there is some). diff --git a/crates/horizon-client/src/furnace.rs b/crates/horizon-client/src/furnace.rs new file mode 100644 index 000000000..9d8ce4392 --- /dev/null +++ b/crates/horizon-client/src/furnace.rs @@ -0,0 +1,173 @@ +//! `AbstractFurnaceBlockEntity`'s cooking over the furnace data the chunk +//! holds (`containers::Furnace`): it needs the recipes, so it lives above them. +use crate::{ + containers::{Furnace, recipe_used_key}, + crafting::Recipes, + items::{ItemStack, Items}, + providers::Providers, +}; +use std::sync::Arc; + +/// The furnace methods that read the recipes. +pub trait FurnaceCooking { + /// `AbstractFurnaceBlockEntity.setItem` on the input slot: anything but + /// the same item with the same components restarts cooking, with the + /// total time of the new input's recipe (200 ticks without one). + fn set_input(&mut self, recipes: &Recipes, stack: Option); + /// `recipesUsed`, each recipe with its `experience` (0 for one this data pack does + /// not have), in the order `getRecipesToAwardAndPopExperience` pays them. + fn recipe_experience(&self, recipes: &Recipes) -> Vec<(u32, f32)>; + /// A menu click wrote its slots into `items`, and `before` was the input + /// slot until then: the menu called `setItem` on it only if it changed + /// (`Slot.setByPlayer`; partial takes and merges keep the stack). + fn input_clicked(&mut self, recipes: &Recipes, before: Option); + /// One server tick. Returns `Some(lit)` when the LIT block state must change. + fn tick(&mut self, recipes: &Recipes, items: &Items, providers: &Providers) -> Option; +} +fn remainder(item: &str) -> Option<&'static str> { + match item { + "minecraft:lava_bucket" | "minecraft:water_bucket" | "minecraft:milk_bucket" => { + Some("minecraft:bucket") + } + _ => None, + } +} +impl FurnaceCooking for Furnace { + fn set_input(&mut self, recipes: &Recipes, stack: Option) { + let same = match (&self.items[0], &stack) { + (Some(old), Some(new)) => old.same_item_same_components(new), + _ => false, + }; + self.items[0] = stack; + if !same { + self.cooking_total = self.items[0] + .as_ref() + .and_then(|input| recipes.cook(self.kind.recipe_type(), input)) + .map_or(200, |recipe| total_time(recipe.time, self.speed)); + self.cooking_timer = 0; + } + } + fn recipe_experience(&self, recipes: &Recipes) -> Vec<(u32, f32)> { + self.recipes_used + .iter() + .map(|(id, count)| (*count, recipes.cooking_experience(id))) + .collect() + } + fn input_clicked(&mut self, recipes: &Recipes, before: Option) { + if self.items[0] != before { + let after = std::mem::replace(&mut self.items[0], before); + self.set_input(recipes, after); + } + } + fn tick(&mut self, recipes: &Recipes, items: &Items, providers: &Providers) -> Option { + let was_lit = self.lit_remaining > 0; + let mut lit = false; + if self.lit_remaining > 0 { + self.lit_remaining -= 1; + lit = self.lit_remaining > 0; + } + let fast = self.kind.fast(); + let has_ingredient = self.items[0].is_some(); + let has_fuel = self.items[1].is_some(); + if lit || has_fuel && has_ingredient { + if let Some(ingredient) = self.items[0].clone() { + if let Some(recipe) = recipes.cook(self.kind.recipe_type(), &ingredient) { + if self.cooking_timer == 0 { + self.cooking_total = total_time(recipe.time, self.speed); + } + let max = items.get(&recipe.result.item).map_or(64, |i| i.max_stack); + let can_burn = match &self.items[2] { + None => true, + Some(out) => { + out.item == recipe.result.item && out.count + recipe.result.count <= max + } + }; + if can_burn { + if !lit { + let fuel = self.items[1].clone(); + let time = fuel + .as_ref() + .map_or(0, |f| providers.burn_time(items, f, fast)); + let speed = fuel + .as_ref() + .map_or(1.0, |f| providers.speed(items, f, fast)); + self.lit_remaining = time; + self.lit_total = time; + self.speed = speed; + if self.cooking_total > 0 && self.cooking_timer < self.cooking_total { + let ratio = self.cooking_timer as f32 / self.cooking_total as f32; + self.cooking_total = total_time(recipe.time, self.speed); + self.cooking_timer = + (ratio * self.cooking_total as f32).ceil() as i32; + } + if time > 0 { + if let Some(f) = self.items[1].as_mut() { + let item = f.item.clone(); + f.count -= 1; + if f.count == 0 { + self.items[1] = remainder(&item).map(|r| ItemStack { + item: Arc::from(r), + count: 1, + damage: 0, + extra: None, + }); + } + } + lit = true; + } + } + if lit { + self.cooking_timer += 1; + if self.cooking_timer >= self.cooking_total { + self.cooking_timer = 0; + self.cooking_total = total_time(recipe.time, self.speed); + match &mut self.items[2] { + Some(out) => out.count += recipe.result.count, + None => self.items[2] = Some(recipe.result.clone()), + } + *self + .recipes_used + .entry(recipe_used_key(&recipe.id)) + .or_insert(0) += 1; + // AbstractFurnaceBlockEntity.burn: drying a wet sponge fills + // the bucket in the fuel slot. + if self.items[0] + .as_ref() + .is_some_and(|i| &*i.item == "minecraft:wet_sponge") + && self.items[1] + .as_ref() + .is_some_and(|f| &*f.item == "minecraft:bucket") + { + self.items[1] = + Some(ItemStack::new("minecraft:water_bucket", 1)); + } + if let Some(i) = self.items[0].as_mut() { + i.count -= 1; + if i.count == 0 { + self.items[0] = None; + } + } + } + } else { + self.cooking_timer = 0; + } + } else { + self.cooking_timer = 0; + } + } + } else { + self.cooking_timer = 0; + } + } else if self.cooking_timer > 0 { + self.cooking_timer = (self.cooking_timer - 2).clamp(0, self.cooking_total); + } + (was_lit != lit).then_some(lit) + } +} +fn total_time(time: u32, speed: f32) -> i32 { + if speed > 0.0 { + (time as f32 / speed).ceil() as i32 + } else { + time as i32 + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 1d7bbbbd6..dddd4f56b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -222,6 +222,7 @@ use horizon_client::{ entities::{Experience, ExperienceOrb, ItemEntity}, environment::Environment, first_person::{Hand, SwingAnimation}, + furnace::FurnaceCooking, gfx::{Frame, Renderer}, hud::{HudAtlas, HudBatch, rgba}, hud_layout, hud_overlays, diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index 2778bb531..1f1c820ad 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -9,6 +9,7 @@ use horizon_client::{ Result, block_entities::{Pos, Ticking}, containers::{BlockEntity, FurnaceKind, HOPPER_SLOTS}, + furnace::FurnaceCooking, }; use horizon_core::position::BlockPos; use horizon_world::registry::BlockStateId; diff --git a/crates/horizon-client/src/game/lan_crafting.rs b/crates/horizon-client/src/game/lan_crafting.rs index 461eaf5e1..2a1c6dc3c 100644 --- a/crates/horizon-client/src/game/lan_crafting.rs +++ b/crates/horizon-client/src/game/lan_crafting.rs @@ -7,6 +7,7 @@ use super::{Game, lan::LanHost}; use horizon_client::{ containers::BlockEntity, + furnace::FurnaceCooking, items::ItemStack, lan::{guest::Guest, menu::Slots}, menu::{Env, Input, Kind, Menu}, diff --git a/crates/horizon-client/src/game/spill.rs b/crates/horizon-client/src/game/spill.rs index d8f15daa1..b8ff5682b 100644 --- a/crates/horizon-client/src/game/spill.rs +++ b/crates/horizon-client/src/game/spill.rs @@ -7,6 +7,7 @@ use super::{Game, GameLevel, Screen, dispenser::new_item_motion, storage::is_shu use horizon_client::{ block_entity_item, block_entity_nbt, containers::{Banner, BlockEntity}, + furnace::FurnaceCooking, item_data, items::ItemStack, save, diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index d4ef4c209..545fc267d 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -69,6 +69,7 @@ pub mod flatgen; pub mod fog; pub mod font; pub mod friends_screen; +pub mod furnace; pub mod game_mode_switcher; pub mod game_rules_screen; pub mod gfx;