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

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.phys.Vec3;
import java.util.Iterator;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Mth;
import java.util.EnumSet;
import net.minecraft.world.entity.PathfinderMob;

public class BreathAirGoal extends Goal {
    private final PathfinderMob mob;
    
    public BreathAirGoal(final PathfinderMob alg) {
        this.mob = alg;
        this.setFlags(EnumSet.<Flag>of(Flag.MOVE, Flag.LOOK));
    }
    
    @Override
    public boolean canUse() {
        return this.mob.getAirSupply() < 140;
    }
    
    @Override
    public boolean canContinueToUse() {
        return this.canUse();
    }
    
    @Override
    public boolean isInterruptable() {
        return false;
    }
    
    @Override
    public void start() {
        this.findAirPosition();
    }
    
    private void findAirPosition() {
        final Iterable<BlockPos> iterable2 = BlockPos.betweenClosed(Mth.floor(this.mob.getX() - 1.0), Mth.floor(this.mob.getY()), Mth.floor(this.mob.getZ() - 1.0), Mth.floor(this.mob.getX() + 1.0), Mth.floor(this.mob.getY() + 8.0), Mth.floor(this.mob.getZ() + 1.0));
        BlockPos fk3 = null;
        for (final BlockPos fk4 : iterable2) {
            if (this.givesAir(this.mob.level, fk4)) {
                fk3 = fk4;
                break;
            }
        }
        if (fk3 == null) {
            fk3 = new BlockPos(this.mob.getX(), this.mob.getY() + 8.0, this.mob.getZ());
        }
        this.mob.getNavigation().moveTo(fk3.getX(), fk3.getY() + 1, fk3.getZ(), 1.0);
    }
    
    @Override
    public void tick() {
        this.findAirPosition();
        this.mob.moveRelative(0.02f, new Vec3(this.mob.xxa, this.mob.yya, this.mob.zza));
        this.mob.move(MoverType.SELF, this.mob.getDeltaMovement());
    }
    
    private boolean givesAir(final LevelReader bjz, final BlockPos fk) {
        final BlockState byj4 = bjz.getBlockState(fk);
        return (bjz.getFluidState(fk).isEmpty() || byj4.getBlock() == Blocks.BUBBLE_COLUMN) && byj4.isPathfindable(bjz, fk, PathComputationType.LAND);
    }
}
