2013-02-17 21:46:16 -05:00
|
|
|
package StevenDimDoors.mod_pocketDim;
|
|
|
|
|
2013-09-01 22:01:17 -04:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2013-02-17 21:46:16 -05:00
|
|
|
import net.minecraft.network.INetworkManager;
|
|
|
|
import net.minecraft.network.packet.Packet250CustomPayload;
|
2013-09-01 22:01:17 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.core.PocketManager;
|
2013-09-03 15:33:09 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.util.Point4D;
|
|
|
|
import StevenDimDoors.mod_pocketDim.watcher.ClientDimData;
|
|
|
|
import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher;
|
2013-02-17 21:46:16 -05:00
|
|
|
import cpw.mods.fml.common.network.IPacketHandler;
|
2013-09-01 22:01:17 -04:00
|
|
|
import cpw.mods.fml.common.network.PacketDispatcher;
|
2013-02-17 21:46:16 -05:00
|
|
|
import cpw.mods.fml.common.network.Player;
|
|
|
|
|
|
|
|
public class ServerPacketHandler implements IPacketHandler
|
|
|
|
{
|
2013-09-01 22:01:17 -04:00
|
|
|
public ServerPacketHandler()
|
|
|
|
{
|
|
|
|
PocketManager.registerDimWatcher(new DimWatcher());
|
|
|
|
PocketManager.registerLinkWatcher(new LinkWatcher());
|
|
|
|
}
|
|
|
|
|
2013-06-13 19:01:54 -04:00
|
|
|
@Override
|
2013-09-03 15:33:09 -04:00
|
|
|
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { }
|
2013-09-01 22:01:17 -04:00
|
|
|
|
2013-09-03 15:33:09 -04:00
|
|
|
private static class DimWatcher implements IUpdateWatcher<ClientDimData>
|
2013-09-01 22:01:17 -04:00
|
|
|
{
|
|
|
|
@Override
|
2013-09-03 15:33:09 -04:00
|
|
|
public void onCreated(ClientDimData message)
|
2013-09-01 22:01:17 -04:00
|
|
|
{
|
2013-09-03 15:33:09 -04:00
|
|
|
sendDimPacket(PacketConstants.CREATE_DIM_PACKET_ID, message);
|
2013-09-01 22:01:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-03 15:33:09 -04:00
|
|
|
public void onDeleted(ClientDimData message)
|
2013-09-01 22:01:17 -04:00
|
|
|
{
|
2013-09-03 15:33:09 -04:00
|
|
|
sendDimPacket(PacketConstants.DELETE_DIM_PACKET_ID, message);
|
2013-09-01 22:01:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:33:09 -04:00
|
|
|
private static class LinkWatcher implements IUpdateWatcher<Point4D>
|
2013-09-01 22:01:17 -04:00
|
|
|
{
|
|
|
|
@Override
|
2013-09-03 15:33:09 -04:00
|
|
|
public void onCreated(Point4D message)
|
2013-09-01 22:01:17 -04:00
|
|
|
{
|
2013-09-03 15:33:09 -04:00
|
|
|
sendLinkPacket(PacketConstants.CREATE_LINK_PACKET_ID, message);
|
2013-09-01 22:01:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-09-03 15:33:09 -04:00
|
|
|
public void onDeleted(Point4D message)
|
2013-09-01 22:01:17 -04:00
|
|
|
{
|
2013-09-03 15:33:09 -04:00
|
|
|
sendLinkPacket(PacketConstants.DELETE_LINK_PACKET_ID, message);
|
2013-09-01 22:01:17 -04:00
|
|
|
}
|
2013-09-03 15:33:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendDimPacket(byte id, ClientDimData data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Packet250CustomPayload packet = new Packet250CustomPayload();
|
|
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
DataOutputStream writer = new DataOutputStream(buffer);
|
|
|
|
writer.writeByte(id);
|
|
|
|
data.write(writer);
|
|
|
|
writer.close();
|
|
|
|
packet.channel = PacketConstants.CHANNEL_NAME;
|
|
|
|
packet.data = buffer.toByteArray();
|
|
|
|
packet.length = packet.data.length;
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(packet);
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
2013-09-01 22:01:17 -04:00
|
|
|
{
|
2013-09-03 15:33:09 -04:00
|
|
|
//This shouldn't happen...
|
|
|
|
e.printStackTrace();
|
2013-09-01 22:01:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:33:09 -04:00
|
|
|
private static void sendLinkPacket(byte id, Point4D data)
|
2013-06-13 19:01:54 -04:00
|
|
|
{
|
2013-09-01 22:01:17 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Packet250CustomPayload packet = new Packet250CustomPayload();
|
|
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
DataOutputStream writer = new DataOutputStream(buffer);
|
|
|
|
writer.writeByte(id);
|
2013-09-03 15:33:09 -04:00
|
|
|
Point4D.write(data, writer);
|
2013-09-01 22:01:17 -04:00
|
|
|
writer.close();
|
|
|
|
packet.channel = PacketConstants.CHANNEL_NAME;
|
|
|
|
packet.data = buffer.toByteArray();
|
|
|
|
packet.length = packet.data.length;
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(packet);
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
//This shouldn't happen...
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-06-13 19:01:54 -04:00
|
|
|
}
|
2013-09-01 22:01:17 -04:00
|
|
|
}
|