/// <summary> /// Reduces item's amount by the given value. Item is removed /// if amount becomes 0. /// </summary> /// <param name="slot"></param> public InventoryResult Remove(Item item, int amount, InventoryItemRemoveMsg msg) { // Check if item exists in inventory lock (_syncLock) { if (!_items[item.Data.Category].Contains(item)) { return(InventoryResult.ItemNotFound); } } // Remove or reduce if (item.Amount <= amount) { this.Remove(item); } else { item.Amount -= amount; Send.ZC_ITEM_REMOVE(_character, item.ObjectId, amount, msg, InventoryType.Inventory); Send.ZC_OBJECT_PROPERTY(_character, PropertyId.PC.NowWeight); } return(InventoryResult.Success); }
/// Removes items with given id from inventory. /// </summary> /// <param name="itemId">Id of the item to remove.</param> /// <param name="amount">Amount of pieces to remove.</param> /// <returns>Amount of pieces removed.</returns> public int Remove(int itemId, int amount, InventoryItemRemoveMsg msg) { if (amount == 0) { return(0); } var result = 0; // Potential optimization: don't search every category. However, // technically any item can be in any category. foreach (var itemkv in this.GetItems(a => a.Id == itemId)) { var index = itemkv.Key; var item = itemkv.Value; var itemAmount = item.Amount; var category = item.Data.Category; var reduce = Math.Min(amount, item.Amount); item.Amount -= reduce; amount -= reduce; result += reduce; if (reduce == itemAmount) { lock (_syncLock) { _items[category].Remove(item); _itemsWorldIndex.Remove(item.ObjectId); } } if (item.Id != 900011 && item.Id != 900012) { Send.ZC_ITEM_REMOVE(_character, item.ObjectId, reduce, msg, InventoryType.Inventory); } else { Send.ZC_ITEM_REMOVE(_character, 0, reduce, msg, InventoryType.Inventory); } //Send.ZC_ITEM_INVENTORY_INDEX_LIST(_character, item.Data.Category); } if (result != 0) { Send.ZC_OBJECT_PROPERTY(_character, PropertyId.PC.NowWeight); } return(result); }