package com.khorn.terraincontrol.bukkit.generator;

import com.khorn.terraincontrol.TerrainControl;
import com.khorn.terraincontrol.bukkit.BukkitWorld;
import com.khorn.terraincontrol.bukkit.TCPlugin;
import com.khorn.terraincontrol.configuration.WorldConfig;
import com.khorn.terraincontrol.generator.ChunkProviderTC;
import com.khorn.terraincontrol.logging.LogMarker;
import com.khorn.terraincontrol.util.ChunkCoordinate;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TCChunkGenerator extends ChunkGenerator
{
    private ChunkProviderTC chunkProviderTC;
    private ArrayList<BlockPopulator> BlockPopulator = new ArrayList<BlockPopulator>();
    private boolean NotGenerate = false;
    private TCPlugin plugin;

    public TCChunkGenerator(TCPlugin _plugin)
    {
        this.plugin = _plugin;
    }

    /**
     * Initializes the world if it hasn't already been initialized.
     * 
     * @param world
     *            The world of this generator.
     */
    private void makeSureWorldIsInitialized(World world)
    {
        if (this.chunkProviderTC == null)
        {
            // Not yet initialized, do it now
            this.plugin.onWorldInit(world);
        }
    }

    /**
     * Called whenever a BukkitWorld instance becomes available.
     * 
     * @param _world
     *            The BukkitWorld instance.
     */
    public void onInitialize(BukkitWorld _world)
    {
        this.chunkProviderTC = new ChunkProviderTC(_world.getConfigs(), _world);

        WorldConfig.TerrainMode mode = _world.getConfigs().getWorldConfig().ModeTerrain;

        if (mode == WorldConfig.TerrainMode.Normal || mode == WorldConfig.TerrainMode.OldGenerator)
            this.BlockPopulator.add(new TCBlockPopulator(_world));

        if (mode == WorldConfig.TerrainMode.NotGenerate)
            this.NotGenerate = true;
    }

    @Override
    public List<BlockPopulator> getDefaultPopulators(World world)
    {
        makeSureWorldIsInitialized(world);

        return this.BlockPopulator;
    }

    @Override
    public boolean canSpawn(World world, int x, int z)
    {
        makeSureWorldIsInitialized(world);

        Material material = world.getHighestBlockAt(x, z).getType();
        return material.isSolid();
    }

    @Override
    public ChunkData generateChunkData(World world, Random random, int chunkX, int chunkZ, BiomeGrid biome)
    {
        makeSureWorldIsInitialized(world);

        ChunkData chunkData = createChunkData(world);

        if (this.NotGenerate)
            return chunkData;

        ChunkCoordinate chunkCoord = ChunkCoordinate.fromChunkCoords(chunkX, chunkZ);
        BukkitChunkBuffer chunkBuffer = new BukkitChunkBuffer(chunkCoord, chunkData);
        this.chunkProviderTC.generate(chunkBuffer);

        return chunkData;
    }

    @Override
    public short[][] generateExtBlockSections(World world, Random random, int chunkX, int chunkZ, BiomeGrid biomes)
    {
        // Newer versions of Spigot call generateChunkData first
        // So when this method is called, we have an outdated Spigot build
        TerrainControl.log(LogMarker.FATAL, "- ------------------------------------------------------------- -");
        TerrainControl.log(LogMarker.FATAL, "Outdated Spigot detected!");
        TerrainControl.log(LogMarker.FATAL, "Please update Spigot to a build released in august 2015 or later.");
        TerrainControl.log(LogMarker.FATAL, "- ------------------------------------------------------------- -");
        throw new UnsupportedOperationException("Please update Spigot to a build released in august 2015 or later.");
    }
}
@@ -2969,821 +2969,5 @@
  %7D%0A%0A
-    @Override%0A    public short%5B%5D%5B%5D generateExtBlockSections(World world, Random random, int chunkX, int chunkZ, BiomeGrid biomes)%0A    %7B%0A        // Newer versions of Spigot call generateChunkData first%0A        // So when this method is called, we have an outdated Spigot build%0A        TerrainControl.log(LogMarker.FATAL, %22- ------------------------------------------------------------- -%22);%0A        TerrainControl.log(LogMarker.FATAL, %22Outdated Spigot detected!%22);%0A        TerrainControl.log(LogMarker.FATAL, %22Please update Spigot to a build released in august 2015 or later.%22);%0A        TerrainControl.log(LogMarker.FATAL, %22- ------------------------------------------------------------- -%22);%0A        throw new UnsupportedOperationException(%22Please update Spigot to a build released in august 2015 or later.%22);%0A    %7D%0A
 %7D

Remove some unneeded code