From ddee920080c48c841f44e92fc140c902b795e9d2 Mon Sep 17 00:00:00 2001 From: jake Date: Sat, 30 May 2020 11:24:15 -0600 Subject: [PATCH] fix bug where picking up a stack caused 2x that item to be picked up --- src/ship/items.rs | 24 +++++++++++++++-------- src/ship/packet/handler/direct_message.rs | 9 ++++++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ship/items.rs b/src/ship/items.rs index 87df145..16f7b81 100644 --- a/src/ship/items.rs +++ b/src/ship/items.rs @@ -28,7 +28,6 @@ pub enum ActiveItemEntityId { Meseta(Meseta), } - #[derive(Debug, Clone, PartialEq)] pub enum HeldItemType { Individual(ItemDetail), @@ -142,6 +141,12 @@ impl<'a> CharacterInventory<'a> { } } +pub enum TriggerCreateItem { + Yes, + No +} + + #[derive(Error, Debug)] #[error("")] pub enum ItemManagerError { @@ -284,7 +289,7 @@ impl ItemManager { .map(Clone::clone) } - pub fn character_picks_up_item(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, floor_item: FloorItem) -> Result<(), ItemManagerError> { + pub fn character_picks_up_item(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, floor_item: FloorItem) -> Result { let local_floor = self.character_floor.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; @@ -333,7 +338,7 @@ impl ItemManager { return Err(ItemManagerError::NoSuchItemId(floor_item.item_id)) } - match floor_item.item { + let trigger_create = match floor_item.item { FloorItemType::Individual(item) => { let inventory_item = InventoryItem { entity_id: floor_item.entity_id, @@ -354,9 +359,10 @@ impl ItemManager { }); // TODO: error check inventory.push(inventory_item); } // else something went very wrong TODO: log it + TriggerCreateItem::Yes }, FloorItemType::Stacked(tool, amount) => { - let inventory_item = inventory.iter_mut() + let (trigger_create, inventory_item) = inventory.iter_mut() .filter(|i| { if let HeldItemType::Stacked(tooltype, _amount) = i.item { tooltype == tool @@ -382,7 +388,7 @@ impl ItemManager { *inv_amount += floor_amount } - existing_inv_item.clone() + (TriggerCreateItem::No, existing_inv_item.clone()) }) .unwrap_or_else(|| { let picked_up_item = InventoryItem { @@ -392,7 +398,7 @@ impl ItemManager { equipped: false, }; inventory.push(picked_up_item.clone()); - picked_up_item + (TriggerCreateItem::Yes, picked_up_item) }); if let ActiveItemEntityId::Stacked(item_ids) = &inventory_item.entity_id { @@ -408,14 +414,16 @@ impl ItemManager { }); // TODO: error check }; } // else something went very wrong TODO: log it + trigger_create }, FloorItemType::Meseta(meseta) => { character.meseta = std::cmp::min(character.meseta + meseta.0, 999999); entity_gateway.save_character(&character); + TriggerCreateItem::No } - } + }; - Ok(()) + Ok(trigger_create) } pub fn enemy_drop_item_on_local_floor(&mut self, entity_gateway: &mut EG, character: &CharacterEntity, item_drop: ItemDrop) -> Result<&FloorItem, ItemManagerError> { diff --git a/src/ship/packet/handler/direct_message.rs b/src/ship/packet/handler/direct_message.rs index 36be335..4823cac 100644 --- a/src/ship/packet/handler/direct_message.rs +++ b/src/ship/packet/handler/direct_message.rs @@ -5,7 +5,7 @@ use crate::common::serverstate::ClientId; use crate::ship::ship::{SendShipPacket, ShipError, Clients, Rooms}; use crate::ship::location::{ClientLocation, ClientLocationError}; use crate::ship::drops::ItemDrop; -use crate::ship::items::{ItemManager, FloorItemType, ClientItemId}; +use crate::ship::items::{ItemManager, FloorItemType, ClientItemId, TriggerCreateItem}; use crate::entity::gateway::EntityGateway; use libpso::utf8_to_utf16_array; use crate::ship::packet::builder; @@ -119,7 +119,7 @@ where }; match item_manager.character_picks_up_item(entity_gateway, &mut client.character, item) { - Ok(_) => { + Ok(trigger_create_item) => { Ok(Box::new(Vec::new().into_iter() .chain(clients_in_area.clone().into_iter() .map(move |c| { @@ -127,7 +127,10 @@ where })) .chain(clients_in_area.into_iter(). filter_map(move |c| { - create_item.clone().map(|ci| (c.client, SendShipPacket::Message(Message::new(GameMessage::CreateItem(ci))))) + match trigger_create_item { + TriggerCreateItem::Yes => create_item.clone().map(|ci| (c.client, SendShipPacket::Message(Message::new(GameMessage::CreateItem(ci))))), + _ => None + } } ))) )