|
|
@ -36,6 +36,7 @@ pub enum ItemManagerError { |
|
|
|
NotEnoughTools(Tool, usize, usize), // have, expected
|
|
|
|
InventoryItemConsumeError(#[from] InventoryItemConsumeError),
|
|
|
|
BankFull,
|
|
|
|
WrongItemType(ClientItemId),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ItemManager {
|
|
|
@ -595,4 +596,41 @@ impl ItemManager { |
|
|
|
|
|
|
|
Ok(inventory_item.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn player_feeds_mag_item<EG: EntityGateway>(&mut self,
|
|
|
|
entity_gateway: &mut EG,
|
|
|
|
character: &CharacterEntity,
|
|
|
|
mag_id: ClientItemId,
|
|
|
|
tool_id: ClientItemId)
|
|
|
|
-> Result<(), ItemManagerError> {
|
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
let consumed_tool = {
|
|
|
|
let item_to_feed = inventory.get_item_handle_by_id(tool_id).ok_or(ItemManagerError::NoSuchItemId(tool_id))?;
|
|
|
|
item_to_feed.consume(1)?
|
|
|
|
};
|
|
|
|
let mut mag_handle = inventory.get_item_handle_by_id(mag_id).ok_or(ItemManagerError::NoSuchItemId(mag_id))?;
|
|
|
|
|
|
|
|
let individual_item = mag_handle.item_mut()
|
|
|
|
.ok_or(ItemManagerError::NoSuchItemId(mag_id))?
|
|
|
|
.individual()
|
|
|
|
.ok_or(ItemManagerError::WrongItemType(mag_id))?;
|
|
|
|
let mag = individual_item
|
|
|
|
.mag()
|
|
|
|
.ok_or(ItemManagerError::WrongItemType(mag_id))?;
|
|
|
|
|
|
|
|
let consumed_tool_type = match &consumed_tool {
|
|
|
|
ConsumedItem::Stacked(stacked_consumed_item) => stacked_consumed_item.tool.tool,
|
|
|
|
_ => return Err(ItemManagerError::WrongItemType(tool_id))
|
|
|
|
};
|
|
|
|
mag.feed(consumed_tool_type);
|
|
|
|
|
|
|
|
for entity_id in consumed_tool.entity_ids() {
|
|
|
|
entity_gateway.feed_mag(&individual_item.entity_id, &entity_id).await;
|
|
|
|
entity_gateway.change_item_location(&entity_id, ItemLocation::FedToMag {
|
|
|
|
mag: individual_item.entity_id,
|
|
|
|
}).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|