Merge pull request #2157 from kacpherek/fix/armor-wear-environment-damage
🤖 Agent: Gate check by Ludovic's AI agent (Claude). Checked against 26.3: damage that doesn't bypass armor (cactus, fire, lava, campfire, berry bushes) wears the armor through `hurtArmor`. Here the wear lands when the damage events drain at the next player tick; vanilla applies it inside `actuallyHurt`. That is close enough for now. #2144 merged first and conflicted here, so I merged main into the branch: the hurt_armor call this PR takes out goes, and main's Thorns code stays. Gate: this wave's 55 PRs merged on main in one tree: clippy `-D warnings` clean, 1186 horizon-client and 163 horizon-world tests pass, and of the 101 harnesses `parity.py --changed-files` picked, 100 pass (`block_removal` failed on one spawner case from #2199's orb merging, fixed there). `lan_e2e.py` passes 24 checks with the host on its server thread and in-frame (chest, xp, pvp, doors, villagers, beds, frames, projectiles, arrows, fishing, damage, mobs, blast, drops, elytra, breath, predict, cracks, barter, kills, hurt, contacts, underwater, plates), plus forced-levels on the server thread. Thanks @kacpherek!
This commit is contained in:
@@ -164,6 +164,8 @@ fn snapshot(out: &mut impl Write, player: &PlayerMotion) -> Result<()> {
|
||||
.iter()
|
||||
.filter_map(|e| match e {
|
||||
MotionEvent::Fall(e) => Some(e.trace()),
|
||||
// The oracle's player wears no armor; the game applies this one.
|
||||
MotionEvent::Health(horizon_client::health::HealthEvent::ArmorHurt { .. }) => None,
|
||||
MotionEvent::Health(e) => Some(e.trace()),
|
||||
MotionEvent::Snowflake { position, velocity } => Some(format!(
|
||||
"snowflake {:016x},{:016x},{:016x} {:016x},{:016x},{:016x}",
|
||||
|
||||
@@ -4511,6 +4511,7 @@ impl Game<'_> {
|
||||
self.tick_avatar(before, riding);
|
||||
self.end_spin_attack(spin_hit);
|
||||
self.record_combat(&events);
|
||||
self.wear_armor(&events);
|
||||
for event in events {
|
||||
match event {
|
||||
MotionEvent::HurtSound(sound) => self.player_hurt_sound(sound, feet),
|
||||
@@ -8603,15 +8604,54 @@ impl Game<'_> {
|
||||
}
|
||||
self.apply_enchantment_attributes();
|
||||
}
|
||||
/// Inventory.hurtArmor: each piece loses max(1, damage / 4) durability.
|
||||
fn hurt_armor(&mut self, damage: f32) {
|
||||
/// The armor hits among a tick's motion events (`Player.hurtArmor` from
|
||||
/// `getDamageAfterArmorAbsorb`), whatever the damage: a mob, an explosion,
|
||||
/// a cactus, lava, a campfire.
|
||||
fn wear_armor(&mut self, events: &[MotionEvent]) {
|
||||
for event in events {
|
||||
if let MotionEvent::Health(horizon_client::health::HealthEvent::ArmorHurt {
|
||||
source,
|
||||
amount,
|
||||
}) = event
|
||||
{
|
||||
self.hurt_armor(*source, *amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `Player.hurtArmor` -> `LivingEntity.doHurtEquipment`: each worn piece that
|
||||
/// wears when its wearer is hurt loses max(1, damage / 4) durability, except
|
||||
/// to a fire source when the piece resists fire (`ItemStack.canBeHurtBy`).
|
||||
fn hurt_armor(&mut self, source: horizon_world::damage_source::DamageTypeId, damage: f32) {
|
||||
if damage <= 0.0 {
|
||||
return;
|
||||
}
|
||||
let amount = ((damage / 4.0) as u32).max(1);
|
||||
let name = self
|
||||
.motion
|
||||
.health
|
||||
.damage
|
||||
.definition(source)
|
||||
.map(|d| d.name.clone())
|
||||
.unwrap_or_default();
|
||||
let fire = self
|
||||
.motion
|
||||
.health
|
||||
.damage
|
||||
.in_tag(source, "minecraft:is_fire");
|
||||
let mut broke = false;
|
||||
let losses: Vec<u32> = (0..4)
|
||||
.map(|i| {
|
||||
self.armor[i]
|
||||
.clone()
|
||||
.map_or(amount, |s| self.durability_loss(&s, amount))
|
||||
.map(|i| match self.armor[i].clone() {
|
||||
Some(s)
|
||||
if self
|
||||
.res
|
||||
.items()
|
||||
.get(&s.item)
|
||||
.is_some_and(|info| info.equip_damage_on_hurt)
|
||||
&& !(fire && self.res.items().resists_fire(&s, &name)) =>
|
||||
{
|
||||
self.durability_loss(&s, amount)
|
||||
}
|
||||
_ => 0,
|
||||
})
|
||||
.collect();
|
||||
for (slot, amount) in self.armor.iter_mut().zip(losses) {
|
||||
|
||||
@@ -1615,7 +1615,6 @@ impl Game<'_> {
|
||||
r.knockback = Some([p.velocity.x, p.velocity.y, p.velocity.z]);
|
||||
}
|
||||
}
|
||||
self.hurt_armor(amount);
|
||||
// The thorns hurt the entity that caused the hit, if there is one.
|
||||
if let Some(a) = attacker.or(owner) {
|
||||
self.thorns(a);
|
||||
|
||||
@@ -316,9 +316,6 @@ impl Game<'_> {
|
||||
);
|
||||
}
|
||||
}
|
||||
if outcome.hurt && !creative {
|
||||
self.hurt_armor(amount);
|
||||
}
|
||||
if raised {
|
||||
self.after_block(outcome, amount, None, false);
|
||||
}
|
||||
|
||||
@@ -108,16 +108,10 @@ impl Game<'_> {
|
||||
} else {
|
||||
amount
|
||||
};
|
||||
let hurt = if let Some(p) = self.player.as_mut()
|
||||
if let Some(p) = self.player.as_mut()
|
||||
&& let Some(id) = self.motion.health.damage.id(kind)
|
||||
{
|
||||
p.hurt(&self.motion.health, self.world.health_rules, id, amount)
|
||||
.unwrap_or(false)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if hurt {
|
||||
self.hurt_armor(amount);
|
||||
let _ = p.hurt(&self.motion.health, self.world.health_rules, id, amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -372,17 +372,11 @@ impl Game<'_> {
|
||||
if self.creative || self.spectator() {
|
||||
return;
|
||||
}
|
||||
let hurt = if let Some(p) = self.player.as_mut()
|
||||
if let Some(p) = self.player.as_mut()
|
||||
&& !p.health.dead_or_dying()
|
||||
&& let Some(id) = self.motion.health.damage.id("minecraft:fireworks")
|
||||
{
|
||||
p.hurt(&self.motion.health, self.world.health_rules, id, amount)
|
||||
.unwrap_or(false)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if hurt {
|
||||
self.hurt_armor(amount);
|
||||
let _ = p.hurt(&self.motion.health, self.world.health_rules, id, amount);
|
||||
}
|
||||
}
|
||||
/// `FireworkEntityRenderer.submit`: the item (GROUND) facing the camera,
|
||||
|
||||
@@ -2301,6 +2301,7 @@ impl Game<'_> {
|
||||
let marked = std::mem::take(&mut player.health.sync_velocity);
|
||||
let velocity = marked.then_some(player.velocity);
|
||||
game.record_combat(&events);
|
||||
game.wear_armor(&events);
|
||||
(events, velocity)
|
||||
});
|
||||
let Some((events, marked)) = events else {
|
||||
|
||||
@@ -111,6 +111,12 @@ pub enum HealthEvent {
|
||||
amount: f32,
|
||||
},
|
||||
Phase(&'static str),
|
||||
/// `Player.hurtArmor`: a hit that doesn't bypass armor wears the worn
|
||||
/// pieces (`LivingEntity.getDamageAfterArmorAbsorb`); the game applies it.
|
||||
ArmorHurt {
|
||||
source: DamageTypeId,
|
||||
amount: f32,
|
||||
},
|
||||
/// `ServerLevel.broadcastDamageEvent`: the client's
|
||||
/// `LivingEntity.handleDamageEvent` plays the source's hurt sound.
|
||||
DamageBroadcast {
|
||||
@@ -139,6 +145,7 @@ impl HealthEvent {
|
||||
format!("record-damage {:08x}", amount.to_bits())
|
||||
}
|
||||
Self::Phase(name) => name.into(),
|
||||
Self::ArmorHurt { amount, .. } => format!("hurt-armor {:08x}", amount.to_bits()),
|
||||
Self::DamageBroadcast { .. } => "broadcast-damage".into(),
|
||||
Self::Criterion { .. } => "hurt-criterion".into(),
|
||||
}
|
||||
@@ -534,9 +541,14 @@ impl ServerDamageEnvironment<Attributes> for Environment<'_> {
|
||||
fn bypasses(&mut self, kind: Bypass) -> io::Result<bool> {
|
||||
Ok(self.source.bypasses(kind))
|
||||
}
|
||||
fn hurt_armor(&mut self, _: &mut Attributes, _: f32) -> io::Result<()> {
|
||||
fn hurt_armor(&mut self, _: &mut Attributes, damage: f32) -> io::Result<()> {
|
||||
// The worn armor lives in the game, which wears it from this event.
|
||||
self.event(HealthEvent::ArmorHurt {
|
||||
source: self.source.type_id(),
|
||||
amount: damage,
|
||||
});
|
||||
Ok(())
|
||||
} // empty equipment
|
||||
}
|
||||
fn has_resistance(&mut self) -> io::Result<bool> {
|
||||
Ok(self.status.resistance.is_some())
|
||||
}
|
||||
|
||||
@@ -113,6 +113,9 @@ pub struct ItemInfo {
|
||||
pub swappable: bool,
|
||||
/// `equippable.dispensable` (dispensers may put it on an entity).
|
||||
pub equip_dispensable: bool,
|
||||
/// `equippable.damage_on_hurt`: the item wears when its wearer is hurt
|
||||
/// (false for the elytra).
|
||||
pub equip_damage_on_hurt: bool,
|
||||
/// `equippable.allowed_entities` resolved to entity type ids; None allows all.
|
||||
pub equip_allowed: Option<HashSet<String>>,
|
||||
pub equip_sound: Option<String>,
|
||||
@@ -626,6 +629,9 @@ impl Items {
|
||||
equip_dispensable: c["minecraft:equippable"]["dispensable"]
|
||||
.as_bool()
|
||||
.unwrap_or(true),
|
||||
equip_damage_on_hurt: c["minecraft:equippable"]["damage_on_hurt"]
|
||||
.as_bool()
|
||||
.unwrap_or(true),
|
||||
equip_allowed: match &c["minecraft:equippable"]["allowed_entities"] {
|
||||
Value::String(spec) => Some(entity_spec(&entity_tags, spec)),
|
||||
Value::Array(list) => Some(
|
||||
|
||||
Reference in New Issue
Block a user