Ejemplo n.º 1
0
        private void OnAfterItemsAdded(MyInventoryItem item, int amount)
        {
            if (item == null || amount == 0)
            {
                return;
            }

            MyDefinitionId outputId;

            if (_definition.Swaps.TryGetValue(item.DefinitionId, out outputId))
            {
                SwapItems(item.DefinitionId, outputId, amount);
                return;
            }

            var itemDef = item.GetDefinition();

            if (itemDef.Tags == null || itemDef.Tags.Count == 0)
            {
                return;
            }

            foreach (var tag in itemDef.Tags)
            {
                if (!_definition.Swaps.TryGetValue(new MyDefinitionId(typeof(MyObjectBuilder_ItemTagDefinition), tag), out outputId))
                {
                    continue;
                }

                SwapItems(item.DefinitionId, outputId, amount);
                return;
            }
        }
Ejemplo n.º 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);
        }