
package org.spongepowered.common.mixin.core.world.gen;

import com.flowpowered.math.GenericMath;
import net.minecraft.world.chunk.ChunkPrimer;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.ChunkGeneratorEnd;
import net.minecraft.world.gen.structure.MapGenEndCity;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.extent.ImmutableBiomeVolume;
import org.spongepowered.api.world.extent.MutableBlockVolume;
import org.spongepowered.api.world.gen.GenerationPopulator;
import org.spongepowered.api.world.gen.Populator;
import org.spongepowered.api.world.gen.WorldGenerator;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.interfaces.world.gen.IPopulatorProvider;
import org.spongepowered.common.util.gen.ChunkBufferPrimer;

import java.util.Random;

@Mixin(ChunkGeneratorEnd.class)
public abstract class MixinChunkGeneratorEnd implements IChunkProvider, GenerationPopulator, IPopulatorProvider {

    @Shadow @Final public Random rand;

    @Shadow
    public abstract void setBlocksInChunk(int p_180520_1_, int p_180520_2_, ChunkPrimer p_180520_3_);

    @Shadow private @Final MapGenEndCity endCityGen;
    @Shadow private @Final boolean mapFeaturesEnabled;

    @Override
    public void addPopulators(WorldGenerator generator) {
        if (this.mapFeaturesEnabled) {
            generator.getPopulators().add((Populator) this.endCityGen);
            generator.getGenerationPopulators().add((GenerationPopulator) this.endCityGen);
        }
    }

    @Override
    public void populate(World world, MutableBlockVolume buffer, ImmutableBiomeVolume biomes) {
        int x = GenericMath.floor(buffer.getBlockMin().getX() / 16f);
        int z = GenericMath.floor(buffer.getBlockMin().getZ() / 16f);
        ChunkPrimer chunkprimer = new ChunkBufferPrimer(buffer);
        this.rand.setSeed((long) x * 341873128712L + (long) z * 132897987541L);
        this.setBlocksInChunk(x, z, chunkprimer);
    }

}
