//private bool TryAdd(Item item, out InventoryError invError) //{ // invError = InventoryError.OK; // return true; //} /// <summary> /// Swaps the items at the given slots without further checks. /// </summary> /// <param name="slot1"></param> /// <param name="slot2"></param> /// <remarks>Make sure the slots are valid before calling.</remarks> public void SwapUnchecked(BaseInventory cont1, int slot1, BaseInventory cont2, int slot2) { var item1 = cont1.m_Items[slot1]; var item2 = cont2.m_Items[slot2]; var handler1 = cont1.GetHandler(slot1); var handler2 = cont2.GetHandler(slot2); if (item2 != null && item1.CanStackWith(item2) && item2.Amount < item2.Template.MaxAmount) { var diff = item1.Template.MaxAmount - item2.Amount; diff = Math.Min(diff, item1.Amount); item1.Amount -= diff; item2.Amount += diff; } else { // remove both Items if (item1 != null) { item1.Slot = slot2; if (handler1 != null) { handler1.Removed(slot1, item1); } } if (item2 != null) { item2.Slot = slot1; if (handler2 != null) { handler2.Removed(slot2, item2); } } // set new items, fields and tell the container about the change cont1.m_Items[slot1] = item2; cont2.m_Items[slot2] = item1; if (item1 != null) { SetContainerEntityId(item1.EntityId, slot2, cont2); item1.Container = cont2; if (handler2 != null) { handler2.Added(item1); } } else { SetContainerEntityId(EntityId.Zero, slot2, cont2); } if (item2 != null) { SetContainerEntityId(item2.EntityId, slot1, cont1); item2.Container = cont1; if (handler1 != null) { handler1.Added(item2); } } else { SetContainerEntityId(EntityId.Zero, slot1, cont1); } } }
/// <summary> /// Swaps the items at the given slots without further checks. /// </summary> /// <param name="slot1"></param> /// <param name="slot2"></param> /// <remarks>Make sure the slots are valid before calling.</remarks> public void SwapUnchecked(BaseInventory cont1, int slot1, BaseInventory cont2, int slot2) { Item obj = cont1.m_Items[slot1]; Item otherItem = cont2.m_Items[slot2]; IItemSlotHandler handler1 = cont1.GetHandler(slot1); IItemSlotHandler handler2 = cont2.GetHandler(slot2); if (otherItem != null && obj.CanStackWith(otherItem) && otherItem.Amount < otherItem.Template.MaxAmount) { int num = Math.Min(obj.Template.MaxAmount - otherItem.Amount, obj.Amount); obj.Amount -= num; otherItem.Amount += num; } else { if (obj != null) { obj.Slot = slot2; if (handler1 != null) { handler1.Removed(slot1, obj); } } if (otherItem != null) { otherItem.Slot = slot1; if (handler2 != null) { handler2.Removed(slot2, otherItem); } } cont1.m_Items[slot1] = otherItem; cont2.m_Items[slot2] = obj; if (obj != null) { this.SetContainerEntityId(obj.EntityId, slot2, cont2); obj.Container = cont2; if (handler2 != null) { handler2.Added(obj); } } else { this.SetContainerEntityId(EntityId.Zero, slot2, cont2); } if (otherItem != null) { this.SetContainerEntityId(otherItem.EntityId, slot1, cont1); otherItem.Container = cont1; if (handler1 == null) { return; } handler1.Added(otherItem); } else { this.SetContainerEntityId(EntityId.Zero, slot1, cont1); } } }