electrodynamics/src/main/scala/edx/core/prefab/part/PacketMultiPart.scala

83 lines
1.9 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.core.prefab.part
2014-07-15 00:08:28 +02:00
import codechicken.multipart.{TMultiPart, TileMultipart}
import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import net.minecraft.entity.player.EntityPlayer
2015-01-26 12:40:32 +01:00
import resonantengine.lib.network.discriminator.PacketType
import resonantengine.lib.network.handle.TPacketReceiver
import resonantengine.lib.transform.vector.Vector3
2014-07-15 00:08:28 +02:00
/**
* Packet handler for blocks and tile entities.
*
* @author Calclavia
*/
class PacketMultiPart extends PacketType
{
2014-07-21 15:15:22 +02:00
var x: Int = 0
var y: Int = 0
var z: Int = 0
var partID: Int = 0
2014-07-15 00:08:28 +02:00
def this(part: TMultiPart, partID: Int)
{
this()
this.x = part.x
this.y = part.y
this.z = part.z
this.partID = partID
2014-07-17 02:07:57 +02:00
this <<< x
this <<< y
this <<< z
this <<< partID
2014-07-15 00:08:28 +02:00
}
def encodeInto(ctx: ChannelHandlerContext, buffer: ByteBuf)
{
buffer.writeInt(x)
buffer.writeInt(y)
buffer.writeInt(z)
buffer.writeInt(partID)
buffer.writeBytes(data)
}
def decodeInto(ctx: ChannelHandlerContext, buffer: ByteBuf)
{
x = buffer.readInt
y = buffer.readInt
z = buffer.readInt
partID = buffer.readInt
data_$eq(buffer.slice)
}
override def handleClientSide(player: EntityPlayer)
{
handle(player)
}
override def handleServerSide(player: EntityPlayer)
{
handle(player)
}
def handle(player: EntityPlayer)
{
2014-07-17 02:07:57 +02:00
val tile = player.getEntityWorld.getTileEntity(this.x, this.y, this.z)
2014-07-15 00:08:28 +02:00
2014-07-17 02:07:57 +02:00
if (tile.isInstanceOf[TileMultipart])
2014-07-15 00:08:28 +02:00
{
2014-07-17 02:07:57 +02:00
val part = tile.asInstanceOf[TileMultipart].partMap(data.readInt)
2014-07-15 00:08:28 +02:00
if (part.isInstanceOf[TPacketReceiver])
{
part.asInstanceOf[TPacketReceiver].read(data.slice, player, this)
}
}
else
{
throw new UnsupportedOperationException("Packet was sent to a multipart not implementing IPacketReceiver, this is a coding error [" + tile + "] in " + new Vector3(x, y, z))
}
}
}