| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.tags.FluidTags;
|
| 7 | import net.minecraft.world.entity.PathfinderMob;
|
| 8 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 9 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 10 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 13 | import org.apache.commons.lang3.mutable.MutableLong;
|
| 14 |
|
| 15 | public class TryFindLand {
|
| 16 | private static final int COOLDOWN_TICKS = 60;
|
| 17 |
|
| 18 | public static BehaviorControl<PathfinderMob> create(final int range, final float speedModifier) {
|
| 19 | MutableLong nextOkStartTime = new MutableLong(0L);
|
| 20 | return BehaviorBuilder.create(
|
| 21 | i -> i.group(i.absent(MemoryModuleType.ATTACK_TARGET), i.absent(MemoryModuleType.WALK_TARGET), i.registered(MemoryModuleType.LOOK_TARGET))
|
| 22 | .apply(
|
| 23 | i,
|
| 24 | (attackTarget, walkTarget, lookTarget) -> (level, body, timestamp) -> {
|
| 25 | if (!level.getFluidState(body.blockPosition()).is(FluidTags.WATER)) {
|
| 26 | return false;
|
| 27 | } else if (timestamp < nextOkStartTime.longValue()) {
|
| 28 | nextOkStartTime.setValue(timestamp + 60L);
|
| 29 | return true;
|
| 30 | } else {
|
| 31 | BlockPos bodyBlockPos = body.blockPosition();
|
| 32 | BlockPos.MutableBlockPos belowPos = new BlockPos.MutableBlockPos();
|
| 33 | CollisionContext context = CollisionContext.of(body);
|
| 34 | level.findBlocksInBoxByManhattanDistance(bodyBlockPos, range)
|
| 35 | .filterPos(pos -> pos.differsHorizontally(bodyBlockPos))
|
| 36 | .filterState(state -> state.getFluidState().isEmpty())
|
| 37 | .findFirst((pos, state) -> canStandOn(level, pos, state, context, belowPos))
|
| 38 | .ifPresent(pos -> {
|
| 39 | BlockPos targetPos = pos.immutable();
|
| 40 | lookTarget.set(new BlockPosTracker(targetPos));
|
| 41 | walkTarget.set(new WalkTarget(new BlockPosTracker(targetPos), speedModifier, 1));
|
| 42 | });
|
| 43 | nextOkStartTime.setValue(timestamp + 60L);
|
| 44 | return true;
|
| 45 | }
|
| 46 | }
|
| 47 | )
|
| 48 | );
|
| 49 | }
|
| 50 |
|
| 51 | private static boolean canStandOn(
|
| 52 | final ServerLevel level, final BlockPos pos, final BlockState state, final CollisionContext context, final BlockPos.MutableBlockPos belowPos
|
| 53 | ) {
|
| 54 | return state.getCollisionShape(level, pos, context).isEmpty()
|
| 55 | && level.getBlockState(belowPos.setWithOffset(pos, Direction.DOWN)).isFaceSturdy(level, belowPos, Direction.UP);
|
| 56 | }
|
| 57 | }
|
| 58 |
|