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

177 lines
6.4 KiB
Scala
Raw Normal View History

package resonantinduction.archaic.fluid.tank
import java.awt.Color
import java.util.{ArrayList, List}
import net.minecraft.block.material.Material
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.world.IBlockAccess
import net.minecraftforge.client.IItemRenderer
import net.minecraftforge.common.util.ForgeDirection
2014-09-07 05:50:03 +02:00
import net.minecraftforge.fluids.{FluidContainerRegistry, FluidStack, FluidTank, IFluidTank}
import org.lwjgl.opengl.GL11
import resonant.api.IRemovable.ISneakPickup
2014-10-26 15:41:40 +01:00
import resonant.content.prefab.scal.render.ISimpleItemRenderer
import resonant.lib.render.{FluidRenderUtility, RenderUtility}
import resonant.lib.utility.FluidUtility
import resonant.lib.utility.render.RenderBlockUtility
import resonantinduction.archaic.ArchaicContent
import resonantinduction.core.Reference
import resonantinduction.core.prefab.node.TileTankNode
import resonant.lib.transform.vector.Vector3
/**
* Tile/Block class for basic Dynamic tanks
*
* @author Darkguardsman
*/
2014-09-07 05:50:03 +02:00
object TileTank
{
final val VOLUME: Int = 16
2014-09-07 05:50:03 +02:00
class ItemRenderer extends ISimpleItemRenderer
{
def renderInventoryItem(`type`: IItemRenderer.ItemRenderType, itemStack: ItemStack, data: AnyRef*)
{
2014-11-02 14:18:55 +01:00
GL11.glPushMatrix()
RenderBlockUtility.tessellateBlockWithConnectedTextures(itemStack.getItemDamage, ArchaicContent.blockTank, null, RenderUtility.getIcon(Reference.prefix + "tankEdge"))
2014-11-02 14:18:55 +01:00
GL11.glPopMatrix()
GL11.glPushMatrix()
2014-09-07 05:50:03 +02:00
if (itemStack.getTagCompound != null && itemStack.getTagCompound.hasKey("fluid"))
{
renderTank(0, 0, 0, FluidStack.loadFluidStackFromNBT(itemStack.getTagCompound.getCompoundTag("fluid")), VOLUME * FluidContainerRegistry.BUCKET_VOLUME)
}
2014-11-02 14:18:55 +01:00
GL11.glPopMatrix()
2014-09-07 05:50:03 +02:00
}
2014-09-07 05:50:03 +02:00
def renderTank(x: Double, y: Double, z: Double, fluid: FluidStack, capacity: Int)
{
val tank: FluidTank = new FluidTank(fluid, capacity)
2014-11-02 14:18:55 +01:00
GL11.glPushMatrix()
GL11.glTranslated(0.02, 0.02, 0.02)
GL11.glScaled(0.92, 0.92, 0.92)
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)
{
val percentageFilled: Double = tank.getFluidAmount.asInstanceOf[Double] / tank.getCapacity.asInstanceOf[Double]
FluidRenderUtility.renderFluidTesselation(tank, percentageFilled, percentageFilled, percentageFilled, percentageFilled)
}
2014-09-07 05:50:03 +02:00
else
{
val filledPercentage: Double = fluid.amount.asInstanceOf[Double] / capacity.asInstanceOf[Double]
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)
2014-11-02 14:18:55 +01:00
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)
2014-11-02 14:18:55 +01:00
RenderUtility.disableBlending()
GL11.glPopAttrib()
}
2014-11-02 14:18:55 +01:00
GL11.glPopMatrix()
}
2014-11-02 14:18:55 +01:00
GL11.glPopMatrix()
}
2014-09-07 05:50:03 +02:00
}
2014-09-07 05:50:03 +02:00
object ItemRenderer
{
var instance: TileTank.ItemRenderer = new TileTank.ItemRenderer
}
}
2014-09-07 05:50:03 +02:00
class TileTank extends TileTankNode(Material.iron) with ISneakPickup
{
isOpaqueCube(false)
normalRender(false)
forceStandardRender(true)
itemBlock(classOf[ItemBlockTank])
setCapacity(16 * FluidContainerRegistry.BUCKET_VOLUME)
2014-09-07 05:50:03 +02:00
override def shouldSideBeRendered(access: IBlockAccess, x: Int, y: Int, z: Int, side: Int): Boolean =
{
return access.getBlock(x, y, z) ne getBlockType
}
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)
}
return true
}
2014-09-07 05:50:03 +02:00
override def getLightValue(access: IBlockAccess): Int =
{
if (getFluid != null && getFluid.getFluid != null)
2014-09-07 05:50:03 +02:00
{
return getFluid.getFluid.getLuminosity
}
return super.getLightValue(access)
}
2014-09-07 05:50:03 +02:00
override def renderDynamic(position: Vector3, frame: Float, pass: Int)
{
renderTank(position.x, position.y, position.z, getFluid)
}
def renderTank(x: Double, y: Double, z: Double, fluid: FluidStack)
{
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)
val tank: IFluidTank = getTank
val percentageFilled: Double = tank.getFluidAmount.asInstanceOf[Double] / tank.getCapacity.asInstanceOf[Double]
val ySouthEast: Double = FluidUtility.getAveragePercentageFilledForSides(classOf[TileTank], percentageFilled, world, asVector3, ForgeDirection.SOUTH, ForgeDirection.EAST)
val yNorthEast: Double = FluidUtility.getAveragePercentageFilledForSides(classOf[TileTank], percentageFilled, world, asVector3, ForgeDirection.NORTH, ForgeDirection.EAST)
val ySouthWest: Double = FluidUtility.getAveragePercentageFilledForSides(classOf[TileTank], percentageFilled, world, asVector3, ForgeDirection.SOUTH, ForgeDirection.WEST)
val yNorthWest: Double = FluidUtility.getAveragePercentageFilledForSides(classOf[TileTank], percentageFilled, world, asVector3, 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-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)
{
if (getTank != null && getTank.getFluid != null)
{
val stack: FluidStack = getTank.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
}
}