<commit_before>package com.khorn.terraincontrol.forge.events;

import static com.khorn.terraincontrol.util.ChunkCoordinate.CHUNK_X_SIZE;
import static com.khorn.terraincontrol.util.ChunkCoordinate.CHUNK_Z_SIZE;

import com.khorn.terraincontrol.LocalMaterialData;
import com.khorn.terraincontrol.LocalWorld;
import com.khorn.terraincontrol.events.EventHandler;
import com.khorn.terraincontrol.forge.ForgeWorld;
import com.khorn.terraincontrol.generator.resource.*;
import com.khorn.terraincontrol.util.minecraftTypes.DefaultMaterial;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.terraingen.*;
import net.minecraftforge.event.terraingen.DecorateBiomeEvent.Decorate;
import net.minecraftforge.event.terraingen.OreGenEvent.GenerateMinable;
import net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Translates TerrainControl events into MinecraftForge terrain events.
 */
public class EventManager extends EventHandler
{
    /*
     * Forge uses the *block* x and z, Terrain Control and Forge Modloader use
     * *chunk* x and z. Be very careful on which one you use.
     *
     * Implementations of these events in the world (may be useful as testing
     * material, or to verify whether blockX or chunkX needs to be used):
     *
     * https://github.com/Vazkii/Botania/blob/da52e52d1707e596cf8c7307b3ebc40a7682a5d3/MODSRC/vazkii/botania/common/core/handler/BiomeDecorationHandler.java#L24
     * https://github.com/modmuss50/Transcraft/blob/71b89d89b209619c514c3068b0268d8b1704b8ea/src/main/java/modmuss50/mods/transcraft/WorldGen/FlowerGen.java#L15
     * https://github.com/Andy5823/Hardmode/blob/029e8ca9673f935ed12c8c46e23ab50dc5a1281c/common/Hardmode/WorldGen/HardmodeWorldGenHandler.java#L11
     *
     * A simple search on Github should yield many more results.
     */

    // Two maps, key is world name, value is boolean
    private Map<String, Boolean> hasOreGenBegun = new HashMap<String, Boolean>();
    private Map<String, Boolean> hasDecorationBegun = new HashMap<String, Boolean>();

    @Override
    public boolean onResourceProcess(Resource resource, LocalWorld localWorld, Random random, boolean villageInChunk, int chunkX, int chunkZ, boolean isCancelled)
    {
        ForgeWorld world = (ForgeWorld) localWorld;
        int blockX = chunkX * CHUNK_X_SIZE;
        int blockZ = chunkZ * CHUNK_Z_SIZE;

        // Convert to Forge event and fire
        if (resource instanceof DungeonGen ||
                resource instanceof SmallLakeGen ||
                resource instanceof UndergroundLakeGen ||
                resource instanceof LiquidGen ||
                resource instanceof CustomObjectGen)
        {
            // Fire population event
            Populate.EventType forgeEvent = getPopulateEventType(resource.getMaterial());
            return TerrainGen.populate(world.getChunkGenerator(), world.getWorld(), random, blockX, blockZ,
                    villageInChunk, forgeEvent);
        } else if (resource instanceof OreGen)
        {
            if (!hasOreGenerationBegun(world))
            {
                // Fire ore generation start event
                MinecraftForge.ORE_GEN_BUS
                        .post(new OreGenEvent.Pre(world.getWorld(), random, blockX, blockZ));
                setOreGenerationBegun(world, true);
            }
            // Fire ore generation event
            GenerateMinable.EventType forgeEvent = getOreEventType(resource.getMaterial());
            return TerrainGen.generateOre(world.getWorld(), random, null, blockX, blockZ, forgeEvent);
        } else
        {
            if (!hasDecorationBegun(world))
            {
                // Fire decoration start event
                MinecraftForge.EVENT_BUS.post(new DecorateBiomeEvent.Pre(world.getWorld(), random, blockX, chunkZ
                        * CHUNK_Z_SIZE));
                setDecorationBegun(world, true);
            }
            // Fire decoration event
            Decorate.EventType forgeEvent = getDecorateEventType(resource.getMaterial());
            return TerrainGen.decorate(world.getWorld(), random, blockX, blockZ, forgeEvent);
        }
    }

    @Override
    public void onPopulateStart(LocalWorld localWorld, Random random, boolean villageInChunk, int chunkX, int chunkZ)
    {
        ForgeWorld world = (ForgeWorld) localWorld;

        // Reset states
        setDecorationBegun(world, false);
        setOreGenerationBegun(world, false);

        // Fire event
        PopulateChunkEvent forgeEvent = new PopulateChunkEvent.Pre(world.getChunkGenerator(), world.getWorld(), random, chunkX
                * CHUNK_X_SIZE, chunkZ * CHUNK_Z_SIZE, villageInChunk);
        MinecraftForge.EVENT_BUS.post(forgeEvent);
    }

    @Override
    public void onPopulateEnd(LocalWorld localWorld, Random random, boolean villageInChunk, int chunkX, int chunkZ)
    {
        ForgeWorld world = (ForgeWorld) localWorld;
        int blockX = chunkX * CHUNK_X_SIZE;
        int blockZ = chunkZ * CHUNK_Z_SIZE;

        // Fire all events

        // Decoration close
        if (hasDecorationBegun(world))
        {
            MinecraftForge.EVENT_BUS.post(new DecorateBiomeEvent.Post(world.getWorld(), random, blockX, blockZ));
            setDecorationBegun(world, false);
        }

        // Ore generation close
        if (hasOreGenerationBegun(world))
        {
            MinecraftForge.EVENT_BUS.post(new OreGenEvent.Post(world.getWorld(), random, blockX, blockZ));
            setOreGenerationBegun(world, false);
        }

        // Population close
        final PopulateChunkEvent forgeEvent = new PopulateChunkEvent.Post(world.getChunkGenerator(), world.getWorld(), random, blockX,
                blockZ, villageInChunk);
        MinecraftForge.EVENT_BUS.post(forgeEvent);

        // Population close (FML and ModLoader style)
        GameRegistry.generateWorld(chunkX, chunkZ, world.getWorld(), world.getChunkGenerator(), world.getChunkGenerator());
    }

    private Decorate.EventType getDecorateEventType(LocalMaterialData block)
    {
        if (block == null)
        {
            // Some resources don't have a main material
            return Decorate.EventType.CUSTOM;
        }
        if (block.isMaterial(DefaultMaterial.WATER_LILY))
            return Decorate.EventType.LILYPAD;
        if (block.isMaterial(DefaultMaterial.CACTUS))
            return Decorate.EventType.CACTUS;
        if (block.isMaterial(DefaultMaterial.LONG_GRASS))
            return Decorate.EventType.GRASS;
        if (block.isMaterial(DefaultMaterial.DEAD_BUSH))<commit_msg>Removed call to GameRegistry.generateWorld from Forge event manager.<commit_after>package com.khorn.terraincontrol.forge.events;

import static com.khorn.terraincontrol.util.ChunkCoordinate.CHUNK_X_SIZE;
import static com.khorn.terraincontrol.util.ChunkCoordinate.CHUNK_Z_SIZE;

import com.khorn.terraincontrol.LocalMaterialData;
import com.khorn.terraincontrol.LocalWorld;
import com.khorn.terraincontrol.events.EventHandler;
import com.khorn.terraincontrol.forge.ForgeWorld;
import com.khorn.terraincontrol.generator.resource.*;
import com.khorn.terraincontrol.util.minecraftTypes.DefaultMaterial;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.terraingen.*;
import net.minecraftforge.event.terraingen.DecorateBiomeEvent.Decorate;
import net.minecraftforge.event.terraingen.OreGenEvent.GenerateMinable;
import net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Translates TerrainControl events into MinecraftForge terrain events.
 */
public class EventManager extends EventHandler
{
    /*
     * Forge uses the *block* x and z, Terrain Control and Forge Modloader use
     * *chunk* x and z. Be very careful on which one you use.
     *
     * Implementations of these events in the world (may be useful as testing
     * material, or to verify whether blockX or chunkX needs to be used):
     *
     * https://github.com/Vazkii/Botania/blob/da52e52d1707e596cf8c7307b3ebc40a7682a5d3/MODSRC/vazkii/botania/common/core/handler/BiomeDecorationHandler.java#L24
     * https://github.com/modmuss50/Transcraft/blob/71b89d89b209619c514c3068b0268d8b1704b8ea/src/main/java/modmuss50/mods/transcraft/WorldGen/FlowerGen.java#L15
     * https://github.com/Andy5823/Hardmode/blob/029e8ca9673f935ed12c8c46e23ab50dc5a1281c/common/Hardmode/WorldGen/HardmodeWorldGenHandler.java#L11
     *
     * A simple search on Github should yield many more results.
     */

    // Two maps, key is world name, value is boolean
    private Map<String, Boolean> hasOreGenBegun = new HashMap<String, Boolean>();
    private Map<String, Boolean> hasDecorationBegun = new HashMap<String, Boolean>();

    @Override
    public boolean onResourceProcess(Resource resource, LocalWorld localWorld, Random random, boolean villageInChunk, int chunkX, int chunkZ, boolean isCancelled)
    {
        ForgeWorld world = (ForgeWorld) localWorld;
        int blockX = chunkX * CHUNK_X_SIZE;
        int blockZ = chunkZ * CHUNK_Z_SIZE;

        // Convert to Forge event and fire
        if (resource instanceof DungeonGen ||
                resource instanceof SmallLakeGen ||
                resource instanceof UndergroundLakeGen ||
                resource instanceof LiquidGen ||
                resource instanceof CustomObjectGen)
        {
            // Fire population event
            Populate.EventType forgeEvent = getPopulateEventType(resource.getMaterial());
            return TerrainGen.populate(world.getChunkGenerator(), world.getWorld(), random, blockX, blockZ,
                    villageInChunk, forgeEvent);
        } else if (resource instanceof OreGen)
        {
            if (!hasOreGenerationBegun(world))
            {
                // Fire ore generation start event
                MinecraftForge.ORE_GEN_BUS
                        .post(new OreGenEvent.Pre(world.getWorld(), random, blockX, blockZ));
                setOreGenerationBegun(world, true);
            }
            // Fire ore generation event
            GenerateMinable.EventType forgeEvent = getOreEventType(resource.getMaterial());
            return TerrainGen.generateOre(world.getWorld(), random, null, blockX, blockZ, forgeEvent);
        } else
        {
            if (!hasDecorationBegun(world))
            {
                // Fire decoration start event
                MinecraftForge.EVENT_BUS.post(new DecorateBiomeEvent.Pre(world.getWorld(), random, blockX, chunkZ
                        * CHUNK_Z_SIZE));
                setDecorationBegun(world, true);
            }
            // Fire decoration event
            Decorate.EventType forgeEvent = getDecorateEventType(resource.getMaterial());
            return TerrainGen.decorate(world.getWorld(), random, blockX, blockZ, forgeEvent);
        }
    }

    @Override
    public void onPopulateStart(LocalWorld localWorld, Random random, boolean villageInChunk, int chunkX, int chunkZ)
    {
        ForgeWorld world = (ForgeWorld) localWorld;

        // Reset states
        setDecorationBegun(world, false);
        setOreGenerationBegun(world, false);

        // Fire event
        PopulateChunkEvent forgeEvent = new PopulateChunkEvent.Pre(world.getChunkGenerator(), world.getWorld(), random, chunkX
                * CHUNK_X_SIZE, chunkZ * CHUNK_Z_SIZE, villageInChunk);
        MinecraftForge.EVENT_BUS.post(forgeEvent);
    }

    @Override
    public void onPopulateEnd(LocalWorld localWorld, Random random, boolean villageInChunk, int chunkX, int chunkZ)
    {
        ForgeWorld world = (ForgeWorld) localWorld;
        int blockX = chunkX * CHUNK_X_SIZE;
        int blockZ = chunkZ * CHUNK_Z_SIZE;

        // Fire all events

        // Decoration close
        if (hasDecorationBegun(world))
        {
            MinecraftForge.EVENT_BUS.post(new DecorateBiomeEvent.Post(world.getWorld(), random, blockX, blockZ));
            setDecorationBegun(world, false);
        }

        // Ore generation close
        if (hasOreGenerationBegun(world))
        {
            MinecraftForge.EVENT_BUS.post(new OreGenEvent.Post(world.getWorld(), random, blockX, blockZ));
            setOreGenerationBegun(world, false);
        }

        // Population close
        final PopulateChunkEvent forgeEvent = new PopulateChunkEvent.Post(world.getChunkGenerator(), world.getWorld(), random, blockX,
                blockZ, villageInChunk);
        MinecraftForge.EVENT_BUS.post(forgeEvent);

        // There is no need to call GameRegistry.generateWorld, because it is done by the Chunk Provider in Forge.
    }

    private Decorate.EventType getDecorateEventType(LocalMaterialData block)
    {
        if (block == null)
        {
            // Some resources don't have a main material
            return Decorate.EventType.CUSTOM;
        }
        if (block.isMaterial(DefaultMaterial.WATER_LILY))
            return Decorate.EventType.LILYPAD;
        if (block.isMaterial(DefaultMaterial.CACTUS))
            return Decorate.EventType.CACTUS;
        if (block.isMaterial(DefaultMaterial.LONG_GRASS))
            return Decorate.EventType.GRASS;
        if (block.isMaterial(DefaultMaterial.DEAD_BUSH))
{'fraction_non_alphanumeric': 0.08527707621452979, 'fraction_numerical': 0.013816669142772247, 'mean_word_length': 2.8843046739757647, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5632528', 'n_tokens_mistral': 4052, 'n_tokens_neox': 3883, 'n_words': 818}