diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 9d155a5a4..08cc5b154 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -2028,6 +2028,8 @@ impl Game<'_> { .get(g.selected) .and_then(|s| s.as_ref()) .is_some_and(|s| s.data().is_some_and(|d| !d.charged.is_empty())); + // ClientInformation.mainHand (HumanoidArm: 0 left, 1 right). + m.extra.main_hand_left = g.info.main_hand == 0; // LivingEntity.DATA_EFFECT_PARTICLES: the swirls of its visible effects. let visible = || g.effects.list.iter().filter(|e| e.visible); m.extra.effect_particles = visible() diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 6ba1566d1..03edd5b6d 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -3193,7 +3193,8 @@ impl Game<'_> { let packet = swing_packet(id, c.hand, c.animation); host.hub.broadcast_near(at, 128.0, &packet, Some(conn)); if let Some(m) = host.models.get_mut(&conn) { - m.swing(); + // The duration already counts the guest's haste or fatigue. + m.swing_hand(c.hand == Hand::Off, c.duration_ticks); } } play.swing.tick(); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index d0f9199c3..78b58a72e 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2097,6 +2097,13 @@ impl Game<'_> { m.extra.ints[1] = code; } } + // Avatar.DATA_PLAYER_MAIN_HAND (HumanoidArm: 0 left, 1 + // right): the arm a swing of the main hand moves. + if let (15, Value::Id(arm)) = (index, value) + && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) + { + m.extra.main_hand_left = *arm == 0; + } } } // LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): this @@ -2421,7 +2428,7 @@ impl Game<'_> { if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) { self.swing_hand(hand, animation); } else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { - m.swing(); + m.swing_hand_modified(hand == Hand::Off, animation.duration); } } // The player's own instances: its maximum health (the hearts the diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 7d596f5ca..0575d8d14 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -53,17 +53,62 @@ pub struct Ai { /// Swing animation progress (attackAnim) and ticks. pub swing_time: i32, pub swinging: bool, + /// The current swing's `SwingDescription.durationTicks`; 0 is the default 6. + pub swing_duration: i32, /// Walked distance and the next footstep. pub steps: super::living::Steps, } impl Mob { + /// `LivingEntity.getCurrentSwingDuration`: 6 ticks unless a swing set its own. + pub fn swing_duration(&self) -> i32 { + if self.ai.swing_duration > 0 { + self.ai.swing_duration + } else { + 6 + } + } + /// `SwingState.startIfAble`: a swing past the first half of the current + /// one restarts it. + fn start_swing(&mut self, duration: i32) -> bool { + let half = self.swing_duration() / 2; + if self.ai.swinging && self.ai.swing_time >= 0 && self.ai.swing_time < half { + return false; + } + self.ai.swing_time = -1; + self.ai.swinging = true; + self.ai.swing_duration = duration.max(1); + true + } /// LivingEntity.swing pub fn swing(&mut self) { - if !self.ai.swinging || self.ai.swing_time >= 3 || self.ai.swing_time < 0 { - self.ai.swing_time = -1; - self.ai.swinging = true; + if self.start_swing(6) { + self.extra.swing_right_arm = None; } } + /// `LivingEntity.swing(hand, animation, false)` for a swing of `duration` + /// ticks (`getModifiedSwingDuration`): the arm is `hand.asArm(mainArm)`. + pub fn swing_hand(&mut self, off_hand: bool, duration: i32) { + if self.start_swing(duration) { + self.extra.swing_right_arm = Some(!off_hand != self.main_arm_left()); + } + } + /// `swing_hand` for a swing of `base` ticks, lengthened or shortened by + /// this mob's haste, conduit power or mining fatigue + /// (`LivingEntity.getModifiedSwingDuration`). + pub fn swing_hand_modified(&mut self, off_hand: bool, base: i32) { + let amplifier = |id: &str| self.extra.effects.iter().find(|e| e.0 == id).map(|e| e.2); + let dig_speed = amplifier("minecraft:haste").max(amplifier("minecraft:conduit_power")); + let fatigue = amplifier("minecraft:mining_fatigue"); + let duration = crate::first_person::swing_duration( + crate::first_person::SwingAnimation { + kind: 1, + duration: base, + }, + dig_speed, + fatigue, + ); + self.swing_hand(off_hand, duration); + } } /// The module implementing a species' behaviour. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -540,18 +585,21 @@ fn effects_from_blocks(m: &mut Mob, w: &MobWorld) { } } } -/// LivingEntity.updateSwingTime (6-tick swing duration). +/// LivingEntity.updateSwingTime. pub fn tick_swing(m: &mut Mob) { + let duration = m.swing_duration(); if m.ai.swinging { m.ai.swing_time += 1; - if m.ai.swing_time >= 6 { + if m.ai.swing_time >= duration { m.ai.swing_time = 0; m.ai.swinging = false; + m.ai.swing_duration = 0; + m.extra.swing_right_arm = None; } } else { m.ai.swing_time = 0; } - m.anim.attack_anim = m.ai.swing_time as f32 / 6.0; + m.anim.attack_anim = m.ai.swing_time as f32 / duration as f32; } /// `Entity.isInRain`: rain at the block position, or at the top of the box. pub(super) fn in_rain(m: &Mob, w: &MobWorld) -> bool {