From 581dc08cf5be4da424c2e308e7185b1f979a6d79 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 15:15:43 +0100 Subject: [PATCH] Fire shot_crossbow and killed_by_arrow advancement triggers --- crates/horizon-client/src/advancements.rs | 37 ++++++++++++++++++- crates/horizon-client/src/arrows.rs | 3 ++ .../horizon-client/src/game/advancements.rs | 13 +++++++ crates/horizon-client/src/game/creatures.rs | 11 ++++++ crates/horizon-client/src/game/crossbow.rs | 7 ++++ 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/advancements.rs b/crates/horizon-client/src/advancements.rs index 6706532b7..dda104281 100644 --- a/crates/horizon-client/src/advancements.rs +++ b/crates/horizon-client/src/advancements.rs @@ -4,7 +4,7 @@ //! conditions advancements use). use serde_json::Value; use std::{ - collections::{BTreeMap, HashMap}, + collections::{BTreeMap, BTreeSet, HashMap}, path::Path, }; @@ -703,6 +703,41 @@ pub fn entity(v: &Value, e: &EntityView, w: &dyn World, origin: [f64; 3]) -> boo } true } +/// `KilledByArrowTrigger.TriggerInstance.matches`: the weapon, each listed +/// victim condition matched by a distinct victim, and the count of entity types. +pub fn killed_by_arrow( + c: &Value, + me: &EntityView, + victims: &[EntityView], + weapon: Option<&ItemView>, + w: &dyn World, +) -> bool { + fn ctx<'a>(e: &'a EntityView, me: &EntityView) -> Entities<'a> { + Entities { + this: Some(e), + origin: me.position, + tool: None, + block_pos: None, + } + } + if !conditions(&c["player"], &ctx(me, me), w) { + return false; + } + if !c["fired_from_weapon"].is_null() + && !weapon.is_some_and(|it| item(&c["fired_from_weapon"], it, w)) + { + return false; + } + let mut left: Vec<&EntityView> = victims.iter().collect(); + for want in c["victims"].as_array().into_iter().flatten() { + let Some(i) = left.iter().position(|e| conditions(want, &ctx(e, me), w)) else { + return false; + }; + left.remove(i); + } + let kinds: BTreeSet<&str> = victims.iter().map(|e| e.kind.as_str()).collect(); + range_i(&c["unique_entity_types"], kinds.len() as i64) +} /// The entities a loot-style condition list may refer to. pub struct Entities<'a> { pub this: Option<&'a EntityView>, diff --git a/crates/horizon-client/src/arrows.rs b/crates/horizon-client/src/arrows.rs index 30b501011..c23e2ec71 100644 --- a/crates/horizon-client/src/arrows.rs +++ b/crates/horizon-client/src/arrows.rs @@ -155,6 +155,8 @@ pub struct ArrowState { pub pickup: Pickup, /// `pickupItemStack`: what picking the arrow up gives. pub pickup_item: crate::items::ItemStack, + /// `AbstractArrow.firedFromWeapon`: the crossbow or bow it was shot from. + pub fired_from_weapon: Option, pub no_physics: bool, pub pierce: u8, /// `Entity.remainingFireTicks`. @@ -197,6 +199,7 @@ impl ArrowState { last_state: None, pickup: Pickup::Disallowed, pickup_item: crate::items::ItemStack::new("minecraft:arrow", 1), + fired_from_weapon: None, no_physics: false, pierce: 0, fire_ticks: 0, diff --git a/crates/horizon-client/src/game/advancements.rs b/crates/horizon-client/src/game/advancements.rs index 0f76d0ca9..ed371417b 100644 --- a/crates/horizon-client/src/game/advancements.rs +++ b/crates/horizon-client/src/game/advancements.rs @@ -681,6 +681,19 @@ impl Game<'_> { adv::conditions(&c["player"], &pctx, w) && adv::conditions(&c[&key], &ectx, w) }); } + /// `KilledByArrowTrigger`: what the arrow killed and the weapon it came from. + pub(super) fn trigger_killed_by_arrow( + &mut self, + victims: &[EntityView], + weapon: Option<&ItemStack>, + ) { + let me = self.player_entity(); + let victims = victims.to_vec(); + let weapon = weapon.map(item_view); + self.trigger("minecraft:killed_by_arrow", move |c, w| { + adv::killed_by_arrow(c, &me, &victims, weapon.as_ref(), w) + }); + } /// A player condition plus one item (`item` key). pub(super) fn trigger_item(&mut self, name: &str, stack: &ItemStack) { let me = self.player_entity(); diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 194256040..0efef9bc6 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -2111,6 +2111,17 @@ impl Game<'_> { .is_some_and(|m| m.damage(amount, source)); if p.owner == Some(Who::Player) { self.last_hurt_by_player.insert(id, self.lan_acting); + // AbstractArrow.onHitEntity: KILLED_BY_ARROW for a kill + // (piercing arrows are not modelled). + if hurt + && let Some(m) = self.mobs.iter().find(|m| m.id == id) + && !m.alive() + { + let victim = self.mob_entity(m); + let weapon = + p.arrow.as_ref().and_then(|a| a.fired_from_weapon.as_ref()); + self.trigger_killed_by_arrow(&[victim], weapon); + } } hurt } diff --git a/crates/horizon-client/src/game/crossbow.rs b/crates/horizon-client/src/game/crossbow.rs index 70ccba8d3..0e0df1a62 100644 --- a/crates/horizon-client/src/game/crossbow.rs +++ b/crates/horizon-client/src/game/crossbow.rs @@ -152,6 +152,7 @@ impl Game<'_> { }; let charged = std::mem::take(&mut bow.data_mut().charged); bow.tidy(); + let weapon = bow.clone(); let count = charged.len(); // getShootingPower: a rocket among them slows every shot. let power = if charged.iter().any(|s| &*s.item == FIREWORK_ROCKET) { @@ -204,6 +205,7 @@ impl Game<'_> { if let Some(a) = arrow.arrow.as_mut() { a.set_item(ammo); a.set_owner(Some(Who::Player)); + a.fired_from_weapon = Some(weapon.clone()); } arrow.shoot(&mut self.world.random, dir.to_array(), power, 1.0); self.creatures.projectiles.push(arrow); @@ -219,6 +221,11 @@ impl Game<'_> { .play("minecraft:item.crossbow.shoot", Some(eye), 1.0, pitch); self.damage_held(if rocket { 3 } else { 1 }); } + // CrossbowItem.performShooting: SHOT_CROSSBOW with the crossbow, which + // is an empty stack (no match) when the shots broke it. + if let Some(stack) = self.held().cloned() { + self.trigger_item("minecraft:shot_crossbow", &stack); + } } }