From ffd0297dc421e499c7567e4602d91e5fe652a1b9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 10:42:34 +0200 Subject: [PATCH] Crossbows load firework rockets from either hand; bows and crossbows take held arrows first Player.getProjectile: what the weapon supports in the off hand, then in the main hand (getHeldProjectile; crossbows also take firework rockets there), else the first arrows in the inventory. Crossbows only looked for arrows in the inventory, so a rocket in the off hand was never loaded. A charged rocket is shot at an angle from 0.15 under the eyes (CrossbowItem.createProjectile), every shot of a load holding one at power 1.6 (getShootingPower), for three durability (getDurabilityUse). Part of #276 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 21 +-- crates/horizon-client/src/game/crossbow.rs | 146 ++++++++++++++++---- crates/horizon-client/src/game/dispenser.rs | 2 +- crates/horizon-client/src/game/fireworks.rs | 5 +- crates/horizon-client/src/game/shield.rs | 2 +- 5 files changed, 133 insertions(+), 43 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a329311c7..9d68cb0c6 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2386,7 +2386,9 @@ impl Game<'_> { return Ok(()); } if &*stack.item == "minecraft:bow" { - if self.using.is_none() && (self.creative || self.arrow_slot().is_some()) { + if self.using.is_none() + && (self.creative || self.projectile_slot("minecraft:bow").is_some()) + { self.using = Some((self.selected, 0)); } return Ok(()); @@ -7046,10 +7048,7 @@ impl Game<'_> { if power < 0.1 { return; } - let arrow_slot = self - .inventory - .iter() - .position(|s| s.as_ref().is_some_and(|s| &*s.item == "minecraft:arrow")); + let arrow_slot = self.projectile_slot("minecraft:bow"); if arrow_slot.is_none() && !self.creative { return; } @@ -7073,7 +7072,7 @@ impl Game<'_> { // using the ammo, so the arrow is CREATIVE_ONLY; else setOwner(player) // lets it be picked up. let ammo = arrow_slot - .and_then(|i| self.inventory[i].clone()) + .and_then(|i| self.stack_in(i).cloned()) .unwrap_or_else(|| ItemStack::new("minecraft:arrow", 1)); if let Some(a) = arrow.arrow.as_mut() { a.set_item(&horizon_client::arrows::drawn_ammo( @@ -7110,11 +7109,13 @@ impl Game<'_> { if !self.creative && !infinity && let Some(i) = arrow_slot - && let Some(s) = self.inventory[i].as_mut() { - s.count -= 1; - if s.count == 0 { - self.inventory[i] = None; + let cell = self.stack_in_mut(i); + if let Some(s) = cell.as_mut() { + s.count -= 1; + if s.count == 0 { + *cell = None; + } } } self.damage_held(1); diff --git a/crates/horizon-client/src/game/crossbow.rs b/crates/horizon-client/src/game/crossbow.rs index 335f62785..4a62aa194 100644 --- a/crates/horizon-client/src/game/crossbow.rs +++ b/crates/horizon-client/src/game/crossbow.rs @@ -1,8 +1,10 @@ //! Crossbows (`CrossbowItem`): charging with the start/middle/end sounds -//! (Quick Charge), loading arrows into `charged_projectiles` (Multishot -//! loads three), and shooting critical arrows at power 3.15 with the fanned -//! Multishot angles and their shot pitches. -use super::{Game, view_vector}; +//! (Quick Charge), loading arrows or a held firework rocket into +//! `charged_projectiles` (Multishot loads three), and shooting critical +//! arrows at power 3.15, or rockets at 1.6 for three durability, with the +//! fanned Multishot angles and their shot pitches. `Player.getProjectile` +//! finds the ammo for bows and crossbows. +use super::{Game, shield::OFFHAND_SLOT, view_vector}; use horizon_client::{ items::ItemStack, mobs::Who, @@ -11,6 +13,31 @@ use horizon_client::{ use horizon_core::{geometry::Vec3, random::RandomSource}; const CROSSBOW: &str = "minecraft:crossbow"; +const FIREWORK_ROCKET: &str = "minecraft:firework_rocket"; + +/// `ProjectileWeaponItem.ARROW_ONLY`, for the arrows Horizon shoots. +fn is_arrow(stack: &ItemStack) -> bool { + &*stack.item == "minecraft:arrow" +} + +/// `Player.getProjectile`: what `weapon` supports in the off hand, then in +/// the main hand (`getHeldProjectile`; crossbows also take firework rockets +/// there), else the first arrows in the inventory. The slot of the stack. +fn projectile_slot( + weapon: &str, + hands: [(usize, Option<&ItemStack>); 2], + inventory: &[Option], +) -> Option { + let held = |s: &ItemStack| is_arrow(s) || weapon == CROSSBOW && &*s.item == FIREWORK_ROCKET; + hands + .into_iter() + .find_map(|(slot, s)| s.is_some_and(held).then_some(slot)) + .or_else(|| { + inventory + .iter() + .position(|s| s.as_ref().is_some_and(is_arrow)) + }) +} impl Game<'_> { pub(super) fn charge_ticks(stack: &ItemStack) -> u32 { @@ -42,10 +69,10 @@ impl Game<'_> { ], } } - pub(super) fn arrow_slot(&self) -> Option { - self.inventory - .iter() - .position(|s| s.as_ref().is_some_and(|s| &*s.item == "minecraft:arrow")) + /// `Player.getProjectile` for `weapon` (`projectile_slot`). + pub(super) fn projectile_slot(&self, weapon: &str) -> Option { + let hands = [OFFHAND_SLOT, self.selected].map(|slot| (slot, self.stack_in(slot))); + projectile_slot(weapon, hands, &self.inventory) } /// `CrossbowItem.use`: shoot when charged, else start charging. pub(super) fn use_crossbow(&mut self, stack: &ItemStack) -> bool { @@ -58,7 +85,9 @@ impl Game<'_> { } if stack.data().is_some_and(|d| !d.charged.is_empty()) { self.shoot_crossbow(); - } else if (self.arrow_slot().is_some() || self.creative) && self.using.is_none() { + } else if (self.projectile_slot(CROSSBOW).is_some() || self.creative) + && self.using.is_none() + { self.using = Some((self.selected, 0)); } true @@ -88,16 +117,17 @@ impl Game<'_> { } else { 1 }; - let slot_ammo = self.arrow_slot(); + let slot_ammo = self.projectile_slot(CROSSBOW); let ammo = slot_ammo - .and_then(|i| self.inventory[i].clone()) + .and_then(|i| self.stack_in(i).cloned()) .unwrap_or_else(|| ItemStack::new("minecraft:arrow", 1)); if !self.creative { let Some(i) = slot_ammo else { return }; - if let Some(s) = self.inventory[i].as_mut() { + let cell = self.stack_in_mut(i); + if let Some(s) = cell.as_mut() { s.count -= 1; if s.count == 0 { - self.inventory[i] = None; + *cell = None; } } } @@ -123,6 +153,12 @@ impl Game<'_> { let charged = std::mem::take(&mut bow.data_mut().charged); bow.tidy(); let count = charged.len(); + // getShootingPower: a rocket among them slows every shot. + let power = if charged.iter().any(|s| &*s.item == FIREWORK_ROCKET) { + 1.6 + } else { + 3.15 + }; // ProjectileWeaponItem.shoot: angles fanned 10 degrees apart. let spread = if count > 1 { 10.0 } else { 0.0 }; let view = glam::DVec3::from_array(view_vector(self.yaw, self.pitch)); @@ -140,22 +176,38 @@ impl Game<'_> { }; let dir = glam::DQuat::from_axis_angle(up.normalize_or_zero(), angle.to_radians()) * view; - let kind = ProjectileKind::Arrow { - base_damage: 2.0, - critical: true, - effect: None, - }; - let mut arrow = Projectile::new( - kind, - Vec3::new(eye[0], eye[1] - 0.1, eye[2]), - Some(Who::Player), - ); - if let Some(a) = arrow.arrow.as_mut() { - a.set_item(ammo); - a.set_owner(Some(Who::Player)); + // createProjectile: a rocket shot at an angle from 0.15 under the + // eyes, spread by its own random; three durability. + let rocket = &*ammo.item == FIREWORK_ROCKET; + if rocket { + let at = Vec3::new(eye[0], eye[1] - f64::from(0.15f32), eye[2]); + let mut shot = Projectile::new(ProjectileKind::Snowball, at, None); + shot.shoot( + &mut super::dispenser::entity_random(), + dir.to_array(), + power, + 1.0, + ); + self.launch_angled_rocket(at, shot.velocity, ammo); + } + if !rocket { + let kind = ProjectileKind::Arrow { + base_damage: 2.0, + critical: true, + effect: None, + }; + let mut arrow = Projectile::new( + kind, + Vec3::new(eye[0], eye[1] - 0.1, eye[2]), + Some(Who::Player), + ); + if let Some(a) = arrow.arrow.as_mut() { + a.set_item(ammo); + a.set_owner(Some(Who::Player)); + } + arrow.shoot(&mut self.world.random, dir.to_array(), power, 1.0); + self.creatures.projectiles.push(arrow); } - arrow.shoot(&mut self.world.random, dir.to_array(), 3.15, 1.0); - self.creatures.projectiles.push(arrow); self.player_projectile_shot(); let pitch = if index == 0 { 1.0 @@ -165,7 +217,43 @@ impl Game<'_> { }; self.audio .play("minecraft:item.crossbow.shoot", Some(eye), 1.0, pitch); - self.damage_held(1); + self.damage_held(if rocket { 3 } else { 1 }); } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn held_projectiles_come_before_the_inventory() { + let stack = |item: &str| Some(ItemStack::new(item, 1)); + let (crossbow, rocket, arrow) = ( + stack(CROSSBOW), + stack(FIREWORK_ROCKET), + stack("minecraft:arrow"), + ); + let mut inventory = vec![None; 36]; + inventory[5] = arrow.clone(); + // The weapon in hotbar slot 0, `off` in the off hand. + let slot = |weapon, off: &Option, inventory: &[Option]| { + let hands = [(OFFHAND_SLOT, off.as_ref()), (0, crossbow.as_ref())]; + projectile_slot(weapon, hands, inventory) + }; + // The off hand's rocket loads a crossbow; a bow only takes arrows. + assert_eq!(slot(CROSSBOW, &rocket, &inventory), Some(OFFHAND_SLOT)); + assert_eq!(slot("minecraft:bow", &rocket, &inventory), Some(5)); + assert_eq!( + slot("minecraft:bow", &arrow, &inventory), + Some(OFFHAND_SLOT) + ); + assert_eq!(slot(CROSSBOW, &None, &inventory), Some(5)); + // A rocket in the inventory is not ammo. + inventory[5] = rocket.clone(); + assert_eq!(slot(CROSSBOW, &None, &inventory), None); + // The main hand's rocket, with the crossbow in the off hand. + let hands = [(OFFHAND_SLOT, crossbow.as_ref()), (3, rocket.as_ref())]; + assert_eq!(projectile_slot(CROSSBOW, hands, &inventory), Some(3)); + } +} diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index e8ceb5be3..15f24f82f 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -950,7 +950,7 @@ impl Game<'_> { "minecraft:firework_rocket" => { let mut shot = Projectile::new(ProjectileKind::Snowball, at, None); shot.shoot(&mut own, dir, power, uncertainty); - self.launch_dispensed_rocket(at, shot.velocity, &one); + self.launch_angled_rocket(at, shot.velocity, &one); } _ => { let kind = match item.as_str() { diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index cd4e01a16..0e074025f 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -85,8 +85,9 @@ impl Game<'_> { self.swing_arm(); true } - /// `FireworkRocketItem.asProjectile` shot out of a dispenser. - pub(super) fn launch_dispensed_rocket(&mut self, at: Vec3, velocity: Vec3, stack: &ItemStack) { + /// A rocket shot at an angle (`DATA_SHOT_AT_ANGLE`): out of a dispenser + /// (`FireworkRocketItem.asProjectile`) or a crossbow. + pub(super) fn launch_angled_rocket(&mut self, at: Vec3, velocity: Vec3, stack: &ItemStack) { let mut rocket = self.new_rocket(at, stack, false); rocket.velocity = velocity; rocket.angled = true; diff --git a/crates/horizon-client/src/game/shield.rs b/crates/horizon-client/src/game/shield.rs index 2821738c5..331257407 100644 --- a/crates/horizon-client/src/game/shield.rs +++ b/crates/horizon-client/src/game/shield.rs @@ -23,7 +23,7 @@ impl Game<'_> { self.inventory.get(slot)?.as_ref() } } - fn stack_in_mut(&mut self, slot: usize) -> &mut Option { + pub(super) fn stack_in_mut(&mut self, slot: usize) -> &mut Option { if slot == OFFHAND_SLOT { &mut self.offhand } else {