BreedGoal .java

net.minecraft.world.entity.ai.goal.BreedGoal (class): members: PARTNER_TARGETING, partnerClass, loveTime, breed, partner, animal, speedModifier, canUse.

net.minecraft.world.entity.ai.goalclass81 linescommonextends Goal2.5 KB

API Surface (5)

boolean canUse()
boolean canContinueToUse()
void stop()
void tick()
void breed()

Fields & Constants (7)

PARTNER_TARGETING
animal
partnerClass
level
partner
loveTime
speedModifier

Code Graph: Dependencies (4)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

BreedGoal.java81 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal;
2
3
import java.util.EnumSet;
4
import java.util.List;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
7
import net.minecraft.world.entity.animal.Animal;
8
import org.jspecify.annotations.Nullable;
9
10
public class BreedGoal extends Goal {
11
	private static final TargetingConditions PARTNER_TARGETING = TargetingConditions.forNonCombat().range(8.0).ignoreLineOfSight();
12
	protected final Animal animal;
13
	private final Class<? extends Animal> partnerClass;
14
	protected final ServerLevel level;
15
	protected @Nullable Animal partner;
16
	private int loveTime;
17
	private final double speedModifier;
18
19
	public BreedGoal(final Animal animal, final double speedModifier) {
20
		this(animal, speedModifier, (Class<? extends Animal>)animal.getClass());
21
	}
22
23
	public BreedGoal(final Animal animal, final double speedModifier, final Class<? extends Animal> clazz) {
24
		this.animal = animal;
25
		this.level = getServerLevel(animal);
26
		this.partnerClass = clazz;
27
		this.speedModifier = speedModifier;
28
		this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
29
	}
30
31
	@Override
32
	public boolean canUse() {
33
		if (!this.animal.isInLove()) {
34
			return false;
35
		}
36
37
		this.partner = this.getFreePartner();
38
		return this.partner != null;
39
	}
40
41
	@Override
42
	public boolean canContinueToUse() {
43
		return this.partner.isAlive() && this.partner.isInLove() && this.loveTime < 60 && !this.partner.isPanicking();
44
	}
45
46
	@Override
47
	public void stop() {
48
		this.partner = null;
49
		this.loveTime = 0;
50
	}
51
52
	@Override
53
	public void tick() {
54
		this.animal.getLookControl().setLookAt(this.partner, 10.0F, this.animal.getMaxHeadXRot());
55
		this.animal.getNavigation().moveTo(this.partner, this.speedModifier);
56
		this.loveTime++;
57
		if (this.loveTime >= this.adjustedTickDelay(60) && this.animal.distanceToSqr(this.partner) < 9.0) {
58
			this.breed();
59
		}
60
	}
61
62
	private @Nullable Animal getFreePartner() {
63
		List<? extends Animal> animals = this.level.getNearbyEntities(this.partnerClass, PARTNER_TARGETING, this.animal, this.animal.getBoundingBox().inflate(8.0));
64
		double dist = Double.MAX_VALUE;
65
		Animal partner = null;
66
67
		for (Animal potentialPartner : animals) {
68
			if (this.animal.canMate(potentialPartner) && !potentialPartner.isPanicking() && this.animal.distanceToSqr(potentialPartner) < dist) {
69
				partner = potentialPartner;
70
				dist = this.animal.distanceToSqr(potentialPartner);
71
			}
72
		}
73
74
		return partner;
75
	}
76
77
	protected void breed() {
78
		this.animal.spawnChildFromBreeding(this.level, this.partner);
79
	}
80
}
81

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