| 1 | package net.minecraft.world.entity.ai.goal.target;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.tags.DamageTypeTags;
|
| 5 | import net.minecraft.world.damagesource.DamageSource;
|
| 6 | import net.minecraft.world.entity.LivingEntity;
|
| 7 | import net.minecraft.world.entity.TamableAnimal;
|
| 8 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 9 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 10 |
|
| 11 | public class OwnerHurtByTargetGoal extends TargetGoal {
|
| 12 | private final TamableAnimal tameAnimal;
|
| 13 | private LivingEntity ownerLastHurtBy;
|
| 14 | private int timestamp;
|
| 15 |
|
| 16 | public OwnerHurtByTargetGoal(final TamableAnimal tameAnimal) {
|
| 17 | super(tameAnimal, false);
|
| 18 | this.tameAnimal = tameAnimal;
|
| 19 | this.setFlags(EnumSet.of(Goal.Flag.TARGET));
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public boolean canUse() {
|
| 24 | if (this.tameAnimal.isTame() && !this.tameAnimal.isOrderedToSit()) {
|
| 25 | LivingEntity owner = this.tameAnimal.getOwner();
|
| 26 | if (owner == null) {
|
| 27 | return false;
|
| 28 | } else {
|
| 29 | DamageSource lastDamageSource = owner.getLastDamageSource(100);
|
| 30 | if (lastDamageSource != null && !lastDamageSource.is(DamageTypeTags.NO_WOLF_RETALIATION)) {
|
| 31 | this.ownerLastHurtBy = owner.getLastHurtByMob();
|
| 32 | int ts = owner.getLastHurtByMobTimestamp();
|
| 33 | return ts != this.timestamp
|
| 34 | && this.canAttack(this.ownerLastHurtBy, TargetingConditions.DEFAULT)
|
| 35 | && this.tameAnimal.wantsToAttack(this.ownerLastHurtBy, owner);
|
| 36 | } else {
|
| 37 | return false;
|
| 38 | }
|
| 39 | }
|
| 40 | } else {
|
| 41 | return false;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public void start() {
|
| 47 | this.mob.setTarget(this.ownerLastHurtBy);
|
| 48 | LivingEntity owner = this.tameAnimal.getOwner();
|
| 49 | if (owner != null) {
|
| 50 | this.timestamp = owner.getLastHurtByMobTimestamp();
|
| 51 | }
|
| 52 |
|
| 53 | super.start();
|
| 54 | }
|
| 55 | }
|
| 56 |
|