2013-06-13 23:37:30 +02:00
|
|
|
package mekanism.common.network;
|
|
|
|
|
2014-06-02 16:52:13 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
2013-06-15 00:25:09 +02:00
|
|
|
import java.util.ArrayList;
|
2013-06-13 23:37:30 +02:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
import mekanism.api.Coord4D;
|
2014-06-02 16:52:13 +02:00
|
|
|
import mekanism.common.PacketHandler;
|
2014-08-10 04:20:49 +02:00
|
|
|
import mekanism.common.base.ITileNetwork;
|
2014-06-11 11:57:49 +02:00
|
|
|
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
|
2013-06-13 23:37:30 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-06-11 11:57:49 +02:00
|
|
|
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
|
|
|
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
|
|
|
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
2013-06-13 23:37:30 +02:00
|
|
|
|
2014-06-11 11:57:49 +02:00
|
|
|
public class PacketTileEntity implements IMessageHandler<TileEntityMessage, IMessage>
|
2013-06-13 23:37:30 +02:00
|
|
|
{
|
2014-04-20 22:15:44 +02:00
|
|
|
@Override
|
2014-06-11 11:57:49 +02:00
|
|
|
public IMessage onMessage(TileEntityMessage message, MessageContext context)
|
2014-04-20 22:15:44 +02:00
|
|
|
{
|
2014-06-11 11:57:49 +02:00
|
|
|
TileEntity tileEntity = message.coord4D.getTileEntity(PacketHandler.getPlayer(context).worldObj);
|
2014-06-02 16:52:13 +02:00
|
|
|
|
|
|
|
if(tileEntity instanceof ITileNetwork)
|
|
|
|
{
|
2014-06-11 11:57:49 +02:00
|
|
|
try {
|
|
|
|
((ITileNetwork)tileEntity).handlePacketData(message.storedBuffer);
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
message.storedBuffer.release();
|
2014-06-02 16:52:13 +02:00
|
|
|
}
|
2014-06-11 11:57:49 +02:00
|
|
|
|
|
|
|
return null;
|
2014-04-20 22:15:44 +02:00
|
|
|
}
|
2014-06-11 11:57:49 +02:00
|
|
|
|
|
|
|
public static class TileEntityMessage implements IMessage
|
2014-04-20 22:15:44 +02:00
|
|
|
{
|
2014-06-11 11:57:49 +02:00
|
|
|
public Coord4D coord4D;
|
|
|
|
|
|
|
|
public ArrayList parameters;
|
2014-06-02 16:52:13 +02:00
|
|
|
|
2014-06-11 11:57:49 +02:00
|
|
|
public ByteBuf storedBuffer = null;
|
|
|
|
|
|
|
|
public TileEntityMessage() {}
|
|
|
|
|
|
|
|
public TileEntityMessage(Coord4D coord, ArrayList params)
|
|
|
|
{
|
|
|
|
coord4D = coord;
|
|
|
|
parameters = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void toBytes(ByteBuf dataStream)
|
|
|
|
{
|
|
|
|
dataStream.writeInt(coord4D.xCoord);
|
|
|
|
dataStream.writeInt(coord4D.yCoord);
|
|
|
|
dataStream.writeInt(coord4D.zCoord);
|
|
|
|
dataStream.writeInt(coord4D.dimensionId);
|
|
|
|
|
|
|
|
PacketHandler.encode(new Object[] {parameters}, dataStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fromBytes(ByteBuf dataStream)
|
2014-06-02 16:52:13 +02:00
|
|
|
{
|
2014-06-11 11:57:49 +02:00
|
|
|
coord4D = new Coord4D(dataStream.readInt(), dataStream.readInt(), dataStream.readInt(), dataStream.readInt());
|
|
|
|
|
|
|
|
storedBuffer = dataStream.copy();
|
2014-06-02 16:52:13 +02:00
|
|
|
}
|
2014-04-20 22:15:44 +02:00
|
|
|
}
|
2013-06-13 23:37:30 +02:00
|
|
|
}
|