Fixed packet functions not static
This commit is contained in:
parent
717ef201fd
commit
5bc0b50219
1 changed files with 82 additions and 72 deletions
|
@ -5,7 +5,6 @@ package resonantinduction;
|
|||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.network.INetworkManager;
|
||||
|
@ -20,7 +19,6 @@ import com.google.common.io.ByteStreams;
|
|||
import cpw.mods.fml.common.network.IPacketHandler;
|
||||
import cpw.mods.fml.common.network.PacketDispatcher;
|
||||
import cpw.mods.fml.common.network.Player;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
|
||||
/**
|
||||
* @author AidanBrady
|
||||
|
@ -31,96 +29,108 @@ public class PacketHandler implements IPacketHandler
|
|||
@Override
|
||||
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
|
||||
{
|
||||
if(packet.channel == ResonantInduction.CHANNEL)
|
||||
if (packet.channel == ResonantInduction.CHANNEL)
|
||||
{
|
||||
ByteArrayDataInput dataStream = ByteStreams.newDataInput(packet.data);
|
||||
EntityPlayer entityplayer = (EntityPlayer)player;
|
||||
World world = entityplayer.worldObj;
|
||||
|
||||
try {
|
||||
int packetType = dataStream.readInt();
|
||||
|
||||
if(packetType == PacketType.TILE.ordinal())
|
||||
{
|
||||
int x = dataStream.readInt();
|
||||
int y = dataStream.readInt();
|
||||
int z = dataStream.readInt();
|
||||
|
||||
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
|
||||
|
||||
if(tileEntity instanceof IPacketReceiver)
|
||||
{
|
||||
((IPacketReceiver)tileEntity).handle(dataStream);
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
EntityPlayer entityplayer = (EntityPlayer) player;
|
||||
World world = entityplayer.worldObj;
|
||||
|
||||
try
|
||||
{
|
||||
int packetType = dataStream.readInt();
|
||||
|
||||
if (packetType == PacketType.TILE.ordinal())
|
||||
{
|
||||
int x = dataStream.readInt();
|
||||
int y = dataStream.readInt();
|
||||
int z = dataStream.readInt();
|
||||
|
||||
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
|
||||
|
||||
if (tileEntity instanceof IPacketReceiver)
|
||||
{
|
||||
((IPacketReceiver) tileEntity).handle(dataStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void encode(Object[] dataValues, DataOutputStream output)
|
||||
{
|
||||
try {
|
||||
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)
|
||||
{
|
||||
output.writeUTF((String)data);
|
||||
}
|
||||
else if(data instanceof Byte)
|
||||
{
|
||||
output.writeByte((Byte)data);
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
try
|
||||
{
|
||||
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)
|
||||
{
|
||||
output.writeUTF((String) data);
|
||||
}
|
||||
else if (data instanceof Byte)
|
||||
{
|
||||
output.writeByte((Byte) data);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void sendTileEntityPacketToServer(TileEntity tileEntity, Object... dataValues)
|
||||
|
||||
public static void sendTileEntityPacketToServer(TileEntity tileEntity, Object... dataValues)
|
||||
{
|
||||
PacketDispatcher.sendPacketToServer(getTileEntityPacket(tileEntity, dataValues));
|
||||
PacketDispatcher.sendPacketToServer(getTileEntityPacket(tileEntity, dataValues));
|
||||
}
|
||||
|
||||
public void sendTileEntityPacketToClients(TileEntity tileEntity, Object... dataValues)
|
||||
|
||||
public static void sendTileEntityPacketToClients(TileEntity tileEntity, Object... dataValues)
|
||||
{
|
||||
PacketDispatcher.sendPacketToAllPlayers(getTileEntityPacket(tileEntity, dataValues));
|
||||
}
|
||||
|
||||
public Packet250CustomPayload getTileEntityPacket(TileEntity tileEntity, Object... dataValues)
|
||||
|
||||
public static Packet250CustomPayload getTileEntityPacket(TileEntity tileEntity, Object... dataValues)
|
||||
{
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
DataOutputStream data = new DataOutputStream(bytes);
|
||||
|
||||
try {
|
||||
DataOutputStream data = new DataOutputStream(bytes);
|
||||
|
||||
try
|
||||
{
|
||||
data.writeInt(tileEntity.xCoord);
|
||||
data.writeInt(tileEntity.yCoord);
|
||||
data.writeInt(tileEntity.zCoord);
|
||||
|
||||
|
||||
encode(dataValues, data);
|
||||
} catch(Exception e) {}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
|
||||
Packet250CustomPayload packet = new Packet250CustomPayload();
|
||||
packet.channel = ResonantInduction.CHANNEL;
|
||||
packet.data = bytes.toByteArray();
|
||||
packet.length = packet.data.length;
|
||||
|
||||
return packet;
|
||||
packet.channel = ResonantInduction.CHANNEL;
|
||||
packet.data = bytes.toByteArray();
|
||||
packet.length = packet.data.length;
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
public static enum PacketType
|
||||
{
|
||||
TILE
|
||||
|
|
Loading…
Add table
Reference in a new issue