Added TankEmulator

This commit is contained in:
bdew committed 2015-04-02 11:22:47 +03:00
1 parent 2a008aa093
commit 00b32712a0
2 files changed
+46

No files matched your search

@@ -19,4 +19,9 @@ abstract class DataSlotNumeric[T](default: T)(implicit n: Numeric[T]) extends Da
def +=(that: T) = update(n.plus(value, that))
def -=(that: T) = update(n.minus(value, that))
def *=(that: T) = update(n.times(value, that))
/* Special Int versions, using the conversion from numeric */
def +=(that: Int) = update(n.plus(value, n.fromInt(that)))
def -=(that: Int) = update(n.minus(value, n.fromInt(that)))
def *=(that: Int) = update(n.times(value, n.fromInt(that)))
}
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright (c) bdew, 2013 - 2015
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.tile
import net.bdew.lib.Misc
import net.bdew.lib.data.base.DataSlotNumeric
import net.minecraftforge.fluids.{Fluid, FluidStack, FluidTankInfo, IFluidTank}
case class TankEmulator[T: Numeric](fluid: Fluid, ds: DataSlotNumeric[T], capacity: Int) extends IFluidTank {
val num = implicitly[Numeric[T]]
override def getFluidAmount: Int = num.toDouble(ds).floor.toInt
override def getCapacity: Int = capacity
override def getFluid: FluidStack = if (getFluidAmount > 0) new FluidStack(fluid, getFluidAmount) else null
override def getInfo: FluidTankInfo = new FluidTankInfo(this)
override def drain(maxDrain: Int, doDrain: Boolean): FluidStack = {
val canDrain = Misc.clamp(maxDrain, 0, getFluidAmount)
if (canDrain > 0) {
if (doDrain) ds -= canDrain
new FluidStack(fluid, canDrain)
} else null
}
override def fill(resource: FluidStack, doFill: Boolean): Int = {
if (resource != null && resource.getFluid == fluid && resource.amount > 0) {
val canFill = Misc.min(resource.amount, getCapacity - getFluidAmount)
if (doFill) ds += canFill
canFill
} else 0
}
}