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

import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Iterator;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.levelgen.Heightmap;
import java.util.Collections;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.List;
import net.minecraft.world.level.ChunkPos;
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.NoneFeatureConfiguration;

public class BonusChestFeature extends Feature<NoneFeatureConfiguration> {
    public BonusChestFeature(final Codec<NoneFeatureConfiguration> codec) {
        super(codec);
    }
    
    @Override
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BlockPos fu, final NoneFeatureConfiguration coa) {
        final ChunkPos bph8 = new ChunkPos(fu);
        final List<Integer> list9 = IntStream.rangeClosed(bph8.getMinBlockX(), bph8.getMaxBlockX()).boxed().collect(Collectors.toList());
        Collections.shuffle(list9, random);
        final List<Integer> list10 = IntStream.rangeClosed(bph8.getMinBlockZ(), bph8.getMaxBlockZ()).boxed().collect(Collectors.toList());
        Collections.shuffle(list10, random);
        final BlockPos.MutableBlockPos a11 = new BlockPos.MutableBlockPos();
        for (final Integer integer13 : list9) {
            for (final Integer integer14 : list10) {
                a11.set(integer13, 0, integer14);
                final BlockPos fu2 = bqu.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, a11);
                if (bqu.isEmptyBlock(fu2) || bqu.getBlockState(fu2).getCollisionShape(bqu, fu2).isEmpty()) {
                    bqu.setBlock(fu2, Blocks.CHEST.defaultBlockState(), 2);
                    RandomizableContainerBlockEntity.setLootTable(bqu, random, fu2, BuiltInLootTables.SPAWN_BONUS_CHEST);
                    final BlockState cfj17 = Blocks.TORCH.defaultBlockState();
                    for (final Direction fz19 : Direction.Plane.HORIZONTAL) {
                        final BlockPos fu3 = fu2.relative(fz19);
                        if (cfj17.canSurvive(bqu, fu3)) {
                            bqu.setBlock(fu3, cfj17, 2);
                        }
                    }
                    return true;
                }
            }
        }
        return false;
    }
}
