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

import com.mojang.datafixers.kinds.Applicative;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.core.BlockPos;
import java.util.Set;
import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration;
import java.util.Random;
import net.minecraft.world.level.LevelSimulatedRW;
import com.mojang.datafixers.kinds.App;
import com.mojang.datafixers.Products;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mojang.serialization.Codec;

public class BlobFoliagePlacer extends FoliagePlacer {
    public static final Codec<BlobFoliagePlacer> CODEC;
    protected final int height;
    
    protected static <P extends BlobFoliagePlacer> Products.P5<RecordCodecBuilder.Mu<P>, Integer, Integer, Integer, Integer, Integer> blobParts(final RecordCodecBuilder.Instance<P> instance) {
        return (Products.P5<RecordCodecBuilder.Mu<P>, Integer, Integer, Integer, Integer, Integer>)FoliagePlacer.<P>foliagePlacerParts(instance).and((App)Codec.INT.fieldOf("height").forGetter(cpc -> cpc.height));
    }
    
    protected BlobFoliagePlacer(final int integer1, final int integer2, final int integer3, final int integer4, final int integer5, final FoliagePlacerType<?> cph) {
        super(integer1, integer2, integer3, integer4);
        this.height = integer5;
    }
    
    public BlobFoliagePlacer(final int integer1, final int integer2, final int integer3, final int integer4, final int integer5) {
        this(integer1, integer2, integer3, integer4, integer5, FoliagePlacerType.BLOB_FOLIAGE_PLACER);
    }
    
    @Override
    protected FoliagePlacerType<?> type() {
        return FoliagePlacerType.BLOB_FOLIAGE_PLACER;
    }
    
    @Override
    protected void createFoliage(final LevelSimulatedRW bqf, final Random random, final TreeConfiguration cou, final int integer4, final FoliageAttachment b, final int integer6, final int integer7, final Set<BlockPos> set, final int integer9, final BoundingBox ctd) {
        for (int integer10 = integer9; integer10 >= integer9 - integer6; --integer10) {
            final int integer11 = Math.max(integer7 + b.radiusOffset() - 1 - integer10 / 2, 0);
            this.placeLeavesRow(bqf, random, cou, b.foliagePos(), integer11, set, integer10, b.doubleTrunk(), ctd);
        }
    }
    
    @Override
    public int foliageHeight(final Random random, final int integer, final TreeConfiguration cou) {
        return this.height;
    }
    
    @Override
    protected boolean shouldSkipLocation(final Random random, final int integer2, final int integer3, final int integer4, final int integer5, final boolean boolean6) {
        return integer2 == integer5 && integer4 == integer5 && (random.nextInt(2) == 0 || integer3 == 0);
    }
    
    static {
        CODEC = RecordCodecBuilder.create(instance -> BlobFoliagePlacer.<BlobFoliagePlacer>blobParts((RecordCodecBuilder.Instance<BlobFoliagePlacer>)instance).apply(instance, BlobFoliagePlacer::new));
    }
}
