aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/mc/InventoryUtil.kt
blob: 74f7b9f51c07cb797d79c3b30562fd3885344acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package moe.nea.firmament.util.mc

import java.util.Spliterator
import java.util.Spliterators
import net.minecraft.inventory.Inventory
import net.minecraft.item.ItemStack

val Inventory.indices get() = 0 until size()
val Inventory.iterableView
	get() = object : Iterable<ItemStack> {
		override fun spliterator(): Spliterator<ItemStack> {
			return Spliterators.spliterator(iterator(), size().toLong(), 0)
		}

		override fun iterator(): Iterator<ItemStack> {
			return object : Iterator<ItemStack> {
				var i = 0
				override fun hasNext(): Boolean {
					return i < size()
				}

				override fun next(): ItemStack {
					if (!hasNext()) throw NoSuchElementException()
					return getStack(i++)
				}
			}
		}
	}