diff options
Diffstat (limited to 'azalea-entity/src')
| -rw-r--r-- | azalea-entity/src/attributes.rs | 70 |
1 files changed, 16 insertions, 54 deletions
diff --git a/azalea-entity/src/attributes.rs b/azalea-entity/src/attributes.rs index 18bbc348..4e42e9c2 100644 --- a/azalea-entity/src/attributes.rs +++ b/azalea-entity/src/attributes.rs @@ -1,14 +1,11 @@ //! See <https://minecraft.fandom.com/wiki/Attribute>. -use std::{ - collections::HashMap, - io::{Cursor, Write}, -}; +use std::collections::HashMap; -use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; +use azalea_buf::McBuf; +use azalea_core::resource_location::ResourceLocation; use bevy_ecs::component::Component; use thiserror::Error; -use uuid::{uuid, Uuid}; #[derive(Clone, Debug, Component)] pub struct Attributes { @@ -19,7 +16,7 @@ pub struct Attributes { #[derive(Clone, Debug)] pub struct AttributeInstance { pub base: f64, - modifiers_by_uuid: HashMap<Uuid, AttributeModifier>, + modifiers_by_id: HashMap<ResourceLocation, AttributeModifier>, } #[derive(Clone, Debug, Error)] @@ -30,13 +27,13 @@ impl AttributeInstance { pub fn new(base: f64) -> Self { Self { base, - modifiers_by_uuid: HashMap::new(), + modifiers_by_id: HashMap::new(), } } pub fn calculate(&self) -> f64 { let mut total = self.base; - for modifier in self.modifiers_by_uuid.values() { + for modifier in self.modifiers_by_id.values() { match modifier.operation { AttributeModifierOperation::Addition => total += modifier.amount, AttributeModifierOperation::MultiplyBase => total += self.base * modifier.amount, @@ -52,8 +49,8 @@ impl AttributeInstance { /// Add a new modifier to this attribute. pub fn insert(&mut self, modifier: AttributeModifier) -> Result<(), AlreadyPresentError> { if self - .modifiers_by_uuid - .insert(modifier.uuid, modifier) + .modifiers_by_id + .insert(modifier.id.clone(), modifier) .is_some() { Err(AlreadyPresentError) @@ -62,17 +59,16 @@ impl AttributeInstance { } } - /// Remove the modifier with the given UUID from this attribute, returning + /// Remove the modifier with the given ID from this attribute, returning /// the previous modifier is present. - pub fn remove(&mut self, uuid: &Uuid) -> Option<AttributeModifier> { - self.modifiers_by_uuid.remove(uuid) + pub fn remove(&mut self, id: &ResourceLocation) -> Option<AttributeModifier> { + self.modifiers_by_id.remove(id) } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, McBuf)] pub struct AttributeModifier { - pub uuid: Uuid, - pub name: String, + pub id: ResourceLocation, pub amount: f64, pub operation: AttributeModifierOperation, } @@ -86,50 +82,16 @@ pub enum AttributeModifierOperation { pub fn sprinting_modifier() -> AttributeModifier { AttributeModifier { - uuid: uuid!("662A6B8D-DA3E-4C1C-8813-96EA6097278D"), - name: "Sprinting speed boost".to_string(), + id: ResourceLocation::new("sprinting"), amount: 0.30000001192092896, operation: AttributeModifierOperation::MultiplyTotal, } } -pub static BASE_ATTACK_SPEED_UUID: Uuid = uuid!("FA233E1C-4180-4865-B01B-BCCE9785ACA3"); -pub fn weapon_attack_speed_modifier(amount: f64) -> AttributeModifier { - AttributeModifier { - uuid: BASE_ATTACK_SPEED_UUID, - name: "Weapon modifier".to_string(), - amount, - operation: AttributeModifierOperation::Addition, - } -} -pub fn tool_attack_speed_modifier(amount: f64) -> AttributeModifier { +pub fn base_attack_speed_modifier(amount: f64) -> AttributeModifier { AttributeModifier { - uuid: BASE_ATTACK_SPEED_UUID, - name: "Tool modifier".to_string(), + id: ResourceLocation::new("base_attack_speed"), amount, operation: AttributeModifierOperation::Addition, } } - -impl McBufReadable for AttributeModifier { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let uuid = Uuid::read_from(buf)?; - let amount = f64::read_from(buf)?; - let operation = AttributeModifierOperation::read_from(buf)?; - Ok(Self { - uuid, - name: "Unknown synced attribute modifier".to_string(), - amount, - operation, - }) - } -} - -impl McBufWritable for AttributeModifier { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - self.uuid.write_into(buf)?; - self.amount.write_into(buf)?; - self.operation.write_into(buf)?; - Ok(()) - } -} |
