A villager that loses its job site offers its new profession's trades

The reported sequence in #1639: a villager works at a bench, the bench is
destroyed and it takes another bench's job, and then it does not open a
trading menu.

`Villager.setVillagerData` drops the offers (`offers = null`) when the
profession changes, so the next `AbstractVillager.getOffers` rolls the new
profession's trade set. The brain reaches that through `set_profession`,
which now also marks the offers as not made (#2009); this test covers the
whole reported sequence, from the lost job site to the new profession's
trades, so a villager that keeps the old profession's offers marked as made
cannot pass it.

Co-Authored-By: minimax-m3.1-flash (Claude Code) <noreply@users.noreply.github.com>
This commit is contained in:
levzzz
2026-10-27 04:35:26 +05:00
parent aba4f19a81
commit e184ab7a42
@@ -905,6 +905,53 @@ mod tests {
assert!(t.unset);
assert_eq!(m.extra.variant2, 2);
}
/// The reported sequence: a villager works at a bench, the bench is
/// destroyed (its job site is gone, so `ResetProfession` makes it
/// unemployed) and it takes another bench's job. The new profession's
/// trades are offered again.
///
/// `Villager.setVillagerData` drops the offers (`offers = null`) when the
/// profession changes, so `AbstractVillager.getOffers` rolls the new
/// profession's trade set the next time the offers are asked for. A
/// villager that keeps its old offers marked as made never trades again:
/// it shakes its head instead of opening the trading menu.
#[test]
fn a_villager_that_loses_its_job_site_offers_its_new_profession_trades() {
let mut m = villager();
// The composter's job: the villager is a farmer.
set_profession(&mut m, 5);
assert_eq!(m.extra.variant2, 5);
// Its trades were made, so the offers are there.
let trade = m.extra.trade.get_or_insert_default();
trade.unset = false;
trade.offers = vec![crate::merchant::MerchantOffer {
base_cost_a: crate::merchant::ItemCost {
stack: ItemStack::new("minecraft:wheat", 20),
},
cost_b: None,
result: ItemStack::new("minecraft:emerald", 1),
uses: 0,
max_uses: 16,
reward_exp: true,
special_price_diff: 0,
demand: 0,
price_multiplier: 0.0,
xp: 1,
}];
// The work bench is destroyed: the job site is gone, so the villager
// is unemployed and its offers are dropped (`offers = null`).
set_profession(&mut m, NONE);
let t = m.extra.trade.as_ref().unwrap();
assert!(t.offers.is_empty());
assert!(t.unset);
// It takes the smithing table's job. The new profession's trades are
// rolled by the next `getOffers` (ensure_offers).
set_profession(&mut m, 13);
let t = m.extra.trade.as_ref().unwrap();
assert!(t.offers.is_empty());
assert!(t.unset);
assert_eq!(m.extra.variant2, 13);
}
fn stack(item: &str, count: u32) -> ItemStack {
ItemStack::new(item, count)