public static void RemoveItem(ItemDef itemDef) { if (Items.TryGetValue(itemDef, out int number)) { if (number == 1) { Items.Remove(itemDef); } else { Items[itemDef]--; } ItemRemoved?.Invoke(itemDef); } }
/// <summary> /// Removes an item from the inventory (Triggers ItemRemoved) /// </summary> /// <param name="itemDef">The item to remove</param> public static void RemoveItem(ItemDef itemDef) { var item = GetItem(itemDef); if (item?.Count > 0) { if (item.Count == 1) { Items.Remove(item); } else { item.Count--; } ItemRemoved?.Invoke(itemDef); } }
public static void AddItem(ItemDef itemDef) { if (itemDef == null) { return; } if (Items.ContainsKey(itemDef)) { Items[itemDef]++; } else { Items.Add(itemDef, 1); } ItemAdded?.Invoke(itemDef); }
public static void SetItemNumber(string itemName, int number) { ItemDef itemDef = _itemDefs.FirstOrDefault(x => x.Name.Equals(itemName, StringComparison.InvariantCultureIgnoreCase)); if (itemDef == null) { return; } if (number <= 0) { RemoveItem(itemDef); return; } Items[itemDef] = number; ItemAdded?.Invoke(itemDef); }
/// <summary> /// Adds an item to the inventory (Triggers ItemAdded) /// </summary> /// <param name="itemDef">The item to add</param> public static void AddItem(ItemDef itemDef) { if (itemDef == null) { return; } var item = GetItem(itemDef); if (item != null) { item.Count++; } else { Items.Add(new InventoryItem(itemDef)); } ItemAdded?.Invoke(itemDef); }
/// <summary> /// Gets an item count /// </summary> /// <param name="itemDef">The item to count</param> /// <returns>The item count</returns> public static int GetItemCount(ItemDef itemDef) { return(GetItem(itemDef)?.Count ?? 0); }
/// <summary> /// Gets an item count /// </summary> /// <param name="itemDef">The item to count</param> /// <returns>The item count</returns> private static InventoryItem GetItem(ItemDef itemDef) { return(Items.FirstOrDefault(x => x.Equals(itemDef))); }