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

import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.block.entity.BlockEntity;
import java.util.Iterator;
import net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity;
import net.minecraft.world.level.LevelWriter;
import net.minecraft.world.level.block.Blocks;
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 com.mojang.serialization.Codec;
import net.minecraft.world.level.levelgen.feature.configurations.EndGatewayConfiguration;

public class EndGatewayFeature extends Feature<EndGatewayConfiguration> {
    public EndGatewayFeature(final Codec<EndGatewayConfiguration> codec) {
        super(codec);
    }
    
    @Override
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BlockPos fu, final EndGatewayConfiguration cnq) {
        for (final BlockPos fu5 : BlockPos.betweenClosed(fu.offset(-1, -2, -1), fu.offset(1, 2, 1))) {
            final boolean boolean10 = fu5.getX() == fu.getX();
            final boolean boolean11 = fu5.getY() == fu.getY();
            final boolean boolean12 = fu5.getZ() == fu.getZ();
            final boolean boolean13 = Math.abs(fu5.getY() - fu.getY()) == 2;
            if (boolean10 && boolean11 && boolean12) {
                final BlockPos fu6 = fu5.immutable();
                this.setBlock(bqu, fu6, Blocks.END_GATEWAY.defaultBlockState());
                final BlockEntity cdl5;
                TheEndGatewayBlockEntity cem6;
                cnq.getExit().ifPresent(fu4 -> {
                    cdl5 = bqu.getBlockEntity(fu6);
                    if (cdl5 instanceof TheEndGatewayBlockEntity) {
                        cem6 = (TheEndGatewayBlockEntity)cdl5;
                        cem6.setExitPosition(fu4, cnq.isExitExact());
                        cdl5.setChanged();
                    }
                    return;
                });
            }
            else if (boolean11) {
                this.setBlock(bqu, fu5, Blocks.AIR.defaultBlockState());
            }
            else if (boolean13 && boolean10 && boolean12) {
                this.setBlock(bqu, fu5, Blocks.BEDROCK.defaultBlockState());
            }
            else if ((!boolean10 && !boolean12) || boolean13) {
                this.setBlock(bqu, fu5, Blocks.AIR.defaultBlockState());
            }
            else {
                this.setBlock(bqu, fu5, Blocks.BEDROCK.defaultBlockState());
            }
        }
        return true;
    }
}
