| 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.GlobalPos;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 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.npc.villager.Villager;
|
| 11 |
|
| 12 | public class WorkAtPoi extends Behavior<Villager> {
|
| 13 | private static final int CHECK_COOLDOWN = 300;
|
| 14 | private static final double DISTANCE = 1.73;
|
| 15 | private long lastCheck;
|
| 16 |
|
| 17 | public WorkAtPoi() {
|
| 18 | super(ImmutableMap.of(MemoryModuleType.JOB_SITE, MemoryStatus.VALUE_PRESENT, MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED));
|
| 19 | }
|
| 20 |
|
| 21 | protected boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
|
| 22 | if (level.getGameTime() - this.lastCheck < 300L) {
|
| 23 | return false;
|
| 24 | }
|
| 25 |
|
| 26 | if (level.getRandom().nextInt(2) != 0) {
|
| 27 | return false;
|
| 28 | }
|
| 29 |
|
| 30 | this.lastCheck = level.getGameTime();
|
| 31 | GlobalPos target = body.getBrain().getMemory(MemoryModuleType.JOB_SITE).get();
|
| 32 | return target.dimension() == level.dimension() && target.pos().closerToCenterThan(body.position(), 1.73);
|
| 33 | }
|
| 34 |
|
| 35 | protected void start(final ServerLevel level, final Villager body, final long timestamp) {
|
| 36 | Brain<Villager> brain = body.getBrain();
|
| 37 | brain.setMemory(MemoryModuleType.LAST_WORKED_AT_POI, timestamp);
|
| 38 | brain.getMemory(MemoryModuleType.JOB_SITE).ifPresent(globalPos -> brain.setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosTracker(globalPos.pos())));
|
| 39 | body.playWorkSound();
|
| 40 | this.useWorkstation(level, body);
|
| 41 | if (body.shouldRestock(level)) {
|
| 42 | body.restock();
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | protected void useWorkstation(final ServerLevel level, final Villager body) {
|
| 47 | }
|
| 48 |
|
| 49 | protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
|
| 50 | Optional<GlobalPos> jobSiteMemory = body.getBrain().getMemory(MemoryModuleType.JOB_SITE);
|
| 51 | if (jobSiteMemory.isEmpty()) {
|
| 52 | return false;
|
| 53 | }
|
| 54 |
|
| 55 | GlobalPos target = jobSiteMemory.get();
|
| 56 | return target.dimension() == level.dimension() && target.pos().closerToCenterThan(body.position(), 1.73);
|
| 57 | }
|
| 58 | }
|
| 59 |
|