package ru.dbotthepony.mc.otm.container

import it.unimi.dsi.fastutil.objects.ObjectIterators.AbstractIndexBasedIterator
import net.minecraftforge.fluids.FluidStack
import net.minecraftforge.fluids.capability.IFluidHandler

class FluidHandlerIterator(private val handler: IFluidHandler, initialPosition: Int = 0) : AbstractIndexBasedIterator<FluidStack>(0, initialPosition) {
	init {
		require(initialPosition in 0 until handler.tanks) { "Invalid initial position: $initialPosition" }
	}

	override fun remove(location: Int) {
		throw UnsupportedOperationException()
	}

	override fun get(location: Int): FluidStack {
		return handler[location]
	}

	override fun getMaxPos(): Int {
		return handler.tanks
	}
}

fun IFluidHandler.iterator(position: Int = 0) = FluidHandlerIterator(this, position)
