Skip to content

Commit

Permalink
fix panic in update_modifiers_for_held_item
Browse files Browse the repository at this point in the history
found from 27cecdb
  • Loading branch information
mat-1 committed Jul 22, 2024
1 parent 7e93c2d commit 86fd316
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
3 changes: 1 addition & 2 deletions azalea-client/src/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ fn update_modifiers_for_held_item(
.attack_speed
.insert(azalea_entity::attributes::base_attack_speed_modifier(
added_attack_speed,
))
.unwrap();
));
}
}
2 changes: 1 addition & 1 deletion azalea-client/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 deletions azalea-entity/src/attributes.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(())
}
}
}

Expand Down

0 comments on commit 86fd316

Please sign in to comment.