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

import net.minecraft.world.level.block.state.AbstractStateHolder;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.core.Registry;
import com.mojang.datafixers.types.DynamicOps;
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.AbstractTreeFeature;
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.datafixers.Dynamic;

public class BeehiveDecorator extends TreeDecorator {
    private final float probability;
    
    public BeehiveDecorator(final float float1) {
        super(TreeDecoratorType.BEEHIVE);
        this.probability = float1;
    }
    
    public <T> BeehiveDecorator(final Dynamic<T> dynamic) {
        this(dynamic.get("probability").asFloat(0.0f));
    }
    
    @Override
    public void place(final LevelAccessor bjx, final Random random, final List<BlockPos> list3, final List<BlockPos> list4, final Set<BlockPos> set, final BoundingBox clb) {
        if (random.nextFloat() >= this.probability) {
            return;
        }
        final Direction fp8 = BeehiveBlock.SPAWN_DIRECTIONS[random.nextInt(BeehiveBlock.SPAWN_DIRECTIONS.length)];
        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(fk -> fk.getY() == integer9).collect(Collectors.toList());
        if (list5.isEmpty()) {
            return;
        }
        final BlockPos fk2 = list5.get(random.nextInt(list5.size()));
        final BlockPos fk3 = fk2.relative(fp8);
        if (!AbstractTreeFeature.isAir(bjx, fk3) || !AbstractTreeFeature.isAir(bjx, fk3.relative(Direction.SOUTH))) {
            return;
        }
        final BlockState byj13 = ((AbstractStateHolder<O, BlockState>)Blocks.BEE_NEST.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)BeehiveBlock.FACING, Direction.SOUTH);
        this.setBlock(bjx, fk3, byj13, set, clb);
        final BlockEntity bwl14 = bjx.getBlockEntity(fk3);
        if (bwl14 instanceof BeehiveBlockEntity) {
            final BeehiveBlockEntity bwi15 = (BeehiveBlockEntity)bwl14;
            for (int integer10 = 2 + random.nextInt(2), integer11 = 0; integer11 < integer10; ++integer11) {
                final Bee atd18 = new Bee(EntityType.BEE, bjx.getLevel());
                bwi15.addOccupantWithPresetTicks(atd18, false, random.nextInt(599));
            }
        }
    }
    
    @Override
    public <T> T serialize(final DynamicOps<T> dynamicOps) {
        return (T)new Dynamic((DynamicOps)dynamicOps, dynamicOps.createMap((Map)ImmutableMap.of(dynamicOps.createString("type"), dynamicOps.createString(Registry.TREE_DECORATOR_TYPES.getKey(this.type).toString()), dynamicOps.createString("probability"), dynamicOps.createFloat(this.probability)))).getValue();
    }
}
