| 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 |
|