Wear armor when cactus, fire, lava, campfires and other environment damage hurt the player
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}",
|
||||
|
||||
@@ -4343,6 +4343,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),
|
||||
@@ -8317,15 +8318,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) {
|
||||
|
||||
@@ -1535,7 +1535,6 @@ impl Game<'_> {
|
||||
r.knockback = Some([p.velocity.x, p.velocity.y, p.velocity.z]);
|
||||
}
|
||||
}
|
||||
self.hurt_armor(amount);
|
||||
self.thorns(from);
|
||||
}
|
||||
if raised {
|
||||
|
||||
@@ -314,9 +314,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -352,17 +352,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,
|
||||
|
||||
@@ -2233,6 +2233,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(),
|
||||
}
|
||||
@@ -517,9 +524,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())
|
||||
}
|
||||
|
||||
@@ -111,6 +111,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>,
|
||||
@@ -584,6 +587,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