From 4f991190d33e8135efe02e20b256285cf64309f8 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 16 Apr 2023 21:02:34 -0400 Subject: [PATCH] Removed 3rd to_owned call --- common/src/comp/inventory/loadout.rs | 16 +++++++++++++ common/src/comp/inventory/mod.rs | 34 +++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/common/src/comp/inventory/loadout.rs b/common/src/comp/inventory/loadout.rs index a3770b9730..df7a1d0c7b 100644 --- a/common/src/comp/inventory/loadout.rs +++ b/common/src/comp/inventory/loadout.rs @@ -263,6 +263,22 @@ impl Loadout { .and_then(|item| item.slot(loadout_slot_id.slot_idx)) } + pub(super) fn inv_slot_with_mutable_recently_unequipped_items( + &mut self, + loadout_slot_id: LoadoutSlotId, + ) -> ( + Option<&InvSlot>, + &mut HashMap, + ) { + ( + self.slots + .get(loadout_slot_id.loadout_idx) + .and_then(|loadout_slot| loadout_slot.slot.as_ref()) + .and_then(|item| item.slot(loadout_slot_id.slot_idx)), + &mut self.recently_unequipped_items, + ) + } + /// Returns the `InvSlot` for a given `LoadoutSlotId` pub(super) fn inv_slot_mut(&mut self, loadout_slot_id: LoadoutSlotId) -> Option<&mut InvSlot> { self.slots diff --git a/common/src/comp/inventory/mod.rs b/common/src/comp/inventory/mod.rs index 730e42fba9..1bbc2a494b 100644 --- a/common/src/comp/inventory/mod.rs +++ b/common/src/comp/inventory/mod.rs @@ -1,4 +1,5 @@ use core::ops::Not; +use hashbrown::HashMap; use serde::{Deserialize, Serialize}; use specs::{Component, DerefFlaggedStorage}; use std::{cmp::Ordering, convert::TryFrom, mem, ops::Range}; @@ -9,7 +10,10 @@ use crate::{ comp::{ body::Body, inventory::{ - item::{tool::AbilityMap, ItemDef, ItemKind, MaterialStatManifest, TagExampleInfo}, + item::{ + tool::AbilityMap, ItemDef, ItemDefinitionIdOwned, ItemKind, MaterialStatManifest, + TagExampleInfo, + }, loadout::Loadout, slot::{EquipSlot, Slot, SlotError}, }, @@ -575,6 +579,24 @@ impl Inventory { } } + fn slot_with_mutable_recently_unequipped_items( + &mut self, + inv_slot_id: InvSlotId, + ) -> ( + Option<&InvSlot>, + &mut HashMap, + ) { + match SlotId::from(inv_slot_id) { + SlotId::Inventory(slot_idx) => ( + self.slots.get(slot_idx), + &mut self.loadout.recently_unequipped_items, + ), + SlotId::Loadout(loadout_slot_id) => self + .loadout + .inv_slot_with_mutable_recently_unequipped_items(loadout_slot_id), + } + } + pub fn slot_mut(&mut self, inv_slot_id: InvSlotId) -> Option<&mut InvSlot> { match SlotId::from(inv_slot_id) { SlotId::Inventory(slot_idx) => self.slots.get_mut(slot_idx), @@ -916,11 +938,11 @@ impl Inventory { .map(|(slot, _item)| slot) .collect::>(); slots.into_iter().for_each(|slot| { - let slot = if let Some(Some(item)) = self.slot(slot) { - if let Some((_unequip_time, count)) = self - .loadout - .recently_unequipped_items - .get_mut(&item.item_definition_id().to_owned()) + let slot = if let (Some(Some(item)), recently_unequipped_items) = + self.slot_with_mutable_recently_unequipped_items(slot) + { + if let Some((_unequip_time, count)) = + recently_unequipped_items.get_mut(&item.item_definition_id()) { if *count > 0 { *count -= 1;