From 86fd3168f75b69739e874cca8a06cdf9888f1ba7 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 22 Jul 2024 01:00:38 +0000 Subject: [PATCH] fix panic in update_modifiers_for_held_item found from https://github.com/azalea-rs/azalea/commit/27cecdb8bf4d0239f6f54e63d3158f129ec0e270 --- azalea-client/src/interact.rs | 3 +-- azalea-client/src/movement.rs | 2 +- azalea-entity/src/attributes.rs | 27 ++++++++++++++++----------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/azalea-client/src/interact.rs b/azalea-client/src/interact.rs index 97e30471..7b14be09 100644 --- a/azalea-client/src/interact.rs +++ b/azalea-client/src/interact.rs @@ -367,7 +367,6 @@ fn update_modifiers_for_held_item( .attack_speed .insert(azalea_entity::attributes::base_attack_speed_modifier( added_attack_speed, - )) - .unwrap(); + )); } } diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index a3964a12..8905d0ef 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -458,7 +458,7 @@ fn set_sprinting( if sprinting { attributes .speed - .insert(azalea_entity::attributes::sprinting_modifier()) + .try_insert(azalea_entity::attributes::sprinting_modifier()) .is_ok() } else { attributes diff --git a/azalea-entity/src/attributes.rs b/azalea-entity/src/attributes.rs index 4e42e9c2..ddafaec3 100644 --- a/azalea-entity/src/attributes.rs +++ b/azalea-entity/src/attributes.rs @@ -1,6 +1,6 @@ //! See . -use std::collections::HashMap; +use std::collections::{hash_map, HashMap}; use azalea_buf::McBuf; use azalea_core::resource_location::ResourceLocation; @@ -46,16 +46,21 @@ impl AttributeInstance { total } - /// Add a new modifier to this attribute. - pub fn insert(&mut self, modifier: AttributeModifier) -> Result<(), AlreadyPresentError> { - if self - .modifiers_by_id - .insert(modifier.id.clone(), modifier) - .is_some() - { - Err(AlreadyPresentError) - } else { - Ok(()) + /// Add a new modifier to this attribute and return the previous value, if + /// present. + pub fn insert(&mut self, modifier: AttributeModifier) -> Option { + self.modifiers_by_id.insert(modifier.id.clone(), modifier) + } + + /// Insert the given modifier if it's not already present, otherwise returns + /// [`AlreadyPresentError`]. + pub fn try_insert(&mut self, modifier: AttributeModifier) -> Result<(), AlreadyPresentError> { + match self.modifiers_by_id.entry(modifier.id.clone()) { + hash_map::Entry::Occupied(_) => Err(AlreadyPresentError), + hash_map::Entry::Vacant(entry) => { + entry.insert(modifier); + Ok(()) + } } }