| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.util.RandomSource;
|
| 6 | import net.minecraft.world.entity.EquipmentSlot;
|
| 7 | import net.minecraft.world.entity.PathfinderMob;
|
| 8 | import net.minecraft.world.level.Level;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public class FleeSunGoal extends Goal {
|
| 13 | protected final PathfinderMob mob;
|
| 14 | private double wantedX;
|
| 15 | private double wantedY;
|
| 16 | private double wantedZ;
|
| 17 | private final double speedModifier;
|
| 18 | private final Level level;
|
| 19 |
|
| 20 | public FleeSunGoal(final PathfinderMob mob, final double speedModifier) {
|
| 21 | this.mob = mob;
|
| 22 | this.speedModifier = speedModifier;
|
| 23 | this.level = mob.level();
|
| 24 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public boolean canUse() {
|
| 29 | if (this.mob.getTarget() != null) {
|
| 30 | return false;
|
| 31 | } else if (!this.level.isBrightOutside()) {
|
| 32 | return false;
|
| 33 | } else if (!this.mob.isOnFire()) {
|
| 34 | return false;
|
| 35 | } else if (!this.level.canSeeSky(this.mob.blockPosition())) {
|
| 36 | return false;
|
| 37 | } else {
|
| 38 | return !this.mob.getItemBySlot(EquipmentSlot.HEAD).isEmpty() ? false : this.setWantedPos();
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | protected boolean setWantedPos() {
|
| 43 | Vec3 pos = this.getHidePos();
|
| 44 | if (pos == null) {
|
| 45 | return false;
|
| 46 | }
|
| 47 |
|
| 48 | this.wantedX = pos.x;
|
| 49 | this.wantedY = pos.y;
|
| 50 | this.wantedZ = pos.z;
|
| 51 | return true;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public boolean canContinueToUse() {
|
| 56 | return !this.mob.getNavigation().isDone();
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public void start() {
|
| 61 | this.mob.getNavigation().moveTo(this.wantedX, this.wantedY, this.wantedZ, this.speedModifier);
|
| 62 | }
|
| 63 |
|
| 64 | protected @Nullable Vec3 getHidePos() {
|
| 65 | RandomSource random = this.mob.getRandom();
|
| 66 | BlockPos pos = this.mob.blockPosition();
|
| 67 |
|
| 68 | for (int i = 0; i < 10; i++) {
|
| 69 | BlockPos randomPos = pos.offset(random.nextInt(20) - 10, random.nextInt(6) - 3, random.nextInt(20) - 10);
|
| 70 | if (!this.level.canSeeSky(randomPos) && this.mob.getWalkTargetValue(randomPos) < 0.0F) {
|
| 71 | return Vec3.atBottomCenterOf(randomPos);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return null;
|
| 76 | }
|
| 77 | }
|
| 78 |
|