| 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.tags.TagKey;
|
| 6 | import net.minecraft.world.entity.PathfinderMob;
|
| 7 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 8 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 9 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 10 | import net.minecraft.world.level.material.Fluid;
|
| 11 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 12 | import org.apache.commons.lang3.mutable.MutableLong;
|
| 13 |
|
| 14 | public class TryFindLandNearLiquid {
|
| 15 | public static BehaviorControl<PathfinderMob> create(final int range, final float speedModifier, final TagKey<Fluid> fluidTag) {
|
| 16 | MutableLong nextOkStartTime = new MutableLong(0L);
|
| 17 | return BehaviorBuilder.create(
|
| 18 | i -> i.group(i.absent(MemoryModuleType.ATTACK_TARGET), i.absent(MemoryModuleType.WALK_TARGET), i.registered(MemoryModuleType.LOOK_TARGET))
|
| 19 | .apply(i, (var4x, walkTarget, lookTarget) -> (level, body, timestamp) -> {
|
| 20 | if (level.getFluidState(body.blockPosition()).is(fluidTag)) {
|
| 21 | return false;
|
| 22 | } else if (timestamp < nextOkStartTime.longValue()) {
|
| 23 | nextOkStartTime.setValue(timestamp + 40L);
|
| 24 | return true;
|
| 25 | } else {
|
| 26 | CollisionContext context = CollisionContext.of(body);
|
| 27 | BlockPos bodyBlockPos = body.blockPosition();
|
| 28 | BlockPos.MutableBlockPos testPos = new BlockPos.MutableBlockPos();
|
| 29 | level.findBlocksInBoxByManhattanDistance(bodyBlockPos, range).filterPos(pos -> pos.differsHorizontally(bodyBlockPos)).findFirst((pos, state) -> {
|
| 30 | if (!state.getCollisionShape(level, pos, context).isEmpty()) {
|
| 31 | return false;
|
| 32 | }
|
| 33 |
|
| 34 | if (level.getBlockState(testPos.setWithOffset(pos, Direction.DOWN)).getCollisionShape(level, pos, context).isEmpty()) {
|
| 35 | return false;
|
| 36 | }
|
| 37 |
|
| 38 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 39 | testPos.setWithOffset(pos, direction);
|
| 40 | if (level.getBlockState(testPos).isAir() && level.getBlockState(testPos.move(Direction.DOWN)).getFluidState().is(fluidTag)) {
|
| 41 | return true;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | return false;
|
| 46 | }).ifPresent(pos -> {
|
| 47 | BlockPos targetPos = pos.immutable();
|
| 48 | lookTarget.set(new BlockPosTracker(targetPos));
|
| 49 | walkTarget.set(new WalkTarget(new BlockPosTracker(targetPos), speedModifier, 0));
|
| 50 | });
|
| 51 | nextOkStartTime.setValue(timestamp + 40L);
|
| 52 | return true;
|
| 53 | }
|
| 54 | })
|
| 55 | );
|
| 56 | }
|
| 57 | }
|
| 58 |
|