electrodynamics/src/main/scala/resonantinduction/archaic/fluid/tank/TileTank.scala

182 lines
6.8 KiB
Scala
Raw Normal View History

package resonantinduction.archaic.fluid.tank
import java.awt.Color
import java.util.{ArrayList, List}
2014-11-04 03:45:29 +01:00
import cpw.mods.fml.relauncher.{Side, SideOnly}
import net.minecraft.block.material.Material
2014-11-07 14:32:38 +01:00
import net.minecraft.client.renderer.RenderBlocks
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.world.IBlockAccess
import net.minecraftforge.common.util.ForgeDirection
2014-11-07 05:27:31 +01:00
import net.minecraftforge.fluids.{FluidContainerRegistry, FluidStack, FluidTank, IFluidTank}
import org.lwjgl.opengl.GL11
import resonant.api.IRemovable.ISneakPickup
2014-11-04 03:45:29 +01:00
import resonant.content.prefab.RenderConnectedTexture
import resonant.lib.render.{FluidRenderUtility, RenderUtility}
2014-11-04 03:45:29 +01:00
import resonant.lib.transform.vector.Vector3
import resonant.lib.utility.FluidUtility
2014-11-07 14:32:38 +01:00
import resonant.lib.utility.render.RenderBlockUtility
import resonantinduction.archaic.ArchaicContent
2014-11-07 05:27:31 +01:00
import resonantinduction.archaic.fluid.gutter.NodePressureGravity
import resonantinduction.core.Reference
2014-11-07 05:27:31 +01:00
import resonantinduction.core.prefab.node.TileFluidProvider
2014-11-07 14:32:38 +01:00
import resonant.lib.wrapper.BitmaskWrapper._
/**
* Tile/Block class for basic Dynamic tanks
*
* @author Darkguardsman
*/
2014-11-06 16:11:08 +01:00
class TileTank extends TileFluidProvider(Material.iron) with ISneakPickup with RenderConnectedTexture
2014-09-07 05:50:03 +02:00
{
2014-11-07 04:16:26 +01:00
edgeTexture = Reference.prefix + "tankEdge"
2014-11-04 03:45:29 +01:00
isOpaqueCube = false
normalRender = false
itemBlock = classOf[ItemBlockTank]
2014-11-07 05:27:31 +01:00
fluidNode = new NodePressureGravity(this, 16 * FluidContainerRegistry.BUCKET_VOLUME)
fluidNode.asInstanceOf[NodePressureGravity].maxFlowRate = FluidContainerRegistry.BUCKET_VOLUME
fluidNode.onFluidChanged = () => markUpdate()
2014-11-07 04:16:26 +01:00
override def shouldSideBeRendered(access: IBlockAccess, x: Int, y: Int, z: Int, side: Int): Boolean = new Vector3(x, y, z).getBlock(access) != block
2014-09-07 05:50:03 +02:00
override def use(player: EntityPlayer, side: Int, vector3: Vector3): Boolean =
{
if (!world.isRemote)
{
return FluidUtility.playerActivatedFluidItem(world, xi, yi, zi, player, side)
}
2014-11-06 16:11:08 +01:00
return true
}
2014-09-07 05:50:03 +02:00
override def getLightValue(access: IBlockAccess): Int =
{
2014-11-07 05:27:31 +01:00
if (fluidNode.getPrimaryTank.getFluid != null && fluidNode.getPrimaryTank.getFluid.getFluid != null)
2014-09-07 05:50:03 +02:00
{
2014-11-07 05:27:31 +01:00
return fluidNode.getPrimaryTank.getFluid.getFluid.getLuminosity
}
2014-11-06 16:11:08 +01:00
return super.getLightValue(access)
}
2014-11-07 14:32:38 +01:00
@SideOnly(Side.CLIENT)
override def renderStatic(renderer: RenderBlocks, pos: Vector3, pass: Int): Boolean =
{
RenderBlockUtility.tessellateBlockWithConnectedTextures(clientRenderMask, world, pos.xi, pos.yi, pos.zi, tile.getBlockType, if (faceTexture != null) RenderUtility.getIcon(faceTexture) else null, RenderUtility.getIcon(edgeTexture))
return true
}
2014-11-04 03:45:29 +01:00
@SideOnly(Side.CLIENT)
2014-09-07 05:50:03 +02:00
override def renderDynamic(position: Vector3, frame: Float, pass: Int)
{
2014-11-07 05:27:31 +01:00
renderTankFluid(position.x, position.y, position.z, fluidNode.getPrimaryTank.getFluid)
2014-09-07 05:50:03 +02:00
}
2014-11-04 03:45:29 +01:00
/**
* Renders the fluid inside the tank
*/
@SideOnly(Side.CLIENT)
def renderTankFluid(x: Double, y: Double, z: Double, fluid: FluidStack)
2014-09-07 05:50:03 +02:00
{
if (world != null)
{
2014-11-02 14:18:55 +01:00
GL11.glPushMatrix()
GL11.glTranslated(x + 0.5, y + 0.5, z + 0.5)
2014-09-07 05:50:03 +02:00
if (fluid != null)
{
2014-11-02 14:18:55 +01:00
GL11.glPushMatrix()
2014-09-07 05:50:03 +02:00
if (!fluid.getFluid.isGaseous)
{
GL11.glScaled(0.99, 0.99, 0.99)
2014-11-07 05:27:31 +01:00
val tank: IFluidTank = fluidNode.getPrimaryTank
2014-11-07 04:16:26 +01:00
val percentageFilled: Double = tank.getFluidAmount.toDouble / tank.getCapacity.toDouble
2014-11-08 13:58:31 +01:00
val ySouthEast: Double = FluidUtility.getAveragePercentageFilledForSides(classOf[TileTank], percentageFilled, world, toVector3, ForgeDirection.SOUTH, ForgeDirection.EAST)
val yNorthEast: Double = FluidUtility.getAveragePercentageFilledForSides(classOf[TileTank], percentageFilled, world, toVector3, ForgeDirection.NORTH, ForgeDirection.EAST)
val ySouthWest: Double = FluidUtility.getAveragePercentageFilledForSides(classOf[TileTank], percentageFilled, world, toVector3, ForgeDirection.SOUTH, ForgeDirection.WEST)
val yNorthWest: Double = FluidUtility.getAveragePercentageFilledForSides(classOf[TileTank], percentageFilled, world, toVector3, ForgeDirection.NORTH, ForgeDirection.WEST)
FluidRenderUtility.renderFluidTesselation(tank, ySouthEast, yNorthEast, ySouthWest, yNorthWest)
}
2014-11-02 14:18:55 +01:00
GL11.glPopMatrix()
}
2014-11-02 14:18:55 +01:00
GL11.glPopMatrix()
}
}
2014-11-04 03:45:29 +01:00
@SideOnly(Side.CLIENT)
override def renderInventory(itemStack: ItemStack)
{
super.renderInventory(itemStack)
GL11.glPushMatrix()
if (itemStack.getTagCompound != null && itemStack.getTagCompound.hasKey("fluid"))
{
2014-11-07 04:16:26 +01:00
renderInventoryFluid(0, 0, 0, FluidStack.loadFluidStackFromNBT(itemStack.getTagCompound.getCompoundTag("fluid")), fluidNode.getPrimaryTank.getCapacity)
2014-11-04 03:45:29 +01:00
}
GL11.glPopMatrix()
}
2014-11-07 04:16:26 +01:00
def renderInventoryFluid(x: Double, y: Double, z: Double, fluid: FluidStack, capacity: Int)
2014-11-04 03:45:29 +01:00
{
2014-11-07 04:16:26 +01:00
val tank = new FluidTank(fluid, capacity)
2014-11-04 03:45:29 +01:00
GL11.glPushMatrix()
GL11.glTranslated(0.02, 0.02, 0.02)
GL11.glScaled(0.92, 0.92, 0.92)
if (fluid != null)
{
GL11.glPushMatrix()
if (!fluid.getFluid.isGaseous)
{
2014-11-07 04:16:26 +01:00
val percentageFilled: Double = tank.getFluidAmount.toDouble / tank.getCapacity.toDouble
2014-11-04 03:45:29 +01:00
FluidRenderUtility.renderFluidTesselation(tank, percentageFilled, percentageFilled, percentageFilled, percentageFilled)
}
else
{
2014-11-07 04:16:26 +01:00
val filledPercentage: Double = fluid.amount.toDouble / capacity.toDouble
2014-11-04 03:45:29 +01:00
GL11.glPushAttrib(GL11.GL_ENABLE_BIT)
GL11.glEnable(GL11.GL_CULL_FACE)
GL11.glDisable(GL11.GL_LIGHTING)
GL11.glEnable(GL11.GL_BLEND)
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA)
val color: Color = new Color(fluid.getFluid.getColor)
RenderUtility.enableBlending()
GL11.glColor4d(color.getRed / 255f, color.getGreen / 255f, color.getBlue / 255f, if (fluid.getFluid.isGaseous) filledPercentage else 1)
RenderUtility.bind(FluidRenderUtility.getFluidSheet(fluid))
FluidRenderUtility.renderFluidTesselation(tank, 1, 1, 1, 1)
RenderUtility.disableBlending()
GL11.glPopAttrib()
}
GL11.glPopMatrix()
}
GL11.glPopMatrix()
}
2014-09-07 05:50:03 +02:00
def getRemovedItems(entity: EntityPlayer): List[ItemStack] =
{
2014-11-02 14:18:55 +01:00
val drops = new ArrayList[ItemStack]
val itemStack: ItemStack = new ItemStack(ArchaicContent.blockTank, 1, 0)
2014-09-07 05:50:03 +02:00
if (itemStack != null)
{
2014-11-07 05:27:31 +01:00
if (fluidNode != null && fluidNode.getFluid != null)
2014-09-07 05:50:03 +02:00
{
2014-11-07 05:27:31 +01:00
val stack: FluidStack = fluidNode.getFluid
2014-09-07 05:50:03 +02:00
if (stack != null)
{
if (itemStack.getTagCompound == null)
{
itemStack.setTagCompound(new NBTTagCompound)
}
drain(ForgeDirection.UNKNOWN, stack.amount, false)
itemStack.getTagCompound.setTag("fluid", stack.writeToNBT(new NBTTagCompound))
}
}
drops.add(itemStack)
}
return drops
}
}