Esempio n. 1
0
        /// <summary>
        /// Move an item from the inventory object to a player's backpack (uses client slots)
        /// </summary>
        public static IDictionary <int, InventoryItem> MoveItemFromObject(this IGameInventoryObject thisObject, GamePlayer player, eInventorySlot fromClientSlot, eInventorySlot toClientSlot)
        {
            // We will only allow moving to the backpack.

            if (toClientSlot < eInventorySlot.FirstBackpack || toClientSlot > eInventorySlot.LastBackpack)
            {
                return(null);
            }

            lock (thisObject.LockObject())
            {
                Dictionary <int, InventoryItem> inventory = thisObject.GetClientInventory(player);

                if (inventory.ContainsKey((int)fromClientSlot) == false)
                {
                    ChatUtil.SendErrorMessage(player, "Item not found in slot " + (int)fromClientSlot);
                    return(null);
                }

                InventoryItem fromItem = inventory[(int)fromClientSlot];
                InventoryItem toItem   = player.Inventory.GetItem(toClientSlot);

                // if there is an item in the players target inventory slot then move it to the object
                if (toItem != null)
                {
                    player.Inventory.RemoveTradeItem(toItem);
                    toItem.SlotPosition = fromItem.SlotPosition;
                    toItem.OwnerID      = thisObject.GetOwner(player);
                    thisObject.OnAddItem(player, toItem);
                    GameServer.Database.SaveObject(toItem);
                }

                thisObject.OnRemoveItem(player, fromItem);

                // Create the GameInventoryItem from this InventoryItem.  This simply wraps the InventoryItem,
                // which is still updated when this item is moved around
                InventoryItem objectItem = GameInventoryItem.Create <InventoryItem>(fromItem);

                player.Inventory.AddTradeItem(toClientSlot, objectItem);

                var updateItems = new Dictionary <int, InventoryItem>(1);
                updateItems.Add((int)fromClientSlot, toItem);

                return(updateItems);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Move an item from a player's backpack to this inventory object (uses client slots)
        /// </summary>
        public static IDictionary <int, InventoryItem> MoveItemToObject(this IGameInventoryObject thisObject, GamePlayer player, eInventorySlot fromClientSlot, eInventorySlot toClientSlot)
        {
            // We will only allow moving from the backpack.

            if (fromClientSlot < eInventorySlot.FirstBackpack || fromClientSlot > eInventorySlot.LastBackpack)
            {
                return(null);
            }

            InventoryItem fromItem = player.Inventory.GetItem(fromClientSlot);

            if (fromItem == null)
            {
                return(null);
            }

            lock (thisObject.LockObject())
            {
                Dictionary <int, InventoryItem> inventory = thisObject.GetClientInventory(player);

                player.Inventory.RemoveTradeItem(fromItem);

                // if there is an item in the objects target slot then move it to the players inventory
                if (inventory.ContainsKey((int)toClientSlot))
                {
                    InventoryItem toItem = inventory[(int)toClientSlot];
                    thisObject.OnRemoveItem(player, toItem);
                    player.Inventory.AddTradeItem(fromClientSlot, toItem);
                }

                fromItem.OwnerID      = thisObject.GetOwner(player);
                fromItem.SlotPosition = (int)(toClientSlot) - (int)(thisObject.FirstClientSlot) + thisObject.FirstDBSlot;
                thisObject.OnAddItem(player, fromItem);
                GameServer.Database.SaveObject(fromItem);

                var updateItems = new Dictionary <int, InventoryItem>(1);
                updateItems.Add((int)toClientSlot, fromItem);

                // for objects that support doing something when added (setting a price, for example)
                player.TempProperties.setProperty(ITEM_BEING_ADDED, fromItem);

                return(updateItems);
            }
        }