Combat: attack damage and speed come from last tick's main hand (attribute swapping)

The player's ATTACK_DAMAGE and ATTACK_SPEED modifiers change in
LivingEntity.detectEquipmentUpdates, once a tick, while Player.attack reads
the weapon's enchantments and smash bonus from the stack in hand at the
attack. So swapping hotbar slots just before an attack hits with the old
item's damage and charge and the new item's enchantments (a Wind Burst or
Breach mace after another weapon): attribute swapping. Horizon read both from
the item in hand at the attack.

Also Player.tick's reset: switching to a different item now resets the attack
strength the damage uses (only the indicator's ticker reset before), for the
host and for LAN guests.

Fixes #505

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-21 20:34:14 +02:00
parent caeffffb94
commit c01739300a
5 changed files with 48 additions and 18 deletions
+5 -1
View File
@@ -29,9 +29,11 @@ impl AttackStrength {
/// The attack-strength part of `Player.tick`: both tickers advance, and a
/// main-hand change to a different item resets them
/// (`!ItemStack.matches` and `!ItemStack.isSameItem`).
pub fn tick(&mut self, main_hand: Option<HeldStack>) {
/// Whether they were reset.
pub fn tick(&mut self, main_hand: Option<HeldStack>) -> bool {
self.ticker += 1;
self.swap_ticker += 1;
let mut reset = false;
if self.last_item != main_hand {
let same_item = match (&self.last_item, &main_hand) {
(Some(a), Some(b)) => a.item == b.item,
@@ -40,9 +42,11 @@ impl AttackStrength {
};
if !same_item {
self.reset();
reset = true;
}
self.last_item = main_hand;
}
reset
}
/// `resetAttackStrengthTicker`: swinging at nothing, attacking or
/// stopping a block break.
+8 -9
View File
@@ -1209,6 +1209,11 @@ struct Game<'a> {
sculk: sculk::SculkState,
raiding: raids::Raiding,
attack_ticks: u32,
/// The main hand's item at the last player tick, whose attack damage and
/// speed modifiers apply (`LivingEntity.detectEquipmentUpdates`): a hotbar
/// swap just before an attack keeps them, with the new item's enchantments
/// (attribute swapping).
attack_item: Option<Arc<str>>,
last_hurt_by_player: HashSet<u32>,
/// Mobs whose death (`LivingEntity.die`) has been handled.
died: HashSet<u32>,
@@ -6581,11 +6586,7 @@ impl Game<'_> {
/// scaled by the attack strength, a mace's smash, the critical hit, then
/// enchantments.
pub(super) fn melee_damage(&mut self, target_kind: &str) -> (f32, f32, bool) {
let hand_damage = 1.0
+ self
.held()
.and_then(|h| self.res.items().get(&h.item))
.map_or(0.0, |i| i.attack_damage);
let hand_damage = 1.0 + self.attack_item_info().map_or(0.0, |i| i.attack_damage);
let damage = hand_damage + self.effect_attack_bonus();
let scale = self.attack_strength_scale();
let mut amount = damage * (0.2 + scale * scale * 0.8);
@@ -6605,10 +6606,7 @@ impl Game<'_> {
}
/// `Player.getAttackStrengthScale(0.5)`.
fn attack_strength_scale(&self) -> f32 {
let speed = self
.held()
.and_then(|h| self.res.items().get(&h.item))
.map_or(0.0, |i| i.attack_speed);
let speed = self.attack_item_info().map_or(0.0, |i| i.attack_speed);
let delay = 20.0 / (4.0 + speed).max(0.1);
((self.attack_ticks as f32 + 0.5) / delay).clamp(0.0, 1.0)
}
@@ -11131,6 +11129,7 @@ impl<'a> Game<'a> {
sculk: Default::default(),
raiding: Default::default(),
attack_ticks: 100,
attack_item: None,
last_hurt_by_player: HashSet::new(),
died: HashSet::new(),
be_anims: {
+2 -4
View File
@@ -761,10 +761,8 @@ impl Game<'_> {
if !self.before_block(d) || self.mob_hit().is_some_and(|m| m.1 < d) {
return false;
}
let held = self.held().cloned();
let modifiers = held
.as_ref()
.and_then(|h| self.res.items().get(&h.item))
let modifiers = self
.attack_item_info()
.map(|i| (i.attack_damage, i.attack_speed))
.unwrap_or((0.0, 0.0));
let speed = (4.0 + modifiers.1).max(0.1);
+12 -4
View File
@@ -209,10 +209,15 @@ impl Game<'_> {
}
self.swing.tick();
let main = self.inventory[self.selected].clone();
self.hand_strength.tick(main.as_ref().map(|s| HeldStack {
// Player.tick: a different item resets the attack strength, and
// detectEquipmentUpdates gives the player its modifiers.
if self.hand_strength.tick(main.as_ref().map(|s| HeldStack {
item: s.item.to_string(),
rest: format!("{} {} {:?}", s.count, s.damage, s.extra),
}));
})) {
self.attack_ticks = 0;
}
self.attack_item = main.as_ref().map(|s| s.item.clone());
let next = [(self.selected, main), (OFFHAND, self.offhand.clone())];
// shouldInstantlyReplaceVisibleItem: the same stack, or an equal one
// (matchesIgnoringComponents; every item plays the swap animation).
@@ -286,10 +291,13 @@ impl Game<'_> {
pub(super) fn main_hand_attack_speed(&self) -> f64 {
horizon_client::attack_strength::BASE_ATTACK_SPEED
+ self
.held()
.and_then(|h| self.res.items().get(&h.item))
.attack_item_info()
.map_or(0.0, |i| f64::from(i.attack_speed))
}
/// The item whose attack damage and speed modifiers the player has (`attack_item`).
pub(super) fn attack_item_info(&self) -> Option<&horizon_client::items::ItemInfo> {
self.res.items().get(self.attack_item.as_deref()?)
}
/// `evaluateWhichHandsToRender`: which of the main and off hand show.
pub(super) fn hands_to_render(&self) -> [bool; 2] {
let held = |slot| {
@@ -35,6 +35,8 @@ pub(super) struct GuestPlay {
using: Option<(usize, u32)>,
use_delay: u32,
attack_ticks: u32,
/// The main hand's item at the guest's last tick (`Game::attack_item`).
attack_item: Option<std::sync::Arc<str>>,
swing: horizon_client::first_person::SwingState,
/// The block being dug and the face the client aimed at.
digging: Option<([i32; 3], [i32; 3])>,
@@ -102,6 +104,7 @@ pub(super) struct GuestSwap {
using: Option<(usize, u32)>,
use_delay: u32,
attack_ticks: u32,
attack_item: Option<std::sync::Arc<str>>,
swing: horizon_client::first_person::SwingState,
left: bool,
right: bool,
@@ -262,6 +265,7 @@ impl Game<'_> {
let using = std::mem::replace(&mut self.using, play.using.take());
let use_delay = std::mem::replace(&mut self.use_delay, play.use_delay);
let attack_ticks = std::mem::replace(&mut self.attack_ticks, play.attack_ticks);
let attack_item = std::mem::replace(&mut self.attack_item, play.attack_item.take());
let swing = std::mem::replace(&mut self.swing, play.swing);
let (left, right) = (self.left, self.right);
self.left = play.digging.is_some();
@@ -327,6 +331,7 @@ impl Game<'_> {
using,
use_delay,
attack_ticks,
attack_item,
swing,
left,
right,
@@ -377,6 +382,7 @@ impl Game<'_> {
using,
use_delay,
attack_ticks,
attack_item,
swing,
left,
right,
@@ -433,6 +439,7 @@ impl Game<'_> {
play.using = std::mem::replace(&mut self.using, using);
play.use_delay = std::mem::replace(&mut self.use_delay, use_delay);
play.attack_ticks = std::mem::replace(&mut self.attack_ticks, attack_ticks);
play.attack_item = std::mem::replace(&mut self.attack_item, attack_item);
play.swing = std::mem::replace(&mut self.swing, swing);
self.container = container;
self.screen = screen;
@@ -1408,6 +1415,20 @@ impl Game<'_> {
}
});
}
// Player.tick: a different main-hand item resets the attack
// strength, and detectEquipmentUpdates gives the guest its modifiers.
let g = &host.hub.guests[gi];
let held = g
.inventory
.get(g.selected)
.cloned()
.flatten()
.map(|s| s.item);
let play = host.play.entry(conn).or_default();
if play.attack_item != held {
play.attack_ticks = 0;
play.attack_item = held;
}
self.guest_delayed_destroy(host, conn);
self.guest_server_tick(host, conn);
// Player.tick's statistics and ServerPlayer.tick's triggers, then