Addressed more comments in MR review.

This commit is contained in:
Sam 2021-11-20 22:19:20 -05:00
parent d7f5b907ff
commit 259db56ca6
5 changed files with 21 additions and 20 deletions

View File

@ -806,7 +806,7 @@ impl Item {
} }
} }
pub fn add_component( pub fn persistence_access_add_component(
&mut self, &mut self,
component: Item, component: Item,
ability_map: &AbilityMap, ability_map: &AbilityMap,
@ -820,7 +820,7 @@ impl Item {
self.update_item_config(ability_map, msm); self.update_item_config(ability_map, msm);
} }
pub fn component_mut(&mut self, index: usize) -> Option<&mut Self> { pub fn persistence_access_mutable_component(&mut self, index: usize) -> Option<&mut Self> {
self.components.get_mut(index) self.components.get_mut(index)
} }
@ -998,11 +998,10 @@ pub trait ItemDesc {
fn num_slots(&self) -> u16; fn num_slots(&self) -> u16;
fn item_definition_id(&self) -> &str; fn item_definition_id(&self) -> &str;
fn tags(&self) -> &[ItemTag]; fn tags(&self) -> &[ItemTag];
fn concrete_item(&self) -> Option<&Item>;
fn is_modular(&self) -> bool; fn is_modular(&self) -> bool;
fn components(&self) -> &[Item] { self.concrete_item().map_or(&[], |i| i.components()) } fn components(&self) -> &[Item];
fn tool_info(&self) -> Option<ToolKind> { fn tool_info(&self) -> Option<ToolKind> {
if let ItemKind::Tool(tool) = &*self.kind() { if let ItemKind::Tool(tool) = &*self.kind() {
@ -1028,9 +1027,9 @@ impl ItemDesc for Item {
fn tags(&self) -> &[ItemTag] { self.tags() } fn tags(&self) -> &[ItemTag] { self.tags() }
fn concrete_item(&self) -> Option<&Item> { Some(self) }
fn is_modular(&self) -> bool { self.is_modular() } fn is_modular(&self) -> bool { self.is_modular() }
fn components(&self) -> &[Item] { self.components() }
} }
impl ItemDesc for ItemDef { impl ItemDesc for ItemDef {
@ -1048,9 +1047,9 @@ impl ItemDesc for ItemDef {
fn tags(&self) -> &[ItemTag] { &self.tags } fn tags(&self) -> &[ItemTag] { &self.tags }
fn concrete_item(&self) -> Option<&Item> { None }
fn is_modular(&self) -> bool { false } fn is_modular(&self) -> bool { false }
fn components(&self) -> &[Item] { &[] }
} }
impl Component for Item { impl Component for Item {
@ -1081,8 +1080,6 @@ impl<'a, T: ItemDesc + ?Sized> ItemDesc for &'a T {
fn tags(&self) -> &[ItemTag] { (*self).tags() } fn tags(&self) -> &[ItemTag] { (*self).tags() }
fn concrete_item(&self) -> Option<&Item> { None }
fn is_modular(&self) -> bool { (*self).is_modular() } fn is_modular(&self) -> bool { (*self).is_modular() }
} }

View File

@ -39,7 +39,6 @@ pub enum ToolKind {
} }
impl ToolKind { impl ToolKind {
// Changing this will break persistence of modular weapons
pub fn identifier_name(&self) -> &'static str { pub fn identifier_name(&self) -> &'static str {
match self { match self {
ToolKind::Sword => "sword", ToolKind::Sword => "sword",

View File

@ -125,15 +125,12 @@ impl Recipe {
} }
let (item_def, quantity) = &self.output; let (item_def, quantity) = &self.output;
let mut crafted_item = Item::new_from_item_base( let crafted_item = Item::new_from_item_base(
ItemBase::Raw(Arc::clone(item_def)), ItemBase::Raw(Arc::clone(item_def)),
&[], &components,
ability_map, ability_map,
msm, msm,
); );
for component in components {
crafted_item.add_component(component, ability_map, msm);
}
let mut crafted_items = Vec::with_capacity(*quantity as usize); let mut crafted_items = Vec::with_capacity(*quantity as usize);
for _ in 0..*quantity { for _ in 0..*quantity {
crafted_items.push(crafted_item.duplicate(ability_map, msm)); crafted_items.push(crafted_item.duplicate(ability_map, msm));

View File

@ -296,7 +296,7 @@ where
// Returns mutable reference to parent item of original item, by grabbing // Returns mutable reference to parent item of original item, by grabbing
// the component representing the parent item from the parent item's parent // the component representing the parent item from the parent item's parent
// item // item
component_index.and_then(move |i| parent.component_mut(i)) component_index.and_then(move |i| parent.persistence_access_mutable_component(i))
} else { } else {
None None
} }
@ -400,7 +400,11 @@ pub fn convert_inventory_from_database_items(
&mut inventory, &mut inventory,
&|inv, s| inv.slot_mut(slot(s).ok()?).and_then(|a| a.as_mut()), &|inv, s| inv.slot_mut(slot(s).ok()?).and_then(|a| a.as_mut()),
) { ) {
parent.add_component(item, &ABILITY_MAP, &MATERIAL_STATS_MANIFEST); parent.persistence_access_add_component(
item,
&ABILITY_MAP,
&MATERIAL_STATS_MANIFEST,
);
} else { } else {
return Err(PersistenceError::ConversionError(format!( return Err(PersistenceError::ConversionError(format!(
"Parent slot {} for component {} was empty even though it occurred earlier in \ "Parent slot {} for component {} was empty even though it occurred earlier in \
@ -459,7 +463,11 @@ pub fn convert_loadout_from_database_items(
l.get_mut_item_at_slot_using_persistence_key(s).ok() l.get_mut_item_at_slot_using_persistence_key(s).ok()
}) })
{ {
parent.add_component(item, &ABILITY_MAP, &MATERIAL_STATS_MANIFEST); parent.persistence_access_add_component(
item,
&ABILITY_MAP,
&MATERIAL_STATS_MANIFEST,
);
} else { } else {
return Err(PersistenceError::ConversionError(format!( return Err(PersistenceError::ConversionError(format!(
"Parent slot {} for component {} was empty even though it occurred earlier in \ "Parent slot {} for component {} was empty even though it occurred earlier in \

View File

@ -70,7 +70,7 @@ pub fn price_desc(
} }
} }
pub fn kind_text<'a>(item: &dyn ItemDesc, i18n: &'a Localization) -> Cow<'a, str> { pub fn kind_text<'a, I: ItemDesc + ?Sized>(item: &I, i18n: &'a Localization) -> Cow<'a, str> {
match &*item.kind() { match &*item.kind() {
ItemKind::Armor(armor) => Cow::Borrowed(armor_kind(armor, i18n)), ItemKind::Armor(armor) => Cow::Borrowed(armor_kind(armor, i18n)),
ItemKind::Tool(tool) => Cow::Owned(format!( ItemKind::Tool(tool) => Cow::Owned(format!(