ProtectPlayerGoal.java 1.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package com.owlmaddie.goals;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;

/**
 * The {@code ProtectPlayerGoal} class instructs a Mob Entity to show aggression towards any attacker
 * of the current player.
 */
public class ProtectPlayerGoal extends AttackPlayerGoal {
    protected final LivingEntity protectedEntity;
    protected int lastAttackedTime;

    public ProtectPlayerGoal(LivingEntity protectEntity, MobEntity attackerEntity, double speed) {
        super(null, attackerEntity, speed);
        this.protectedEntity = protectEntity;
        this.lastAttackedTime = 0;
    }

    @Override
    public boolean canStart() {
22
        LivingEntity lastAttackedByEntity = this.protectedEntity.getLastAttacker();
23
        int i = this.protectedEntity.getLastAttackedTime();
24
        if (i != this.lastAttackedTime && lastAttackedByEntity != null && !this.attackerEntity.equals(lastAttackedByEntity)) {
25 26 27 28 29 30 31
            // Set target to attack
            this.lastAttackedTime = i;
            this.targetEntity = lastAttackedByEntity;
            this.attackerEntity.setTarget(this.targetEntity);
        }

        if (this.targetEntity != null && !this.targetEntity.isAlive()) {
32
            // clear dead target
33 34 35 36 37 38
            this.targetEntity = null;
        }

        return super.canStart();
    }
}