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.levelgen.ChunkGeneratorSettings;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.LevelAccessor;
import com.mojang.datafixers.Dynamic;
import java.util.function.Function;
import net.minecraft.world.level.levelgen.feature.configurations.EndGatewayConfiguration;

public class EndGatewayFeature extends Feature<EndGatewayConfiguration> {
    public EndGatewayFeature(final Function<Dynamic<?>, ? extends EndGatewayConfiguration> function) {
        super(function);
    }
    
    @Override
    public boolean place(final LevelAccessor bjx, final ChunkGenerator<? extends ChunkGeneratorSettings> caa, final Random random, final BlockPos fk, final EndGatewayConfiguration cgm) {
        for (final BlockPos fk5 : BlockPos.betweenClosed(fk.offset(-1, -2, -1), fk.offset(1, 2, 1))) {
            final boolean boolean9 = fk5.getX() == fk.getX();
            final boolean boolean10 = fk5.getY() == fk.getY();
            final boolean boolean11 = fk5.getZ() == fk.getZ();
            final boolean boolean12 = Math.abs(fk5.getY() - fk.getY()) == 2;
            if (boolean9 && boolean10 && boolean11) {
                final BlockPos fk6 = fk5.immutable();
                this.setBlock(bjx, fk6, Blocks.END_GATEWAY.defaultBlockState());
                final BlockEntity bwl5;
                TheEndGatewayBlockEntity bxm6;
                cgm.getExit().ifPresent(fk4 -> {
                    bwl5 = bjx.getBlockEntity(fk6);
                    if (bwl5 instanceof TheEndGatewayBlockEntity) {
                        bxm6 = (TheEndGatewayBlockEntity)bwl5;
                        bxm6.setExitPosition(fk4, cgm.isExitExact());
                        bwl5.setChanged();
                    }
                    return;
                });
            }
            else if (boolean10) {
                this.setBlock(bjx, fk5, Blocks.AIR.defaultBlockState());
            }
            else if (boolean12 && boolean9 && boolean11) {
                this.setBlock(bjx, fk5, Blocks.BEDROCK.defaultBlockState());
            }
            else if ((!boolean9 && !boolean11) || boolean12) {
                this.setBlock(bjx, fk5, Blocks.AIR.defaultBlockState());
            }
            else {
                this.setBlock(bjx, fk5, Blocks.BEDROCK.defaultBlockState());
            }
        }
        return true;
    }
}
