aboutsummaryrefslogtreecommitdiff
path: root/azalea-entity/src
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2024-07-22 01:00:38 +0000
committermat <git@matdoes.dev>2024-07-22 01:00:56 +0000
commit86fd3168f75b69739e874cca8a06cdf9888f1ba7 (patch)
treef2c6fc1f7c6dbf8e81bc1ccef3881df3917f0d4a /azalea-entity/src
parent7e93c2d766f15e6559bfe10913f0964e43a79092 (diff)
downloadazalea-drasl-86fd3168f75b69739e874cca8a06cdf9888f1ba7.tar.xz
fix panic in update_modifiers_for_held_item
found from https://github.com/azalea-rs/azalea/commit/27cecdb8bf4d0239f6f54e63d3158f129ec0e270
Diffstat (limited to 'azalea-entity/src')
-rw-r--r--azalea-entity/src/attributes.rs27
1 files changed, 16 insertions, 11 deletions
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 <https://minecraft.fandom.com/wiki/Attribute>.
-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<AttributeModifier> {
+ 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(())
+ }
}
}