예제 #1
0
        public override bool TransferItemsFrom(MyInventoryBase sourceInventory, MyInventoryItem item, int amount)
        {
            if (sourceInventory == null || item == null || amount > item.Amount)
            {
                return(false);
            }
            if (amount == 0)
            {
                return(true);
            }
            var limitedItem = amount < item.Amount ? item.Clone(amount) : item;

            if (!CanAddItems(item.DefinitionId, amount) && this != sourceInventory)
            {
                return(false);
            }
            if (this == sourceInventory)
            {
                return(sourceInventory.Remove(item, amount) && Add(limitedItem));
            }
            if (!Add(limitedItem))
            {
                return(false);
            }
            if (sourceInventory.Remove(item, amount))
            {
                return(true);
            }
            Remove(item, amount);
            return(false);
        }
예제 #2
0
        public override bool Add(MyInventoryItem item, NewItemParams parameters = NewItemParams.None)
        {
            if ((parameters & NewItemParams.AsNewStack) != 0)
            {
                _items.Add(item);
                return(true);
            }

            var stackSize = item.GetDefinition().MaxStackAmount;

            for (var i = 0; i < _items.Count; i++)
            {
                var other = _items[i];
                if (!other.CanStack(item))
                {
                    continue;
                }
                var add = Math.Min(item.Amount, stackSize - other.Amount);
                _items[i] = other.Clone(other.Amount + add);
                item      = item.Clone(item.Amount - add);
                if (item.Amount == 0)
                {
                    break;
                }
            }

            if (item.Amount > 0)
            {
                _items.Add(item);
            }
            return(true);
        }