//! Container block entities: chests/barrels (27 slots) and the three furnace //! kinds, ticking as `AbstractFurnaceBlockEntity.serverTick` with fuel times //! from the data-driven `context_int_provider`/`context_float_provider` files. use crate::{ crafting::Recipes, items::{ItemStack, Items}, }; use serde_json::Value; use std::{collections::HashMap, fs, path::Path, sync::Arc}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum FurnaceKind { Furnace, BlastFurnace, Smoker, } impl FurnaceKind { pub fn recipe_type(self) -> &'static str { match self { Self::Furnace => "minecraft:smelting", Self::BlastFurnace => "minecraft:blasting", Self::Smoker => "minecraft:smoking", } } pub fn fast(self) -> bool { self != Self::Furnace } pub fn from_block(name: &str) -> Option { match name { "minecraft:furnace" => Some(Self::Furnace), "minecraft:blast_furnace" => Some(Self::BlastFurnace), "minecraft:smoker" => Some(Self::Smoker), _ => None, } } } #[derive(Clone, Debug)] pub struct Furnace { pub kind: FurnaceKind, /// input, fuel, result pub items: [Option; 3], pub lit_remaining: i32, pub lit_total: i32, pub cooking_timer: i32, pub cooking_total: i32, pub speed: f32, /// `AbstractFurnaceBlockEntity.recipesUsed`: how many of each recipe this furnace has /// made since a player last took from its output. The experience is owed per recipe /// rather than per item, and a save keeps it as the `RecipesUsed` compound. pub recipes_used: HashMap, } /// The key `recipes_used` keeps a recipe under: its full id, as vanilla's `ResourceKey` /// writes it in `RecipesUsed`. Horizon's own recipe ids are file names without the namespace, so /// keeping both forms would write a key twice, and vanilla refuses such a furnace /// ("Duplicate entry for key"). pub fn recipe_used_key(id: &str) -> String { if id.contains(':') { id.to_owned() } else { format!("minecraft:{id}") } } /// A hopper's slots: the only plain container with five. pub const HOPPER_SLOTS: usize = 5; #[derive(Clone, Debug)] pub enum BlockEntity { Chest(Vec>), Furnace(Furnace), Brewing(Box), /// Signs and hanging signs (`SignBlockEntity`). Sign(crate::signs::SignData), /// `JukeboxBlockEntity`: the record and its `JukeboxSongPlayer`. Jukebox(Jukebox), /// `BeaconBlockEntity`. Beacon(Box), /// `LecternBlockEntity`: the book and the open page. Lectern(Lectern), /// Sculk sensors, shriekers and catalysts (`crate::sculk`). Sculk(Box), /// `CreakingHeartBlockEntity`: its protector and the hurt emitter. CreakingHeart(Box), /// `BannerBlockEntity`: the pattern layers. Banner(Banner), /// `CrafterBlockEntity`. Crafter(Crafter), /// `SpawnerBlockEntity` (`crate::mobs::spawner`). Spawner(Box), /// `TrialSpawnerBlockEntity` (`crate::mobs::trial_spawner`). TrialSpawner(Box), /// `VaultBlockEntity` (`crate::vault`). Vault(Box), /// `BeehiveBlockEntity` of beehives and bee nests (`crate::beehive`). Beehive(Box), /// `CommandBlockEntity` of the three command blocks (`crate::command_block`). CommandBlock(Box), /// `CampfireBlockEntity` of campfires and soul campfires. Campfire(Box), /// `SkullBlockEntity` of every skull and head block. Skull(Skull), } /// `CampfireBlockEntity`: four cooking slots, each with its /// `cookingProgress` and `cookingTime` (the recipe's). #[derive(Clone, Debug, Default, PartialEq)] pub struct Campfire { pub items: [Option; 4], pub progress: [i32; 4], pub total: [i32; 4], } impl Campfire { /// `placeFood`'s slot: the first empty one, given the item's /// `campfire_cooking` time; it takes one item of `held`. pub fn place_food(&mut self, held: &mut ItemStack, cooking_time: i32) -> bool { let Some(slot) = self.items.iter().position(Option::is_none) else { return false; }; self.total[slot] = cooking_time; self.progress[slot] = 0; self.items[slot] = Some(ItemStack { count: 1, ..held.clone() }); held.count = held.count.saturating_sub(1); true } /// `cookTick` (lit): each item's progress rises by one; the slots whose /// progress reached their cooking time give up their item (the caller /// drops its result). pub fn cook_tick(&mut self) -> Vec<(usize, ItemStack)> { let mut done = Vec::new(); for i in 0..4 { if self.items[i].is_none() { continue; } self.progress[i] += 1; if self.progress[i] >= self.total[i] && let Some(item) = self.items[i].take() { done.push((i, item)); } } done } /// `cooldownTick` (unlit): the progress falls by two, no lower than 0. /// Whether anything changed (`setChanged`). pub fn cooldown_tick(&mut self) -> bool { let mut changed = false; for i in 0..4 { if self.progress[i] > 0 { changed = true; self.progress[i] = (self.progress[i] - 2).clamp(0, self.total[i]); } } changed } } /// `SkullBlockEntity`: the player profile the head shows (kept as loaded: /// a compound, or a string when it names a player only), the note block /// sound and `custom_name`. #[derive(Clone, Debug, Default)] pub struct Skull { pub profile: Option, pub note_block_sound: Option, pub custom_name: Option, /// What `profile` draws, read from it once rather than every frame. pub skin: Option, } impl Skull { pub fn new( profile: Option, note_block_sound: Option, custom_name: Option, ) -> Self { let skin = profile.as_ref().map(SkullSkin::of); Self { profile, note_block_sound, custom_name, skin, } } } /// `SkullBlockRenderer.getPlayerSkin` of a head's profile: its skin's URL /// with the `skins/` sprite it downloads to, and the /// `DefaultPlayerSkin` drawn until that sprite is there (steve for a /// profile with no id). #[derive(Clone, Debug)] pub struct SkullSkin { pub download: Option<(String, String)>, pub fallback: &'static str, } impl SkullSkin { pub fn of(profile: &horizon_world::nbt::Tag) -> Self { let download = crate::skins::profile_skin_url_nbt(profile).and_then(|url| { let location = crate::skins::skin_location(&crate::skins::texture_hash(&url)?); Some((url, location)) }); // DefaultPlayerSkin.get(id), or getDefaultSkin (slim steve) without an id. let fallback = crate::skins::profile_uuid_nbt(profile) .map_or("entity/player/slim/steve", crate::tab_list::default_skin); Self { download, fallback } } } /// The skull and head blocks (`SkullBlock`, `WallSkullBlock`): every /// `*_skull` and `*_head` but the piston's head. fn is_skull(name: &str) -> bool { name.ends_with("_skull") || (name.ends_with("_head") && name != "minecraft:piston_head") } /// `CrafterBlockEntity`: nine slots that can be disabled, the powered flag /// and the ticks left in the crafting animation. #[derive(Clone, Debug, Default)] pub struct Crafter { pub items: [Option; 9], pub disabled: [bool; 9], pub triggered: bool, pub crafting_ticks: i32, /// The block state's `triggered` when last ticked (not saved): the block /// entity flag follows it only when it changes (`CrafterBlock.neighborChanged`). pub state_triggered: Option, } impl Crafter { /// `slotCanBeDisabled`: only empty slots. pub fn slot_can_be_disabled(&self, slot: usize) -> bool { slot < 9 && self.items[slot].is_none() } /// `setSlotState`. pub fn set_slot_state(&mut self, slot: usize, enabled: bool) { if self.slot_can_be_disabled(slot) { self.disabled[slot] = !enabled; } } /// `getRedstoneSignal`: filled or disabled slots. pub fn redstone_signal(&self) -> i32 { (0..9) .filter(|&i| self.items[i].is_some() || self.disabled[i]) .count() as i32 } /// `canPlaceItem`: a hopper fills the smallest stacks first. pub fn can_place(&self, slot: usize, _stack: &ItemStack, max_stack: u32) -> bool { if slot >= 9 || self.disabled[slot] { return false; } let Some(current) = &self.items[slot] else { return true; }; if current.count >= max_stack { return false; } !(slot + 1..9).any(|i| { !self.disabled[i] && match &self.items[i] { None => true, Some(other) => { other.count < current.count && other.same_item_same_components(current) } } }) } } #[derive(Clone, Debug, Default)] pub struct Banner { pub layers: Vec, pub name: Option, /// Components not consumed by BannerBlockEntity's implicit fields. pub components: horizon_world::nbt::Compound, } /// `CreakingHeartBlockEntity` (the logic is in `game/creaking.rs`). #[derive(Clone, Debug, Default)] pub struct CreakingHeart { /// The protector: its entity id once resolved, and its UUID (the saved form). pub creaking: Option<(Option, [i32; 4])>, pub ticks_existed: i64, pub ticker: i32, /// Ticks left of the hurt effect (`emitter`) and where it heads. pub emitter: i32, pub emitter_target: Option<[f64; 3]>, pub output_signal: i32, } #[derive(Clone, Debug, Default)] pub struct Lectern { pub book: Option, pub page: i32, } #[derive(Clone, Debug, Default)] pub struct Beacon { pub levels: i32, /// Effect ids (`minecraft:speed`). pub primary: Option, pub secondary: Option, /// Beam sections (RGB colour, height) and the ones being rebuilt. pub sections: Vec<(u32, i32)>, pub checking: Vec<(u32, i32)>, pub last_check_y: i32, } #[derive(Clone, Debug, Default)] pub struct Jukebox { pub record: Option, /// `ticks_since_song_started`, while a song plays. pub ticks: i64, pub playing: bool, } impl BlockEntity { pub fn for_block(name: &str) -> Option { if let Some(hanging) = crate::signs::sign_kind(name) { return Some(Self::Sign(crate::signs::SignData { hanging, ..Default::default() })); } if let Some(kind) = FurnaceKind::from_block(name) { return Some(Self::Furnace(Furnace { kind, items: [None, None, None], lit_remaining: 0, lit_total: 0, cooking_timer: 0, cooking_total: 0, speed: 1.0, recipes_used: HashMap::new(), })); } if name == "minecraft:creaking_heart" { return Some(Self::CreakingHeart(Box::default())); } if name == "minecraft:spawner" { return Some(Self::Spawner(Box::default())); } if name == "minecraft:trial_spawner" { return Some(Self::TrialSpawner(Box::default())); } if name == "minecraft:vault" { return Some(Self::Vault(Box::default())); } if name == "minecraft:beehive" || name == "minecraft:bee_nest" { return Some(Self::Beehive(Box::default())); } if name == "minecraft:lectern" { return Some(Self::Lectern(Lectern::default())); } if crate::command_block::Mode::of_block(name).is_some() { // CommandBlock.newBlockEntity: setAutomatic(this.automatic). return Some(Self::CommandBlock(Box::new( crate::command_block::CommandBlock { auto: crate::command_block::automatic_block(name), ..Default::default() }, ))); } if let Some(sculk) = crate::sculk::SculkEntity::for_block(name) { return Some(Self::Sculk(Box::new(sculk))); } if name == "minecraft:crafter" { // CrafterBlock.newBlockEntity copies the placed state's flag: the // first tick sees it as a change from untriggered. return Some(Self::Crafter(Crafter { state_triggered: Some(false), ..Crafter::default() })); } if name.ends_with("_banner") { return Some(Self::Banner(Banner::default())); } if is_skull(name) { return Some(Self::Skull(Skull::default())); } if name == "minecraft:beacon" { return Some(Self::Beacon(Box::new(Beacon { last_check_y: i32::MIN / 2, ..Default::default() }))); } if name == "minecraft:brewing_stand" { return Some(Self::Brewing(Box::new(crate::brewing::BrewingStand::new()))); } if name == "minecraft:campfire" || name == "minecraft:soul_campfire" { return Some(Self::Campfire(Box::default())); } let size = match name { "minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel" => 27, n if n.ends_with("copper_chest") => 27, "minecraft:hopper" => HOPPER_SLOTS, "minecraft:dispenser" | "minecraft:dropper" => 9, n if n.ends_with("shulker_box") => 27, _ => return None, }; Some(Self::Chest(vec![None; size])) } pub fn id(&self) -> &'static str { match self { Self::Chest(items) => match items.len() { HOPPER_SLOTS => "minecraft:hopper", 9 => "minecraft:dispenser", _ => "minecraft:chest", }, Self::Furnace(f) => match f.kind { FurnaceKind::Furnace => "minecraft:furnace", FurnaceKind::BlastFurnace => "minecraft:blast_furnace", FurnaceKind::Smoker => "minecraft:smoker", }, Self::Brewing(_) => "minecraft:brewing_stand", Self::Sign(s) => s.id(), Self::Jukebox(_) => "minecraft:jukebox", Self::Beacon(_) => "minecraft:beacon", Self::Lectern(_) => "minecraft:lectern", Self::Sculk(s) => s.id(), Self::Banner(_) => "minecraft:banner", Self::Crafter(_) => "minecraft:crafter", Self::CreakingHeart(_) => "minecraft:creaking_heart", Self::Spawner(_) => "minecraft:mob_spawner", Self::TrialSpawner(_) => "minecraft:trial_spawner", Self::Vault(_) => "minecraft:vault", Self::Beehive(_) => "minecraft:beehive", Self::CommandBlock(_) => "minecraft:command_block", Self::Skull(_) => "minecraft:skull", Self::Campfire(_) => "minecraft:campfire", } } pub fn contents(&self) -> Vec { match self { Self::Chest(items) => items.iter().flatten().cloned().collect(), Self::Furnace(f) => f.items.iter().flatten().cloned().collect(), Self::Brewing(b) => b.items.iter().flatten().cloned().collect(), Self::Sign(_) => Vec::new(), Self::Jukebox(j) => j.record.iter().cloned().collect(), Self::Beacon(_) | Self::CreakingHeart(_) | Self::Spawner(_) | Self::TrialSpawner(_) | Self::Vault(_) | Self::Beehive(_) | Self::CommandBlock(_) | Self::Skull(_) => Vec::new(), Self::Lectern(l) => l.book.iter().cloned().collect(), Self::Sculk(_) => Vec::new(), Self::Banner(_) => Vec::new(), Self::Crafter(c) => c.items.iter().flatten().cloned().collect(), Self::Campfire(c) => c.items.iter().flatten().cloned().collect(), } } pub fn slots(&self) -> &[Option] { match self { Self::Chest(items) => items, Self::Furnace(f) => &f.items, Self::Brewing(b) => &b.items, Self::Sign(_) | Self::Jukebox(_) | Self::Beacon(_) | Self::Lectern(_) | Self::Sculk(_) | Self::Banner(_) | Self::Skull(_) => &[], Self::Crafter(c) => &c.items, Self::CreakingHeart(_) | Self::Spawner(_) | Self::TrialSpawner(_) | Self::Vault(_) | Self::Beehive(_) | Self::CommandBlock(_) // Not a Container (only Clearable): hoppers don't reach in. | Self::Campfire(_) => &[], } } pub fn slots_mut(&mut self) -> &mut [Option] { match self { Self::Chest(items) => items, Self::Furnace(f) => &mut f.items, Self::Brewing(b) => &mut b.items, Self::Sign(_) | Self::Jukebox(_) | Self::Beacon(_) | Self::Lectern(_) | Self::Sculk(_) | Self::Banner(_) | Self::Skull(_) => &mut [], Self::Crafter(c) => &mut c.items, Self::CreakingHeart(_) | Self::Spawner(_) | Self::TrialSpawner(_) | Self::Vault(_) | Self::Beehive(_) | Self::CommandBlock(_) | Self::Campfire(_) => &mut [], } } } /// Evaluator for `context_int_provider` / `context_float_provider` values. pub struct Providers { ints: HashMap, floats: HashMap, } fn load_dir(dir: &Path, prefix: &str, out: &mut HashMap) { let Ok(entries) = fs::read_dir(dir) else { return; }; for e in entries.flatten() { let p = e.path(); let name = p .file_stem() .and_then(|s| s.to_str()) .unwrap_or("") .to_owned(); if p.is_dir() { load_dir(&p, &format!("{prefix}{name}/"), out); } else if let Ok(bytes) = fs::read(&p) && let Ok(v) = serde_json::from_slice(&bytes) { out.insert(format!("minecraft:{prefix}{name}"), v); } } } impl Providers { pub fn load(root: &Path) -> Self { let mut ints = HashMap::new(); let mut floats = HashMap::new(); load_dir( &root.join("data/minecraft/context_int_provider"), "", &mut ints, ); load_dir( &root.join("data/minecraft/context_float_provider"), "", &mut floats, ); Self { ints, floats } } fn eval(v: &Value, fast: bool, table: &HashMap, depth: u32) -> f64 { if depth > 16 { return 0.0; } match v { Value::Number(n) => n.as_f64().unwrap_or(0.0), Value::String(name) => table .get(name) .map_or(0.0, |v| Self::eval(v, fast, table, depth + 1)), Value::Object(o) => { let f = |k: &str| Self::eval(o.get(k).unwrap_or(&Value::Null), fast, table, depth + 1); match o.get("type").and_then(Value::as_str).unwrap_or("") { "minecraft:div" => { let r = f("right"); if r == 0.0 { 0.0 } else { (f("left") / r).trunc() } } "minecraft:mul" => f("left") * f("right"), "minecraft:add" | "minecraft:sum" => f("left") + f("right"), "minecraft:sub" => f("left") - f("right"), "minecraft:constant" => f("value"), "minecraft:conditional" => { // The only condition in the pinned data: block/fast_cooking. let condition = o.get("condition").and_then(Value::as_str) == Some("minecraft:block/fast_cooking"); if condition && fast { f("on_true") } else { f("on_false") } } _ => 0.0, } } _ => 0.0, } } /// A plain `context_int_provider` value or reference (no context conditions). pub fn int_value(&self, v: &Value) -> i32 { Self::eval(v, false, &self.ints, 0) as i32 } /// A plain `context_float_provider` value or reference; `default` when absent. pub fn float_value(&self, v: &Value, default: f32) -> f32 { if v.is_null() { default } else { Self::eval(v, false, &self.floats, 0) as f32 } } #[cfg(test)] pub fn empty() -> Self { Self { ints: HashMap::new(), floats: HashMap::new(), } } pub fn burn_time(&self, items: &Items, stack: &ItemStack, fast: bool) -> i32 { items .get(&stack.item) .and_then(|i| i.fuel.as_ref()) .map_or(0, |(time, _)| Self::eval(time, fast, &self.ints, 0) as i32) } pub fn speed(&self, items: &Items, stack: &ItemStack, fast: bool) -> f32 { items .get(&stack.item) .and_then(|i| i.fuel.as_ref()) .map_or(1.0, |(_, speed)| { Self::eval(speed, fast, &self.floats, 0) as f32 }) } } 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; 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). pub fn create_experience( count: u32, experience: f32, random: &mut impl horizon_core::random::RandomSource, ) -> u32 { let total = count as f32 * experience; let whole = total.floor(); let fraction = total - whole; let mut xp = whole.max(0.0) as u32; if fraction != 0.0 && random.next_float() < fraction { xp += 1; } xp } #[cfg(test)] mod tests { use super::*; use horizon_core::random::{LegacyRandom, RandomSource}; #[test] fn campfire_slots_take_food_cook_it_and_cool_down() { let mut c = Campfire::default(); let mut beef = ItemStack::new("minecraft:beef", 2); // placeFood: the first free slot, one item, its recipe's time. assert!(c.place_food(&mut beef, 600)); assert_eq!(beef.count, 1); assert_eq!(c.items[0].as_ref().map(|s| s.count), Some(1)); assert_eq!((c.progress[0], c.total[0]), (0, 600)); let mut fish = ItemStack::new("minecraft:cod", 1); assert!(c.place_food(&mut fish, 3)); assert_eq!(c.items[1].as_ref().map(|s| &*s.item), Some("minecraft:cod")); // cookTick: the cod is done on its third tick, the beef keeps cooking. assert!(c.cook_tick().is_empty()); assert!(c.cook_tick().is_empty()); let done = c.cook_tick(); assert_eq!(done.len(), 1); assert_eq!((done[0].0, &*done[0].1.item), (1, "minecraft:cod")); assert!(c.items[1].is_none()); assert_eq!(c.progress[0], 3); // cooldownTick: two a tick, never below zero. assert!(c.cooldown_tick()); assert_eq!(c.progress[0], 1); assert!(c.cooldown_tick()); assert_eq!(c.progress[0], 0); // Full: four slots, no fifth. for _ in 0..3 { let mut more = ItemStack::new("minecraft:potato", 1); assert!(c.place_food(&mut more, 600)); } let mut fifth = ItemStack::new("minecraft:potato", 1); assert!(!c.place_food(&mut fifth, 600)); assert_eq!(fifth.count, 1); } /// A whole amount pays itself without a draw; a fraction is a chance of one more. #[test] fn create_experience_rounds_the_fraction_by_chance() { let mut random = LegacyRandom::new(7); let mut untouched = LegacyRandom::new(7); assert_eq!(create_experience(4, 0.5, &mut random), 2); assert_eq!(create_experience(3, 0.0, &mut random), 0); assert_eq!(random.next_long(), untouched.next_long()); let paid: u32 = (0..1000) .map(|_| create_experience(1, 0.35, &mut random)) .sum(); assert!((250..450).contains(&paid), "{paid}"); assert!((0..100).all(|_| (1..=2).contains(&create_experience(1, 1.7, &mut random)))); } }