Example #1
0
        public static bool RemoveItemsThenAdd(ItemAmountRow[] itemsToAdd, params ItemAmountRow[] itemsToRemoveFirst)
        {
            RebuildCounter();
            bool removeSucceeded = _counter.TryRemoveItems(itemsToRemoveFirst);

            if (removeSucceeded == false)
            {
                return(false);
            }

            var unAdded = _counter.TryAdd(itemsToAdd);

            if (unAdded.Length > 0)
            {
                return(false);
            }

            foreach (var item in itemsToRemoveFirst)
            {
                uint removed = RemoveItem(item.item.ID, item.amount, false);
                Assert.IsTrue(removed == item.amount, "Couldn't remove item even though check passed!");
            }

            bool added = AddItems(InventoryItemUtility.RowsToItems(itemsToAdd, true));

            Assert.IsTrue(added);

            return(true);
        }
        public virtual void GiveCraftReward(CraftingProgressContainer.CraftInfo craftInfo)
        {
            var itemsToAdd = InventoryItemUtility.RowsToItems(craftInfo.blueprint.resultItems, true);

#if UFPS_MULTIPLAYER
            foreach (var item in itemsToAdd)
            {
                var i = item as Devdog.InventoryPro.Integration.UFPS.UFPSInventoryItemBase;
                if (i != null && Devdog.InventoryPro.Integration.UFPS.Multiplayer.InventoryMPUFPSPickupManager.instance != null)
                {
                    Devdog.InventoryPro.Integration.UFPS.Multiplayer.InventoryMPUFPSPickupManager.instance.InstantiateAndRegisterPickupOnAllClients(i);
                }
            }
#endif

            if (craftInfo.storeItemsInCollection != null)
            {
                var before = craftInfo.storeItemsInCollection.canPutItemsInCollection;
                craftInfo.storeItemsInCollection.canPutItemsInCollection = true;

                bool added = craftInfo.storeItemsInCollection.AddItems(itemsToAdd);

                craftInfo.storeItemsInCollection.canPutItemsInCollection = before;

                Assert.IsTrue(added, "Couldn't add items even though check passed. Please report this error + stack trace.");
            }
            else
            {
                // Store crafted item
                bool added = InventoryManager.AddItems(itemsToAdd);
                Assert.IsTrue(added, "Couldn't add items even though check passed. Please report this error + stack trace.");
            }
        }
 protected void Awake()
 {
     _items = InventoryItemUtility.RowsToItems(items, true);
     for (int i = 0; i < _items.Length; i++)
     {
         _items[i].transform.SetParent(transform);
         _items[i].gameObject.SetActive(false);
     }
 }
Example #4
0
        /// <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);
        }
Example #5
0
 public bool TryRemoveItems(IList <InventoryItemBase> itemsToRemove)
 {
     return(TryRemoveItems(InventoryItemUtility.ItemsToRows(itemsToRemove)));
 }
Example #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="itemsToAdd"></param>
 /// <returns>Returns all items that couldn't be added. AkA failed action is returnArr.Length > 0</returns>
 public ItemAmountRow[] TryAdd(IList <InventoryItemBase> itemsToAdd)
 {
     return(TryAdd(InventoryItemUtility.ItemsToRows(itemsToAdd)));
 }
Example #7
0
        /// <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());
        }
Example #8
0
 public static bool CanRemoveItems(IList <InventoryItemBase> items)
 {
     return(CanRemoveItems(InventoryItemUtility.ItemsToRows(items)));
 }
Example #9
0
 public static bool CanAddItems(InventoryItemBase[] items, bool rebuildCounter = true)
 {
     return(CanAddItems(InventoryItemUtility.ItemsToRows(items), rebuildCounter));
 }
Example #10
0
 /// <summary>
 /// Remove items first, then attempt to add them. The first remove could potentially clear up a slot for the adding.
 /// </summary>
 /// <returns></returns>
 public static bool CanRemoveItemsThenAdd(InventoryItemBase[] itemsToAdd, params ItemAmountRow[] itemsToRemoveFirst)
 {
     return(CanRemoveItemsThenAdd(InventoryItemUtility.ItemsToRows(itemsToAdd), itemsToRemoveFirst));
 }