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

import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import java.util.EnumSet;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.TamableAnimal;

public class OwnerHurtByTargetGoal extends TargetGoal {
    private final TamableAnimal tameAnimal;
    private LivingEntity ownerLastHurtBy;
    private int timestamp;
    
    public OwnerHurtByTargetGoal(final TamableAnimal apq) {
        super(apq, false);
        this.tameAnimal = apq;
        this.setFlags(EnumSet.<Flag>of(Flag.TARGET));
    }
    
    @Override
    public boolean canUse() {
        if (!this.tameAnimal.isTame() || this.tameAnimal.isOrderedToSit()) {
            return false;
        }
        final LivingEntity aoy2 = this.tameAnimal.getOwner();
        if (aoy2 == null) {
            return false;
        }
        this.ownerLastHurtBy = aoy2.getLastHurtByMob();
        final int integer3 = aoy2.getLastHurtByMobTimestamp();
        return integer3 != this.timestamp && this.canAttack(this.ownerLastHurtBy, TargetingConditions.DEFAULT) && this.tameAnimal.wantsToAttack(this.ownerLastHurtBy, aoy2);
    }
    
    @Override
    public void start() {
        this.mob.setTarget(this.ownerLastHurtBy);
        final LivingEntity aoy2 = this.tameAnimal.getOwner();
        if (aoy2 != null) {
            this.timestamp = aoy2.getLastHurtByMobTimestamp();
        }
        super.start();
    }
}
