| 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.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.SimpleContainer;
|
| 9 | import net.minecraft.world.entity.EquipmentSlot;
|
| 10 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 11 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 12 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 13 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 14 | import net.minecraft.world.item.BoneMealItem;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.Items;
|
| 17 | import net.minecraft.world.level.block.CropBlock;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 |
|
| 20 | public class UseBonemeal extends Behavior<Villager> {
|
| 21 | private static final int BONEMEALING_DURATION = 80;
|
| 22 | private long nextWorkCycleTime;
|
| 23 | private long lastBonemealingSession;
|
| 24 | private int timeWorkedSoFar;
|
| 25 | private Optional<BlockPos> cropPos = Optional.empty();
|
| 26 |
|
| 27 | public UseBonemeal() {
|
| 28 | super(ImmutableMap.of(MemoryModuleType.LOOK_TARGET, MemoryStatus.VALUE_ABSENT, MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT));
|
| 29 | }
|
| 30 |
|
| 31 | protected boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
|
| 32 | if (body.tickCount % 10 == 0 && (this.lastBonemealingSession == 0L || this.lastBonemealingSession + 160L <= body.tickCount)) {
|
| 33 | if (body.getInventory().countItem(Items.BONE_MEAL) <= 0) {
|
| 34 | return false;
|
| 35 | }
|
| 36 |
|
| 37 | this.cropPos = this.pickNextTarget(level, body);
|
| 38 | return this.cropPos.isPresent();
|
| 39 | } else {
|
| 40 | return false;
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
|
| 45 | return this.timeWorkedSoFar < 80 && this.cropPos.isPresent();
|
| 46 | }
|
| 47 |
|
| 48 | private Optional<BlockPos> pickNextTarget(final ServerLevel level, final Villager body) {
|
| 49 | BlockPos.MutableBlockPos mutPos = new BlockPos.MutableBlockPos();
|
| 50 | RandomSource random = level.getRandom();
|
| 51 | Optional<BlockPos> result = Optional.empty();
|
| 52 | int count = 0;
|
| 53 |
|
| 54 | for (int x = -1; x <= 1; x++) {
|
| 55 | for (int y = -1; y <= 1; y++) {
|
| 56 | for (int z = -1; z <= 1; z++) {
|
| 57 | mutPos.setWithOffset(body.blockPosition(), x, y, z);
|
| 58 | if (this.validPos(mutPos, level)) {
|
| 59 | if (random.nextInt(++count) == 0) {
|
| 60 | result = Optional.of(mutPos.immutable());
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | return result;
|
| 68 | }
|
| 69 |
|
| 70 | private boolean validPos(final BlockPos blockPos, final ServerLevel level) {
|
| 71 | BlockState state = level.getBlockState(blockPos);
|
| 72 | return state.getBlock() instanceof CropBlock cropBlock && !cropBlock.isMaxAge(state);
|
| 73 | }
|
| 74 |
|
| 75 | protected void start(final ServerLevel level, final Villager body, final long timestamp) {
|
| 76 | this.setCurrentCropAsTarget(body);
|
| 77 | body.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.BONE_MEAL));
|
| 78 | this.nextWorkCycleTime = timestamp;
|
| 79 | this.timeWorkedSoFar = 0;
|
| 80 | }
|
| 81 |
|
| 82 | private void setCurrentCropAsTarget(final Villager body) {
|
| 83 | this.cropPos.ifPresent(pos -> {
|
| 84 | BlockPosTracker cropPosWrapper = new BlockPosTracker(pos);
|
| 85 | body.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, cropPosWrapper);
|
| 86 | body.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(cropPosWrapper, 0.5F, 1));
|
| 87 | });
|
| 88 | }
|
| 89 |
|
| 90 | protected void stop(final ServerLevel level, final Villager body, final long timestamp) {
|
| 91 | body.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
|
| 92 | this.lastBonemealingSession = body.tickCount;
|
| 93 | }
|
| 94 |
|
| 95 | protected void tick(final ServerLevel level, final Villager body, final long timestamp) {
|
| 96 | BlockPos targetPos = this.cropPos.get();
|
| 97 | if (timestamp >= this.nextWorkCycleTime && targetPos.closerToCenterThan(body.position(), 1.0)) {
|
| 98 | ItemStack bonemealStack = ItemStack.EMPTY;
|
| 99 | SimpleContainer inventory = body.getInventory();
|
| 100 | int containerSize = inventory.getContainerSize();
|
| 101 |
|
| 102 | for (int i = 0; i < containerSize; i++) {
|
| 103 | ItemStack item = inventory.getItem(i);
|
| 104 | if (item.is(Items.BONE_MEAL)) {
|
| 105 | bonemealStack = item;
|
| 106 | break;
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | if (!bonemealStack.isEmpty() && BoneMealItem.growCrop(bonemealStack, level, targetPos)) {
|
| 111 | level.levelEvent(1505, targetPos, 15);
|
| 112 | this.cropPos = this.pickNextTarget(level, body);
|
| 113 | this.setCurrentCropAsTarget(body);
|
| 114 | this.nextWorkCycleTime = timestamp + 40L;
|
| 115 | }
|
| 116 |
|
| 117 | this.timeWorkedSoFar++;
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|