electrodynamics/src/main/scala/edx/basic/fluid/tank/ItemBlockTank.scala

180 lines
5.5 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.basic.fluid.tank
import java.util.List
import net.minecraft.block.Block
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.{ItemBlock, ItemStack}
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.tileentity.TileEntity
import net.minecraft.world.World
2014-11-07 03:08:34 +01:00
import net.minecraftforge.fluids.{FluidContainerRegistry, FluidStack, IFluidContainerItem}
2015-01-26 12:40:32 +01:00
import resonantengine.lib.utility.LanguageUtility
import resonantengine.lib.utility.science.UnitDisplay
import resonantengine.lib.utility.science.UnitDisplay.Unit
import resonantengine.lib.wrapper.CollectionWrapper._
/**
* @author Darkguardsman
*/
2014-09-07 05:50:03 +02:00
class ItemBlockTank(block: Block) extends ItemBlock(block: Block) with IFluidContainerItem
{
2014-09-07 05:50:03 +02:00
this.setMaxDamage(0)
this.setHasSubtypes(true)
2014-09-07 05:50:03 +02:00
override def getMetadata(damage: Int): Int =
{
return damage
}
2014-09-07 05:50:03 +02:00
@SuppressWarnings(Array("unchecked", "rawtypes")) override def addInformation(itemStack: ItemStack, player: EntityPlayer, list: List[_], par4: Boolean)
{
if (itemStack.getTagCompound != null && itemStack.getTagCompound.hasKey("fluid"))
{
val fluid: FluidStack = getFluid(itemStack)
2014-09-07 05:50:03 +02:00
if (fluid != null)
{
list.add("Fluid: " + fluid.getFluid.getLocalizedName)
list.add("Volume: " + new UnitDisplay(Unit.LITER, fluid.amount))
}
}
}
2015-01-14 12:52:31 +01:00
def getFluid(container: ItemStack): FluidStack =
{
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid"))
{
return null
}
return FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid"))
}
2014-09-07 05:50:03 +02:00
override def getItemStackLimit(stack: ItemStack): Int =
{
if (stack.getTagCompound != null && stack.getTagCompound.hasKey("fluid"))
{
return 1
}
return this.maxStackSize
}
2014-09-07 05:50:03 +02:00
override def getUnlocalizedName(itemStack: ItemStack): String =
{
2014-08-11 07:44:06 +02:00
val translation: String = LanguageUtility.getLocal(getUnlocalizedName() + "." + itemStack.getItemDamage)
2014-09-07 05:50:03 +02:00
if (translation == null || translation.isEmpty)
{
2014-08-11 07:44:06 +02:00
return getUnlocalizedName()
}
2014-08-11 07:44:06 +02:00
return getUnlocalizedName() + "." + itemStack.getItemDamage
}
2014-09-07 05:50:03 +02:00
override def placeBlockAt(stack: ItemStack, player: EntityPlayer, world: World, x: Int, y: Int, z: Int, side: Int, hitX: Float, hitY: Float, hitZ: Float, metadata: Int): Boolean =
{
if (super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata))
{
val tile: TileEntity = world.getTileEntity(x, y, z)
2014-09-07 05:50:03 +02:00
if (tile.isInstanceOf[TileTank])
{
2014-11-07 05:27:31 +01:00
tile.asInstanceOf[TileTank].fluidNode.fill(getFluid(stack), true)
}
return true
}
return false
}
2014-09-07 05:50:03 +02:00
def fill(container: ItemStack, resource: FluidStack, doFill: Boolean): Int =
{
if (resource == null)
{
return 0
}
2014-09-07 05:50:03 +02:00
if (!doFill)
{
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid"))
{
return Math.min(getCapacity(container), resource.amount)
}
val stack: FluidStack = FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid"))
2014-09-07 05:50:03 +02:00
if (stack == null)
{
return Math.min(getCapacity(container), resource.amount)
}
2014-09-07 05:50:03 +02:00
if (!stack.isFluidEqual(resource))
{
return 0
}
return Math.min(getCapacity(container) - stack.amount, resource.amount)
}
2014-09-07 05:50:03 +02:00
if (container.stackTagCompound == null)
{
container.stackTagCompound = new NBTTagCompound
}
2014-09-07 05:50:03 +02:00
if (!container.stackTagCompound.hasKey("fluid"))
{
val fluidTag: NBTTagCompound = resource.writeToNBT(new NBTTagCompound)
2014-09-07 05:50:03 +02:00
if (getCapacity(container) < resource.amount)
{
fluidTag.setInteger("Amount", getCapacity(container))
container.stackTagCompound.setTag("fluid", fluidTag)
return getCapacity(container)
}
container.stackTagCompound.setTag("fluid", fluidTag)
return resource.amount
}
val fluidTag: NBTTagCompound = container.stackTagCompound.getCompoundTag("fluid")
val stack: FluidStack = FluidStack.loadFluidStackFromNBT(fluidTag)
2014-09-07 05:50:03 +02:00
if (!stack.isFluidEqual(resource))
{
return 0
}
var filled: Int = getCapacity(container) - stack.amount
2014-09-07 05:50:03 +02:00
if (resource.amount < filled)
{
stack.amount += resource.amount
filled = resource.amount
}
2014-09-07 05:50:03 +02:00
else
{
stack.amount = getCapacity(container)
}
container.stackTagCompound.setTag("fluid", stack.writeToNBT(fluidTag))
return filled
}
2014-11-07 03:08:34 +01:00
override def getCapacity(container: ItemStack): Int =
2014-09-07 05:50:03 +02:00
{
2014-11-07 03:08:34 +01:00
return 16 * FluidContainerRegistry.BUCKET_VOLUME
2014-09-07 05:50:03 +02:00
}
def drain(container: ItemStack, maxDrain: Int, doDrain: Boolean): FluidStack =
{
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid") || maxDrain == 0)
{
return null
}
val stack: FluidStack = FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid"))
2014-09-07 05:50:03 +02:00
if (stack == null)
{
return null
}
val drained: Int = Math.min(stack.amount, maxDrain)
2014-09-07 05:50:03 +02:00
if (doDrain)
{
if (maxDrain >= stack.amount)
{
container.stackTagCompound.removeTag("fluid")
2014-09-07 05:50:03 +02:00
if (container.stackTagCompound.hasNoTags)
{
container.stackTagCompound = null
}
return stack
}
val fluidTag: NBTTagCompound = container.stackTagCompound.getCompoundTag("fluid")
fluidTag.setInteger("Amount", fluidTag.getInteger("Amount") - maxDrain)
container.stackTagCompound.setTag("fluid", fluidTag)
}
stack.amount = drained
return stack
}
}