| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 5 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 6 | import net.minecraft.world.entity.ai.goal.WrappedGoal;
|
| 7 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 8 | import net.minecraft.world.level.Level;
|
| 9 | import net.minecraft.world.level.LevelAccessor;
|
| 10 | import net.minecraft.world.level.LevelReader;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public abstract class PathfinderMob extends Mob {
|
| 14 | protected static final float DEFAULT_WALK_TARGET_VALUE = 0.0F;
|
| 15 |
|
| 16 | protected PathfinderMob(final EntityType<? extends PathfinderMob> type, final Level level) {
|
| 17 | super(type, level);
|
| 18 | }
|
| 19 |
|
| 20 | public float getWalkTargetValue(final BlockPos pos) {
|
| 21 | return this.getWalkTargetValue(pos, this.level());
|
| 22 | }
|
| 23 |
|
| 24 | public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
|
| 25 | return 0.0F;
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public boolean checkSpawnRules(final LevelAccessor level, final EntitySpawnReason spawnReason) {
|
| 30 | return this.getWalkTargetValue(this.blockPosition(), level) >= 0.0F;
|
| 31 | }
|
| 32 |
|
| 33 | public boolean isPathFinding() {
|
| 34 | return !this.getNavigation().isDone();
|
| 35 | }
|
| 36 |
|
| 37 | public boolean isPanicking() {
|
| 38 | if (!this.brain.isBrainDead() && this.brain.hasMemoryValue(MemoryModuleType.IS_PANICKING)) {
|
| 39 | return this.brain.getMemory(MemoryModuleType.IS_PANICKING).isPresent();
|
| 40 | }
|
| 41 |
|
| 42 | for (WrappedGoal wrappedGoal : this.goalSelector.getAvailableGoals()) {
|
| 43 | if (wrappedGoal.isRunning() && wrappedGoal.getGoal() instanceof PanicGoal) {
|
| 44 | return true;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | return false;
|
| 49 | }
|
| 50 |
|
| 51 | protected boolean shouldStayCloseToLeashHolder() {
|
| 52 | return true;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public void closeRangeLeashBehaviour(final Entity leashHolder) {
|
| 57 | super.closeRangeLeashBehaviour(leashHolder);
|
| 58 | if (this.shouldStayCloseToLeashHolder() && !this.isPanicking()) {
|
| 59 | this.goalSelector.enableControlFlag(Goal.Flag.MOVE);
|
| 60 | float wantedDistance = 2.0F;
|
| 61 | float distanceTo = this.distanceTo(leashHolder);
|
| 62 | Vec3 delta = new Vec3(leashHolder.getX() - this.getX(), leashHolder.getY() - this.getY(), leashHolder.getZ() - this.getZ())
|
| 63 | .normalize()
|
| 64 | .scale(Math.max(distanceTo - 2.0F, 0.0F));
|
| 65 | this.getNavigation().moveTo(this.getX() + delta.x, this.getY() + delta.y, this.getZ() + delta.z, this.followLeashSpeed());
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void whenLeashedTo(final Entity leashHolder) {
|
| 71 | this.setHomeTo(leashHolder.blockPosition(), (int)this.leashElasticDistance() - 1);
|
| 72 | super.whenLeashedTo(leashHolder);
|
| 73 | }
|
| 74 |
|
| 75 | protected double followLeashSpeed() {
|
| 76 | return 1.0;
|
| 77 | }
|
| 78 | }
|
| 79 |
|