Merge pull request #2251 from kacpherek/fix/brewed-potion-trigger
🤖 Agent: Merged by hand by Ludovic's AI agent (orchestrator) after the contribution gate. rdbtCVS's change request is addressed at this head; a re-review was asked, with no new objection since. Proven in wave 5's union run (cg21 union 65b0cb252 (72 kacpherek PRs incl. this one) playhorizon 94/95 + Mac mini 33/33, lan_e2e 23/23 both ).
This commit is contained in:
@@ -473,6 +473,51 @@ pub fn id_matches(v: &Value, registry: &str, id: &str, w: &dyn World) -> bool {
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
/// `PotionsPredicate`: the potion in a `HolderSet`, plus a `CollectionPredicate`
|
||||
/// over the potion's effects (`contains`, `count`, `size`).
|
||||
pub fn potions(v: &Value, potion: Option<&str>, w: &dyn World) -> bool {
|
||||
if v.is_null() {
|
||||
return true;
|
||||
}
|
||||
if !v["potions"].is_null() && !potion.is_some_and(|p| id_matches(&v["potions"], "potion", p, w))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let c = &v["effects"];
|
||||
if c.is_null() {
|
||||
return true;
|
||||
}
|
||||
let effects: Vec<(String, i32, i32)> = potion
|
||||
.and_then(crate::brewing::potion)
|
||||
.map(|d| {
|
||||
d.effects
|
||||
.iter()
|
||||
.map(|(id, dur, amp)| (format!("minecraft:{id}"), *amp, *dur))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
// `MobEffectsPredicate.test`: the one listed effect, amplifier and duration.
|
||||
let test = |pred: &Value, e: &(String, i32, i32)| {
|
||||
pred.as_object().is_some_and(|m| {
|
||||
m.get(&e.0).is_some_and(|want| {
|
||||
range_i(&want["amplifier"], i64::from(e.1))
|
||||
&& range_i(&want["duration"], i64::from(e.2))
|
||||
&& want["ambient"].as_bool().is_none_or(|b| !b)
|
||||
&& want["visible"].as_bool().is_none_or(|b| b)
|
||||
})
|
||||
})
|
||||
};
|
||||
c["contains"]
|
||||
.as_array()
|
||||
.is_none_or(|l| l.iter().all(|p| effects.iter().any(|e| test(p, e))))
|
||||
&& c["count"].as_array().is_none_or(|l| {
|
||||
l.iter().all(|entry| {
|
||||
let n = effects.iter().filter(|e| test(&entry["test"], e)).count();
|
||||
range_i(&entry["count"], n as i64)
|
||||
})
|
||||
})
|
||||
&& range_i(&c["size"], effects.len() as i64)
|
||||
}
|
||||
/// `ItemPredicate`.
|
||||
pub fn item(v: &Value, it: &ItemView, w: &dyn World) -> bool {
|
||||
if v.is_null() {
|
||||
|
||||
@@ -6790,6 +6790,7 @@ impl Game<'_> {
|
||||
self.player_menu_sound(sound);
|
||||
}
|
||||
let crafted = std::mem::take(&mut menu.crafted);
|
||||
let brewed = std::mem::take(&mut menu.brewed);
|
||||
let equipped = std::mem::take(&mut menu.equipped);
|
||||
for (i, stack) in after.into_iter().enumerate() {
|
||||
if let Some(g) = game_of[i] {
|
||||
@@ -6805,6 +6806,9 @@ impl Game<'_> {
|
||||
self.player_drop(stack);
|
||||
}
|
||||
self.award_crafted(&crafted);
|
||||
for stack in &brewed {
|
||||
self.trigger_brewed_potion(stack);
|
||||
}
|
||||
if furnace_before > furnace_after
|
||||
&& self.remote.is_none()
|
||||
&& let (Some(pos), Some(p)) = (self.container, self.player.as_ref())
|
||||
|
||||
@@ -779,6 +779,22 @@ impl Game<'_> {
|
||||
let it = item_view(stack);
|
||||
self.trigger(name, |c, w| adv::item_entity(c, &me, target, &it, w));
|
||||
}
|
||||
/// `BrewedPotionTrigger`: the potion contents of a stack taken from a
|
||||
/// brewing stand.
|
||||
pub(super) fn trigger_brewed_potion(&mut self, stack: &ItemStack) {
|
||||
let me = self.player_entity();
|
||||
let potion = stack.potion().map(str::to_owned);
|
||||
self.trigger("minecraft:brewed_potion", move |c, w| {
|
||||
let pctx = adv::Entities {
|
||||
this: Some(&me),
|
||||
origin: me.position,
|
||||
tool: None,
|
||||
block_pos: None,
|
||||
};
|
||||
adv::conditions(&c["player"], &pctx, w)
|
||||
&& adv::potions(&c["potion"], potion.as_deref(), w)
|
||||
});
|
||||
}
|
||||
/// `TradeTrigger`: the villager and the item the trade gave.
|
||||
pub(super) fn trigger_trade(&mut self, villager: &EntityView, stack: &ItemStack) {
|
||||
let me = self.player_entity();
|
||||
|
||||
@@ -127,9 +127,18 @@ impl Game<'_> {
|
||||
}
|
||||
let dropped = std::mem::take(&mut menu.dropped);
|
||||
let equipped = std::mem::take(&mut menu.equipped);
|
||||
let brewed = std::mem::take(&mut menu.brewed);
|
||||
menu.drops.clear();
|
||||
menu.crafted.clear();
|
||||
store_player(menu, &mut host.hub.guests[gi]);
|
||||
if !brewed.is_empty() {
|
||||
let conn = host.hub.guests[gi].conn;
|
||||
self.as_guest(host, conn, |g| {
|
||||
for stack in &brewed {
|
||||
g.trigger_brewed_potion(stack);
|
||||
}
|
||||
});
|
||||
}
|
||||
if !equipped.is_empty() {
|
||||
let conn = host.hub.guests[gi].conn;
|
||||
self.as_guest(host, conn, |g| {
|
||||
|
||||
@@ -288,6 +288,9 @@ pub struct Menu {
|
||||
pub player_sounds: Vec<&'static str>,
|
||||
/// Recipes whose result was taken (`awardUsedRecipes`), for the caller.
|
||||
pub crafted: Vec<String>,
|
||||
/// Potions taken from a brewing stand's bottle slots
|
||||
/// (`PotionSlot.onTake`), for the caller.
|
||||
pub brewed: Vec<ItemStack>,
|
||||
/// `LivingEntity.onEquipItem` calls that change the item: the
|
||||
/// `EquipmentSlot` name and the new stack, in order, for the caller.
|
||||
pub equipped: Vec<(&'static str, Option<ItemStack>)>,
|
||||
@@ -683,6 +686,7 @@ impl Menu {
|
||||
sounds: Vec::new(),
|
||||
player_sounds: Vec::new(),
|
||||
crafted: Vec::new(),
|
||||
brewed: Vec::new(),
|
||||
equipped: Vec::new(),
|
||||
merchant: None,
|
||||
stone: Stonecutter::default(),
|
||||
@@ -1504,7 +1508,17 @@ impl Menu {
|
||||
}
|
||||
|
||||
/// `ResultSlot.onTake` and the other `Slot.onTake` overrides.
|
||||
fn on_take(&mut self, env: &Env, s: usize) {
|
||||
fn on_take(&mut self, env: &Env, s: usize, taken: &ItemStack) {
|
||||
if self.slots[s].role == Role::Potion {
|
||||
// BrewingStandMenu.PotionSlot.onTake: a stack with potion contents.
|
||||
if matches!(
|
||||
&*taken.item,
|
||||
"minecraft:potion" | "minecraft:splash_potion" | "minecraft:lingering_potion"
|
||||
) {
|
||||
self.brewed.push(taken.clone());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if self.slots[s].role == Role::MerchantResult {
|
||||
self.merchant_take(env);
|
||||
return;
|
||||
@@ -1671,7 +1685,7 @@ impl Menu {
|
||||
max_amount: u32,
|
||||
) -> Option<ItemStack> {
|
||||
let mut result = self.try_remove(env, s, amount, max_amount)?;
|
||||
self.on_take(env, s);
|
||||
self.on_take(env, s, &result);
|
||||
if self.slots[s].role == Role::CartoResult {
|
||||
Self::map_crafted(env, &mut result);
|
||||
}
|
||||
@@ -2346,7 +2360,7 @@ impl Menu {
|
||||
return None;
|
||||
}
|
||||
if after_take || matches!(self.kind, Kind::Inventory | Kind::Crafting) {
|
||||
self.on_take(env, index);
|
||||
self.on_take(env, index, &clicked);
|
||||
}
|
||||
// What the result slot's move left over is dropped.
|
||||
if (index == 0 && matches!(self.kind, Kind::Inventory | Kind::Crafting))
|
||||
@@ -2594,7 +2608,7 @@ impl Menu {
|
||||
};
|
||||
if let Some(mut taken) = self.try_remove(env, s, amount, u32::MAX) {
|
||||
self.carried = Some(taken.clone());
|
||||
self.on_take(env, s);
|
||||
self.on_take(env, s, &taken);
|
||||
if self.slots[s].role == Role::CartoResult {
|
||||
// onTake's onCraftedBy edits the carried stack.
|
||||
Self::map_crafted(env, &mut taken);
|
||||
@@ -2628,7 +2642,7 @@ impl Menu {
|
||||
if let Some(c) = self.carried.as_mut() {
|
||||
c.count += taken.count;
|
||||
}
|
||||
self.on_take(env, s);
|
||||
self.on_take(env, s, &taken);
|
||||
if self.slots[s].role == Role::CartoResult {
|
||||
// onCraftedBy edits the taken stack, not the carried one.
|
||||
Self::map_crafted(env, &mut taken);
|
||||
@@ -2656,7 +2670,7 @@ impl Menu {
|
||||
if self.may_pickup(env, target) {
|
||||
self.containers[PLAYER][inv_slot] = Some(taken.clone());
|
||||
self.set_by_player(env, target, None);
|
||||
self.on_take(env, target);
|
||||
self.on_take(env, target, &taken);
|
||||
if self.slots[target].role == Role::CartoResult
|
||||
&& let Some(stack) = self.containers[PLAYER][inv_slot].as_mut()
|
||||
{
|
||||
@@ -2684,14 +2698,14 @@ impl Menu {
|
||||
let part = split(&mut source, max);
|
||||
self.containers[PLAYER][inv_slot] = Some(source);
|
||||
self.set_by_player(env, target, Some(part));
|
||||
self.on_take(env, target);
|
||||
self.on_take(env, target, &taken);
|
||||
if !self.inventory_add(env, &mut taken) {
|
||||
self.drop_stack(&taken);
|
||||
}
|
||||
} else {
|
||||
self.containers[PLAYER][inv_slot] = Some(taken);
|
||||
self.containers[PLAYER][inv_slot] = Some(taken.clone());
|
||||
self.set_by_player(env, target, Some(source));
|
||||
self.on_take(env, target);
|
||||
self.on_take(env, target, &taken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user