/// <summary> /// Add an item to an inventory. /// </summary> /// <param name="item">The item to add</param> /// <param name="storedItems">The items that were stored, item might be broken up into stacks</param> /// <returns></returns> public static bool AddItem(InventoryItemBase item, ICollection <InventoryItemBase> storedItems = null, bool repaint = true) { Assert.IsNotNull(item, "Given item is null - Can't add."); Assert.IsTrue(_lootToCollections.Count > 0, "Can't add item, no collectionso attached to the current player"); var currency = item as CurrencyInventoryItem; if (currency != null) { var added = AddCurrency(currency.currency.ID, currency.amount); if (added) { Destroy(currency.gameObject); } return(added); } var bestCollection = instance.GetBestLootCollectionForItem(item, true); if (bestCollection == null) { langDatabase.collectionFull.Show(item.name, item.description, "Inventories"); return(false); } var itemArr = InventoryItemUtility.EnforceMaxStackSize(item); foreach (var i in itemArr) { bool added = bestCollection.collection.AddItem(i); Assert.IsTrue(added, "Item wasn't added even though check passed!"); if (storedItems != null) { storedItems.Add(item); } } return(true); }
/// <summary> /// Try adding a list of items. /// </summary> /// <param name="itemsToAdd"></param> /// <returns>All items that couldn't be added. If returnValue.Length == 0 the action was sucesful.</returns> public ItemAmountRow[] TryAdd(IList <ItemAmountRow> itemsToAdd) { Assert.IsFalse(itemsToAdd.Any(o => o.item == null), "Given array contains an empty item! (NULL)"); itemsToAdd = itemsToAdd.OrderByDescending(o => o.item.layoutSize).ToArray(); // Order from large to small to make sure large items can always be placed. itemsToAdd = InventoryItemUtility.EnforceMaxStackSize(itemsToAdd); var unAddedItems = new List <ItemAmountRow>(itemsToAdd.Count); unAddedItems.AddRange(itemsToAdd); for (int j = 0; j < itemsToAdd.Count; j++) { var currency = itemsToAdd[j].item as CurrencyInventoryItem; if (currency != null) { var l = GetBestCollectionForCurrency(currency); if (l == null) { break; } unAddedItems[j] = new ItemAmountRow(null, 0); continue; } bool added = false; var collection = GetBestCollectionForItem(itemsToAdd[j].item); if (collection == null) { break; } for (int i = 0; i < collection.collection.Length; i++) { if (collection.collection[i].itemID == itemsToAdd[j].item.ID) { if (collection.collection[i].amount + itemsToAdd[j].amount <= itemsToAdd[j].item.maxStackSize) { // Doesn't exceed stack size. collection.collection[i].amount += itemsToAdd[j].amount; unAddedItems[j] = new ItemAmountRow(null, 0); added = true; break; } // Exceeds stack size, try to add as much as possible. uint canAddAmount = itemsToAdd[j].item.maxStackSize - collection.collection[i].amount; unAddedItems[j].SetAmount(unAddedItems[j].amount - canAddAmount); collection.collection[i].amount += canAddAmount; } } if (added == false) { for (uint i = 0; i < collection.collection.Length; i++) { if (collection.collection[i].itemID == null) { if (CanSetItem(i, itemsToAdd[j].item, collection)) { var t = collection.collection[i]; t.itemID = itemsToAdd[j].item.ID; t.amount = itemsToAdd[j].item.currentStackSize; SetItem(collection, i, t); unAddedItems[j] = new ItemAmountRow(null, 0); break; } } } } } unAddedItems.RemoveAll(o => o.item == null || o.amount == 0); return(unAddedItems.ToArray()); }