package me.km.world;

import java.util.Set;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.provider.BiomeProvider;
import net.minecraft.world.biome.provider.OverworldBiomeProviderSettings;
import net.minecraft.world.gen.layer.Layer;
import net.minecraft.world.gen.layer.LayerUtil;

public class ModBiomeProvider extends BiomeProvider {
    private final Layer genBiomes;
    private final Biome[] biomeArray;

    public ModBiomeProvider(Set<Biome> biomes, OverworldBiomeProviderSettings settingsProvider) {
        super(biomes);
        biomeArray = biomes.toArray(new Biome[biomes.size()]);
        this.genBiomes = LayerUtil.func_227474_a_(settingsProvider.func_226850_a_(), settingsProvider.func_226851_b_(), settingsProvider.getGeneratorSettings());
    }

    @Override
    public Biome getNoiseBiome(int x, int y, int z) {
        Biome vanillaBiome = genBiomes.func_215738_a(x, z);

        float min = Float.MAX_VALUE;
        float min2 = Float.MAX_VALUE;
        Biome minBiome = null;
        Biome minBiome2 = null;
        for(Biome b : biomeArray) {
            float distance = Math.abs(b.getDepth() - vanillaBiome.getDepth());
            if(distance < min) {
                min2 = min;
                minBiome2 = minBiome;

                min = distance;
                minBiome = b;
            } else if(distance < min2) {
                min2 = distance;
                minBiome2 = b;
            }
        }

        if((Registry.BIOME.getId(vanillaBiome) % 2) == 1) {
            return minBiome;
        }
        return minBiome2;
    }
}
