Skip to content

Instantly share code, notes, and snippets.

@nbness2
Created May 20, 2023 10:46
Show Gist options
  • Save nbness2/3c13a36fae4cbe9dfa0ab6d1b1706ea7 to your computer and use it in GitHub Desktop.
Save nbness2/3c13a36fae4cbe9dfa0ab6d1b1706ea7 to your computer and use it in GitHub Desktop.
inline fun <I: Item> ItemContainer<I>.nbxShift(predicate: (I) -> Boolean = Item::isInvalidItem) {
var firstSlotToFill = -1
var currentSlot = 0
var item: Item
while (currentSlot < items.size) {
item = this[currentSlot]
if (predicate(item)) {
if (firstSlotToFill < 0) firstSlotToFill = currentSlot
currentSlot += 1
continue
}
if (firstSlotToFill >= 0) {
this[firstSlotToFill] = item.icopy()
items[currentSlot] = InvalidItem
currentSlot = firstSlotToFill + 1
firstSlotToFill = -1
continue
}
currentSlot += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment