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

import net.minecraft.world.level.block.state.StateHolder;
import java.util.function.Function;
import java.util.Iterator;
import net.minecraft.world.level.LevelWriter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.CocoaBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.LevelSimulatedReader;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.core.Direction;
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 CocoaDecorator extends TreeDecorator {
    public static final Codec<CocoaDecorator> CODEC;
    private final float probability;
    
    public CocoaDecorator(final float float1) {
        this.probability = float1;
    }
    
    @Override
    protected TreeDecoratorType<?> type() {
        return TreeDecoratorType.COCOA;
    }
    
    @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 int integer8 = list3.get(0).getY();
        final Iterator<Direction> iterator;
        Direction fz8;
        Direction fz9;
        BlockPos fu2;
        BlockState cfj11;
        list3.stream().filter(fu -> fu.getY() - integer8 <= 2).forEach(fu -> {
            Direction.Plane.HORIZONTAL.iterator();
            while (iterator.hasNext()) {
                fz8 = iterator.next();
                if (random.nextFloat() <= 0.25f) {
                    fz9 = fz8.getOpposite();
                    fu2 = fu.offset(fz9.getStepX(), 0, fz9.getStepZ());
                    if (Feature.isAir(bqc, fu2)) {
                        cfj11 = (((StateHolder<O, BlockState>)Blocks.COCOA.defaultBlockState()).setValue((Property<Comparable>)CocoaBlock.AGE, random.nextInt(3))).<Comparable, Direction>setValue((Property<Comparable>)CocoaBlock.FACING, fz8);
                        this.setBlock(bqc, fu2, cfj11, set, ctd);
                    }
                    else {
                        continue;
                    }
                }
            }
        });
    }
    
    static {
        CODEC = Codec.FLOAT.fieldOf("probability").xmap((Function)CocoaDecorator::new, cqk -> cqk.probability).codec();
    }
}
