//! Compose ordinary server-player inner damage with registered attributes, //! synchronized health/absorption and stored hunger. Outer hurt gates stay with //! the caller; equipment, effect, statistic and combat services stay explicit. use crate::{ attribute_health::HealthAttributes, attributes::entity::{EntityAttributes, LookupError}, combat_rules::ArmorWorld, compression::invalid, damage_reduction::{self, Bypass, DamageReductionWorld}, food::FoodData, living_damage::{self, DamageStat, DamageWorld}, player_health::{PlayerAttributeHealth, PlayerFloatIds, PlayerHealthError}, synced_floats::{DataError, Observer, SyncedFloats}, }; use std::{fmt, io}; type EnvError = ::Error; #[derive(Debug)] pub enum ServerDamageError { Attribute(A), Data(DataError), Environment(E), } impl fmt::Display for ServerDamageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Attribute(e) => write!(f, "damage attribute lookup: {e:?}"), Self::Data(e) => e.fmt(f), Self::Environment(e) => e.fmt(f), } } } impl std::error::Error for ServerDamageError {} fn health_error(e: PlayerHealthError) -> ServerDamageError { match e { PlayerHealthError::Attribute(e) => ServerDamageError::Attribute(e), PlayerHealthError::Data(e) => ServerDamageError::Data(e), } } pub trait ServerDamageEnvironment: ArmorWorld + Observer::Error> { type Attacker: Copy; fn invulnerable(&mut self) -> Result>; fn bypasses(&mut self, kind: Bypass) -> Result>; /// Equipment breakage may update the live attribute map before armor and /// toughness are queried by the rest of this hit. fn hurt_armor(&mut self, attributes: &mut A, damage: f32) -> Result<(), EnvError>; fn has_resistance(&mut self) -> Result>; fn resistance_amplifier(&mut self) -> Result>; fn award_resisted(&mut self, amount: i32) -> Result<(), EnvError>; fn attacker(&mut self) -> Result, EnvError>; fn award_dealt_resisted(&mut self, amount: i32) -> Result<(), EnvError>; fn damage_protection(&mut self, level: Self::ServerLevel) -> Result>; fn award_self(&mut self, stat: DamageStat, amount: i32) -> Result<(), EnvError>; fn award_attacker( &mut self, attacker: Self::Attacker, stat: DamageStat, amount: i32, ) -> Result<(), EnvError>; fn source_food_exhaustion(&mut self) -> Result>; fn ability_invulnerable(&self) -> bool; fn level_is_client(&mut self) -> Result>; fn record_damage(&mut self, amount: f32) -> Result<(), EnvError>; fn damage_event(&mut self) -> Result<(), EnvError>; } pub struct ServerPlayerDamage<'a, E, A: HealthAttributes = EntityAttributes> { attributes: &'a mut A, data: &'a mut SyncedFloats, food: &'a mut FoodData, ids: PlayerFloatIds, armor: A::Key, toughness: A::Key, environment: &'a mut E, } impl<'a, A: HealthAttributes, E: ServerDamageEnvironment> ServerPlayerDamage<'a, E, A> { pub fn new( attributes: &'a mut A, data: &'a mut SyncedFloats, food: &'a mut FoodData, ids: PlayerFloatIds, environment: &'a mut E, ) -> io::Result { let armor = attributes .health_attribute_key("minecraft:armor") .ok_or_else(|| invalid("missing ARMOR definition"))?; let toughness = attributes .health_attribute_key("minecraft:armor_toughness") .ok_or_else(|| invalid("missing ARMOR_TOUGHNESS definition"))?; PlayerAttributeHealth::new(attributes, data, ids, false, environment)?; Ok(Self { attributes, data, food, ids, armor, toughness, environment, }) } fn health_binding(&mut self) -> PlayerAttributeHealth<'_, E, A> { PlayerAttributeHealth::new( self.attributes, self.data, self.ids, false, self.environment, ) .expect("validated immutable player float schema") } pub fn apply(&mut self, damage: f32) -> Result<(), ServerDamageError, A::Error>> { living_damage::actually_hurt(self, damage, true) } pub(crate) fn apply_after_immunity_check( &mut self, damage: f32, ) -> Result<(), ServerDamageError, A::Error>> { living_damage::actually_hurt_after_immunity_check(self, damage, true) } } impl> ArmorWorld for ServerPlayerDamage<'_, E, A> { type Error = ServerDamageError, A::Error>; type Weapon = E::Weapon; type ServerLevel = E::ServerLevel; fn weapon(&mut self) -> Result, Self::Error> { self.environment .weapon() .map_err(ServerDamageError::Environment) } fn server_level(&mut self) -> Result, Self::Error> { self.environment .server_level() .map_err(ServerDamageError::Environment) } fn modify_armor_effectiveness( &mut self, l: Self::ServerLevel, w: Self::Weapon, v: f32, ) -> Result { self.environment .modify_armor_effectiveness(l, w, v) .map_err(ServerDamageError::Environment) } } impl> DamageReductionWorld for ServerPlayerDamage<'_, E, A> { fn bypasses(&mut self, b: Bypass) -> Result { self.environment .bypasses(b) .map_err(ServerDamageError::Environment) } fn hurt_armor(&mut self, v: f32) -> Result<(), Self::Error> { self.environment .hurt_armor(self.attributes, v) .map_err(ServerDamageError::Environment) } fn armor_value(&mut self) -> Result { self.attributes .health_attribute_value(self.armor.clone()) .map(damage_reduction::armor_from_attribute) .map_err(ServerDamageError::Attribute) } fn toughness_attribute(&mut self) -> Result { self.attributes .health_attribute_value(self.toughness.clone()) .map_err(ServerDamageError::Attribute) } fn has_resistance(&mut self) -> Result { self.environment .has_resistance() .map_err(ServerDamageError::Environment) } fn resistance_amplifier(&mut self) -> Result { self.environment .resistance_amplifier() .map_err(ServerDamageError::Environment) } fn victim_is_server_player(&self) -> bool { true } fn award_resisted(&mut self, v: i32) -> Result<(), Self::Error> { self.environment .award_resisted(v) .map_err(ServerDamageError::Environment) } fn attacker_is_server_player(&mut self) -> Result { self.environment .attacker() .map(|v| v.is_some()) .map_err(ServerDamageError::Environment) } fn award_dealt_resisted(&mut self, v: i32) -> Result<(), Self::Error> { self.environment .award_dealt_resisted(v) .map_err(ServerDamageError::Environment) } fn damage_protection(&mut self, l: Self::ServerLevel) -> Result { self.environment .damage_protection(l) .map_err(ServerDamageError::Environment) } } impl> DamageWorld for ServerPlayerDamage<'_, E, A> { type Error = ServerDamageError, A::Error>; type Attacker = E::Attacker; fn invulnerable(&mut self) -> Result { self.environment .invulnerable() .map_err(ServerDamageError::Environment) } fn after_armor(&mut self, v: f32) -> Result { damage_reduction::after_armor(self, v) } fn after_magic(&mut self, v: f32) -> Result { damage_reduction::after_magic(self, v) } fn absorption(&mut self) -> Result { self.health_binding().absorption().map_err(health_error) } fn set_absorption(&mut self, v: f32) -> Result<(), Self::Error> { self.health_binding() .set_absorption(v) .map_err(health_error) } fn server_player_attacker(&mut self) -> Result, Self::Error> { self.environment .attacker() .map_err(ServerDamageError::Environment) } fn award_attacker( &mut self, a: Self::Attacker, s: DamageStat, v: i32, ) -> Result<(), Self::Error> { self.environment .award_attacker(a, s, v) .map_err(ServerDamageError::Environment) } fn award_self(&mut self, s: DamageStat, v: i32) -> Result<(), Self::Error> { self.environment .award_self(s, v) .map_err(ServerDamageError::Environment) } fn source_food_exhaustion(&mut self) -> Result { self.environment .source_food_exhaustion() .map_err(ServerDamageError::Environment) } fn cause_food_exhaustion(&mut self, v: f32) -> Result<(), Self::Error> { if !self.environment.ability_invulnerable() && !self .environment .level_is_client() .map_err(ServerDamageError::Environment)? { self.food.add_exhaustion(v); } Ok(()) } fn record_damage(&mut self, v: f32) -> Result<(), Self::Error> { self.environment .record_damage(v) .map_err(ServerDamageError::Environment) } fn health(&mut self) -> Result { self.data .get(self.ids.health) .map_err(|id| ServerDamageError::Data(DataError::UnknownId(id))) } fn set_health(&mut self, v: f32) -> Result<(), Self::Error> { self.health_binding().set_health(v).map_err(health_error) } fn damage_event(&mut self) -> Result<(), Self::Error> { self.environment .damage_event() .map_err(ServerDamageError::Environment) } }