2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.common;
|
2012-08-15 22:41:41 +02:00
|
|
|
|
2014-06-02 16:52:13 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-10-14 03:48:29 +02:00
|
|
|
|
2014-06-02 16:52:13 +02:00
|
|
|
import java.util.ArrayList;
|
2012-09-03 18:11:25 +02:00
|
|
|
|
2012-08-15 22:41:41 +02:00
|
|
|
|
2012-09-03 18:11:25 +02:00
|
|
|
/**
|
2012-11-05 20:29:04 +01:00
|
|
|
* Mekanism packet handler. As always, use packets sparingly!
|
2012-09-03 18:11:25 +02:00
|
|
|
* @author AidanBrady
|
|
|
|
*
|
|
|
|
*/
|
2014-04-20 04:44:06 +02:00
|
|
|
public class PacketHandler //implements IPacketHandler
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
2014-04-20 04:44:06 +02:00
|
|
|
/*
|
|
|
|
/** The ArrayList of registered packet classes, who's index tells which packet is which. *
|
2013-06-13 23:37:30 +02:00
|
|
|
public static List<Class<? extends IMekanismPacket>> packets = new ArrayList<Class<? extends IMekanismPacket>>();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-10-25 23:55:32 +02:00
|
|
|
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
2012-09-03 18:11:25 +02:00
|
|
|
ByteArrayDataInput dataStream = ByteStreams.newDataInput(packet.data);
|
2014-03-08 02:00:25 +01:00
|
|
|
EntityPlayer entityplayer = (EntityPlayer)player;
|
|
|
|
|
2013-06-14 03:45:29 +02:00
|
|
|
if(packet.channel.equals("MEK"))
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
|
|
|
try {
|
2013-06-13 23:37:30 +02:00
|
|
|
int packetIndex = dataStream.readInt();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-13 23:37:30 +02:00
|
|
|
if(packets.get(packetIndex) == null)
|
|
|
|
{
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.error("Received unknown packet identifier '" + packetIndex + ".' Ignorning!");
|
2013-06-13 23:37:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-13 23:37:30 +02:00
|
|
|
IMekanismPacket packetType = packets.get(packetIndex).newInstance();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-13 23:37:30 +02:00
|
|
|
if(packetType == null)
|
|
|
|
{
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.error("Unable to create instance of packet type '" + packetIndex + ".' Ignoring!");
|
2013-06-13 23:37:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-13 23:37:30 +02:00
|
|
|
try {
|
2014-04-20 23:34:45 +02:00
|
|
|
packetType.read(dataStream, entityplayer, entityplayer.worldObj);
|
2013-06-13 23:37:30 +02:00
|
|
|
} catch(Exception e) {
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.error("Error while reading '" + packetType.getName() + "' packet.");
|
2013-10-25 23:11:15 +02:00
|
|
|
e.printStackTrace();
|
2013-06-13 23:37:30 +02:00
|
|
|
}
|
2013-03-31 00:13:59 +01:00
|
|
|
} catch(Exception e) {
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.error("Error while handling packet.");
|
2012-08-15 22:41:41 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-15 17:05:14 +02:00
|
|
|
/**
|
|
|
|
* Registers a packet class for identification and reflection purposes. This MUST be called both server-side and client-side,
|
|
|
|
* otherwise the packet will not be handled correctly.
|
|
|
|
* @param packetClass - class of the packet to register
|
2014-04-20 04:44:06 +02:00
|
|
|
*
|
2013-06-13 23:37:30 +02:00
|
|
|
public static void registerPacket(Class<? extends IMekanismPacket> packetClass)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2013-10-30 20:22:43 +01:00
|
|
|
if(!packets.contains(packetClass))
|
2013-06-07 17:16:47 +02:00
|
|
|
{
|
2013-10-30 20:22:43 +01:00
|
|
|
packets.add(packetClass);
|
2013-06-07 17:16:47 +02:00
|
|
|
}
|
2014-06-02 16:52:13 +02:00
|
|
|
}*/
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-15 17:05:14 +02:00
|
|
|
/**
|
|
|
|
* Encodes an Object[] of data into a DataOutputStream.
|
|
|
|
* @param dataValues - an Object[] of data to encode
|
|
|
|
* @param output - the output stream to write to
|
2014-06-02 16:52:13 +02:00
|
|
|
*/
|
|
|
|
public static void encode(Object[] dataValues, ByteBuf output)
|
2013-06-07 17:16:47 +02:00
|
|
|
{
|
|
|
|
try {
|
2014-03-08 02:00:25 +01:00
|
|
|
for(Object data : dataValues)
|
|
|
|
{
|
|
|
|
if(data instanceof Integer)
|
|
|
|
{
|
|
|
|
output.writeInt((Integer)data);
|
|
|
|
}
|
|
|
|
else if(data instanceof Boolean)
|
|
|
|
{
|
|
|
|
output.writeBoolean((Boolean)data);
|
|
|
|
}
|
|
|
|
else if(data instanceof Double)
|
|
|
|
{
|
|
|
|
output.writeDouble((Double)data);
|
|
|
|
}
|
|
|
|
else if(data instanceof Float)
|
|
|
|
{
|
|
|
|
output.writeFloat((Float)data);
|
|
|
|
}
|
|
|
|
else if(data instanceof String)
|
|
|
|
{
|
2014-06-02 16:52:13 +02:00
|
|
|
writeString(output, (String)data);
|
2014-03-08 02:00:25 +01:00
|
|
|
}
|
|
|
|
else if(data instanceof Byte)
|
|
|
|
{
|
|
|
|
output.writeByte((Byte)data);
|
|
|
|
}
|
|
|
|
else if(data instanceof int[])
|
|
|
|
{
|
|
|
|
for(int i : (int[])data)
|
|
|
|
{
|
|
|
|
output.writeInt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(data instanceof byte[])
|
|
|
|
{
|
|
|
|
for(byte b : (byte[])data)
|
|
|
|
{
|
|
|
|
output.writeByte(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(data instanceof ArrayList)
|
|
|
|
{
|
|
|
|
encode(((ArrayList)data).toArray(), output);
|
|
|
|
}
|
|
|
|
}
|
2013-06-07 17:16:47 +02:00
|
|
|
} catch(Exception e) {
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.error("Error while encoding packet data.");
|
2014-03-08 02:00:25 +01:00
|
|
|
e.printStackTrace();
|
2013-06-07 17:16:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-02 16:52:13 +02:00
|
|
|
|
|
|
|
public static void writeString(ByteBuf output, String s)
|
|
|
|
{
|
|
|
|
output.writeInt(s.getBytes().length);
|
|
|
|
output.writeBytes(s.getBytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String readString(ByteBuf input)
|
|
|
|
{
|
|
|
|
return new String(input.readBytes(input.readInt()).array());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*/**
|
2013-06-15 17:05:14 +02:00
|
|
|
* Sends a packet with the defined type of transmission.
|
|
|
|
* @param trans - the type of transmission to use with this packet
|
|
|
|
* @param packetType - the object representing this packet, both registered and properly using write()
|
|
|
|
* @param transParams - any extra parameters the transmission type requires
|
2014-04-20 04:44:06 +02:00
|
|
|
*
|
2013-06-13 23:37:30 +02:00
|
|
|
public static void sendPacket(Transmission trans, IMekanismPacket packetType, Object... transParams)
|
|
|
|
{
|
|
|
|
if(packetType == null)
|
|
|
|
{
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.error("Attempted to send null packet, ignoring!");
|
2013-06-13 23:37:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-13 23:37:30 +02:00
|
|
|
if(!packets.contains(packetType.getClass()))
|
|
|
|
{
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.error("Attempted to send unregistered packet '" + packetType.getName() + ",' ignoring!");
|
2013-06-13 23:37:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
|
|
DataOutputStream data = new DataOutputStream(bytes);
|
|
|
|
|
|
|
|
try {
|
|
|
|
data.writeInt(packets.indexOf(packetType.getClass()));
|
|
|
|
packetType.write(data);
|
|
|
|
} catch(Exception e) {
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.error("Error while encoding packet data.");
|
2014-03-08 02:00:25 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet250CustomPayload packet = new Packet250CustomPayload();
|
|
|
|
packet.channel = "MEK";
|
|
|
|
packet.data = bytes.toByteArray();
|
|
|
|
packet.length = packet.data.length;
|
|
|
|
|
|
|
|
switch(trans)
|
|
|
|
{
|
|
|
|
case SERVER:
|
|
|
|
PacketDispatcher.sendPacketToServer(packet);
|
|
|
|
break;
|
|
|
|
case ALL_CLIENTS:
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(packet);
|
|
|
|
break;
|
|
|
|
case CLIENTS_RANGE:
|
|
|
|
Coord4D obj = (Coord4D)transParams[0];
|
|
|
|
PacketDispatcher.sendPacketToAllAround(obj.xCoord, obj.yCoord, obj.zCoord, (Double)transParams[1], obj.dimensionId, packet);
|
|
|
|
break;
|
|
|
|
case CLIENTS_DIM:
|
|
|
|
PacketDispatcher.sendPacketToAllInDimension(packet, (Integer)transParams[0]);
|
|
|
|
break;
|
|
|
|
case SINGLE_CLIENT:
|
|
|
|
((EntityPlayerMP)transParams[0]).playerNetServerHandler.sendPacketToPlayer(packet);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
log(trans, packetType, transParams);
|
2013-06-13 23:37:30 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-15 17:05:14 +02:00
|
|
|
/**
|
|
|
|
* Writes a log to the console with information about a packet recently sent.
|
|
|
|
* @param trans - transmission type this packet used when it was sent
|
|
|
|
* @param packetType - object representing this packet
|
|
|
|
* @param transParams - any extra parameters the transmission type requires
|
2014-04-20 04:44:06 +02:00
|
|
|
*
|
2013-06-13 23:37:30 +02:00
|
|
|
private static void log(Transmission trans, IMekanismPacket packetType, Object[] transParams)
|
|
|
|
{
|
|
|
|
if(Mekanism.logPackets)
|
|
|
|
{
|
|
|
|
switch(trans)
|
|
|
|
{
|
|
|
|
case SERVER:
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.info("Sent '" + packetType.getName() + "' packet to server.");
|
2013-06-13 23:37:30 +02:00
|
|
|
break;
|
|
|
|
case ALL_CLIENTS:
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.info("Sent '" + packetType.getName() + "' packet to all clients.");
|
2013-06-13 23:37:30 +02:00
|
|
|
break;
|
|
|
|
case CLIENTS_RANGE:
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.info("Sent '" + packetType.getName() + "' packet to clients in a " + (Double)transParams[1] + " block range.");
|
2013-06-13 23:37:30 +02:00
|
|
|
break;
|
2013-11-25 06:00:26 +01:00
|
|
|
case CLIENTS_DIM:
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.info("Sent '" + packetType.getName() + "' packet to clients in dimension ID " + (Integer)transParams[0] + ".");
|
2013-11-25 06:00:26 +01:00
|
|
|
break;
|
2013-06-13 23:37:30 +02:00
|
|
|
case SINGLE_CLIENT:
|
2014-06-03 08:46:03 +02:00
|
|
|
Mekanism.logger.info("Sent '" + packetType.getName() + "' packet to " + ((EntityPlayer)transParams[0]).username);
|
2013-06-13 23:37:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-07 17:16:47 +02:00
|
|
|
public static enum Transmission
|
|
|
|
{
|
2014-04-20 04:44:06 +02:00
|
|
|
/** No additional parameters. *
|
2013-06-07 17:16:47 +02:00
|
|
|
SERVER(0),
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 04:44:06 +02:00
|
|
|
/** No additional parameters. *
|
2013-06-07 17:16:47 +02:00
|
|
|
ALL_CLIENTS(0),
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 04:44:06 +02:00
|
|
|
/** 2 parameters - Object3D representing the location of the transmission, and a double of the distance this packet can be sent in. *
|
2013-06-07 17:16:47 +02:00
|
|
|
CLIENTS_RANGE(2),
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 04:44:06 +02:00
|
|
|
/** 1 parameter - int representing the dimension ID to send this packet to. *
|
2013-11-25 06:00:26 +01:00
|
|
|
CLIENTS_DIM(1),
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 04:44:06 +02:00
|
|
|
/** 1 parameter - EntityPlayer to send this packet to. *
|
|
|
|
SINGLE_CLIENT(1),
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-07 17:16:47 +02:00
|
|
|
public int parameters;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-06-07 17:16:47 +02:00
|
|
|
private Transmission(int params)
|
|
|
|
{
|
|
|
|
parameters = params;
|
|
|
|
}
|
2014-04-20 04:44:06 +02:00
|
|
|
}*/
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|