diff --git a/crates/horizon-client/src/mobs/villager.rs b/crates/horizon-client/src/mobs/villager.rs index aad0ec911..20280b190 100644 --- a/crates/horizon-client/src/mobs/villager.rs +++ b/crates/horizon-client/src/mobs/villager.rs @@ -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)