From e60cbe8d4df0ad5ea87d571984f2bfd8119cfca8 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 18:48:03 +0100 Subject: [PATCH] Keep whole nugget stacks and picked-up enchantments on piglins --- crates/horizon-client/src/game/piglins.rs | 24 ++++++++++++++-------- crates/horizon-client/src/mobs/finalize.rs | 8 ++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game/piglins.rs b/crates/horizon-client/src/game/piglins.rs index 7e58620bf..edfd9a3a0 100644 --- a/crates/horizon-client/src/game/piglins.rs +++ b/crates/horizon-client/src/game/piglins.rs @@ -6,7 +6,10 @@ use horizon_client::{ items::ItemStack, mobs::{ Mob, Species, Who, - finalize::{SLOT_NAMES, set_drop_chance, set_item, stored_drop_chance}, + finalize::{ + SLOT_NAMES, set_drop_chance, set_item, set_slot_enchantments, slot_enchantments, + stored_drop_chance, + }, piglin::{ADMIRE, ATE_RECENTLY}, }, }; @@ -88,7 +91,8 @@ impl Game<'_> { /// `Mob.equipItemIfPossible` for a piglin: put it in its slot when that /// slot takes it (an armour piece that cannot replace what is worn goes /// to an empty main hand), dropping what it replaces by drop chance. - fn piglin_equip(&mut self, index: usize, item: &str) -> bool { + fn piglin_equip(&mut self, index: usize, stack: &ItemStack) -> bool { + let item = &*stack.item; let mut slot = self.piglin_slot(item); let m = &self.mobs[index]; let mut replace = self.piglin_can_replace(m, item, slot); @@ -103,18 +107,18 @@ impl Game<'_> { let old = horizon_client::mobs::finalize::item(m, slot); let chance = stored_drop_chance(m, slot); let drop_old = old.is_some() && (m.random.next_float() - 0.1).max(0.0) < chance; + let old_enchantments = slot_enchantments(m, slot); set_item(m, slot, item); + // The piglin keeps what the stack carried (Protection IV stays on). + set_slot_enchantments(m, slot, stack.enchantments().to_vec()); // setItemSlotAndDropWhenKilled, then persistenceRequired. set_drop_chance(m, slot, 2.0); m.persistent = true; let at = [m.position.x, m.position.y, m.position.z]; if let (true, Some(old)) = (drop_old, old) { - self.spawn_item( - ItemStack::new(&old, 1), - at, - super::dispenser::new_item_motion(), - 10, - ); + let mut dropped = ItemStack::new(&old, 1); + dropped.set_crafting_enchantments(old_enchantments); + self.spawn_item(dropped, at, super::dispenser::new_item_motion(), 10); } true } @@ -153,7 +157,9 @@ impl Game<'_> { /// `PiglinAi.stopHoldingOffHandItem` for something that is not /// bartering currency: wear it if it can, else keep it in the inventory. fn piglin_keep(&mut self, index: usize, stack: ItemStack) { - if !self.piglin_equip(index, &stack.item) { + // Nuggets keep their whole stack in the inventory (a hand slot here + // holds one item id only). + if &*stack.item == "minecraft:gold_nugget" || !self.piglin_equip(index, &stack) { self.piglin_stash(index, stack); } } diff --git a/crates/horizon-client/src/mobs/finalize.rs b/crates/horizon-client/src/mobs/finalize.rs index 4eeed885b..58aaa02bb 100644 --- a/crates/horizon-client/src/mobs/finalize.rs +++ b/crates/horizon-client/src/mobs/finalize.rs @@ -829,6 +829,14 @@ pub fn set_item(m: &mut Mob, slot: usize, id: &str) { } gear::set(m, gear_slot(slot), Vec::new()); } +/// The enchantments on what `m` has in `slot`. +pub fn slot_enchantments(m: &Mob, slot: usize) -> gear::Levels { + gear::gear(m).map_or_else(Vec::new, |g| g.slots[gear_slot(slot)].clone()) +} +/// Replace the enchantments of what `m` has in `slot` (after `set_item`). +pub fn set_slot_enchantments(m: &mut Mob, slot: usize, levels: gear::Levels) { + gear::set(m, gear_slot(slot), levels); +} /// `Mob.setDropChance`. pub fn set_drop_chance(m: &mut Mob, slot: usize, chance: f32) { let c = m