Exemplo n.º 1
0
        public bool PlaceItemToOtherStorage(Player player, Player second, Storage from, int fromSlot, Storage to, int toSlot, int count)
        {
            if (to.IsFull())
                return false;

            if (count < 0)
                return false;

            if (!(player.Controller is DefaultController) || !(second.Controller is DefaultController))
                return false;

            if(player == second)
            {
                StorageItem item = null;

                if(to.StorageType == StorageType.CharacterWarehouse)
                {
                    if(toSlot == -1)
                        toSlot = to.GetFreeSlot(player.PlayerCurrentBankSection * 72);

                    if(toSlot == 0 && to.Items.ContainsKey(toSlot))
                        toSlot = to.GetFreeSlot(player.PlayerCurrentBankSection * 72);

                    if (toSlot == to.LastIdRanged(player.PlayerCurrentBankSection * 72, (player.PlayerCurrentBankSection + 1) * 72 - 1))
                        toSlot = to.GetFreeSlot(player.PlayerCurrentBankSection * 72);
                }
                else
                    if ((toSlot == 0 && to.Items.ContainsKey(toSlot)) || toSlot == -1)
                        toSlot = to.GetFreeSlot();

                if (to.Items.ContainsKey(toSlot))
                {
                    if (!PlaceItemToOtherStorage(player, player, to, toSlot, player.Inventory, player.Inventory.GetFreeSlot(), to.Items[toSlot].Amount))
                        return false;
                }

                lock (from.ItemsLock)
                {
                    if(from.Items.ContainsKey(fromSlot))
                    {
                        item = from.Items[fromSlot];

                        if(item.Amount < count)
                            return false;

                        if (item.Amount == count)
                            from.Items.Remove(fromSlot);
                        else
                        {
                            from.Items[fromSlot].Amount -= count;
                            item = new StorageItem { ItemId = item.ItemId, Amount = count };
                        }
                    }
                }

                if (item == null)
                    return false;

                if (!AddItem(player, to, item.ItemId, count, toSlot))
                    AddItem(player, from, item.ItemId, count, fromSlot);
            }
            return false;
        }
Exemplo n.º 2
0
        public bool AddItem(Player player, Storage storage, int itemId, int count, int slot = -1)
        {
            if (count < 0)
                return false;

            if (slot > (storage.StorageType == StorageType.Inventory ? storage.Size + 20 : storage.Size))
                return false;

            if ((storage.StorageType == StorageType.Inventory && slot < 20 && slot != -1))
                return false;

            if (slot < -1)
                return false;

            lock (storage.ItemsLock)
            {

                int stackSize = ItemTemplate.Factory(itemId).MaxStack;

                if (stackSize == 1 && storage.IsFull())
                {
                    SystemMessages.InventoryIsFull.Send(player);
                    return false;
                }

                if (stackSize == 1)
                {
                    if (slot == -1)
                        storage.Items.Add(storage.GetFreeSlot(), new StorageItem {ItemId = itemId, Amount = count});
                    else if (storage.Items.ContainsKey(slot))
                    {
                        SystemMessages.YouCantPutItemInInventory(ItemTemplate.Factory(itemId).Name).Send(player);
                        return false;
                    }
                    else
                        storage.Items.Add(slot, new StorageItem {ItemId = itemId, Amount = count});
                }
                else
                {
                    if (slot != -1)
                    {
                        // Certain slot + Stackable
                        if (storage.Items.ContainsKey(slot))
                        {
                            SystemMessages.YouCantPutItemInInventory(ItemTemplate.Factory(itemId).Name).Send(player);
                            return false;
                        }

                        storage.Items.Add(slot, new StorageItem { ItemId = itemId, Amount = count });
                    }
                    else
                    {
                        #region Any slot + Stackable
                        Dictionary<int, StorageItem> itemsById = storage.GetItemsById(itemId);

                        int canBeAdded =
                            itemsById.Values.Where(storageItem => storageItem.Amount < stackSize).Sum(
                                storageItem => stackSize - storageItem.Amount);

                        if (canBeAdded >= count)
                        {
                            foreach (var storageItem in itemsById.Values)
                            {
                                int added = Math.Min(stackSize - storageItem.Amount, count);
                                storageItem.Amount += added;
                                count -= added;
                                if (count == 0)
                                    break;
                            }
                        }
                        else
                        {
                            if (storage.IsFull() || count > GetFreeSlots(storage).Count*stackSize)
                            {
                                SystemMessages.InventoryIsFull.Send(player);
                                return false;
                            }

                            foreach (var storageItem in itemsById.Values)
                            {
                                int added = Math.Min(stackSize - storageItem.Amount, count);
                                storageItem.Amount += added;
                                count -= added;
                            }
                            while (count > 0)
                            {
                                int added = Math.Min(stackSize, count);
                                StorageItem item = new StorageItem {ItemId = itemId, Amount = added};
                                storage.Items.Add(storage.GetFreeSlot(), item);
                                count -= added;
                            }
                        }
                        #endregion
                    }
                }

                ShowPlayerStorage(player, storage.StorageType);
                return true;
            }
        }