package com.khorn.terraincontrol.forge;

import com.khorn.terraincontrol.TerrainControl;
import com.khorn.terraincontrol.configuration.WorldConfig;
import com.khorn.terraincontrol.configuration.WorldSettings;
import com.khorn.terraincontrol.forge.generator.BiomeCacheWrapper;
import com.khorn.terraincontrol.forge.generator.TCWorldChunkManager;
import com.khorn.terraincontrol.forge.util.WorldHelper;
import com.khorn.terraincontrol.generator.biome.BiomeGenerator;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.world.World;
import net.minecraft.world.WorldType;
import net.minecraft.world.biome.WorldChunkManager;
import net.minecraft.world.chunk.IChunkProvider;

import java.io.File;

public class TCWorldType extends WorldType
{
    public ForgeWorld worldTC;
    public TCWorldType(String paramString)
    {
        super(paramString);
    }
    
    @SideOnly(Side.CLIENT)
    public boolean showWorldInfoNotice()
    {
        return true;
    }

    // Actually: getBiomeManager
    @Override
    public WorldChunkManager getChunkManager(World world)
    {
        try
        {
            if (world instanceof WorldClient)
            {
                return super.getChunkManager(world);
            }
        } catch (NoClassDefFoundError e)
        {
            // There isn't a WorldClient class, so we are on a stand-alone
            // server. Continue normally.
        }

        // Restore old biomes
        ForgeWorld.restoreBiomes();

        // Load everything
        File worldDirectory = new File(TerrainControl.getEngine().getTCDataFolder(), "worlds" + File.separator + world.getSaveHandler().getWorldDirectoryName());

        if (!worldDirectory.exists())
        {
            System.out.println("TerrainControl: settings does not exist, creating defaults");

            if (!worldDirectory.mkdirs())
                System.out.println("TerrainControl: cant create folder " + worldDirectory.getAbsolutePath());
        }

        this.worldTC = new ForgeWorld(world.getSaveHandler().getWorldDirectoryName());
        WorldSettings config = new WorldSettings(worldDirectory, worldTC, false);
        this.worldTC.Init(world, config);

        WorldChunkManager chunkManager = null;

        Class<? extends BiomeGenerator> biomeManagerClass = this.worldTC.getSettings().worldConfig.biomeMode;

        if (biomeManagerClass == TerrainControl.getBiomeModeManager().VANILLA)
        {
            chunkManager = super.getChunkManager(world);
        } else
        {
            chunkManager = new TCWorldChunkManager(this.worldTC);
            BiomeGenerator biomeManager = TerrainControl.getBiomeModeManager().create(biomeManagerClass, worldTC, new BiomeCacheWrapper(chunkManager));
            ((TCWorldChunkManager) chunkManager).setBiomeManager(biomeManager);
            this.worldTC.setBiomeManager(biomeManager);
        }

        return chunkManager;
    }

    @Override
    public IChunkProvider getChunkGenerator(World world, String generatorOptions)
    {
        if (this.worldTC.getSettings().worldConfig.ModeTerrain != WorldConfig.TerrainMode.Default)
        {
            return this.worldTC.getChunkGenerator();
        } else
            return super.getChunkGenerator(world, generatorOptions);
    }

    @Override
    public int getMinimumSpawnHeight(World world)
    {
        return WorldHelper.toLocalWorld(world).getSettings().worldConfig.waterLevelMax;
    }
}

@@ -34,16 +34,60 @@
 forge;%0A%0A
+import com.khorn.terraincontrol.LocalWorld;%0A
 import c
@@ -905,16 +905,17 @@
 orldTC;%0A
+%0A
     publ
@@ -989,20 +989,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Si
@@ -3445,25 +3445,27 @@
 eight(World 
-w
+mcW
 orld)%0A    %7B%0A
@@ -3472,22 +3472,34 @@
         
-return
+LocalWorld world =
  WorldHe
@@ -3516,22 +3516,177 @@
 alWorld(
+mcWorld);%0A        if (world == null)%0A        %7B%0A            // MCPC+ has an interesting load order sometimes%0A            return 64;%0A        %7D%0A        return 
 world
-)
 .getSett

Fix for MCPC+ load order. Fixes #196.