| 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.server.level.ServerLevel;
|
| 8 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 9 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 10 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 11 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 12 | import net.minecraft.world.entity.schedule.Activity;
|
| 13 |
|
| 14 | public class GoToPotentialJobSite extends Behavior<Villager> {
|
| 15 | private static final int TICKS_UNTIL_TIMEOUT = 1200;
|
| 16 | private final float speedModifier;
|
| 17 |
|
| 18 | public GoToPotentialJobSite(final float speedModifier) {
|
| 19 | super(ImmutableMap.of(MemoryModuleType.POTENTIAL_JOB_SITE, MemoryStatus.VALUE_PRESENT), 1200);
|
| 20 | this.speedModifier = speedModifier;
|
| 21 | }
|
| 22 |
|
| 23 | protected boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
|
| 24 | return body.getBrain()
|
| 25 | .getActiveNonCoreActivity()
|
| 26 | .map(activity -> activity == Activity.IDLE || activity == Activity.WORK || activity == Activity.PLAY)
|
| 27 | .orElse(true);
|
| 28 | }
|
| 29 |
|
| 30 | protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
|
| 31 | return body.getBrain().hasMemoryValue(MemoryModuleType.POTENTIAL_JOB_SITE);
|
| 32 | }
|
| 33 |
|
| 34 | protected void tick(final ServerLevel level, final Villager body, final long timestamp) {
|
| 35 | BehaviorUtils.setWalkAndLookTargetMemories(body, body.getBrain().getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).get().pos(), this.speedModifier, 1);
|
| 36 | }
|
| 37 |
|
| 38 | protected void stop(final ServerLevel level, final Villager body, final long timestamp) {
|
| 39 | Optional<GlobalPos> potentialJobSitePos = body.getBrain().getMemory(MemoryModuleType.POTENTIAL_JOB_SITE);
|
| 40 | potentialJobSitePos.ifPresent(globalPos -> {
|
| 41 | BlockPos pos = globalPos.pos();
|
| 42 | ServerLevel serverLevel = level.getServer().getLevel(globalPos.dimension());
|
| 43 | if (serverLevel != null) {
|
| 44 | PoiManager manager = serverLevel.getPoiManager();
|
| 45 | if (manager.exists(pos, p -> true)) {
|
| 46 | manager.release(pos);
|
| 47 | }
|
| 48 |
|
| 49 | level.debugSynchronizers().updatePoi(pos);
|
| 50 | }
|
| 51 | });
|
| 52 | body.getBrain().eraseMemory(MemoryModuleType.POTENTIAL_JOB_SITE);
|
| 53 | }
|
| 54 | }
|
| 55 |
|