package net.minecraft.world.entity.ai.goal;

import net.minecraft.world.level.block.Block;
import java.util.Iterator;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.LeavesBlock;
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import javax.annotation.Nullable;
import net.minecraft.world.entity.ai.util.RandomPos;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.PathfinderMob;

public class WaterAvoidingRandomFlyingGoal extends WaterAvoidingRandomStrollGoal {
    public WaterAvoidingRandomFlyingGoal(final PathfinderMob alg, final double double2) {
        super(alg, double2);
    }
    
    @Nullable
    @Override
    protected Vec3 getPosition() {
        Vec3 cvl2 = null;
        if (this.mob.isInWater()) {
            cvl2 = RandomPos.getLandPos(this.mob, 15, 15);
        }
        if (this.mob.getRandom().nextFloat() >= this.probability) {
            cvl2 = this.getTreePos();
        }
        return (cvl2 == null) ? super.getPosition() : cvl2;
    }
    
    @Nullable
    private Vec3 getTreePos() {
        final BlockPos fk2 = new BlockPos(this.mob);
        final BlockPos.MutableBlockPos a3 = new BlockPos.MutableBlockPos();
        final BlockPos.MutableBlockPos a4 = new BlockPos.MutableBlockPos();
        final Iterable<BlockPos> iterable5 = BlockPos.betweenClosed(Mth.floor(this.mob.getX() - 3.0), Mth.floor(this.mob.getY() - 6.0), Mth.floor(this.mob.getZ() - 3.0), Mth.floor(this.mob.getX() + 3.0), Mth.floor(this.mob.getY() + 6.0), Mth.floor(this.mob.getZ() + 3.0));
        for (final BlockPos fk3 : iterable5) {
            if (fk2.equals(fk3)) {
                continue;
            }
            final Block bph8 = this.mob.level.getBlockState(a4.set(fk3).move(Direction.DOWN)).getBlock();
            final boolean boolean9 = bph8 instanceof LeavesBlock || bph8.is(BlockTags.LOGS);
            if (boolean9 && this.mob.level.isEmptyBlock(fk3) && this.mob.level.isEmptyBlock(a3.set(fk3).move(Direction.UP))) {
                return new Vec3(fk3);
            }
        }
        return null;
    }
}
