electrodynamics/src/main/scala/edx/basic/process/mixing/TileGlassJar.scala

231 lines
6.7 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.basic.process.mixing
2015-01-10 10:43:59 +01:00
import java.awt.Color
2015-01-13 05:27:14 +01:00
import java.util.ArrayList
2015-01-10 10:43:59 +01:00
import cpw.mods.fml.relauncher.{Side, SideOnly}
2015-01-14 12:06:03 +01:00
import edx.basic.BasicContent
import edx.core.Reference
import edx.core.resource.alloy.{Alloy, AlloyUtility}
import edx.core.resource.content.{ItemDust, ItemRefinedDust}
2015-01-10 10:43:59 +01:00
import io.netty.buffer.ByteBuf
2015-01-13 05:27:14 +01:00
import net.minecraft.block.Block
2015-01-10 10:43:59 +01:00
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.util.ResourceLocation
2015-01-13 09:43:24 +01:00
import net.minecraftforge.client.IItemRenderer.ItemRenderType
2015-01-10 10:43:59 +01:00
import net.minecraftforge.client.model.AdvancedModelLoader
import org.lwjgl.opengl.GL11
2015-01-26 13:17:04 +01:00
import resonantengine.api.item.ISimpleItemRenderer
2015-01-26 13:28:38 +01:00
import resonantengine.core.network.discriminator.PacketType
2015-01-26 12:40:32 +01:00
import resonantengine.lib.factory.resources.ResourceFactory
import resonantengine.lib.factory.resources.item.TItemResource
2015-01-26 13:28:38 +01:00
import resonantengine.lib.modcontent.block.ResonantTile
2015-01-26 12:40:32 +01:00
import resonantengine.lib.render.RenderUtility
import resonantengine.lib.render.model.ModelCube
import resonantengine.lib.transform.region.Cuboid
import resonantengine.lib.transform.vector.{Vector3, VectorWorld}
import resonantengine.lib.utility.inventory.InventoryUtility
import resonantengine.lib.utility.nbt.NBTUtility
import resonantengine.lib.wrapper.ByteBufWrapper._
2015-01-26 13:28:38 +01:00
import resonantengine.prefab.block.itemblock.ItemBlockSaved
import resonantengine.prefab.network.{TPacketReceiver, TPacketSender}
2015-01-10 10:43:59 +01:00
/**
* A glass jar for mixing different dusts/refined together.
* @author Calclavia
*/
object TileGlassJar
{
val model = AdvancedModelLoader.loadModel(new ResourceLocation(Reference.domain, Reference.modelPath + "glassJar.tcn"))
val dustMaterialTexture = new ResourceLocation(Reference.domain, Reference.blockTextureDirectory + "material_sand.png")
}
2015-01-26 11:17:24 +01:00
class TileGlassJar extends ResonantTile(Material.wood) with TPacketReceiver with TPacketSender with ISimpleItemRenderer
2015-01-10 10:43:59 +01:00
{
2015-01-12 16:01:41 +01:00
var alloy = new Alloy(8)
2015-01-12 05:03:44 +01:00
var mixed = false
2015-01-10 10:43:59 +01:00
setTextureName("glass")
bounds = new Cuboid(0.2, 0, 0.2, 0.8, 1, 0.8)
normalRender = false
isOpaqueCube = false
2015-01-12 03:24:51 +01:00
itemBlock = classOf[ItemGlassJar]
2015-01-10 10:43:59 +01:00
2015-01-14 12:19:04 +01:00
override def canUpdate: Boolean = false
2015-01-10 10:43:59 +01:00
/**
* Override this method
* Be sure to super this method or manually write the id into the packet when sending
2015-01-10 10:43:59 +01:00
*/
override def write(buf: ByteBuf, id: Int)
{
super.write(buf, id)
buf <<<< writeToNBT
}
override def writeToNBT(nbt: NBTTagCompound)
{
super.writeToNBT(nbt)
2015-01-12 05:03:44 +01:00
nbt.setBoolean("mixed", mixed)
2015-01-12 16:01:41 +01:00
alloy.save(nbt)
2015-01-10 10:43:59 +01:00
}
override def read(buf: ByteBuf, id: Int, packetType: PacketType)
{
super.read(buf, id, packetType)
buf >>>> readFromNBT
}
override def readFromNBT(nbt: NBTTagCompound)
{
super.readFromNBT(nbt)
2015-01-12 05:03:44 +01:00
mixed = nbt.getBoolean("mixed")
2015-01-12 16:01:41 +01:00
alloy.load(nbt)
2015-01-10 10:43:59 +01:00
}
2015-01-13 05:27:14 +01:00
override def getDrops(metadata: Int, fortune: Int): ArrayList[ItemStack] = new ArrayList[ItemStack]
override def onRemove(block: Block, par6: Int)
{
2021-04-05 14:41:30 +02:00
val stack: ItemStack = ItemBlockSaved.getItemStackWithNBT(block, world, x.toInt, y.toInt, z.toInt)
2015-01-13 05:27:14 +01:00
InventoryUtility.dropItemStack(world, center, stack)
}
2015-01-13 09:43:24 +01:00
override def renderInventoryItem(`type`: ItemRenderType, itemStack: ItemStack, data: AnyRef*): Unit =
{
renderInventory(itemStack)
}
2015-01-10 10:43:59 +01:00
@SideOnly(Side.CLIENT)
override def renderInventory(itemStack: ItemStack): Unit =
{
GL11.glPushMatrix()
renderMixture(itemStack)
GL11.glPopMatrix()
GL11.glPushMatrix()
GL11.glTranslated(0, 0.3, 0)
renderJar()
GL11.glPopMatrix()
}
def renderMixture(itemStack: ItemStack = null)
{
2015-01-12 16:01:41 +01:00
val alloy: Alloy =
2015-01-10 10:43:59 +01:00
if (itemStack != null)
2015-01-13 09:43:24 +01:00
new Alloy(NBTUtility.getNBTTagCompound(itemStack))
2015-01-10 10:43:59 +01:00
else
2015-01-12 16:01:41 +01:00
this.alloy
2015-01-10 10:43:59 +01:00
2015-01-14 05:39:00 +01:00
val mixed =
if (itemStack != null)
NBTUtility.getNBTTagCompound(itemStack).getBoolean("mixed")
else
this.mixed
2015-01-12 16:01:41 +01:00
if (alloy != null && alloy.size > 0)
2015-01-10 10:43:59 +01:00
{
GL11.glPushMatrix()
RenderUtility.bind(TileGlassJar.dustMaterialTexture)
2015-01-14 05:39:00 +01:00
if (mixed)
{
val color = new Color(alloy.color).darker.darker.darker
GL11.glTranslated(0, -0.5 + 0.75f / 2 * alloy.percentage, 0)
GL11.glScalef(0.4f, 0.75f * alloy.percentage, 0.4f)
GL11.glColor4f(color.getRed / 255f, color.getGreen / 255f, color.getBlue / 255f, 1)
ModelCube.INSTNACE.render()
}
else
{
val size = alloy.size
val height = 0.72f / 8f
//Translate to bottom of the jar
GL11.glTranslatef(0, -0.45f, 0)
alloy.content.foreach
{
case (material, materialCount) =>
{
val color = new Color(ResourceFactory.getColor(material)).darker.darker.darker
GL11.glPushMatrix()
GL11.glTranslated(0, (height * materialCount) / 2, 0)
GL11.glScalef(0.4f, height * materialCount, 0.4f)
GL11.glColor4f(color.getRed / 255f, color.getGreen / 255f, color.getBlue / 255f, 1)
ModelCube.INSTNACE.render()
GL11.glPopMatrix()
//Translate each section of the jar upwards
GL11.glTranslatef(0, height * materialCount, 0)
}
}
}
2015-01-10 10:43:59 +01:00
GL11.glPopMatrix()
}
}
def renderJar()
{
RenderUtility.enableBlending()
GL11.glScalef(1.6f, 1.6f, 1.6f)
GL11.glColor4f(1, 1, 1, 1)
RenderUtility.bind(Reference.domain, Reference.modelPath + "glassJar.png")
TileGlassJar.model.renderAll()
RenderUtility.disableBlending()
}
2015-01-27 09:15:44 +01:00
@SideOnly(Side.CLIENT)
override def renderDynamic(pos: Vector3, frame: Float, pass: Int)
{
GL11.glPushMatrix()
GL11.glTranslated(pos.x + 0.5, pos.y + 0.5, pos.z + 0.5)
renderMixture()
GL11.glPopMatrix()
GL11.glPushMatrix()
GL11.glTranslated(pos.x + 0.5, pos.y + 0.8, pos.z + 0.5)
renderJar()
GL11.glPopMatrix()
}
@SideOnly(Side.CLIENT)
override protected def getTextureName: String = textureName
2015-01-10 10:43:59 +01:00
override protected def use(player: EntityPlayer, side: Int, hit: Vector3): Boolean =
{
2015-01-13 15:53:38 +01:00
if (!world.isRemote)
2015-01-10 10:43:59 +01:00
{
2015-01-13 15:53:38 +01:00
if (player.getCurrentEquippedItem != null)
2015-01-10 10:43:59 +01:00
{
2015-01-13 15:53:38 +01:00
val item = player.getCurrentEquippedItem.getItem
if (item.isInstanceOf[ItemDust] || item.isInstanceOf[ItemRefinedDust])
2015-01-10 10:43:59 +01:00
{
2015-01-13 15:53:38 +01:00
if (alloy.mix(item.asInstanceOf[TItemResource].material))
{
player.getCurrentEquippedItem.splitStack(1)
2015-01-14 05:39:00 +01:00
sendDescPacket()
2015-01-13 15:53:38 +01:00
return true
}
2015-01-10 10:43:59 +01:00
}
}
2015-01-13 15:53:38 +01:00
else if (mixed)
{
//Eject dust
2015-01-14 12:06:03 +01:00
InventoryUtility.dropItemStack(new VectorWorld(player), AlloyUtility.setAlloy(new ItemStack(BasicContent.itemAlloyDust, alloy.size), alloy))
2015-01-13 15:53:38 +01:00
alloy = new Alloy(8)
mixed = false
sendDescPacket()
}
2015-01-10 10:43:59 +01:00
}
return true
}
}