/// <summary> /// Adds an item to the inventory if there's space available. /// </summary> /// <param name="item"> Item to add.</param> /// <param name="amount"> How many to add.</param> /// <param name="fullPopup"> Show popup? </param> public bool AddItemEntry(InventoryItemEntry item, int amount = 1, bool fullPopup = true) { if (!configured) { SetUpPopup(); } // while(amount > 0) { // if(ItemsInInventory.Exists(x => (x.ItemSettings.itemId == item.ItemSettings.itemId) && (x.Stack < item.ItemSettings.maxStackQuantity))) { // // var itemEntry = ItemsInInventory.First(x => (x.ItemSettings.itemId == item.ItemSettings.itemId) && (x.Stack < item.ItemSettings.maxStackQuantity)); // // //var freeSpaceInStack = (item.ItemSettings.maxStackQuantity - itemEntry.Stack); // // //var amountToAddToStack = Mathf.Min(amount, freeSpaceInStack); // // itemEntry.AddToStack(1); // // amount--; // // OnInventoryUpdate?.Invoke(); // } else { // if(ItemsInInventory.Count < InventorySize) { // ItemsInInventory.Add(new InventoryItemEntry(item.ItemSettings)); // amount--; // OnInventoryUpdate?.Invoke(); // } else { // if(fullPopup) FullInventoryPopup(); // OnInventoryUpdate?.Invoke(); // return false; // } // } // } if (ItemsInInventory.Count < InventorySize) { ItemsInInventory.Add(new InventoryItemEntry(item.ItemSettings)); OnInventoryUpdate?.Invoke(); } else { if (fullPopup) { FullInventoryPopup(); } OnInventoryUpdate?.Invoke(); return(false); } OnInventoryUpdate?.Invoke(); return(true); }
/// <summary> /// Removes an item to the inventory if it exists and returns it. Will return null if can't find an item. /// </summary> /// <param name="item"> Item to remove.</param> public InventoryItemEntry?RemoveItemEntry(InventoryItemEntry item) { OnInventoryUpdate?.Invoke(); if (!ItemsInInventory.Exists(x => (x.ItemSettings.itemId == item.ItemSettings.itemId))) { return(null); } var itemEntry = ItemsInInventory.First(x => (x.ItemSettings.itemId == item.ItemSettings.itemId)); if (itemEntry.Stack > 1) { ItemsInInventory.First(x => (x.ItemSettings.itemId == item.ItemSettings.itemId) && (x.Stack > 1)) .AddToStack(-1); } else { ItemsInInventory.Remove(itemEntry); } OnInventoryUpdate?.Invoke(); return(itemEntry); }