HurtByTargetGoal .java

net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal (class): members: HURT_BY_TARGETING, setAlertOthers, toIgnoreDamage, alertSameType, alertOthers.

net.minecraft.world.entity.ai.goal.targetclass117 linescommonextends TargetGoal3.5 KB

API Surface (5)

boolean canUse()
HurtByTargetGoal setAlertOthers(final Class<?>... exceptTheseTypes)
void start()
void alertOthers()
void alertOther(final Mob other, final LivingEntity hurtByMob)

Fields & Constants (6)

HURT_BY_TARGETING
ALERT_RANGE_Y
alertSameType
timestamp
toIgnoreDamage
toIgnoreAlert

Full Source

HurtByTargetGoal.java117 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal.target;
2
3
import java.util.EnumSet;
4
import java.util.Iterator;
5
import java.util.List;
6
import net.minecraft.world.entity.EntitySelector;
7
import net.minecraft.world.entity.EntityTypes;
8
import net.minecraft.world.entity.LivingEntity;
9
import net.minecraft.world.entity.Mob;
10
import net.minecraft.world.entity.PathfinderMob;
11
import net.minecraft.world.entity.TamableAnimal;
12
import net.minecraft.world.entity.ai.goal.Goal;
13
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
14
import net.minecraft.world.level.gamerules.GameRules;
15
import net.minecraft.world.phys.AABB;
16
import org.jspecify.annotations.Nullable;
17
18
public class HurtByTargetGoal extends TargetGoal {
19
	private static final TargetingConditions HURT_BY_TARGETING = TargetingConditions.forCombat().ignoreLineOfSight().ignoreInvisibilityTesting();
20
	private static final int ALERT_RANGE_Y = 10;
21
	private boolean alertSameType;
22
	private int timestamp;
23
	private final Class<?>[] toIgnoreDamage;
24
	private Class<?> @Nullable [] toIgnoreAlert;
25
26
	public HurtByTargetGoal(final PathfinderMob mob, final Class<?>... ignoreDamageFromTheseTypes) {
27
		super(mob, true);
28
		this.toIgnoreDamage = ignoreDamageFromTheseTypes;
29
		this.setFlags(EnumSet.of(Goal.Flag.TARGET));
30
	}
31
32
	@Override
33
	public boolean canUse() {
34
		int timestamp = this.mob.getLastHurtByMobTimestamp();
35
		LivingEntity lastHurtByMob = this.mob.getLastHurtByMob();
36
		if (timestamp != this.timestamp && lastHurtByMob != null) {
37
			if (lastHurtByMob.is(EntityTypes.PLAYER) && getServerLevel(this.mob).getGameRules().get(GameRules.UNIVERSAL_ANGER)) {
38
				return false;
39
			}
40
41
			for (Class<?> ignoreClass : this.toIgnoreDamage) {
42
				if (ignoreClass.isAssignableFrom(lastHurtByMob.getClass())) {
43
					return false;
44
				}
45
			}
46
47
			return this.canAttack(lastHurtByMob, HURT_BY_TARGETING);
48
		} else {
49
			return false;
50
		}
51
	}
52
53
	public HurtByTargetGoal setAlertOthers(final Class<?>... exceptTheseTypes) {
54
		this.alertSameType = true;
55
		this.toIgnoreAlert = exceptTheseTypes;
56
		return this;
57
	}
58
59
	@Override
60
	public void start() {
61
		this.mob.setTarget(this.mob.getLastHurtByMob());
62
		this.targetMob = this.mob.getTarget();
63
		this.timestamp = this.mob.getLastHurtByMobTimestamp();
64
		this.unseenMemoryTicks = 300;
65
		if (this.alertSameType) {
66
			this.alertOthers();
67
		}
68
69
		super.start();
70
	}
71
72
	protected void alertOthers() {
73
		double within = this.getFollowDistance();
74
		AABB searchAabb = AABB.unitCubeFromLowerCorner(this.mob.position()).inflate(within, 10.0, within);
75
		List<? extends Mob> nearby = this.mob.level().getEntitiesOfClass((Class<? extends Mob>)this.mob.getClass(), searchAabb, EntitySelector.NO_SPECTATORS);
76
		Iterator var5 = nearby.iterator();
77
78
		while (true) {
79
			Mob other;
80
			while (true) {
81
				if (!var5.hasNext()) {
82
					return;
83
				}
84
85
				other = (Mob)var5.next();
86
				if (this.mob != other
87
					&& other.getTarget() == null
88
					&& (!(this.mob instanceof TamableAnimal tamableAnimal) || tamableAnimal.getOwner() == ((TamableAnimal)other).getOwner())
89
					&& !other.isAlliedTo(this.mob.getLastHurtByMob())) {
90
					if (this.toIgnoreAlert == null) {
91
						break;
92
					}
93
94
					boolean ignore = false;
95
96
					for (Class<?> ignoreClass : this.toIgnoreAlert) {
97
						if (other.getClass() == ignoreClass) {
98
							ignore = true;
99
							break;
100
						}
101
					}
102
103
					if (!ignore) {
104
						break;
105
					}
106
				}
107
			}
108
109
			this.alertOther(other, this.mob.getLastHurtByMob());
110
		}
111
	}
112
113
	protected void alertOther(final Mob other, final LivingEntity hurtByMob) {
114
		other.setTarget(hurtByMob);
115
	}
116
}
117

Decompiled Minecraft 26.3 source of HurtByTargetGoal (net/minecraft/world/entity/ai/goal/target/HurtByTargetGoal.java) with Mojang official mappings, stored verbatim on the site.