Exemple #1
0
        public virtual void AddItem(byte inventory, short slot, BaseItem item, bool isLoading)
        {
            if (slot == 0)
            {
                // Would bug the client, so ignore
                Trace.WriteLine($"Ignoring item {item.ItemID} because its in the wrong slot (0)");
                return;
            }

            int itemid = item.ItemID;

            if (Constants.getInventory(itemid) != inventory)
            {
                Trace.WriteLine($"Ignoring item {item.ItemID} because its in the wrong inventory ({inventory} vs {Constants.getInventory(itemid)})");
                return;
            }

            item.InventorySlot = slot;

            short amount;

            if (!ItemAmounts.TryGetValue(itemid, out amount))
            {
                amount = 0;
            }
            amount += item.Amount;
            ItemAmounts[itemid] = amount;

            if (slot < 0)
            {
                if (item is EquipItem equipItem)
                {
                    slot = Math.Abs(slot);
                    if (slot > 100)
                    {
                        Equips[1][(byte)(slot - 100)] = equipItem;
                    }
                    else
                    {
                        Equips[0][(byte)slot] = equipItem;
                    }
                }
                else
                {
                    throw new Exception("Tried to AddItem on an equip slot but its not an equip! " + item);
                }
            }
            else
            {
                Items[inventory - 1][slot] = item;
            }
        }
Exemple #2
0
        public virtual void RemoveItem(BaseItem item)
        {
            var inventory = Constants.getInventory(item.ItemID);
            var slot      = item.InventorySlot;
            int itemid    = item.ItemID;

            if (slot == 0)
            {
                // Would bug the client, so ignore
                Trace.WriteLine($"Ignoring item {itemid} because its in the wrong slot (0)");
                return;
            }

            if (ItemAmounts.TryGetValue(itemid, out var amount))
            {
                if (amount - item.Amount <= 0)
                {
                    ItemAmounts.Remove(itemid);
                }
                else
                {
                    ItemAmounts[itemid] -= item.Amount;
                }
            }

            if (slot < 0)
            {
                if (item is EquipItem)
                {
                    slot = Math.Abs(slot);
                    if (slot > 100)
                    {
                        Equips[1][(byte)(slot - 100)] = null;
                    }
                    else
                    {
                        Equips[0][(byte)slot] = null;
                    }
                }
                else
                {
                    throw new Exception("Tried to RemoveItem on an equip slot but its not an equip! " + item);
                }
            }
            else
            {
                Items[inventory - 1][slot] = null;
            }
        }