| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.function.Function;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.tags.DamageTypeTags;
|
| 7 | import net.minecraft.tags.FluidTags;
|
| 8 | import net.minecraft.tags.TagKey;
|
| 9 | import net.minecraft.world.damagesource.DamageType;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.PathfinderMob;
|
| 12 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 13 | import net.minecraft.world.level.LevelReader;
|
| 14 | import net.minecraft.world.phys.Vec3;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public class PanicGoal extends Goal {
|
| 18 | public static final int WATER_CHECK_DISTANCE_VERTICAL = 1;
|
| 19 | protected final PathfinderMob mob;
|
| 20 | protected final double speedModifier;
|
| 21 | protected double posX;
|
| 22 | protected double posY;
|
| 23 | protected double posZ;
|
| 24 | protected boolean isRunning;
|
| 25 | private final Function<PathfinderMob, TagKey<DamageType>> panicCausingDamageTypes;
|
| 26 |
|
| 27 | public PanicGoal(final PathfinderMob mob, final double speedModifier) {
|
| 28 | this(mob, speedModifier, DamageTypeTags.PANIC_CAUSES);
|
| 29 | }
|
| 30 |
|
| 31 | public PanicGoal(final PathfinderMob mob, final double speedModifier, final TagKey<DamageType> panicCausingDamageTypes) {
|
| 32 | this(mob, speedModifier, entity -> panicCausingDamageTypes);
|
| 33 | }
|
| 34 |
|
| 35 | public PanicGoal(final PathfinderMob mob, final double speedModifier, final Function<PathfinderMob, TagKey<DamageType>> panicCausingDamageTypes) {
|
| 36 | this.mob = mob;
|
| 37 | this.speedModifier = speedModifier;
|
| 38 | this.panicCausingDamageTypes = panicCausingDamageTypes;
|
| 39 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public boolean canUse() {
|
| 44 | if (!this.shouldPanic()) {
|
| 45 | return false;
|
| 46 | }
|
| 47 |
|
| 48 | if (this.mob.isOnFire()) {
|
| 49 | BlockPos blockPos = this.lookForWater(this.mob.level(), this.mob, 5);
|
| 50 | if (blockPos != null) {
|
| 51 | this.posX = blockPos.getX();
|
| 52 | this.posY = blockPos.getY();
|
| 53 | this.posZ = blockPos.getZ();
|
| 54 | return true;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | return this.findRandomPosition();
|
| 59 | }
|
| 60 |
|
| 61 | protected boolean shouldPanic() {
|
| 62 | return this.mob.getLastDamageSource() != null && this.mob.getLastDamageSource().is(this.panicCausingDamageTypes.apply(this.mob));
|
| 63 | }
|
| 64 |
|
| 65 | protected boolean findRandomPosition() {
|
| 66 | Vec3 pos = DefaultRandomPos.getPos(this.mob, 5, 4);
|
| 67 | if (pos == null) {
|
| 68 | return false;
|
| 69 | }
|
| 70 |
|
| 71 | this.posX = pos.x;
|
| 72 | this.posY = pos.y;
|
| 73 | this.posZ = pos.z;
|
| 74 | return true;
|
| 75 | }
|
| 76 |
|
| 77 | public boolean isRunning() {
|
| 78 | return this.isRunning;
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public void start() {
|
| 83 | this.mob.getNavigation().moveTo(this.posX, this.posY, this.posZ, this.speedModifier);
|
| 84 | this.isRunning = true;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public void stop() {
|
| 89 | this.isRunning = false;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public boolean canContinueToUse() {
|
| 94 | return !this.mob.getNavigation().isDone();
|
| 95 | }
|
| 96 |
|
| 97 | protected @Nullable BlockPos lookForWater(final LevelReader level, final Entity mob, final int xzDist) {
|
| 98 | BlockPos mobPosition = mob.blockPosition();
|
| 99 | return !level.getBlockState(mobPosition).getCollisionShape(level, mobPosition).isEmpty()
|
| 100 | ? null
|
| 101 | : level.findBlocksInBoxByManhattanDistance(mobPosition, xzDist, 1).filterState(state -> state.getFluidState().is(FluidTags.WATER)).findFirst().orElse(null);
|
| 102 | }
|
| 103 | }
|
| 104 |
|