package com.khorn.terraincontrol.bukkit;

import com.khorn.terraincontrol.BiomeIds;
import com.khorn.terraincontrol.bukkit.util.MobSpawnGroupHelper;
import com.khorn.terraincontrol.configuration.BiomeConfig;
import com.khorn.terraincontrol.configuration.WeightedMobSpawnGroup;
import com.khorn.terraincontrol.configuration.standard.PluginStandardValues;
import net.minecraft.server.v1_9_R1.BiomeBase;
import net.minecraft.server.v1_9_R1.MinecraftKey;

import java.util.List;

public class CustomBiome extends BiomeBase
{
    public final int generationId;

    /**
     * Mojang made the methods on BiomeBase.a protected (so only accessable for
     * classes in the package net.minecraft.world.biome package and for
     * subclasses of BiomeBase.a). To get around this, we have to subclass
     * BiomeBase.a.
     *
     */
    private static class BiomeBase_a extends BiomeBase.a
    {

        public BiomeBase_a(String name, BiomeConfig biomeConfig)
        {
            super(name);

            // Minecraft doesn't like temperatures between 0.1 and 0.2, so avoid
            // them: round them to either 0.1 or 0.2
            float adjustedTemperature = biomeConfig.biomeTemperature;
            if (adjustedTemperature >= 0.1 && adjustedTemperature <= 0.2)
            {
                if (adjustedTemperature >= 1.5)
                    adjustedTemperature = 0.2f;
                else
                    adjustedTemperature = 0.1f;
            }

            c(biomeConfig.biomeHeight);
            d(biomeConfig.biomeVolatility);
            a(adjustedTemperature);
            b(biomeConfig.biomeWetness);
            if (biomeConfig.biomeWetness <= 0.0001)
            {
                a(); // disableRain()
            }
        }
    }

    /**
     * Creates a CustomBiome instance. Minecraft automatically registers those
     * instances in the BiomeBase constructor. We don't want this for virtual
     * biomes (the shouldn't overwrite real biomes), so we restore the old
     * biome, unregistering the virtual biome.
     *
     * @param biomeConfig Settings of the biome
     * @param biomeIds Ids of the biome.
     * @return The CustomBiome instance.
     */
    public static CustomBiome createInstance(BiomeConfig biomeConfig, BiomeIds biomeIds)
    {
        CustomBiome customBiome = new CustomBiome(biomeConfig);

        if (!biomeIds.isVirtual())
        {
            // Insert the biome in Minecraft's biome mapping
            MinecraftKey biomeName = new MinecraftKey(PluginStandardValues.PLUGIN_NAME, biomeConfig.getName());
            BiomeBase.REGISTRY_ID.a(biomeIds.getSavedId(), biomeName, customBiome);
        }

        return customBiome;
    }

    private CustomBiome(BiomeConfig biomeConfig)
    {
        super(new BiomeBase_a(biomeConfig.getName(), biomeConfig));
        this.generationId = biomeConfig.generationId;

        // Sanity check
        if (this.getHumidity() != biomeConfig.biomeWetness)
        {
            throw new AssertionError("Biome temperature mismatch");
        }

        this.r = ((BukkitMaterialData) biomeConfig.surfaceBlock).internalBlock();
        this.s = ((BukkitMaterialData) biomeConfig.groundBlock).internalBlock();

        // Mob spawning
        addMobs(this.u, biomeConfig.spawnMonsters);
        addMobs(this.v, biomeConfig.spawnCreatures);
        addMobs(this.w, biomeConfig.spawnWaterCreatures);
        addMobs(this.x, biomeConfig.spawnAmbientCreatures);
    }

    // Adds the mobs to the internal list.
    protected void addMobs(List<BiomeMeta> internalList, List<WeightedMobSpawnGroup> configList)
    {
        internalList.clear();
        internalList.addAll(MobSpawnGroupHelper.toMinecraftlist(configList));
    }
}

@@ -142,16 +142,73 @@
 Helper;%0A
+import com.khorn.terraincontrol.bukkit.util.WorldHelper;%0A
 import c
@@ -2414,299 +2414,1344 @@
     
-if (!biomeIds.isVirtual())%0A        %7B%0A            // Insert the biome in Minecraft's biome mapping%0A            MinecraftKey biomeName = new MinecraftKey(PluginStandardValues.PLUGIN_NAME, biomeConfig.getName());%0A            BiomeBase.REGISTRY_ID.a(biomeIds.getSavedId(), biomeName, customBiome
+// Insert the biome in Minecraft's biome mapping%0A        MinecraftKey biomeName = new MinecraftKey(PluginStandardValues.PLUGIN_NAME, biomeConfig.getName());%0A        int savedBiomeId = biomeIds.getSavedId();%0A        if (biomeIds.isVirtual())%0A        %7B%0A            // Virtual biomes hack: register, then let original biome overwrite%0A            // In this way, the id --%3E biome mapping returns the original biome,%0A            // and the biome --%3E id mapping returns savedBiomeId for both the%0A            // original and custom biome%0A            BiomeBase existingBiome = BiomeBase.getBiome(savedBiomeId);%0A            MinecraftKey existingBiomeName = BiomeBase.REGISTRY_ID.b(existingBiome);%0A            BiomeBase.REGISTRY_ID.a(savedBiomeId, biomeName, customBiome);%0A            BiomeBase.REGISTRY_ID.a(savedBiomeId, existingBiomeName, existingBiome);%0A        %7D else %7B%0A            // Normal insertion%0A            BiomeBase.REGISTRY_ID.a(biomeIds.getSavedId(), biomeName, customBiome);%0A        %7D%0A%0A        // Sanity check: check if biome was actually registered%0A        int registeredSavedId = WorldHelper.getSavedId(customBiome);%0A        if (registeredSavedId != savedBiomeId) %7B%0A            throw new AssertionError(%22Biome %22 + biomeConfig.getName() + %22 is not properly registered: got id %22 + registeredSavedId + %22, should be %22 + savedBiomeId
 );%0A 

Fix ReplaceToBiomeName not working