Exemple #1
0
        protected override async Task Handle(InventoryAddItemEvent e, CancellationToken cancellation)
        {
            if (!(e.Sender is IInventoriedEntity inventoried))
            {
                return;
            }

            InventoryComponent inv = inventoried.Inventory;

            ItemInstanceDto[] subinv = inv.GetSubInvFromItemInstance(e.ItemInstance);

            short slot = inv.GetFirstFreeSlot(subinv, e.ItemInstance);

            if (slot == -1)
            {
                Log.Info("No available slot");
                //Not enough space
                return;
            }

            ItemInstanceDto mergeable = subinv[slot];

            if (mergeable != null)
            {
                mergeable.Amount += e.ItemInstance.Amount;
                e.ItemInstance    = mergeable;
            }
            else
            {
                e.ItemInstance.Slot = slot;
                subinv[slot]        = e.ItemInstance;
            }

            if (!(inv.Entity is IPlayerEntity player))
            {
                return;
            }

            e.ItemInstance.CharacterId = player.Character.Id;
            await player.ActualizeUiInventorySlot(e.ItemInstance.Type, e.ItemInstance.Slot);
        }
Exemple #2
0
        public static bool CanAddItem(this InventoryComponent inv, ItemDto item)
        {
            ItemInstanceDto[] subinv = inv.GetSubInvFromItem(item);

            return(inv.GetFirstFreeSlot(subinv, item, 1) != -1);
        }