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 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.AbstractTreeFeature;
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.datafixers.Dynamic;

public class CocoaDecorator extends TreeDecorator {
    private final float probability;
    
    public CocoaDecorator(final float float1) {
        super(TreeDecoratorType.COCOA);
        this.probability = float1;
    }
    
    public <T> CocoaDecorator(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 int integer8 = list3.get(0).getY();
        final Iterator<Direction> iterator;
        Direction fp8;
        Direction fp9;
        BlockPos fk2;
        BlockState byj11;
        list3.stream().filter(fk -> fk.getY() - integer8 <= 2).forEach(fk -> {
            Direction.Plane.HORIZONTAL.iterator();
            while (iterator.hasNext()) {
                fp8 = iterator.next();
                if (random.nextFloat() <= 0.25f) {
                    fp9 = fp8.getOpposite();
                    fk2 = fk.offset(fp9.getStepX(), 0, fp9.getStepZ());
                    if (AbstractTreeFeature.isAir(bjx, fk2)) {
                        byj11 = (((AbstractStateHolder<O, BlockState>)Blocks.COCOA.defaultBlockState()).setValue((Property<Comparable>)CocoaBlock.AGE, random.nextInt(3))).<Comparable, Direction>setValue((Property<Comparable>)CocoaBlock.FACING, fp8);
                        this.setBlock(bjx, fk2, byj11, set, clb);
                    }
                    else {
                        continue;
                    }
                }
            }
        });
    }
    
    @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();
    }
}
