Furnace experience: createExperience's chance, orbs, every removal, guests

- createExperience floors count * experience and adds one with the chance of
  the fraction (a level random nextFloat); flooring alone paid nothing for a
  single iron ore (0.7).
- The payout pops ExperienceOrb.award's orbs at the player, recipe by recipe,
  instead of adding points directly.
- A server's player (the default single-player path, LAN and dedicated)
  gets it too: lan_machine_click pays when the guest takes from the output.
- AbstractFurnaceBlockEntity.preRemoveSideEffects pays at the block's centre
  on any removal (explosions, /setblock, creative breaking), after the
  contents spill, so it moves from break_block to the removal side effects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 17:37:22 +01:00
parent bf81ae6358
commit b729cfc833
4 changed files with 90 additions and 69 deletions
+37 -32
View File
@@ -488,25 +488,13 @@ impl Furnace {
self.cooking_timer = 0;
}
}
/// `AbstractFurnaceBlockEntity.createExperience`: each recipe's own share is floored,
/// then the floors are added. Flooring the sum instead pays one point for two recipes
/// worth 0.6 each, which vanilla does not.
fn owed(used: &HashMap<String, u32>, experience: impl Fn(&str) -> f32) -> u32 {
used.iter()
.map(|(id, count)| (experience(id) * *count as f32).floor().max(0.0) as u32)
.sum()
}
/// What this furnace owes right now, without forgetting it: breaking the block reads
/// the same counter, and only a payout clears it.
pub fn experience_owed(&self, recipes: &Recipes) -> u32 {
Self::owed(&self.recipes_used, |id| recipes.cooking_experience(id))
}
/// `AbstractFurnaceBlockEntity.awardUsedRecipesAndPopExperience`: the experience this
/// furnace owes, forgotten as it is paid.
pub fn take_experience(&mut self, recipes: &Recipes) -> u32 {
let owed = self.experience_owed(recipes);
self.recipes_used.clear();
owed
/// `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
@@ -621,23 +609,40 @@ fn total_time(time: u32, speed: f32) -> 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};
/// `createExperience` floors each recipe on its own. Two recipes worth 0.6 each pay
/// nothing, where flooring their sum would pay one point.
/// A whole amount pays itself without a draw; a fraction is a chance of one more.
#[test]
fn experience_is_floored_per_recipe_and_not_on_the_sum() {
let mut used = HashMap::new();
used.insert("a".to_owned(), 1);
used.insert("b".to_owned(), 1);
assert_eq!(Furnace::owed(&used, |_| 0.6), 0);
used.insert("c".to_owned(), 10);
assert_eq!(
Furnace::owed(&used, |id| if id == "c" { 0.1 } else { 0.0 }),
1
);
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))));
}
}
+28 -36
View File
@@ -537,6 +537,8 @@ struct GameLevel<'w> {
seed: i64,
behaviours: &'w Behaviours,
items: &'w Items,
/// Cooking recipes' experience, which a removed furnace pays out.
recipes: &'w horizon_client::crafting::Recipes,
/// Container loot tables (`unpackLootTable`).
loot: Option<std::sync::Arc<horizon_world::loot::LootData>>,
registry: &'static horizon_world::registry::BlockRegistry,
@@ -2420,6 +2422,7 @@ impl Game<'_> {
seed: self.level.seed,
behaviours: self.res.behaviours(),
items: self.res.items(),
recipes: self.res.recipes(),
loot: horizon_client::structure_blocks::loot_data(&self.options.resources),
registry: self.pack.assets.registry,
rules,
@@ -3246,12 +3249,6 @@ impl Game<'_> {
}
// The block entity as it was, for what the drops copy from it.
let key = (pos[0], pos[1], pos[2]);
// AbstractFurnaceBlockEntity.playerDestroy: the experience its recipes earned drops
// with the block. Read now, paid only if the block is really removed.
let furnace_xp = match self.world.block_entities.get(&key) {
Some(BlockEntity::Furnace(f)) => f.experience_owed(self.res.recipes()),
_ => 0,
};
if let Some(entity) = self.world.block_entities.get(&key).cloned() {
let name = self.world.block_names.get(&key).cloned();
self.pending.record(pos, &entity, &facts.name, name);
@@ -3321,18 +3318,6 @@ impl Game<'_> {
);
}
}
// Stored recipe experience is not a loot drop: it leaves with the block
// whatever the tool, as `AbstractFurnaceBlockEntity` does.
if furnace_xp > 0 {
self.spawn_orbs(
[
f64::from(pos[0]) + 0.5,
f64::from(pos[1]) + 0.5,
f64::from(pos[2]) + 0.5,
],
furnace_xp,
);
}
}
self.pending.clear(pos);
self.process_level_events()?;
@@ -6007,14 +5992,12 @@ impl Game<'_> {
self.player_drop(stack);
}
self.award_crafted(&crafted);
// shortcut: paid straight to the player instead of through `ExperienceOrb.award`
// (no orb flies at them), which a furnace's orbs reach within a tick anyway;
// upgrade if the visual matters or another block pays out far from the player.
if furnace_before > furnace_after {
let owed = self.take_furnace_experience();
if owed > 0 {
self.give_orb_points(owed);
}
if furnace_before > furnace_after
&& self.remote.is_none()
&& let (Some(pos), Some(p)) = (self.container, self.player.as_ref())
{
let at = [p.position.x, p.position.y, p.position.z];
self.award_furnace_recipes(pos, at);
}
}
if kind == Kind::Loom {
@@ -8807,17 +8790,26 @@ impl Game<'_> {
self.give_orb_points(gained);
}
}
/// `AbstractFurnaceBlockEntity.awardUsedRecipesAndPopExperience`: what the open furnace
/// owes for everything it made since the last payout, cleared as it is read.
fn take_furnace_experience(&mut self) -> u32 {
let Some(pos) = self.container else {
return 0;
/// `FurnaceResultSlot.checkTakeAchievements`: the player took from the output of the
/// furnace at `pos`, which pops the experience of what it made at them
/// (`awardUsedRecipesAndPopExperience`) and forgets it.
pub(super) fn award_furnace_recipes(&mut self, pos: (i32, i32, i32), at: [f64; 3]) {
let used = match self.world.block_entities.get_mut(&pos) {
Some(BlockEntity::Furnace(f)) => {
let used = f.recipe_experience(self.res.recipes());
f.recipes_used.clear();
used
}
_ => return,
};
let recipes = self.res.recipes();
let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) else {
return 0;
};
f.take_experience(recipes)
for (count, experience) in used {
let xp = horizon_client::containers::create_experience(
count,
experience,
&mut self.world.random,
);
self.spawn_orbs(at, xp);
}
}
/// `Player.giveExperiencePoints`, and `giveExperienceLevels`' level-up
/// sound every fifth level (at most every 100 ticks).
@@ -225,6 +225,7 @@ impl Game<'_> {
Some(BlockEntity::Crafter(c)) => (Kind::CrafterBlock, c.items.to_vec()),
_ => return false,
};
let furnace = matches!(kind, Kind::Furnace(_));
// CrafterBlockEntity.containerData: the disabled slots, then triggered.
let crafter_data = match self.world.block_entities.get(&pos) {
Some(BlockEntity::Crafter(c)) => Some((c.disabled, c.triggered)),
@@ -284,6 +285,12 @@ impl Game<'_> {
}
_ => {}
}
// FurnaceResultSlot.checkTakeAchievements: the guest took from the output.
let count = |s: Option<&ItemStack>| s.map_or(0, |s| s.count);
if furnace && count(menu.slot(2)) < count(items[2].as_ref()) {
let at = host.hub.guests[gi].position;
self.award_furnace_recipes(pos, at);
}
host.play.entry(conn).or_default().crafting.machine = Some((p.container, menu));
host.hub.inventory_clicked(conn, p, &self.world, &changed);
true
+18 -1
View File
@@ -373,12 +373,29 @@ impl GameLevel<'_> {
let mut neighbour_updates = 0;
if side_effects {
match kind {
PreRemove::Contents { slots, .. } => {
PreRemove::Contents { slots, furnace } => {
let items = entity.as_ref().map_or(&[][..], BlockEntity::slots);
for slot in 0..slots {
let stack = items.get(slot).cloned().flatten();
self.drop_item_stack(pos, stack);
}
// AbstractFurnaceBlockEntity.preRemoveSideEffects: then the
// experience of what it made, at the block's centre.
if furnace && let Some(BlockEntity::Furnace(f)) = &entity {
let at = [
f64::from(pos.x) + 0.5,
f64::from(pos.y) + 0.5,
f64::from(pos.z) + 0.5,
];
for (count, experience) in f.recipe_experience(self.recipes) {
let xp = horizon_client::containers::create_experience(
count,
experience,
&mut self.world.random,
);
self.award_orbs(at, xp);
}
}
}
PreRemove::Campfire => {
for _ in 0..4 {