Esempio n. 1
0
        // Remove Item by Meta
        public int Remove(ItemMeta itemMeta, int amount)
        {
            if (itemMeta == null || amount == 0)
            {
                return(0);
            }
            int minStackSize = 0;

            foreach (ItemStack itemStack in itemStacks)
            {
                if (itemStack.IsEmpty())
                {
                    continue;
                }

                if (!itemMeta.Equals(itemStack.GetItemMeta()))
                {
                    continue;
                }

                if (itemStack.GetAmount() - amount > minStackSize)
                {
                    itemStack.SetAmount(itemStack.GetAmount() - amount);
                    amount = 0;
                    break;
                }
                else
                {
                    amount = Mathf.Abs(itemStack.GetAmount() - amount);
                    itemStack.Clear();
                }
            }
            return(amount);
        }
Esempio n. 2
0
        // Add Item by Meta
        public int Add(ItemMeta itemMeta, int amount)
        {
            if (itemMeta == null || amount == 0)
            {
                return(0);
            }
            int maxStackSize = itemMeta.GetItemType().MaxStackSize;

            foreach (ItemStack itemStack in itemStacks)
            {
                if (itemStack.IsEmpty())
                {
                    continue;
                }

                if (!itemMeta.Equals(itemStack.GetItemMeta()))
                {
                    continue;
                }

                if (itemStack.GetAmount() == maxStackSize)
                {
                    continue;
                }

                if (itemStack.GetAmount() + amount <= maxStackSize)
                {
                    itemStack.SetAmount(itemStack.GetAmount() + amount);
                    amount = 0;
                    break;
                }
                else
                {
                    amount -= itemStack.GetAmount() + amount - maxStackSize;
                    itemStack.SetAmount(maxStackSize);
                }
            }

            if (amount > 0)
            {
                int firstEmpty = FindEmpty();

                if (firstEmpty != -1 && firstEmpty < itemStacks.Length)
                {
                    ItemStack itemStack = itemStacks[firstEmpty];

                    if (itemStack == null || !itemStack.IsEmpty())
                    {
                        return(amount);
                    }

                    if (amount <= maxStackSize)
                    {
                        itemStack.SetItemStack(itemMeta, amount);
                        amount = 0;
                    }
                    else
                    {
                        amount -= maxStackSize;
                        itemStack.SetItemStack(itemMeta, maxStackSize);
                        Add(itemMeta, amount);
                    }
                }
            }
            return(amount);
        }