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

import net.minecraft.world.level.block.state.StateHolder;
import java.util.function.Function;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.animal.Bee;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
import net.minecraft.world.level.LevelWriter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelSimulatedReader;
import net.minecraft.world.level.levelgen.feature.Feature;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import net.minecraft.world.level.block.BeehiveBlock;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import java.util.Set;
import net.minecraft.core.BlockPos;
import java.util.List;
import java.util.Random;
import net.minecraft.world.level.LevelAccessor;
import com.mojang.serialization.Codec;

public class BeehiveDecorator extends TreeDecorator {
    public static final Codec<BeehiveDecorator> CODEC;
    private final float probability;
    
    public BeehiveDecorator(final float float1) {
        this.probability = float1;
    }
    
    @Override
    protected TreeDecoratorType<?> type() {
        return TreeDecoratorType.BEEHIVE;
    }
    
    @Override
    public void place(final LevelAccessor bqc, final Random random, final List<BlockPos> list3, final List<BlockPos> list4, final Set<BlockPos> set, final BoundingBox ctd) {
        if (random.nextFloat() >= this.probability) {
            return;
        }
        final Direction fz8 = BeehiveBlock.getRandomOffset(random);
        final int integer9 = list4.isEmpty() ? Math.min(list3.get(0).getY() + 1 + random.nextInt(3), list3.get(list3.size() - 1).getY()) : Math.max(list4.get(0).getY() - 1, list3.get(0).getY());
        final List<BlockPos> list5 = list3.stream().filter(fu -> fu.getY() == integer9).collect(Collectors.toList());
        if (list5.isEmpty()) {
            return;
        }
        final BlockPos fu2 = list5.get(random.nextInt(list5.size()));
        final BlockPos fu3 = fu2.relative(fz8);
        if (!Feature.isAir(bqc, fu3) || !Feature.isAir(bqc, fu3.relative(Direction.SOUTH))) {
            return;
        }
        final BlockState cfj13 = ((StateHolder<O, BlockState>)Blocks.BEE_NEST.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)BeehiveBlock.FACING, Direction.SOUTH);
        this.setBlock(bqc, fu3, cfj13, set, ctd);
        final BlockEntity cdl14 = bqc.getBlockEntity(fu3);
        if (cdl14 instanceof BeehiveBlockEntity) {
            final BeehiveBlockEntity cdi15 = (BeehiveBlockEntity)cdl14;
            for (int integer10 = 2 + random.nextInt(2), integer11 = 0; integer11 < integer10; ++integer11) {
                final Bee ayl18 = new Bee(EntityType.BEE, bqc.getLevel());
                cdi15.addOccupantWithPresetTicks(ayl18, false, random.nextInt(599));
            }
        }
    }
    
    static {
        CODEC = Codec.FLOAT.fieldOf("probability").xmap((Function)BeehiveDecorator::new, cqj -> cqj.probability).codec();
    }
}
