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.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.NoneFeatureConfiguration;

public class BonusChestFeature extends Feature<NoneFeatureConfiguration> {
    public BonusChestFeature(final Function<Dynamic<?>, ? extends NoneFeatureConfiguration> function) {
        super(function);
    }
    
    @Override
    public boolean place(final LevelAccessor bjx, final ChunkGenerator<? extends ChunkGeneratorSettings> caa, final Random random, final BlockPos fk, final NoneFeatureConfiguration cgv) {
        final ChunkPos bjh7 = new ChunkPos(fk);
        final List<Integer> list8 = IntStream.rangeClosed(bjh7.getMinBlockX(), bjh7.getMaxBlockX()).boxed().collect(Collectors.toList());
        Collections.shuffle(list8, random);
        final List<Integer> list9 = IntStream.rangeClosed(bjh7.getMinBlockZ(), bjh7.getMaxBlockZ()).boxed().collect(Collectors.toList());
        Collections.shuffle(list9, random);
        final BlockPos.MutableBlockPos a10 = new BlockPos.MutableBlockPos();
        for (final Integer integer12 : list8) {
            for (final Integer integer13 : list9) {
                a10.set(integer12, 0, integer13);
                final BlockPos fk2 = bjx.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, a10);
                if (bjx.isEmptyBlock(fk2) || bjx.getBlockState(fk2).getCollisionShape(bjx, fk2).isEmpty()) {
                    bjx.setBlock(fk2, Blocks.CHEST.defaultBlockState(), 2);
                    RandomizableContainerBlockEntity.setLootTable(bjx, random, fk2, BuiltInLootTables.SPAWN_BONUS_CHEST);
                    final BlockState byj16 = Blocks.TORCH.defaultBlockState();
                    for (final Direction fp18 : Direction.Plane.HORIZONTAL) {
                        final BlockPos fk3 = fk2.relative(fp18);
                        if (byj16.canSurvive(bjx, fk3)) {
                            bjx.setBlock(fk3, byj16, 2);
                        }
                    }
                    return true;
                }
            }
        }
        return false;
    }
}
