package net.minecraft.world.level.levelgen.feature;

import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.StructureFeatureManager;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.LevelWriter;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.core.BlockPos;
import java.util.Random;
import net.minecraft.world.level.LevelAccessor;
import com.mojang.serialization.Codec;
import net.minecraft.world.level.levelgen.feature.configurations.HugeMushroomFeatureConfiguration;

public abstract class AbstractHugeMushroomFeature extends Feature<HugeMushroomFeatureConfiguration> {
    public AbstractHugeMushroomFeature(final Codec<HugeMushroomFeatureConfiguration> codec) {
        super(codec);
    }
    
    protected void placeTrunk(final LevelAccessor bqc, final Random random, final BlockPos fu, final HugeMushroomFeatureConfiguration cnt, final int integer, final BlockPos.MutableBlockPos a) {
        for (int integer2 = 0; integer2 < integer; ++integer2) {
            a.set(fu).move(Direction.UP, integer2);
            if (!bqc.getBlockState(a).isSolidRender(bqc, a)) {
                this.setBlock(bqc, a, cnt.stemProvider.getState(random, fu));
            }
        }
    }
    
    protected int getTreeHeight(final Random random) {
        int integer3 = random.nextInt(3) + 4;
        if (random.nextInt(12) == 0) {
            integer3 *= 2;
        }
        return integer3;
    }
    
    protected boolean isValidPosition(final LevelAccessor bqc, final BlockPos fu, final int integer, final BlockPos.MutableBlockPos a, final HugeMushroomFeatureConfiguration cnt) {
        final int integer2 = fu.getY();
        if (integer2 < 1 || integer2 + integer + 1 >= 256) {
            return false;
        }
        final Block bvr8 = bqc.getBlockState(fu.below()).getBlock();
        if (!Feature.isDirt(bvr8)) {
            return false;
        }
        for (int integer3 = 0; integer3 <= integer; ++integer3) {
            for (int integer4 = this.getTreeRadiusForHeight(-1, -1, cnt.foliageRadius, integer3), integer5 = -integer4; integer5 <= integer4; ++integer5) {
                for (int integer6 = -integer4; integer6 <= integer4; ++integer6) {
                    final BlockState cfj13 = bqc.getBlockState(a.setWithOffset(fu, integer5, integer3, integer6));
                    if (!cfj13.isAir() && !cfj13.is(BlockTags.LEAVES)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    
    @Override
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BlockPos fu, final HugeMushroomFeatureConfiguration cnt) {
        final int integer8 = this.getTreeHeight(random);
        final BlockPos.MutableBlockPos a9 = new BlockPos.MutableBlockPos();
        if (!this.isValidPosition(bqu, fu, integer8, a9, cnt)) {
            return false;
        }
        this.makeCap(bqu, random, fu, integer8, a9, cnt);
        this.placeTrunk(bqu, random, fu, cnt, integer8, a9);
        return true;
    }
    
    protected abstract int getTreeRadiusForHeight(final int integer1, final int integer2, final int integer3, final int integer4);
    
    protected abstract void makeCap(final LevelAccessor bqc, final Random random, final BlockPos fu, final int integer, final BlockPos.MutableBlockPos a, final HugeMushroomFeatureConfiguration cnt);
}
