| 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.core.BlockPos;
|
| 6 | import net.minecraft.core.GlobalPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.world.entity.AgeableMob;
|
| 10 | import net.minecraft.world.entity.EntityTypes;
|
| 11 | import net.minecraft.world.entity.ai.Brain;
|
| 12 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 13 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 14 | import net.minecraft.world.entity.ai.village.poi.PoiType;
|
| 15 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 16 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 17 | import net.minecraft.world.level.pathfinder.Path;
|
| 18 |
|
| 19 | public class VillagerMakeLove extends Behavior<Villager> {
|
| 20 | private long birthTimestamp;
|
| 21 |
|
| 22 | public VillagerMakeLove() {
|
| 23 | super(
|
| 24 | ImmutableMap.of(MemoryModuleType.BREED_TARGET, MemoryStatus.VALUE_PRESENT, MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, MemoryStatus.VALUE_PRESENT),
|
| 25 | 350,
|
| 26 | 350
|
| 27 | );
|
| 28 | }
|
| 29 |
|
| 30 | protected boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
|
| 31 | return this.isBreedingPossible(body);
|
| 32 | }
|
| 33 |
|
| 34 | protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
|
| 35 | return timestamp <= this.birthTimestamp && this.isBreedingPossible(body);
|
| 36 | }
|
| 37 |
|
| 38 | protected void start(final ServerLevel level, final Villager body, final long timestamp) {
|
| 39 | AgeableMob breedTarget = body.getBrain().getMemory(MemoryModuleType.BREED_TARGET).get();
|
| 40 | BehaviorUtils.lockGazeAndWalkToEachOther(body, breedTarget, 0.5F, 2);
|
| 41 | level.broadcastEntityEvent(breedTarget, (byte)18);
|
| 42 | level.broadcastEntityEvent(body, (byte)18);
|
| 43 | int duration = 275 + body.getRandom().nextInt(50);
|
| 44 | this.birthTimestamp = timestamp + duration;
|
| 45 | }
|
| 46 |
|
| 47 | protected void tick(final ServerLevel level, final Villager body, final long timestamp) {
|
| 48 | Villager target = (Villager)body.getBrain().getMemory(MemoryModuleType.BREED_TARGET).get();
|
| 49 | if (!(body.distanceToSqr(target) > 5.0)) {
|
| 50 | BehaviorUtils.lockGazeAndWalkToEachOther(body, target, 0.5F, 2);
|
| 51 | if (timestamp >= this.birthTimestamp) {
|
| 52 | body.eatAndDigestFood();
|
| 53 | target.eatAndDigestFood();
|
| 54 | this.tryToGiveBirth(level, body, target);
|
| 55 | } else if (body.getRandom().nextInt(35) == 0) {
|
| 56 | level.broadcastEntityEvent(target, (byte)12);
|
| 57 | level.broadcastEntityEvent(body, (byte)12);
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | private void tryToGiveBirth(final ServerLevel level, final Villager body, final Villager target) {
|
| 63 | Optional<BlockPos> childsBed = this.takeVacantBed(level, body);
|
| 64 | if (childsBed.isEmpty()) {
|
| 65 | level.broadcastEntityEvent(target, (byte)13);
|
| 66 | level.broadcastEntityEvent(body, (byte)13);
|
| 67 | } else {
|
| 68 | Optional<Villager> child = this.breed(level, body, target);
|
| 69 | if (child.isPresent()) {
|
| 70 | this.giveBedToChild(level, child.get(), childsBed.get());
|
| 71 | } else {
|
| 72 | level.getPoiManager().release(childsBed.get());
|
| 73 | level.debugSynchronizers().updatePoi(childsBed.get());
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | protected void stop(final ServerLevel level, final Villager body, final long timestamp) {
|
| 79 | body.getBrain().eraseMemory(MemoryModuleType.BREED_TARGET);
|
| 80 | }
|
| 81 |
|
| 82 | private boolean isBreedingPossible(final Villager myBody) {
|
| 83 | Brain<Villager> brain = myBody.getBrain();
|
| 84 | Optional<AgeableMob> breedTarget = brain.getMemory(MemoryModuleType.BREED_TARGET).filter(entity -> entity.is(EntityTypes.VILLAGER));
|
| 85 | return breedTarget.isEmpty()
|
| 86 | ? false
|
| 87 | : BehaviorUtils.targetIsValid(brain, MemoryModuleType.BREED_TARGET, EntityTypes.VILLAGER) && myBody.canBreed() && breedTarget.get().canBreed();
|
| 88 | }
|
| 89 |
|
| 90 | private Optional<BlockPos> takeVacantBed(final ServerLevel level, final Villager body) {
|
| 91 | return level.getPoiManager().take(p -> p.is(PoiTypes.HOME), (poiType, poiPos) -> this.canReach(body, poiPos, poiType), body.blockPosition(), 48);
|
| 92 | }
|
| 93 |
|
| 94 | private boolean canReach(final Villager body, final BlockPos poiPos, final Holder<PoiType> poiType) {
|
| 95 | Path path = body.getNavigation().createPath(poiPos, poiType.value().validRange());
|
| 96 | return path != null && path.canReach();
|
| 97 | }
|
| 98 |
|
| 99 | private Optional<Villager> breed(final ServerLevel level, final Villager source, final Villager target) {
|
| 100 | Villager child = source.getBreedOffspring(level, target);
|
| 101 | if (child == null) {
|
| 102 | return Optional.empty();
|
| 103 | }
|
| 104 |
|
| 105 | source.setAge(6000);
|
| 106 | target.setAge(6000);
|
| 107 | child.setAge(-24000);
|
| 108 | child.snapTo(source.getX(), source.getY(), source.getZ(), 0.0F, 0.0F);
|
| 109 | level.addFreshEntityWithPassengers(child);
|
| 110 | level.broadcastEntityEvent(child, (byte)12);
|
| 111 | return Optional.of(child);
|
| 112 | }
|
| 113 |
|
| 114 | private void giveBedToChild(final ServerLevel level, final Villager child, final BlockPos bedPos) {
|
| 115 | GlobalPos globalBedPos = GlobalPos.of(level.dimension(), bedPos);
|
| 116 | child.getBrain().setMemory(MemoryModuleType.HOME, globalBedPos);
|
| 117 | }
|
| 118 | }
|
| 119 |
|