Updated PacketMultiPart
This commit is contained in:
parent
ab24fa1fa1
commit
8fb8062ae6
2 changed files with 84 additions and 64 deletions
|
@ -1,64 +0,0 @@
|
|||
package resonantinduction.core.prefab.part;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import resonant.lib.network.IPacketReceiver;
|
||||
import resonant.lib.network.PacketType;
|
||||
import universalelectricity.core.transform.vector.Vector3;
|
||||
import codechicken.multipart.TMultiPart;
|
||||
import codechicken.multipart.TileMultipart;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
/**
|
||||
* Packet handler for blocks and tile entities.
|
||||
*
|
||||
* @author Calclavia
|
||||
*/
|
||||
public class PacketMultiPart extends PacketType
|
||||
{
|
||||
public PacketMultiPart(String channel)
|
||||
{
|
||||
super(channel);
|
||||
}
|
||||
|
||||
public Packet getPacket(Vector3 position, int partID, Object... args)
|
||||
{
|
||||
List newArgs = new ArrayList();
|
||||
|
||||
newArgs.add(position.intX());
|
||||
newArgs.add(position.intY());
|
||||
newArgs.add(position.intZ());
|
||||
newArgs.add(partID);
|
||||
|
||||
for (Object obj : args)
|
||||
{
|
||||
newArgs.add(obj);
|
||||
}
|
||||
|
||||
return super.getPacket(newArgs.toArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivePacket(ByteArrayDataInput data, EntityPlayer player)
|
||||
{
|
||||
int x = data.readInt();
|
||||
int y = data.readInt();
|
||||
int z = data.readInt();
|
||||
TileEntity tileEntity = player.worldObj.getBlockTileEntity(x, y, z);
|
||||
|
||||
if (tileEntity instanceof TileMultipart)
|
||||
{
|
||||
TMultiPart part = ((TileMultipart) tileEntity).partMap(data.readInt());
|
||||
|
||||
if (part instanceof IPacketReceiver)
|
||||
{
|
||||
((IPacketReceiver) part).onReceivePacket(data, player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package resonantinduction.core.prefab.part
|
||||
|
||||
import codechicken.multipart.{TMultiPart, TileMultipart}
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import net.minecraft.tileentity.TileEntity
|
||||
import resonant.lib.network.discriminator.PacketType
|
||||
import resonant.lib.network.handle.TPacketReceiver
|
||||
import universalelectricity.core.transform.vector.Vector3
|
||||
|
||||
/**
|
||||
* Packet handler for blocks and tile entities.
|
||||
*
|
||||
* @author Calclavia
|
||||
*/
|
||||
class PacketMultiPart extends PacketType
|
||||
{
|
||||
var x: Int
|
||||
var y: Int
|
||||
var z: Int
|
||||
var partID: Int
|
||||
|
||||
def this(part: TMultiPart, partID: Int)
|
||||
{
|
||||
this()
|
||||
this.x = part.x
|
||||
this.y = part.y
|
||||
this.z = part.z
|
||||
this.partID = partID
|
||||
|
||||
this << x
|
||||
this << y
|
||||
this << z
|
||||
this << partID
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
val tile: TileEntity = player.getEntityWorld.getTileEntity(this.x, this.y, this.z)
|
||||
|
||||
if (tileEntity.isInstanceOf[TileMultipart])
|
||||
{
|
||||
val part = (tileEntity.asInstanceOf[TileMultipart]).partMap(data.readInt)
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue