/// <summary> /// Move an item around inside this object (uses client slots) /// </summary> public static IDictionary <int, InventoryItem> MoveItemInsideObject(this IGameInventoryObject thisObject, GamePlayer player, eInventorySlot fromSlot, eInventorySlot toSlot) { lock (thisObject.LockObject()) { IDictionary <int, InventoryItem> inventory = thisObject.GetClientInventory(player); if (!inventory.ContainsKey((int)fromSlot)) { return(null); } var updateItems = new Dictionary <int, InventoryItem>(2); InventoryItem fromItem = null, toItem = null; fromItem = inventory[(int)fromSlot]; if (inventory.ContainsKey((int)toSlot)) { toItem = inventory[(int)toSlot]; toItem.SlotPosition = fromItem.SlotPosition; GameServer.Database.SaveObject(toItem); } fromItem.SlotPosition = (int)toSlot - (int)thisObject.FirstClientSlot + thisObject.FirstDBSlot; GameServer.Database.SaveObject(fromItem); updateItems.Add((int)fromSlot, toItem); updateItems.Add((int)toSlot, fromItem); return(updateItems); } }
/// <summary> /// Can this object handle the move request? /// </summary> public static bool CanHandleRequest(this IGameInventoryObject thisObject, GamePlayer player, ushort fromClientSlot, ushort toClientSlot) { // make sure from or to slots involve this object if ((fromClientSlot >= thisObject.FirstClientSlot && fromClientSlot <= thisObject.LastClientSlot) || (toClientSlot >= thisObject.FirstClientSlot && toClientSlot <= thisObject.LastClientSlot)) { return(true); } return(false); }
/// <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); } }
/// <summary> /// Get the items of this object, mapped to the client inventory slots /// </summary> public static Dictionary <int, InventoryItem> GetClientItems(this IGameInventoryObject thisObject, GamePlayer player) { var inventory = new Dictionary <int, InventoryItem>(); int slotOffset = thisObject.FirstClientSlot - thisObject.FirstDBSlot; foreach (InventoryItem item in thisObject.DBItems(player)) { if (item != null) { if (!inventory.ContainsKey(item.SlotPosition + slotOffset)) { inventory.Add(item.SlotPosition + slotOffset, item); } } } return(inventory); }
/// <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); } }