public bool MovePet(Creature pet, Item item, Creature other, Pocket target, int targetX, int targetY)
{
if (!this.Has(target) || !other.Inventory.Has(target))
return false;
var source = item.Info.Pocket;
var amount = item.Info.Amount;
// We have to copy the item to get a new id, otherwise there could
// be collisions when saving, because the moved item is still in
// the inventory of the pet/character (from the pov of the db).
// http://dev.mabinoger.com/forum/index.php/topic/804-pet-inventory/
var newItem = new Item(item);
Item collidingItem = null;
lock (_pockets)
{
if (!other.Inventory._pockets[target].TryAdd(newItem, (byte)targetX, (byte)targetY, out collidingItem))
return false;
// If amount differs (item was added to stack)
if (collidingItem != null && newItem.Info.Amount != amount)
{
Send.ItemAmount(other, collidingItem);
// Left overs, update
if (newItem.Info.Amount > 0)
{
item.Info.Amount = newItem.Info.Amount;
Send.ItemAmount(_creature, item);
}
// All in, remove from cursor.
else
{
_pockets[item.Info.Pocket].Remove(item);
Send.ItemRemove(_creature, item);
}
}
else
{
// Remove the item from the source pocket
_pockets[source].Remove(item);
Send.ItemRemove(_creature, item, source);
if (collidingItem != null)
{
// Remove colliding item
Send.ItemRemove(other, collidingItem, target);
var collidingItemCopy = new Item(collidingItem);
// Toss it in, it should be the cursor.
_pockets[source].Add(collidingItemCopy);
Send.ItemNew(_creature, collidingItemCopy);
}
Send.ItemNew(other, newItem);
Send.ItemMoveInfo(_creature, item, source, collidingItem);
}
}
// Check movement
pet.Inventory.CheckLeftHand(item, source, target);
pet.Inventory.CheckRightHand(item, source, target);
// Equip handling
if (target.IsEquip())
{
pet.Inventory.UpdateEquipReferences();
pet.Inventory.OnEquip(item);
if (collidingItem != null)
pet.Inventory.OnUnequip(collidingItem);
pet.Inventory.UpdateEquipStats();
Send.EquipmentChanged(pet, newItem);
}
else if (source.IsEquip())
{
pet.Inventory.UpdateEquipReferences();
pet.Inventory.OnUnequip(item);
pet.Inventory.UpdateEquipStats();
Send.EquipmentMoved(pet, source);
}
return true;
}