Esempio n. 1
0
        public bool Remove(GameSession session, long uid, short slot, int amount, out Item outItem)
        {
            outItem = null;
            if (session.Player.Inventory.Items.ContainsKey(slot))
            {
                return(false);
            }

            int outItemIndex = Array.FindIndex(Items, 0, Items.Length, x => x != null && x.Uid == uid);

            outItem = Items[outItemIndex];
            if (amount >= outItem.Amount)
            {
                Items[outItemIndex] = null;
                session.Send(StorageInventoryPacket.Remove(uid));
                return(true);
            }

            if (outItem.TrySplit(amount, out Item splitItem))
            {
                outItem.Amount -= amount;
                outItem.Slot    = slot;
                session.Send(StorageInventoryPacket.Add(outItem));
                outItem = splitItem;
                return(true);
            }
            return(false);
        }
Esempio n. 2
0
    public void Add(GameSession session, long uid, int amount, short slot)
    {
        Item item = session.Player.Inventory.GetByUid(uid);

        if (amount < item.Amount)
        {
            item.TrySplit(amount, out Item splitItem);
            session.Send(ItemInventoryPacket.UpdateAmount(uid, item.Amount));
            item = splitItem;
        }
        else
        {
            session.Player.Inventory.RemoveItem(session, uid, out Item removedItem);
            item = removedItem;
        }

        // If slot is free, add item to it
        if (Items[slot] is null)
        {
            item.Slot   = slot;
            Items[slot] = item;
            session.Send(StorageInventoryPacket.Add(item));
            return;
        }

        // Find first item with the same id, if true update the amount
        Item existingItem = Items.FirstOrDefault(x => x is not null && x.Id == item.Id && x.Rarity == item.Rarity);

        if (existingItem is not null)
        {
            if (existingItem.Amount + item.Amount <= existingItem.StackLimit)
            {
                existingItem.Amount += item.Amount;
                session.Send(StorageInventoryPacket.UpdateItem(existingItem));
                return;
            }

            existingItem.Amount = existingItem.StackLimit;
            item.Amount         = existingItem.Amount + item.Amount - existingItem.StackLimit;
            session.Send(StorageInventoryPacket.UpdateItem(existingItem));
        }

        // Find first free slot
        for (short i = 0; i < Items.Length; i++)
        {
            if (Items[i] is not null)
            {
                continue;
            }

            item.Slot = i;
            Items[i]  = item;
            session.Send(StorageInventoryPacket.Add(item));
            return;
        }
    }
Esempio n. 3
0
        public void Add(GameSession session, long uid, int amount, short slot)
        {
            Item item = session.Player.Inventory.Items[uid];

            if (amount < item.Amount)
            {
                item.TrySplit(amount, out Item splitItem);
                session.Send(ItemInventoryPacket.Update(uid, item.Amount));
                item = splitItem;
            }
            else
            {
                InventoryController.Remove(session, uid, out Item removedItem);
                item = removedItem;
            }

            if (slot >= 0)
            {
                if (Items[slot] == null)
                {
                    item.Slot   = slot;
                    Items[slot] = item;
                    session.Send(StorageInventoryPacket.Add(item));
                    return;
                }
                else
                {
                    slot = -1;
                }
            }

            if (slot == -1)
            {
                for (slot = 0; slot < Items.Length; slot++)
                {
                    if (Items[slot] != null)
                    {
                        continue;
                    }
                    item.Slot   = slot;
                    Items[slot] = item;
                    session.Send(StorageInventoryPacket.Add(item));
                    return;
                }
            }
        }