| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.world.entity.EntityType;
|
| 7 | import net.minecraft.world.entity.ai.Brain;
|
| 8 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 9 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 10 | import net.minecraft.world.entity.animal.Animal;
|
| 11 |
|
| 12 | public class AnimalMakeLove extends Behavior<Animal> {
|
| 13 | private static final int BREED_RANGE = 3;
|
| 14 | private static final int MIN_DURATION = 60;
|
| 15 | private static final int MAX_DURATION = 110;
|
| 16 | private final EntityType<? extends Animal> partnerType;
|
| 17 | private final float speedModifier;
|
| 18 | private final int closeEnoughDistance;
|
| 19 | private static final int DEFAULT_CLOSE_ENOUGH_DISTANCE = 2;
|
| 20 | private long spawnChildAtTime;
|
| 21 |
|
| 22 | public AnimalMakeLove(final EntityType<? extends Animal> partnerType) {
|
| 23 | this(partnerType, 1.0F, 2);
|
| 24 | }
|
| 25 |
|
| 26 | public AnimalMakeLove(final EntityType<? extends Animal> partnerType, final float speedModifier, final int closeEnoughDistance) {
|
| 27 | super(
|
| 28 | ImmutableMap.of(
|
| 29 | MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES,
|
| 30 | MemoryStatus.VALUE_PRESENT,
|
| 31 | MemoryModuleType.BREED_TARGET,
|
| 32 | MemoryStatus.VALUE_ABSENT,
|
| 33 | MemoryModuleType.WALK_TARGET,
|
| 34 | MemoryStatus.REGISTERED,
|
| 35 | MemoryModuleType.LOOK_TARGET,
|
| 36 | MemoryStatus.REGISTERED,
|
| 37 | MemoryModuleType.IS_PANICKING,
|
| 38 | MemoryStatus.VALUE_ABSENT
|
| 39 | ),
|
| 40 | 110
|
| 41 | );
|
| 42 | this.partnerType = partnerType;
|
| 43 | this.speedModifier = speedModifier;
|
| 44 | this.closeEnoughDistance = closeEnoughDistance;
|
| 45 | }
|
| 46 |
|
| 47 | protected boolean checkExtraStartConditions(final ServerLevel level, final Animal body) {
|
| 48 | return body.isInLove() && this.findValidBreedPartner(body).isPresent();
|
| 49 | }
|
| 50 |
|
| 51 | protected void start(final ServerLevel level, final Animal body, final long timestamp) {
|
| 52 | Animal partner = this.findValidBreedPartner(body).get();
|
| 53 | body.getBrain().setMemory(MemoryModuleType.BREED_TARGET, partner);
|
| 54 | partner.getBrain().setMemory(MemoryModuleType.BREED_TARGET, body);
|
| 55 | BehaviorUtils.lockGazeAndWalkToEachOther(body, partner, this.speedModifier, this.closeEnoughDistance);
|
| 56 | int duration = 60 + body.getRandom().nextInt(50);
|
| 57 | this.spawnChildAtTime = timestamp + duration;
|
| 58 | }
|
| 59 |
|
| 60 | protected boolean canStillUse(final ServerLevel level, final Animal body, final long timestamp) {
|
| 61 | if (!this.hasBreedTargetOfRightType(body)) {
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | Animal partner = this.getBreedTarget(body);
|
| 66 | return partner.isAlive()
|
| 67 | && body.canMate(partner)
|
| 68 | && BehaviorUtils.entityIsVisible(body.getBrain(), partner)
|
| 69 | && timestamp <= this.spawnChildAtTime
|
| 70 | && !body.isPanicking()
|
| 71 | && !partner.isPanicking();
|
| 72 | }
|
| 73 |
|
| 74 | protected void tick(final ServerLevel level, final Animal body, final long timestamp) {
|
| 75 | Animal partner = this.getBreedTarget(body);
|
| 76 | BehaviorUtils.lockGazeAndWalkToEachOther(body, partner, this.speedModifier, this.closeEnoughDistance);
|
| 77 | if (body.closerThan(partner, 3.0)) {
|
| 78 | if (timestamp >= this.spawnChildAtTime) {
|
| 79 | body.spawnChildFromBreeding(level, partner);
|
| 80 | body.getBrain().eraseMemory(MemoryModuleType.BREED_TARGET);
|
| 81 | partner.getBrain().eraseMemory(MemoryModuleType.BREED_TARGET);
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | protected void stop(final ServerLevel level, final Animal body, final long timestamp) {
|
| 87 | body.getBrain().eraseMemory(MemoryModuleType.BREED_TARGET);
|
| 88 | body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 89 | body.getBrain().eraseMemory(MemoryModuleType.LOOK_TARGET);
|
| 90 | this.spawnChildAtTime = 0L;
|
| 91 | }
|
| 92 |
|
| 93 | private Animal getBreedTarget(final Animal body) {
|
| 94 | return (Animal)body.getBrain().getMemory(MemoryModuleType.BREED_TARGET).get();
|
| 95 | }
|
| 96 |
|
| 97 | private boolean hasBreedTargetOfRightType(final Animal body) {
|
| 98 | Brain<?> brain = body.getBrain();
|
| 99 | return brain.hasMemoryValue(MemoryModuleType.BREED_TARGET) && brain.getMemory(MemoryModuleType.BREED_TARGET).get().is(this.partnerType);
|
| 100 | }
|
| 101 |
|
| 102 | private Optional<? extends Animal> findValidBreedPartner(final Animal body) {
|
| 103 | return body.getBrain()
|
| 104 | .getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
|
| 105 | .get()
|
| 106 | .findClosest(entity -> entity.is(this.partnerType) && entity instanceof Animal animal && body.canMate(animal) && !animal.isPanicking())
|
| 107 | .map(Animal.class::cast);
|
| 108 | }
|
| 109 | }
|
| 110 |
|