package zmaster587.advancedRocketry.network;

import java.io.IOException;

import zmaster587.advancedRocketry.world.DimensionManager;
import zmaster587.advancedRocketry.world.DimensionProperties;
import zmaster587.advancedRocketry.world.provider.WorldProviderPlanet;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;

public class PacketDimInfo extends BasePacket {

	DimensionProperties dimProperties;
	int dimNumber;

	public PacketDimInfo() {}

	public PacketDimInfo(int dimNumber,DimensionProperties dimProperties) {
		this.dimProperties = dimProperties;
		this.dimNumber = dimNumber;
	}

	@Override
	public void write(ByteBuf out) {
		NBTTagCompound nbt = new NBTTagCompound();
		out.writeInt(dimNumber);
		boolean flag = dimProperties == null;
		out.writeBoolean(flag);
		if(!flag) {
			dimProperties.writeToNBT(nbt);

			PacketBuffer packetBuffer = new PacketBuffer(out);
			//TODO: error handling
			try {
				packetBuffer.writeNBTTagCompoundToBuffer(nbt);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public void readClient(ByteBuf in) {
		PacketBuffer packetBuffer = new PacketBuffer(in);
		NBTTagCompound nbt;
		dimNumber = in.readInt();

		if(in.readBoolean()) {
			if(DimensionManager.getInstance().isDimensionCreated(dimNumber)) {
				DimensionManager.getInstance().deleteDimension(dimNumber);
			}
		}
		else {
			//TODO: error handling
			try {
				nbt = packetBuffer.readNBTTagCompoundFromBuffer();

			} catch (IOException e) {
				e.printStackTrace();
				return;
			}

			if(dimNumber == 0) {
				DimensionManager.overworldProperties.readFromNBT(nbt);
			}
			else if( DimensionManager.getInstance().isDimensionCreated(dimNumber)) {
				DimensionManager.getInstance().getDimensionProperties(dimNumber).readFromNBT(nbt);
			} else {
				DimensionManager.getInstance().registerDimNoUpdate(DimensionProperties.createFromNBT(dimNumber, nbt));
				//net.minecraftforge.common.DimensionManager.registerProviderType(dimNumber,ProviderPlanet.class, false);
				//net.minecraftforge.common.DimensionManager.registerDimension(dimNumber, dimNumber);
			}
		}

	}

	@Override
	public void read(ByteBuf in) {
		//Should never be read on the server!
	}

	@Override
	public void executeClient(EntityPlayer thePlayer) {}

	@Override
	public void executeServer(EntityPlayerMP player) {}

}

@@ -68,16 +68,69 @@
 ption;%0A%0A
+import zmaster587.advancedRocketry.AdvancedRocketry;%0A
 import z
@@ -968,46 +968,177 @@
 ;%0A%09%09
-out.writeBoolean(flag);%0A%09%09if(!flag)
+%0A%09%09if(!flag) %7B%0A%09%09%09%0A%09%09%09//Try to send the nbt data of the dimension to the client, if it fails(probably due to non existant Biome ids) then remove the dimension%0A%09%09%09try
  %7B%0A
+%09
 %09%09%09d
@@ -1167,20 +1167,20 @@
 T(nbt);%0A
-%0A
 %09%09%09
+%09
 PacketBu
@@ -1221,24 +1221,25 @@
 er(out);%0A%09%09%09
+%09
 //TODO: erro
@@ -1248,30 +1248,32 @@
 handling%0A%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09packetBu
@@ -1310,24 +1310,25 @@
 er(nbt);%0A%09%09%09
+%09
 %7D catch (IOE
@@ -1337,32 +1337,33 @@
 eption e) %7B%0A%09%09%09%09
+%09
 e.printStackTrac
@@ -1367,24 +1367,317 @@
 race();%0A
+%09
 %09%09%09%7D%0A%09%09
-%7D
+%09%7D catch(NullPointerException e) %7B%0A%09%09%09%09out.writeBoolean(false);%0A%09%09%09%09AdvancedRocketry.logger.warning(%22Dimension %22 + dimNumber + %22 has thrown an exception trying to write NBT, deleting!%22);%0A%09%09%09%09DimensionManager.getInstance().deleteDimension(dimNumber);%0A%09%09%09%7D%0A%0A%09%09%7D%0A%09%09else%0A%09%09%09out.writeBoolean(flag);
 %0A%0A%09%7D%0A%0A%09@

Delete dimensions with no biome