package com.khorn.terraincontrol.forge;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import net.minecraft.src.INetworkManager;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.WorldClient;

import com.khorn.terraincontrol.configuration.TCDefaultValues;
import com.khorn.terraincontrol.configuration.WorldConfig;

import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;

public class PacketHandler implements IPacketHandler
{
    TCPlugin plugin;

    public PacketHandler(TCPlugin plugin)
    {
        this.plugin = plugin;
    }

    @Override
    public void onPacketData(INetworkManager manager, Packet250CustomPayload receivedPacket, Player player)
    {
        // This method receives the TerrainControl packet with the custom biome colors and weather.
        
        if (!receivedPacket.channel.equals(TCDefaultValues.ChannelName.stringValue()))
        {
            // Make sure that the right channel is being received
            return;
        }
        if (FMLCommonHandler.instance().getEffectiveSide() != Side.CLIENT)
        {
            // Make sure that we are on the client (the server only sends packets to the client)
            return;
        }

        // We're on the client, receive the packet
        ByteArrayInputStream inputStream = new ByteArrayInputStream(receivedPacket.data);
        DataInputStream stream = new DataInputStream(inputStream);
        try
        {
            int serverProtocolVersion = stream.readInt();
            int clientProtocolVersion = TCDefaultValues.ProtocolVersion.intValue();
            if (serverProtocolVersion == clientProtocolVersion)
            {
                // Server sent config
            	
            	// Restore old biomes
            	SingleWorld.restoreBiomes();
            
            	if(receivedPacket.length > 4) {
            		// If the packet wasn't empty, add the new biomes
            		WorldClient worldMC = FMLClientHandler.instance().getClient().theWorld;
                    
                    SingleWorld worldTC = new SingleWorld("external");
                    WorldConfig config = new WorldConfig(stream, worldTC);

                    worldTC.InitM(worldMC, config);
            	}
                

                System.out.println("TerrainControl: config received from server");
            } else
            {
                // Server or client is outdated
                System.out.println("TerrainControl: server has different protocol version! " + "Client: " + TCDefaultValues.ProtocolVersion.intValue() + " Server: " + serverProtocolVersion);
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }

    }

}

@@ -448,88 +448,8 @@
 r;%0D%0A
-import cpw.mods.fml.common.FMLCommonHandler;%0D%0Aimport cpw.mods.fml.common.Side;%0D%0A
 impo
@@ -488,32 +488,32 @@
 PacketHandler;%0D%0A
+
 import cpw.mods.
@@ -923,16 +923,28 @@
 om biome
+%0D%0A        //
  colors 
@@ -957,24 +957,16 @@
 ather.%0D%0A
-        
 %0D%0A      
@@ -1160,225 +1160,8 @@
   %7D%0D
-%0A        if (FMLCommonHandler.instance().getEffectiveSide() != Side.CLIENT)%0D%0A        %7B%0D%0A            // Make sure that we are on the client (the server only sends packets to the client)%0D%0A            return;%0D%0A        %7D%0D
 %0A%0D%0A 
@@ -1661,23 +1661,15 @@
 ig%0D%0A
-       
+%0D%0A
      
-%09%0D%0A
     
@@ -1675,18 +1675,16 @@
         
- %09
 // Resto
@@ -1710,17 +1710,20 @@
         
+    
-%09
 SingleWo
@@ -1740,24 +1740,26 @@
 eBiomes();%0D%0A
+%0D%0A
             
@@ -1762,25 +1762,15 @@
     
-%0D%0A
     
-        %09
 if
+ 
 (rec
@@ -1792,16 +1792,33 @@
 gth %3E 4)
+%0D%0A               
  %7B%0D%0A    
@@ -1825,18 +1825,24 @@
         
-%09%09
+        
 // If th
@@ -1900,10 +1900,16 @@
     
+        
-%09%09
 Worl
@@ -1977,36 +1977,16 @@
 World;%0D%0A
-                    
 %0D%0A      
@@ -2186,24 +2186,8 @@
 );%0D%0A
-            %09%7D%0D%0A
     
@@ -2198,16 +2198,17 @@
         
+%7D
 %0D%0A%0D%0A    

Fix unnessary client check