Swing the right arm and for the right time on other players

This commit is contained in:
Kacpherek
2026-10-27 06:26:53 +01:00
parent d74d39cb05
commit 2a1132387f
4 changed files with 66 additions and 8 deletions
+2
View File
@@ -1492,6 +1492,8 @@ impl Game<'_> {
.map(|s| s.as_ref().map(|s| s.item.clone()));
m.extra.crouching = g.sneaking;
m.extra.sprinting = g.sprinting;
// 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()
@@ -2872,7 +2872,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();
+8 -1
View File
@@ -1946,6 +1946,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
@@ -2187,7 +2194,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
+54 -6
View File
@@ -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)]
@@ -435,18 +480,21 @@ pub fn tick(
}
true
}
/// 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;
}
/// `LivingEntity.baseTick` refills a breather's air by 4, then
/// `Axolotl.handleAirSupply` (unless the mob has no AI): water or rain fills