| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.tags.ItemTags;
|
| 11 | import net.minecraft.world.SimpleContainer;
|
| 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.memory.WalkTarget;
|
| 15 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 16 | import net.minecraft.world.entity.npc.villager.VillagerProfession;
|
| 17 | import net.minecraft.world.item.BlockItem;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import net.minecraft.world.level.block.Block;
|
| 20 | import net.minecraft.world.level.block.CropBlock;
|
| 21 | import net.minecraft.world.level.block.FarmlandBlock;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 24 | import net.minecraft.world.level.gamerules.GameRules;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class HarvestFarmland extends Behavior<Villager> {
|
| 28 | private static final int HARVEST_DURATION = 200;
|
| 29 | public static final float SPEED_MODIFIER = 0.5F;
|
| 30 | private @Nullable BlockPos aboveFarmlandPos;
|
| 31 | private long nextOkStartTime;
|
| 32 | private int timeWorkedSoFar;
|
| 33 | private final List<BlockPos> validFarmlandAroundVillager = Lists.newArrayList();
|
| 34 |
|
| 35 | public HarvestFarmland() {
|
| 36 | super(
|
| 37 | ImmutableMap.of(
|
| 38 | MemoryModuleType.LOOK_TARGET,
|
| 39 | MemoryStatus.VALUE_ABSENT,
|
| 40 | MemoryModuleType.WALK_TARGET,
|
| 41 | MemoryStatus.VALUE_ABSENT,
|
| 42 | MemoryModuleType.SECONDARY_JOB_SITE,
|
| 43 | MemoryStatus.VALUE_PRESENT
|
| 44 | )
|
| 45 | );
|
| 46 | }
|
| 47 |
|
| 48 | protected boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
|
| 49 | if (!level.getGameRules().get(GameRules.MOB_GRIEFING)) {
|
| 50 | return false;
|
| 51 | }
|
| 52 |
|
| 53 | if (!body.getVillagerData().profession().is(VillagerProfession.FARMER)) {
|
| 54 | return false;
|
| 55 | }
|
| 56 |
|
| 57 | BlockPos.MutableBlockPos mutPos = body.blockPosition().mutable();
|
| 58 | this.validFarmlandAroundVillager.clear();
|
| 59 |
|
| 60 | for (int x = -1; x <= 1; x++) {
|
| 61 | for (int y = -1; y <= 1; y++) {
|
| 62 | for (int z = -1; z <= 1; z++) {
|
| 63 | mutPos.set(body.getX() + x, body.getY() + y, body.getZ() + z);
|
| 64 | if (this.validPos(mutPos, level)) {
|
| 65 | this.validFarmlandAroundVillager.add(mutPos.immutable());
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | this.aboveFarmlandPos = this.getValidFarmland(level);
|
| 72 | return this.aboveFarmlandPos != null;
|
| 73 | }
|
| 74 |
|
| 75 | private @Nullable BlockPos getValidFarmland(final ServerLevel level) {
|
| 76 | return this.validFarmlandAroundVillager.isEmpty()
|
| 77 | ? null
|
| 78 | : this.validFarmlandAroundVillager.get(level.getRandom().nextInt(this.validFarmlandAroundVillager.size()));
|
| 79 | }
|
| 80 |
|
| 81 | private boolean validPos(final BlockPos blockPos, final ServerLevel level) {
|
| 82 | BlockState state = level.getBlockState(blockPos);
|
| 83 | Block block = state.getBlock();
|
| 84 | Block blockBelow = level.getBlockState(blockPos.below()).getBlock();
|
| 85 | return block instanceof CropBlock cropBlock && cropBlock.isMaxAge(state) || state.isAir() && blockBelow instanceof FarmlandBlock;
|
| 86 | }
|
| 87 |
|
| 88 | protected void start(final ServerLevel level, final Villager body, final long timestamp) {
|
| 89 | if (timestamp > this.nextOkStartTime && this.aboveFarmlandPos != null) {
|
| 90 | body.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosTracker(this.aboveFarmlandPos));
|
| 91 | body.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(new BlockPosTracker(this.aboveFarmlandPos), 0.5F, 1));
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | protected void stop(final ServerLevel level, final Villager body, final long timestamp) {
|
| 96 | body.getBrain().eraseMemory(MemoryModuleType.LOOK_TARGET);
|
| 97 | body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 98 | this.timeWorkedSoFar = 0;
|
| 99 | this.nextOkStartTime = timestamp + 40L;
|
| 100 | }
|
| 101 |
|
| 102 | protected void tick(final ServerLevel level, final Villager body, final long timestamp) {
|
| 103 | if (this.aboveFarmlandPos == null || this.aboveFarmlandPos.closerToCenterThan(body.position(), 1.0)) {
|
| 104 | if (this.aboveFarmlandPos != null && timestamp > this.nextOkStartTime) {
|
| 105 | BlockState blockState = level.getBlockState(this.aboveFarmlandPos);
|
| 106 | Block block = blockState.getBlock();
|
| 107 | Block blockBelow = level.getBlockState(this.aboveFarmlandPos.below()).getBlock();
|
| 108 | if (block instanceof CropBlock cropBlock && cropBlock.isMaxAge(blockState)) {
|
| 109 | level.destroyBlock(this.aboveFarmlandPos, true, body);
|
| 110 | }
|
| 111 |
|
| 112 | if (blockState.isAir() && blockBelow instanceof FarmlandBlock && body.hasFarmSeeds()) {
|
| 113 | SimpleContainer inventory = body.getInventory();
|
| 114 |
|
| 115 | for (int i = 0; i < inventory.getContainerSize(); i++) {
|
| 116 | ItemStack itemStack = inventory.getItem(i);
|
| 117 | boolean ok = false;
|
| 118 | if (!itemStack.isEmpty() && itemStack.is(ItemTags.VILLAGER_PLANTABLE_SEEDS) && itemStack.getItem() instanceof BlockItem blockItem) {
|
| 119 | BlockState place = blockItem.getBlock().defaultBlockState();
|
| 120 | level.setBlockAndUpdate(this.aboveFarmlandPos, place);
|
| 121 | level.gameEvent(GameEvent.BLOCK_PLACE, this.aboveFarmlandPos, GameEvent.Context.of(body, place));
|
| 122 | ok = true;
|
| 123 | }
|
| 124 |
|
| 125 | if (ok) {
|
| 126 | level.playSound(
|
| 127 | null,
|
| 128 | this.aboveFarmlandPos.getX(),
|
| 129 | this.aboveFarmlandPos.getY(),
|
| 130 | this.aboveFarmlandPos.getZ(),
|
| 131 | SoundEvents.CROP_PLANTED,
|
| 132 | SoundSource.BLOCKS,
|
| 133 | 1.0F,
|
| 134 | 1.0F
|
| 135 | );
|
| 136 | itemStack.shrink(1);
|
| 137 | if (itemStack.isEmpty()) {
|
| 138 | inventory.setItem(i, ItemStack.EMPTY);
|
| 139 | }
|
| 140 | break;
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | if (block instanceof CropBlock cropBlock && !cropBlock.isMaxAge(blockState)) {
|
| 146 | this.validFarmlandAroundVillager.remove(this.aboveFarmlandPos);
|
| 147 | this.aboveFarmlandPos = this.getValidFarmland(level);
|
| 148 | if (this.aboveFarmlandPos != null) {
|
| 149 | this.nextOkStartTime = timestamp + 20L;
|
| 150 | body.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(new BlockPosTracker(this.aboveFarmlandPos), 0.5F, 1));
|
| 151 | body.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosTracker(this.aboveFarmlandPos));
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | this.timeWorkedSoFar++;
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
|
| 161 | return this.timeWorkedSoFar < 200;
|
| 162 | }
|
| 163 | }
|
| 164 |
|