2013-02-18 03:46:16 +01:00
|
|
|
package StevenDimDoors.mod_pocketDim;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
2013-05-01 02:52:57 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2013-02-18 03:46:16 +01:00
|
|
|
import net.minecraft.network.INetworkManager;
|
|
|
|
import net.minecraft.network.packet.Packet250CustomPayload;
|
2013-05-01 02:52:57 +02:00
|
|
|
import net.minecraft.world.World;
|
2013-02-18 03:46:16 +01:00
|
|
|
|
2013-06-10 23:03:52 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.helpers.dimHelper;
|
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
import com.google.common.io.ByteArrayDataInput;
|
|
|
|
import com.google.common.io.ByteStreams;
|
|
|
|
|
2013-05-01 02:52:57 +02:00
|
|
|
import cpw.mods.fml.client.FMLClientHandler;
|
|
|
|
import cpw.mods.fml.common.network.FMLNetworkHandler;
|
2013-02-18 03:46:16 +01:00
|
|
|
import cpw.mods.fml.common.network.IPacketHandler;
|
|
|
|
import cpw.mods.fml.common.network.PacketDispatcher;
|
|
|
|
import cpw.mods.fml.common.network.Player;
|
|
|
|
|
|
|
|
public class PacketHandler implements IPacketHandler
|
|
|
|
{
|
|
|
|
public static int regsiterDimPacketID = 3;
|
|
|
|
public static int registerLinkPacketID = 4;
|
|
|
|
public static int removeLinkPacketID = 5;
|
|
|
|
public static int linkKeyPacketID = 7;
|
|
|
|
public static int dimPacketID = 6;
|
2013-06-14 01:01:54 +02:00
|
|
|
public static int dimUpdatePacketID = 1;
|
|
|
|
private static DDProperties properties = null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (packet.channel.equals("DimDoorPackets"))
|
|
|
|
{
|
|
|
|
handleRandom(packet,player);
|
|
|
|
}
|
2013-02-18 03:46:16 +01:00
|
|
|
|
|
|
|
|
2013-06-14 01:01:54 +02:00
|
|
|
}
|
2013-02-18 03:46:16 +01:00
|
|
|
|
2013-06-14 01:01:54 +02:00
|
|
|
private void handleRandom(Packet250CustomPayload packet, Player player)
|
|
|
|
{
|
|
|
|
ByteArrayDataInput data = ByteStreams.newDataInput(packet.data);
|
2013-02-18 03:46:16 +01:00
|
|
|
|
2013-06-14 01:01:54 +02:00
|
|
|
int id=data.readByte();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(id==regsiterDimPacketID)
|
2013-02-18 03:46:16 +01:00
|
|
|
{
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int dimId = data.readInt();
|
|
|
|
// System.out.println("regsitered dim ID" + dimId);
|
|
|
|
try
|
2013-02-18 03:46:16 +01:00
|
|
|
{
|
2013-06-14 01:01:54 +02:00
|
|
|
DimData dimDataToAdd = new DimData(dimId, data.readBoolean(), data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readInt());
|
|
|
|
|
|
|
|
if(!dimHelper.dimList.containsKey(dimId))
|
|
|
|
{
|
|
|
|
dimHelper.dimList.put(dimId, dimDataToAdd);
|
|
|
|
}
|
|
|
|
if(dimDataToAdd.isPocket)
|
|
|
|
{
|
|
|
|
if (properties == null)
|
|
|
|
properties = DDProperties.instance();
|
|
|
|
|
|
|
|
dimHelper.registerDimension(dimId, properties.PocketProviderID);
|
|
|
|
//System.out.println("regsitered dim ID" + dimId);
|
|
|
|
}
|
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
catch (Exception e)
|
2013-02-18 03:46:16 +01:00
|
|
|
{
|
2013-06-14 01:01:54 +02:00
|
|
|
// e.printStackTrace();
|
|
|
|
if(dimId!=0)
|
|
|
|
{
|
|
|
|
// System.out.println(String.valueOf(dimId)+"dimID already registered");
|
|
|
|
}
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
if(id==registerLinkPacketID)
|
2013-02-18 03:46:16 +01:00
|
|
|
{
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int dimId = data.readInt();
|
|
|
|
try
|
|
|
|
{
|
2013-08-01 01:34:08 +02:00
|
|
|
DimData dimDataToAddLink= dimHelper.instance.getDimData(dimId);
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
LinkData linkToAdd = new LinkData(dimId, data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readBoolean(),data.readInt());
|
|
|
|
linkToAdd.hasGennedDoor=data.readBoolean();
|
|
|
|
|
|
|
|
dimHelper.instance.createLink(linkToAdd);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
System.out.println("Tried to update client link data & failed!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
if(id==removeLinkPacketID)
|
2013-02-18 03:46:16 +01:00
|
|
|
{
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int dimId = data.readInt();
|
|
|
|
try
|
|
|
|
{
|
2013-08-01 01:34:08 +02:00
|
|
|
DimData dimDataToRemoveFrom= dimHelper.instance.getDimData(dimId);
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
LinkData linkToAdd = new LinkData(dimId, data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readInt(), data.readBoolean(),data.readInt());
|
|
|
|
dimDataToRemoveFrom.removeLinkAtCoords(linkToAdd.locDimID, linkToAdd.locXCoord,linkToAdd.locYCoord, linkToAdd.locZCoord);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
//e.printStackTrace();
|
|
|
|
System.out.println("Tried to update client link data & failed!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
if(id==this.linkKeyPacketID)
|
2013-02-18 03:46:16 +01:00
|
|
|
{
|
2013-06-14 01:01:54 +02:00
|
|
|
LinkData link = new LinkData(data.readInt(), data.readInt(), data.readInt(), data.readInt());
|
|
|
|
dimHelper.instance.interDimLinkList.put(data.readInt(), link);
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void onClientJoinPacket(INetworkManager manager, HashMap<Integer, DimData> dimList)
|
|
|
|
{
|
|
|
|
|
|
|
|
Collection<Integer> dimIDs= dimList.keySet();
|
|
|
|
Collection<DimData> dimDataSet= dimList.values();
|
|
|
|
Collection<Packet250CustomPayload> packetsToSend = new HashSet();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(DimData data : dimDataSet)
|
2013-02-18 03:46:16 +01:00
|
|
|
{
|
|
|
|
|
2013-06-14 01:01:54 +02:00
|
|
|
manager.addToSendQueue(PacketHandler.onDimCreatedPacket(data));
|
2013-02-18 03:46:16 +01:00
|
|
|
|
2013-06-14 01:01:54 +02:00
|
|
|
Collection <HashMap<Integer, HashMap<Integer, LinkData>>> linkList = data.linksInThisDim.values();
|
|
|
|
|
|
|
|
for(HashMap map : linkList )
|
|
|
|
{
|
|
|
|
|
|
|
|
Collection <HashMap<Integer, LinkData>> linkList2 = map.values();
|
|
|
|
|
|
|
|
for(HashMap map2 : linkList2)
|
|
|
|
{
|
|
|
|
Collection <LinkData> linkList3 = map2.values();
|
|
|
|
|
|
|
|
for(LinkData link : linkList3)
|
|
|
|
{
|
|
|
|
|
|
|
|
packetsToSend.add(( PacketHandler.onLinkCreatedPacket(link)));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
for (Packet250CustomPayload packet : packetsToSend)
|
|
|
|
{
|
|
|
|
manager.addToSendQueue(packet);
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Packet250CustomPayload onLinkCreatedPacket(LinkData link)
|
|
|
|
{
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
DataOutputStream dataOut = new DataOutputStream(bos);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
dataOut.writeByte(PacketHandler.registerLinkPacketID);
|
|
|
|
dataOut.writeInt(link.locDimID);
|
|
|
|
dataOut.writeInt(link.destDimID);
|
|
|
|
dataOut.writeInt(link.locXCoord);
|
|
|
|
dataOut.writeInt(link.locYCoord);
|
|
|
|
dataOut.writeInt(link.locZCoord);
|
|
|
|
dataOut.writeInt(link.destXCoord);
|
|
|
|
dataOut.writeInt(link.destYCoord);
|
|
|
|
dataOut.writeInt(link.destZCoord);
|
|
|
|
dataOut.writeBoolean(link.isLocPocket);
|
|
|
|
|
|
|
|
dataOut.writeInt(link.linkOrientation);
|
|
|
|
dataOut.writeBoolean(link.hasGennedDoor);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet250CustomPayload packet= new Packet250CustomPayload();
|
|
|
|
packet.channel="DimDoorPackets";
|
|
|
|
packet.data = bos.toByteArray();
|
|
|
|
packet.length = bos.size();;
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(packet);
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Packet250CustomPayload linkKeyPacket(LinkData link, int key)
|
|
|
|
{
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
DataOutputStream dataOut = new DataOutputStream(bos);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
dataOut.writeByte(PacketHandler.linkKeyPacketID);
|
|
|
|
|
|
|
|
dataOut.writeInt(link.destDimID);
|
|
|
|
dataOut.writeInt(link.destXCoord);
|
|
|
|
dataOut.writeInt(link.destYCoord);
|
|
|
|
dataOut.writeInt(link.destZCoord);
|
|
|
|
dataOut.writeInt(key);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet250CustomPayload packet= new Packet250CustomPayload();
|
|
|
|
packet.channel="DimDoorPackets";
|
|
|
|
packet.data = bos.toByteArray();
|
|
|
|
packet.length = bos.size();;
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(packet);
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void onLinkRemovedPacket(LinkData link)
|
|
|
|
{
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
DataOutputStream dataOut = new DataOutputStream(bos);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
dataOut.writeByte(PacketHandler.removeLinkPacketID);
|
|
|
|
dataOut.writeInt(link.locDimID);
|
|
|
|
dataOut.writeInt(link.destDimID);
|
|
|
|
dataOut.writeInt(link.locXCoord);
|
|
|
|
dataOut.writeInt(link.locYCoord);
|
|
|
|
dataOut.writeInt(link.locZCoord);
|
|
|
|
dataOut.writeInt(link.destXCoord);
|
|
|
|
dataOut.writeInt(link.destYCoord);
|
|
|
|
dataOut.writeInt(link.destZCoord);
|
|
|
|
dataOut.writeBoolean(link.isLocPocket);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet250CustomPayload packet= new Packet250CustomPayload();
|
|
|
|
packet.channel="DimDoorPackets";
|
|
|
|
packet.data = bos.toByteArray();
|
|
|
|
packet.length = bos.size();;
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Packet250CustomPayload onDimCreatedPacket(DimData data)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
DataOutputStream dataOut = new DataOutputStream(bos);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
dataOut.writeByte(PacketHandler.regsiterDimPacketID);
|
|
|
|
dataOut.writeInt(data.dimID);
|
|
|
|
dataOut.writeBoolean(data.isPocket);
|
|
|
|
|
|
|
|
dataOut.writeInt(data.depth);
|
|
|
|
dataOut.writeInt(data.exitDimLink.destDimID);
|
|
|
|
dataOut.writeInt(data.exitDimLink.destXCoord);
|
|
|
|
dataOut.writeInt(data.exitDimLink.destYCoord);
|
|
|
|
dataOut.writeInt(data.exitDimLink.destZCoord);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet250CustomPayload packet= new Packet250CustomPayload();
|
|
|
|
packet.channel="DimDoorPackets";
|
|
|
|
packet.data = bos.toByteArray();
|
|
|
|
packet.length = bos.size();
|
|
|
|
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(packet);
|
|
|
|
return packet;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
2013-02-18 03:46:16 +01:00
|
|
|
private void handleObjectPacket(Packet250CustomPayload packet, Player player)
|
|
|
|
{
|
|
|
|
ObjectInputStream data = new ObjectInputStream;
|
|
|
|
int length = data.readInt();
|
|
|
|
int id=data.readByte();
|
|
|
|
System.out.println(id);
|
|
|
|
if(id==dimPacketID)
|
|
|
|
{
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
DimData dimData = data.read
|
2013-06-14 01:01:54 +02:00
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
dimHelper.dimList.put(key, value)
|
2013-06-14 01:01:54 +02:00
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
|
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
**/
|
|
|
|
public static void sendDimObject(DimData dim)
|
|
|
|
{
|
|
|
|
try
|
2013-02-18 03:46:16 +01:00
|
|
|
{
|
2013-06-14 01:01:54 +02:00
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
ObjectOutputStream dataOut = new ObjectOutputStream(bos);
|
|
|
|
dataOut.writeObject(dim);
|
|
|
|
|
|
|
|
Packet250CustomPayload packet= new Packet250CustomPayload();
|
|
|
|
packet.channel="DimDoorPackets";
|
|
|
|
packet.data = bos.toByteArray();
|
|
|
|
packet.length = bos.size();;
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(packet);
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
2013-02-18 03:46:16 +01:00
|
|
|
}
|
2013-06-14 01:01:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-18 03:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
}
|