| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.tags.TagKey;
|
| 5 | import net.minecraft.util.Continuation;
|
| 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.block.LiquidBlock;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.material.Fluid;
|
| 13 | import org.apache.commons.lang3.mutable.MutableLong;
|
| 14 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 15 |
|
| 16 | public class TryFindLiquid {
|
| 17 | public static BehaviorControl<PathfinderMob> create(final int range, final float speedModifier, final TagKey<Fluid> fluidTag) {
|
| 18 | MutableLong nextOkStartTime = new MutableLong(0L);
|
| 19 | return BehaviorBuilder.create(
|
| 20 | i -> i.group(i.absent(MemoryModuleType.ATTACK_TARGET), i.absent(MemoryModuleType.WALK_TARGET), i.registered(MemoryModuleType.LOOK_TARGET))
|
| 21 | .apply(
|
| 22 | i,
|
| 23 | (var4x, walkTarget, lookTarget) -> (level, body, timestamp) -> {
|
| 24 | if (level.getFluidState(body.blockPosition()).is(fluidTag)) {
|
| 25 | return false;
|
| 26 | }
|
| 27 |
|
| 28 | if (timestamp < nextOkStartTime.longValue()) {
|
| 29 | nextOkStartTime.setValue(timestamp + 20L + 2L);
|
| 30 | return true;
|
| 31 | }
|
| 32 |
|
| 33 | MutableObject<BlockPos> foundPos = new MutableObject<>(null);
|
| 34 | BlockPos bodyBlockPos = body.blockPosition();
|
| 35 | level.findBlocksInBoxByManhattanDistance(bodyBlockPos, range)
|
| 36 | .filterPos(pos -> pos.differsHorizontally(bodyBlockPos))
|
| 37 | .filterState(state -> state.getBlock() instanceof LiquidBlock && state.getFluidState().is(fluidTag))
|
| 38 | .forEachUntil((pos, var4xx) -> {
|
| 39 | BlockState aboveState = level.getBlockState(pos.above());
|
| 40 | if (aboveState.isAir()) {
|
| 41 | foundPos.setValue(pos.immutable());
|
| 42 | return Continuation.ABORT;
|
| 43 | }
|
| 44 |
|
| 45 | if (foundPos.get() == null && !pos.closerToCenterThan(body.position(), 1.5)) {
|
| 46 | foundPos.setValue(pos.immutable());
|
| 47 | }
|
| 48 |
|
| 49 | return Continuation.CONTINUE;
|
| 50 | });
|
| 51 | BlockPos blockPos = foundPos.get();
|
| 52 | if (blockPos != null) {
|
| 53 | lookTarget.set(new BlockPosTracker(blockPos));
|
| 54 | walkTarget.set(new WalkTarget(new BlockPosTracker(blockPos), speedModifier, 0));
|
| 55 | }
|
| 56 |
|
| 57 | nextOkStartTime.setValue(timestamp + 40L);
|
| 58 | return true;
|
| 59 | }
|
| 60 | )
|
| 61 | );
|
| 62 | }
|
| 63 | }
|
| 64 |
|