package net.minecraft.world.level.levelgen;

import net.minecraft.world.level.block.Blocks;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import net.minecraft.core.Registry;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.Decoder;
import com.mojang.serialization.Encoder;
import java.util.stream.Stream;
import net.minecraft.world.level.block.Block;
import net.minecraft.util.Mth;
import net.minecraft.world.level.NoiseColumn;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.StructureFeatureManager;
import net.minecraft.world.level.biome.BiomeManager;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.server.level.WorldGenRegion;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.biome.FixedBiomeSource;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.block.state.BlockState;
import java.util.List;
import com.mojang.serialization.Codec;
import net.minecraft.world.level.chunk.ChunkGenerator;

public class DebugLevelSource extends ChunkGenerator {
    public static final DebugLevelSource INSTANCE;
    public static final Codec<DebugLevelSource> CODEC;
    private static final List<BlockState> ALL_BLOCKS;
    private static final int GRID_WIDTH;
    private static final int GRID_HEIGHT;
    protected static final BlockState AIR;
    protected static final BlockState BARRIER;
    
    private DebugLevelSource() {
        super(new FixedBiomeSource(Biomes.PLAINS), new StructureSettings(false));
    }
    
    @Override
    protected Codec<? extends ChunkGenerator> codec() {
        return DebugLevelSource.CODEC;
    }
    
    @Override
    public ChunkGenerator withSeed(final long long1) {
        return this;
    }
    
    @Override
    public void buildSurfaceAndBedrock(final WorldGenRegion zj, final ChunkAccess cgy) {
    }
    
    @Override
    public void applyCarvers(final long long1, final BiomeManager brg, final ChunkAccess cgy, final GenerationStep.Carving a) {
    }
    
    @Override
    public void applyBiomeDecoration(final WorldGenRegion zj, final StructureFeatureManager bqq) {
        final BlockPos.MutableBlockPos a4 = new BlockPos.MutableBlockPos();
        final int integer5 = zj.getCenterX();
        final int integer6 = zj.getCenterZ();
        for (int integer7 = 0; integer7 < 16; ++integer7) {
            for (int integer8 = 0; integer8 < 16; ++integer8) {
                final int integer9 = (integer5 << 4) + integer7;
                final int integer10 = (integer6 << 4) + integer8;
                zj.setBlock(a4.set(integer9, 60, integer10), DebugLevelSource.BARRIER, 2);
                final BlockState cfj11 = getBlockStateFor(integer9, integer10);
                if (cfj11 != null) {
                    zj.setBlock(a4.set(integer9, 70, integer10), cfj11, 2);
                }
            }
        }
    }
    
    @Override
    public void fillFromNoise(final LevelAccessor bqc, final StructureFeatureManager bqq, final ChunkAccess cgy) {
    }
    
    @Override
    public int getBaseHeight(final int integer1, final int integer2, final Heightmap.Types a) {
        return 0;
    }
    
    @Override
    public BlockGetter getBaseColumn(final int integer1, final int integer2) {
        return new NoiseColumn(new BlockState[0]);
    }
    
    public static BlockState getBlockStateFor(int integer1, int integer2) {
        BlockState cfj3 = DebugLevelSource.AIR;
        if (integer1 > 0 && integer2 > 0 && integer1 % 2 != 0 && integer2 % 2 != 0) {
            integer1 /= 2;
            integer2 /= 2;
            if (integer1 <= DebugLevelSource.GRID_WIDTH && integer2 <= DebugLevelSource.GRID_HEIGHT) {
                final int integer3 = Mth.abs(integer1 * DebugLevelSource.GRID_WIDTH + integer2);
                if (integer3 < DebugLevelSource.ALL_BLOCKS.size()) {
                    cfj3 = DebugLevelSource.ALL_BLOCKS.get(integer3);
                }
            }
        }
        return cfj3;
    }
    
    static {
        INSTANCE = new DebugLevelSource();
        CODEC = MapCodec.of(Encoder.empty(), Decoder.unit(() -> DebugLevelSource.INSTANCE)).stable().codec();
        ALL_BLOCKS = StreamSupport.<Block>stream(Registry.BLOCK.spliterator(), false).flatMap(bvr -> bvr.getStateDefinition().getPossibleStates().stream()).collect(Collectors.toList());
        GRID_WIDTH = Mth.ceil(Mth.sqrt((float)DebugLevelSource.ALL_BLOCKS.size()));
        GRID_HEIGHT = Mth.ceil(DebugLevelSource.ALL_BLOCKS.size() / (float)DebugLevelSource.GRID_WIDTH);
        AIR = Blocks.AIR.defaultBlockState();
        BARRIER = Blocks.BARRIER.defaultBlockState();
    }
}
