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

import java.util.Optional;
import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration;
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.Random;
import net.minecraft.world.level.LevelSimulatedRW;
import com.mojang.datafixers.Dynamic;
import java.util.function.Function;
import net.minecraft.world.level.levelgen.feature.configurations.SmallTreeConfiguration;

public class AcaciaFeature extends AbstractSmallTreeFeature<SmallTreeConfiguration> {
    public AcaciaFeature(final Function<Dynamic<?>, ? extends SmallTreeConfiguration> function) {
        super(function);
    }
    
    public boolean doPlace(final LevelSimulatedRW bkb, final Random random, final BlockPos fk, final Set<BlockPos> set4, final Set<BlockPos> set5, final BoundingBox clb, final SmallTreeConfiguration chi) {
        final int integer9 = chi.baseHeight + random.nextInt(chi.heightRandA + 1) + random.nextInt(chi.heightRandB + 1);
        final int integer10 = (chi.trunkHeight >= 0) ? (chi.trunkHeight + random.nextInt(chi.trunkHeightRandom + 1)) : (integer9 - (chi.foliageHeight + random.nextInt(chi.foliageHeightRandom + 1)));
        final int integer11 = chi.foliagePlacer.foliageRadius(random, integer10, integer9, chi);
        final Optional<BlockPos> optional12 = this.getProjectedOrigin(bkb, integer9, integer10, integer11, fk, chi);
        if (!optional12.isPresent()) {
            return false;
        }
        final BlockPos fk2 = optional12.get();
        this.setDirtAt(bkb, fk2.below());
        final Direction fp14 = Direction.Plane.HORIZONTAL.getRandomDirection(random);
        final int integer12 = integer9 - random.nextInt(4) - 1;
        int integer13 = 3 - random.nextInt(3);
        final BlockPos.MutableBlockPos a17 = new BlockPos.MutableBlockPos();
        int integer14 = fk2.getX();
        int integer15 = fk2.getZ();
        int integer16 = 0;
        for (int integer17 = 0; integer17 < integer9; ++integer17) {
            final int integer18 = fk2.getY() + integer17;
            if (integer17 >= integer12 && integer13 > 0) {
                integer14 += fp14.getStepX();
                integer15 += fp14.getStepZ();
                --integer13;
            }
            if (this.placeLog(bkb, random, a17.set(integer14, integer18, integer15), set4, clb, chi)) {
                integer16 = integer18;
            }
        }
        final BlockPos fk3 = new BlockPos(integer14, integer16, integer15);
        chi.foliagePlacer.createFoliage(bkb, random, chi, integer9, integer10, integer11 + 1, fk3, set5);
        integer14 = fk2.getX();
        integer15 = fk2.getZ();
        final Direction fp15 = Direction.Plane.HORIZONTAL.getRandomDirection(random);
        if (fp15 != fp14) {
            final int integer18 = integer12 - random.nextInt(2) - 1;
            int integer19 = 1 + random.nextInt(3);
            integer16 = 0;
            for (int integer20 = integer18; integer20 < integer9 && integer19 > 0; ++integer20, --integer19) {
                if (integer20 >= 1) {
                    final int integer21 = fk2.getY() + integer20;
                    integer14 += fp15.getStepX();
                    integer15 += fp15.getStepZ();
                    if (this.placeLog(bkb, random, a17.set(integer14, integer21, integer15), set4, clb, chi)) {
                        integer16 = integer21;
                    }
                }
            }
            if (integer16 > 0) {
                final BlockPos fk4 = new BlockPos(integer14, integer16, integer15);
                chi.foliagePlacer.createFoliage(bkb, random, chi, integer9, integer10, integer11, fk4, set5);
            }
        }
        return true;
    }
}
