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

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.util.RandomPos;
import java.util.EnumSet;
import net.minecraft.world.entity.animal.horse.AbstractHorse;

public class RunAroundLikeCrazyGoal extends Goal {
    private final AbstractHorse horse;
    private final double speedModifier;
    private double posX;
    private double posY;
    private double posZ;
    
    public RunAroundLikeCrazyGoal(final AbstractHorse azm, final double double2) {
        this.horse = azm;
        this.speedModifier = double2;
        this.setFlags(EnumSet.<Flag>of(Flag.MOVE));
    }
    
    @Override
    public boolean canUse() {
        if (this.horse.isTamed() || !this.horse.isVehicle()) {
            return false;
        }
        final Vec3 dem2 = RandomPos.getPos(this.horse, 5, 4);
        if (dem2 == null) {
            return false;
        }
        this.posX = dem2.x;
        this.posY = dem2.y;
        this.posZ = dem2.z;
        return true;
    }
    
    @Override
    public void start() {
        this.horse.getNavigation().moveTo(this.posX, this.posY, this.posZ, this.speedModifier);
    }
    
    @Override
    public boolean canContinueToUse() {
        return !this.horse.isTamed() && !this.horse.getNavigation().isDone() && this.horse.isVehicle();
    }
    
    @Override
    public void tick() {
        if (!this.horse.isTamed() && this.horse.getRandom().nextInt(50) == 0) {
            final Entity aom2 = this.horse.getPassengers().get(0);
            if (aom2 == null) {
                return;
            }
            if (aom2 instanceof Player) {
                final int integer3 = this.horse.getTemper();
                final int integer4 = this.horse.getMaxTemper();
                if (integer4 > 0 && this.horse.getRandom().nextInt(integer4) < integer3) {
                    this.horse.tameWithName((Player)aom2);
                    return;
                }
                this.horse.modifyTemper(5);
            }
            this.horse.ejectPassengers();
            this.horse.makeMad();
            this.horse.level.broadcastEntityEvent(this.horse, (byte)6);
        }
    }
}
