diff --git a/crates/horizon-client/src/game/item_draw.rs b/crates/horizon-client/src/game/item_draw.rs index aae200b50..b05f4fbf5 100644 --- a/crates/horizon-client/src/game/item_draw.rs +++ b/crates/horizon-client/src/game/item_draw.rs @@ -213,7 +213,7 @@ impl Game<'_> { owner.is_local_player = true; owner.is_view_entity = true; if let (Some((slot, ticks)), Some(stack)) = (self.using, stack) - && self.inventory.get(slot).and_then(Option::as_ref) == Some(stack) + && self.stack_in(slot) == Some(stack) { let view = item_render::stack_view(stack); let duration = use_duration(&self.pack.item_renderer.models.components(&view)); diff --git a/crates/horizon-client/src/game/mob_blocks.rs b/crates/horizon-client/src/game/mob_blocks.rs index c2246cf5b..8b2b2da40 100644 --- a/crates/horizon-client/src/game/mob_blocks.rs +++ b/crates/horizon-client/src/game/mob_blocks.rs @@ -257,20 +257,36 @@ impl Game<'_> { out: &mut Vec, ) { // ItemInHandLayer submits with OverlayTexture.NO_OVERLAY: no hurt tint. - for (item, left) in [(&mob.extra.main_hand, false), (&mob.extra.off_hand, true)] { + for (item, off_hand) in [(&mob.extra.main_hand, false), (&mob.extra.off_hand, true)] { let Some(item) = item else { continue }; + let left = off_hand != mob.extra.main_hand_left; let Some(t) = render::held_item_transform(mob, alpha as f32, camera, &self.res.mob_data, left) else { continue; }; - let slot = if left { + let slot = if off_hand { horizon_client::mobs::gear::OFF } else { horizon_client::mobs::gear::MAIN }; - let mut stack = ItemStack::new(item, 1); - if horizon_client::mobs::gear::enchanted(mob, slot) { + let local_player = self + .player_model + .as_ref() + .is_some_and(|p| p.id == mob.id && p.kind.id == mob.kind.id); + let mut stack = if local_player { + let Some(stack) = self.stack_in(if off_hand { + super::shield::OFFHAND_SLOT + } else { + self.selected + }) else { + continue; + }; + stack.clone() + } else { + ItemStack::new(item, 1) + }; + if !local_player && horizon_client::mobs::gear::enchanted(mob, slot) { // The gear's enchantments are not tracked, only that it has some. stack.enchant("minecraft:unbreaking", 1); } @@ -284,12 +300,13 @@ impl Game<'_> { ); // The local player's rod while its hook is out. owner.fishing_cast_with_this_item = self.cast_rod().is_some() - && self.main_hand_rod() != left + && self.main_hand_rod() != off_hand && self .player_model .as_ref() .is_some_and(|p| std::ptr::eq(p, mob)); - if !left && mob.extra.using_item > 0 { + owner.main_arm_left = mob.extra.main_hand_left; + if off_hand == mob.extra.using_off_hand && mob.extra.using_item > 0 { owner.using_this_item = true; owner.use_duration = 72_000; owner.use_item_remaining_ticks = 72_000 - mob.extra.using_item; diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index f65a95438..50abc8e56 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -305,6 +305,13 @@ impl Game<'_> { let (position, flying) = (p.position, p.flying || p.fall_flying); let (fall_flying, fly_ticks, velocity) = (p.fall_flying, p.fall_fly_ticks, p.velocity); let using = self.using.map(|(_, ticks)| ticks as i32 + 1).unwrap_or(0); + let using_off_hand = self + .using + .is_some_and(|(slot, _)| slot == super::shield::OFFHAND_SLOT); + let blocking = self + .using + .and_then(|(slot, _)| self.stack_in(slot)) + .is_some_and(|stack| self.blocks_attacks(stack).is_some()); let charged = self.inventory[self.selected] .as_ref() .is_some_and(|s| s.data().is_some_and(|d| !d.charged.is_empty())); @@ -375,6 +382,8 @@ impl Game<'_> { m.extra.fall_fly_ticks = fly_ticks; m.extra.main_hand_left = self.settings.main_hand_left; m.extra.using_item = using; + m.extra.using_off_hand = using_off_hand; + m.extra.blocking = blocking; m.extra.charged = charged; m.extra.ints[5] = charge_duration; m.extra.elytra.tick(fall_flying, crouching, velocity); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 8b51593bc..0a58c376c 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -686,6 +686,8 @@ pub struct Extra { pub legs: Option>, pub feet: Option>, pub using_item: i32, + pub using_off_hand: bool, + pub blocking: bool, pub fall_flying: bool, pub fall_fly_ticks: i32, pub main_hand_left: bool, diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 161db24f9..36e6d78de 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -232,6 +232,7 @@ fn bob(p: &mut Poses, n: &str, age: f32, scale: f32) { pub enum ArmPose { Empty, Item, + Block, Bow, CrossbowHold, CrossbowCharge, @@ -344,6 +345,15 @@ fn pose_arm(p: &mut Poses, pose: ArmPose, right: bool, h: &Hum) { p.xrot(arm, x * 0.5 - PI / 10.0); p.yrot(arm, 0.0); } + ArmPose::Block => { + let x = p.get(arm)[3] * 0.5 - 0.942_477_9 + hx.clamp(-1.396_263_4, 0.436_332_32); + p.xrot(arm, x); + p.yrot( + arm, + if right { -0.523_598_8 } else { 0.523_598_8 } + + hy.clamp(-0.523_598_8, 0.523_598_8), + ); + } ArmPose::Bow => { p.yrot("right_arm", -0.1 + hy - if right { 0.0 } else { 0.4 }); p.yrot("left_arm", 0.1 + hy + if right { 0.4 } else { 0.0 }); @@ -546,10 +556,12 @@ pub fn setup( } else { ArmPose::Empty }; + let using_main = m.extra.using_item > 0 && !m.extra.using_off_hand; let pose = if player { match m.extra.main_hand.as_deref() { - Some("minecraft:bow") if m.extra.using_item > 0 => ArmPose::Bow, - Some("minecraft:crossbow") if m.extra.using_item > 0 && !m.extra.charged => { + Some(_) if using_main && m.extra.blocking => ArmPose::Block, + Some("minecraft:bow") if using_main => ArmPose::Bow, + Some("minecraft:crossbow") if using_main && !m.extra.charged => { ArmPose::CrossbowCharge } Some("minecraft:crossbow") if m.extra.charged && s.swing == 0.0 => { @@ -562,7 +574,11 @@ pub fn setup( right }; let off = if player && m.extra.off_hand.is_some() && !pose.two_handed() { - ArmPose::Item + if m.extra.using_item > 0 && m.extra.using_off_hand && m.extra.blocking { + ArmPose::Block + } else { + ArmPose::Item + } } else { ArmPose::Empty }; @@ -672,6 +688,42 @@ pub fn setup( #[cfg(test)] mod flight_pose_tests { use super::*; + + #[test] + fn blocking_arm_follows_the_head_with_limits_in_both_hands() { + for right in [false, true] { + for (pitch, yaw, expected_pitch, expected_yaw) in [ + (0.0, 0.0, -0.742_477_9, 0.0), + (2.0, 2.0, -0.306_145_58, 0.523_598_8), + (-2.0, -2.0, -2.138_741_3, -0.523_598_8), + ] { + let mut p = Poses { + map: ["head", "right_arm", "left_arm"] + .into_iter() + .map(|n| (n.to_string(), [0.0; 9])) + .collect(), + dummy: [0.0; 9], + skipped: Default::default(), + hidden: Default::default(), + }; + let arm = if right { "right_arm" } else { "left_arm" }; + p.xrot("head", pitch); + p.yrot("head", yaw); + p.xrot(arm, 0.4); + pose_arm( + &mut p, + ArmPose::Block, + right, + &Hum::new(ArmPose::Empty, ArmPose::Empty), + ); + let pose = *p.get(arm); + assert!((pose[3] - expected_pitch).abs() < 1e-6); + let side = if right { -0.523_598_8 } else { 0.523_598_8 }; + assert!((pose[4] - (side + expected_yaw)).abs() < 1e-6); + assert_eq!(p.get(if right { "left_arm" } else { "right_arm" })[3], 0.0); + } + } + } #[test] fn flight_slows_limbs_and_sets_the_head_pose() { let state = State {