Updated TileMechanical packet system

This commit is contained in:
Calclavia 2014-11-09 00:41:34 +08:00
parent d73b2ff520
commit c0ac84bc22
2 changed files with 39 additions and 45 deletions

View file

@ -34,21 +34,10 @@ class TilePump extends TileMechanical(Material.iron) with IRotatable with IFluid
setTextureName("material_steel")
pressureNode = new PumpNode(this)
override def start
override def update()
{
pressureNode.reconstruct
super.start
}
super.update()
override def invalidate
{
super.invalidate
pressureNode.deconstruct
}
override def update
{
super.update
if (!worldObj.isRemote && mechanicalNode.getPower > 0)
{
val tileIn: TileEntity = toVector3.add(getDirection.getOpposite).getTileEntity(this.worldObj)
@ -66,7 +55,7 @@ class TilePump extends TileMechanical(Material.iron) with IRotatable with IFluid
override def renderDynamic(pos: Vector3, frame: Float, pass: Int): Unit =
{
GL11.glPushMatrix()
GL11.glTranslated(x + 0.5, y + 0.5, z + 0.5)
GL11.glTranslated(pos.x + 0.5, pos.y + 0.5, pos.z + 0.5)
GL11.glRotatef(-90, 0, 1, 0)
if (tile.getWorldObj != null) RenderUtility.rotateBlockBasedOnDirection(getDirection)
RenderUtility.bind(TilePump.texture)

View file

@ -9,46 +9,46 @@ import net.minecraft.init.Items
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.network.Packet
import net.minecraftforge.common.util.ForgeDirection
import resonant.api.grid.{INode, INodeProvider}
import resonant.content.prefab.java.TileNode
import resonant.engine.ResonantEngine
import resonant.lib.grid.node.TSpatialNodeProvider
import resonant.lib.network.discriminator.{PacketTile, PacketType}
import resonant.lib.network.handle.IPacketIDReceiver
import resonant.lib.network.handle.{TPacketSender, TPacketReceiver, IPacketReceiver, IPacketIDReceiver}
import resonant.lib.transform.vector.Vector3
import resonant.lib.network.ByteBufWrapper._
/** Prefab for resonantinduction.mechanical tiles
*
* @author Calclavia */
abstract class TileMechanical(material: Material) extends TileNode(material) with INodeProvider with IPacketIDReceiver
abstract class TileMechanical(material: Material) extends TileNode(material) with TSpatialNodeProvider with TPacketSender with TPacketReceiver
{
/** Internal packet ID for NBTTagCompound parsing from packets */
protected var nbt_packet_id: Int = 0
/** Internal packet ID for rotation and velocity */
protected var vel_packet_id: Int = 1
/** Node that handles most mechanical actions */
var mechanicalNode: MechanicalNode = new MechanicalNode(this)
var mechanicalNode = new MechanicalNode(this)
/** External debug GUI */
var frame: DebugFrameMechanical = null
override def update
nodes.add(mechanicalNode)
override def update()
{
super.update
super.update()
if (frame != null)
{
frame.update
frame.update()
if (!frame.isVisible)
{
frame.dispose
frame.dispose()
frame = null
}
}
if (!this.getWorldObj.isRemote)
if (!world.isRemote)
{
if (ticks % 3 == 0 && (mechanicalNode.markTorqueUpdate || mechanicalNode.markRotationUpdate))
{
sendRotationPacket
sendRotationPacket()
mechanicalNode.markRotationUpdate = false
mechanicalNode.markTorqueUpdate = false
}
@ -58,11 +58,12 @@ abstract class TileMechanical(material: Material) extends TileNode(material) wit
override def use(player: EntityPlayer, side: Int, hit: Vector3): Boolean =
{
val itemStack: ItemStack = player.getHeldItem
if (ResonantEngine.runningAsDev)
{
if (itemStack != null && !world.isRemote)
{
if (itemStack.getItem eq Items.stick)
if (itemStack.getItem == Items.stick)
{
if (ControlKeyModifer.isControlDown(player))
{
@ -83,14 +84,6 @@ abstract class TileMechanical(material: Material) extends TileNode(material) wit
return false
}
override def getNode[N <: INode](nodeType: Class[_ <: N], from: ForgeDirection): N =
{
if (classOf[MechanicalNode].isAssignableFrom(nodeType))
return mechanicalNode.asInstanceOf[N]
return null.asInstanceOf[N]
}
override def getDescriptionPacket: Packet =
{
val tag: NBTTagCompound = new NBTTagCompound
@ -99,24 +92,36 @@ abstract class TileMechanical(material: Material) extends TileNode(material) wit
}
/** Sends the torque and angular velocity to the client */
private def sendRotationPacket
private def sendRotationPacket()
{
ResonantEngine.instance.packetHandler.sendToAllAround(new PacketTile(xi, yi, zi, Array(vel_packet_id, mechanicalNode.angularVelocity, mechanicalNode.torque)), this)
}
override def read(data: ByteBuf, id: Int, player: EntityPlayer, `type`: PacketType): Boolean =
override def write(buf: ByteBuf, id: Int): Unit =
{
super.write(buf, id)
id match
{
case 0 =>
case 1 => buf <<< mechanicalNode.angularVelocity <<< mechanicalNode.torque
}
}
override def read(buf: ByteBuf, id: Int, packetType: PacketType)
{
super.read(buf,id,packetType)
if (world.isRemote)
{
if (id == nbt_packet_id)
{
readFromNBT(ByteBufUtils.readTag(data))
readFromNBT(ByteBufUtils.readTag(buf))
return true
}
else if (id == vel_packet_id)
{
mechanicalNode.angularVelocity = data.readDouble
mechanicalNode.torque = data.readDouble
mechanicalNode.angularVelocity = buf.readDouble
mechanicalNode.torque = buf.readDouble
return true
}
}