| 1 | package net.minecraft.world.entity.ai.sensing;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.GlobalPos;
|
| 9 | import net.minecraft.resources.ResourceKey;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.world.entity.ai.Brain;
|
| 12 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 13 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 |
|
| 16 | public class SecondaryPoiSensor extends Sensor<Villager> {
|
| 17 | private static final int SCAN_RATE = 40;
|
| 18 |
|
| 19 | public SecondaryPoiSensor() {
|
| 20 | super(40);
|
| 21 | }
|
| 22 |
|
| 23 | protected void doTick(final ServerLevel level, final Villager body) {
|
| 24 | ResourceKey<Level> dimensionType = level.dimension();
|
| 25 | BlockPos center = body.blockPosition();
|
| 26 | List<GlobalPos> jobSites = Lists.newArrayList();
|
| 27 | int horizontalSearch = 4;
|
| 28 |
|
| 29 | for (int x = -4; x <= 4; x++) {
|
| 30 | for (int y = -2; y <= 2; y++) {
|
| 31 | for (int z = -4; z <= 4; z++) {
|
| 32 | BlockPos testPos = center.offset(x, y, z);
|
| 33 | if (body.getVillagerData().profession().value().secondaryPoi().contains(level.getBlockState(testPos).getBlock())) {
|
| 34 | jobSites.add(GlobalPos.of(dimensionType, testPos));
|
| 35 | }
|
| 36 | }
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | Brain<?> brain = body.getBrain();
|
| 41 | if (!jobSites.isEmpty()) {
|
| 42 | brain.setMemory(MemoryModuleType.SECONDARY_JOB_SITE, jobSites);
|
| 43 | } else {
|
| 44 | brain.eraseMemory(MemoryModuleType.SECONDARY_JOB_SITE);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public Set<MemoryModuleType<?>> requires() {
|
| 50 | return ImmutableSet.of(MemoryModuleType.SECONDARY_JOB_SITE);
|
| 51 | }
|
| 52 | }
|
| 53 |
|