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

import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import java.util.Iterator;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import java.util.Random;
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.Blocks;
import com.mojang.serialization.Codec;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.predicate.BlockStatePredicate;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;

public class DesertWellFeature extends Feature<NoneFeatureConfiguration> {
    private static final BlockStatePredicate IS_SAND;
    private final BlockState sandSlab;
    private final BlockState sandstone;
    private final BlockState water;
    
    public DesertWellFeature(final Codec<NoneFeatureConfiguration> codec) {
        super(codec);
        this.sandSlab = Blocks.SANDSTONE_SLAB.defaultBlockState();
        this.sandstone = Blocks.SANDSTONE.defaultBlockState();
        this.water = Blocks.WATER.defaultBlockState();
    }
    
    @Override
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, BlockPos fu, final NoneFeatureConfiguration coa) {
        for (fu = fu.above(); bqu.isEmptyBlock(fu) && fu.getY() > 2; fu = fu.below()) {}
        if (!DesertWellFeature.IS_SAND.test(bqu.getBlockState(fu))) {
            return false;
        }
        for (int integer8 = -2; integer8 <= 2; ++integer8) {
            for (int integer9 = -2; integer9 <= 2; ++integer9) {
                if (bqu.isEmptyBlock(fu.offset(integer8, -1, integer9)) && bqu.isEmptyBlock(fu.offset(integer8, -2, integer9))) {
                    return false;
                }
            }
        }
        for (int integer8 = -1; integer8 <= 0; ++integer8) {
            for (int integer9 = -2; integer9 <= 2; ++integer9) {
                for (int integer10 = -2; integer10 <= 2; ++integer10) {
                    bqu.setBlock(fu.offset(integer9, integer8, integer10), this.sandstone, 2);
                }
            }
        }
        bqu.setBlock(fu, this.water, 2);
        for (final Direction fz9 : Direction.Plane.HORIZONTAL) {
            bqu.setBlock(fu.relative(fz9), this.water, 2);
        }
        for (int integer8 = -2; integer8 <= 2; ++integer8) {
            for (int integer9 = -2; integer9 <= 2; ++integer9) {
                if (integer8 == -2 || integer8 == 2 || integer9 == -2 || integer9 == 2) {
                    bqu.setBlock(fu.offset(integer8, 1, integer9), this.sandstone, 2);
                }
            }
        }
        bqu.setBlock(fu.offset(2, 1, 0), this.sandSlab, 2);
        bqu.setBlock(fu.offset(-2, 1, 0), this.sandSlab, 2);
        bqu.setBlock(fu.offset(0, 1, 2), this.sandSlab, 2);
        bqu.setBlock(fu.offset(0, 1, -2), this.sandSlab, 2);
        for (int integer8 = -1; integer8 <= 1; ++integer8) {
            for (int integer9 = -1; integer9 <= 1; ++integer9) {
                if (integer8 == 0 && integer9 == 0) {
                    bqu.setBlock(fu.offset(integer8, 4, integer9), this.sandstone, 2);
                }
                else {
                    bqu.setBlock(fu.offset(integer8, 4, integer9), this.sandSlab, 2);
                }
            }
        }
        for (int integer8 = 1; integer8 <= 3; ++integer8) {
            bqu.setBlock(fu.offset(-1, integer8, -1), this.sandstone, 2);
            bqu.setBlock(fu.offset(-1, integer8, 1), this.sandstone, 2);
            bqu.setBlock(fu.offset(1, integer8, -1), this.sandstone, 2);
            bqu.setBlock(fu.offset(1, integer8, 1), this.sandstone, 2);
        }
        return true;
    }
    
    static {
        IS_SAND = BlockStatePredicate.forBlock(Blocks.SAND);
    }
}
