/// <summary> /// Finds and consumes all of the given items. /// Does not consume anything and returns false if not all items were found. /// </summary> public bool Consume(bool inclBank, ItemStackDescription[] items) { var slotIdLists = new List<SimpleSlotId>[items.Length]; // the slots of the found items var foundAmounts = new int[items.Length]; List<SimpleSlotId> slotIds; for (var i = 0; i < items.Length; i++) { var template = items[i]; slotIdLists[i] = slotIds = new List<SimpleSlotId>(); foundAmounts[i] = Find(inclBank, template.Amount, slotIds, item => item.Template.ItemId == template.ItemId); if (foundAmounts[i] < template.Amount) { return false; } } for (var i = 0; i < items.Length; i++) { var template = items[i]; slotIds = slotIdLists[i]; var found = foundAmounts[i]; var count = slotIds.Count; var lastDelPos = found == template.Amount ? 0 : 1; if (count > lastDelPos) { for (var j = count - 1; j >= lastDelPos; j--) { slotIds[j].Container.Destroy(slotIds[j].Slot); } } if (found > template.Amount) { // we didn't delete the first item, instead we have to remove the remaining amount from it var firstSlotId = slotIds[0]; firstSlotId.Item.Amount = found - template.Amount; } } return true; }
/// <summary> /// Finds and consumes all of the given items. /// Does not consume anything and returns false if not all items were found. /// </summary> public bool Consume(ItemStackDescription[] items, bool inclBank) { var slotIdLists = new List<SimpleSlotId>[items.Length]; // the slots of the found items var foundAmounts = new int[items.Length]; List<SimpleSlotId> slotIds; // find all necessary items for (var i = 0; i < items.Length; i++) { var template = items[i]; slotIdLists[i] = slotIds = new List<SimpleSlotId>(3); foundAmounts[i] = Find(inclBank, template.Amount, slotIds, item => item.Template.ItemId == template.ItemId); if (foundAmounts[i] < template.Amount) { // one of the required items does not have a sufficient amount -> Cancel return false; } } // we made sure that we have enough of all items // => Remove all of them for (var i = 0; i < items.Length; i++) { var template = items[i]; slotIds = slotIdLists[i]; var found = foundAmounts[i]; var count = slotIds.Count; var lastDelPos = found == template.Amount ? 0 : 1; if (count > lastDelPos) { for (var j = count - 1; j >= lastDelPos; j--) { slotIds[j].Container.Destroy(slotIds[j].Slot); } } if (found > template.Amount) { // we didn't delete the first item, instead we have to remove the remaining amount from it var firstSlotId = slotIds[0]; firstSlotId.Item.Amount = found - template.Amount; } } return true; }