public void Add(BaseItem item) { int loc = CheckInventory(item.ItemID, true); int slot = FindEmptySlot(); if ((loc != NoItem) || (slot != NoItem)) { int temp = NoItem; if (!item.IsStackable() || (loc == NoItem)) { // Add item to inventory items[slot] = (BaseItem)item.Clone(); temp = slot; } else { // Add to existing items stack size. items[loc].Count++; temp = loc; } if (NewItemAdded != null) { NewItemAdded(temp, item); } } }
/// <summary> /// Gets an item /// </summary> /// <param name="id">Item's id</param> /// <returns>Item or default value</returns> public BaseItem GetItem(int id) { BaseItem result = Items.Where(t => t.Id == id).FirstOrDefault(); if (!(result is null)) { return((BaseItem)result.Clone()); }
/// <summary> /// Adds an item /// </summary> /// <param name="item">Item to add</param> /// <returns>new item's id</returns> public BaseItem Add(BaseItem item) { if (item.IsValid()) { item.Id = Items.Count + 1; Items.Add(item); _ = item.Id; SaveItems(); return((BaseItem)item.Clone()); } else { throw new Exception("Not valid Item"); } }