From bd195a2ffd21ccb9ac0d065323a22c6c20796b40 Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 26 Apr 2020 22:23:59 -0600 Subject: [PATCH] remove unwrap --- src/ship/items.rs | 10 +++++----- src/ship/packet/handler/direct_message.rs | 2 +- src/ship/ship.rs | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ship/items.rs b/src/ship/items.rs index 575c1ef..ebce01e 100644 --- a/src/ship/items.rs +++ b/src/ship/items.rs @@ -14,6 +14,7 @@ use crate::entity::item::mag::Mag; use crate::entity::item::{Meseta, NewItemEntity}; use crate::ship::map::MapArea; use crate::ship::drops::{ItemDrop, ItemDropType}; +use crate::ship::ship::ShipError; #[derive(Debug)] @@ -182,8 +183,7 @@ impl ActiveItemDatabase { ActiveInventory(activated.take(30).collect()) } - // TODO: Result - pub fn activate_item_drop(&mut self, entity_gateway: &mut EG, item_drop: ItemDrop) -> ActiveItemOnFloor { + pub fn activate_item_drop(&mut self, entity_gateway: &mut EG, item_drop: ItemDrop) -> Result { let item_detail = match item_drop.item { ItemDropType::Weapon(w) => Some(ItemDetail::Weapon(w)), ItemDropType::Armor(w) => Some(ItemDetail::Armor(w)), @@ -205,7 +205,7 @@ impl ActiveItemDatabase { z: item_drop.z, } }).unwrap(); - stack_items(vec![item_entity]).pop().unwrap() + stack_items(vec![item_entity]).pop().ok_or(ShipError::ItemError)? }, None => { let meseta = match item_drop.item { @@ -217,13 +217,13 @@ impl ActiveItemDatabase { }; let active_item = self.activate_item(item_instance); - ActiveItemOnFloor { + Ok(ActiveItemOnFloor { map_area: item_drop.map_area, x: item_drop.x, y: item_drop.y, z: item_drop.z, item: active_item, - } + }) } } diff --git a/src/ship/packet/handler/direct_message.rs b/src/ship/packet/handler/direct_message.rs index 4afafea..03855cb 100644 --- a/src/ship/packet/handler/direct_message.rs +++ b/src/ship/packet/handler/direct_message.rs @@ -88,7 +88,7 @@ where EG: EntityGateway { item: item_drop_type, }; - let activated_item = active_items.activate_item_drop(entity_gateway, item_drop); + let activated_item = active_items.activate_item_drop(entity_gateway, item_drop)?; let mut client = clients.get_mut(&area_client.client).ok_or(ShipError::ClientNotFound(area_client.client))?; let item_drop_msg = builder::message::item_drop(request_item.client, request_item.target, &activated_item)?; client.floor_items.push(activated_item); diff --git a/src/ship/ship.rs b/src/ship/ship.rs index a468dfd..10d2db5 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -44,6 +44,7 @@ pub enum ShipError { InvalidRoom(u32), MonsterAlreadyDroppedItem(ClientId, u16), SliceError(#[from] std::array::TryFromSliceError), + ItemError, // TODO: refine this } #[derive(Debug)]