electrodynamics/src/main/scala/edx/quantum/machine/quantum/TileQuantumAssembler.scala

223 lines
5.5 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.quantum.machine.quantum
2014-09-29 21:08:43 +02:00
2015-01-14 12:06:03 +01:00
import edx.core.Reference
import edx.quantum.QuantumContent
2014-09-29 21:08:43 +02:00
import io.netty.buffer.ByteBuf
import net.minecraft.block.material.Material
import net.minecraft.entity.item.EntityItem
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.{Item, ItemStack}
import net.minecraft.nbt.NBTTagCompound
2015-01-26 13:17:04 +01:00
import resonantengine.api.edx.recipe.QuantumAssemblerRecipes
import resonantengine.api.network.IPacketReceiver
2015-01-26 13:28:38 +01:00
import resonantengine.core.network.discriminator.{PacketTile, PacketType}
2015-01-26 12:40:32 +01:00
import resonantengine.lib.content.prefab.TInventory
import resonantengine.lib.grid.energy.EnergyStorage
import resonantengine.lib.grid.energy.electric.NodeElectricComponent
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.transform.vector.Vector3
import resonantengine.prefab.block.impl.{TBlockNodeProvider, TEnergyProvider}
2014-09-29 21:08:43 +02:00
/**
* Atomic assembler of items *
*
* @author Calclavia, Darkguardsman
*/
2015-01-26 11:17:24 +01:00
class TileQuantumAssembler extends ResonantTile(Material.iron) with TInventory with TBlockNodeProvider with IPacketReceiver with TEnergyProvider
2014-09-29 21:08:43 +02:00
{
2015-01-26 11:13:56 +01:00
private val electricNode = new NodeElectricComponent(this)
2015-01-21 04:13:51 +01:00
private[quantum] var power: Long = 1000000000L
2014-11-02 13:56:11 +01:00
private[quantum] var MAX_TIME: Int = 20 * 120
private[quantum] var time: Int = 0
/**
* Used for rendering arm motion, X Y Z are not used as location data
*/
private[quantum] var rotation: Vector3 = new Vector3
/**
* Used for rendering.
*/
private[quantum] var entityItem: EntityItem = null
//Constructor
2014-09-29 21:08:43 +02:00
2014-11-02 13:56:11 +01:00
//TODO: Dummy
2015-01-21 04:13:51 +01:00
energy = new EnergyStorage
energy.max = power
2015-01-17 06:13:26 +01:00
isOpaqueCube = false
normalRender = false
customItemRender = true
textureName = "machine"
2015-01-26 11:13:56 +01:00
nodes.add(electricNode)
2015-01-17 06:13:26 +01:00
override def getSizeInventory: Int = 7
2014-09-29 21:08:43 +02:00
2014-11-02 13:56:11 +01:00
/**
* Called when the block is right clicked by the player
*/
override def use(player: EntityPlayer, side: Int, hit: Vector3): Boolean =
{
if (!world.isRemote)
2014-09-29 21:08:43 +02:00
{
2015-01-14 12:06:03 +01:00
player.openGui(QuantumContent, 0, world, xi, yi, zi)
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
return true
}
2014-09-29 21:08:43 +02:00
2014-11-02 13:56:11 +01:00
override def update
{
super.update
if (!this.worldObj.isRemote)
2014-09-29 21:08:43 +02:00
{
2014-11-02 13:56:11 +01:00
if (this.canProcess)
{
2015-01-21 04:13:51 +01:00
if (energy >= power / 20)
2014-09-29 21:08:43 +02:00
{
2014-11-02 13:56:11 +01:00
if (this.time == 0)
{
this.time = this.MAX_TIME
}
if (this.time > 0)
{
this.time -= 1
if (this.time < 1)
2014-09-29 21:08:43 +02:00
{
2014-11-02 13:56:11 +01:00
this.process
this.time = 0
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
}
else
{
this.time = 0
}
2015-01-21 04:13:51 +01:00
energy -= power / 20
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
}
else
{
this.time = 0
}
if (this.ticks % 10 == 0)
{
//TODO send packets to each player with the GUI open
}
}
else if (this.time > 0)
{
var middleStack: ItemStack = this.getStackInSlot(6)
if (middleStack != null)
{
middleStack = middleStack.copy
middleStack.stackSize = 1
if (this.entityItem == null)
2014-09-29 21:08:43 +02:00
{
2014-11-02 13:56:11 +01:00
this.entityItem = new EntityItem(this.worldObj, 0, 0, 0, middleStack)
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
else if (!middleStack.isItemEqual(this.entityItem.getEntityItem))
{
this.entityItem = new EntityItem(this.worldObj, 0, 0, 0, middleStack)
}
this.entityItem.age += 1
}
else
{
this.entityItem = null
}
if (this.ticks % 600 == 0)
{
this.worldObj.playSoundEffect(this.xCoord, this.yCoord, this.zCoord, Reference.prefix + "assembler", 0.7f, 1f)
}
this.rotation.add(3, 2, 1)
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
}
2014-09-29 21:08:43 +02:00
2015-01-14 12:06:03 +01:00
/**
* Turn one item from the furnace source stack into the appropriate smelted item in the furnace
* result stack
*/
def process
2014-11-02 13:56:11 +01:00
{
2015-01-14 12:06:03 +01:00
if (this.canProcess)
2014-09-29 21:08:43 +02:00
{
2015-01-14 12:06:03 +01:00
for (i <- 0 to 6)
{
if (getStackInSlot(i) != null)
{
decrStackSize(i, 1)
}
}
if (getStackInSlot(6) != null)
{
getStackInSlot(6).stackSize += 1
}
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
}
2014-09-29 21:08:43 +02:00
2014-11-02 13:56:11 +01:00
/**
* Checks to see if the assembler can run
*/
def canProcess: Boolean =
{
if (getStackInSlot(6) != null)
2014-09-29 21:08:43 +02:00
{
2014-11-02 13:56:11 +01:00
if (QuantumAssemblerRecipes.hasItemStack(getStackInSlot(6)))
{
for (i <- 0 to 6)
2014-09-29 21:08:43 +02:00
{
2014-11-02 13:56:11 +01:00
if (getStackInSlot(i) == null)
{
return false
}
2015-01-14 12:06:03 +01:00
if (getStackInSlot(i).getItem ne QuantumContent.itemDarkMatter)
2014-11-02 13:56:11 +01:00
{
return false
}
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
return getStackInSlot(6).stackSize < 64
}
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
return false
}
2014-09-29 21:08:43 +02:00
2015-01-14 12:06:03 +01:00
def read(data: ByteBuf, player: EntityPlayer, `type`: PacketType)
2014-11-02 13:56:11 +01:00
{
2015-01-14 12:06:03 +01:00
this.time = data.readInt
val itemID: Int = data.readInt
val itemAmount: Int = data.readInt
val itemMeta: Int = data.readInt
if (itemID != -1 && itemAmount != -1 && itemMeta != -1)
2014-09-29 21:08:43 +02:00
{
2015-01-14 12:06:03 +01:00
this.setInventorySlotContents(6, new ItemStack(Item.getItemById(itemID), itemAmount, itemMeta))
2014-09-29 21:08:43 +02:00
}
2014-11-02 13:56:11 +01:00
}
2014-09-29 21:08:43 +02:00
2015-01-14 12:06:03 +01:00
override def getDescPacket: PacketTile =
{
if (this.getStackInSlot(6) != null)
{
return new PacketTile(xi, yi, zi, Array[Any](time, getStackInSlot(6)))
}
return new PacketTile(xi, yi, zi, Array[Any](time, -1, -1, -1))
}
2014-11-02 13:56:11 +01:00
override def readFromNBT(nbt: NBTTagCompound)
{
super.readFromNBT(nbt)
this.time = nbt.getInteger("smeltingTicks")
}
2014-09-29 21:08:43 +02:00
2014-11-02 13:56:11 +01:00
override def writeToNBT(nbt: NBTTagCompound)
{
super.writeToNBT(nbt)
nbt.setInteger("smeltingTicks", this.time)
}
2014-09-29 21:08:43 +02:00
2014-11-02 13:56:11 +01:00
override def isItemValidForSlot(slotID: Int, itemStack: ItemStack): Boolean =
{
if (slotID == 6)
2014-09-29 21:08:43 +02:00
{
2014-11-02 13:56:11 +01:00
return true
2014-09-29 21:08:43 +02:00
}
2015-01-14 12:06:03 +01:00
return itemStack.getItem eq QuantumContent.itemDarkMatter
2014-11-02 13:56:11 +01:00
}
2014-09-29 21:08:43 +02:00
}