| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFixUtils;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.world.entity.animal.fish.AbstractSchoolingFish;
|
| 7 |
|
| 8 | public class FollowFlockLeaderGoal extends Goal {
|
| 9 | private static final int INTERVAL_TICKS = 200;
|
| 10 | private final AbstractSchoolingFish mob;
|
| 11 | private int timeToRecalcPath;
|
| 12 | private int nextStartTick;
|
| 13 |
|
| 14 | public FollowFlockLeaderGoal(final AbstractSchoolingFish mob) {
|
| 15 | this.mob = mob;
|
| 16 | this.nextStartTick = this.nextStartTick(mob);
|
| 17 | }
|
| 18 |
|
| 19 | protected int nextStartTick(final AbstractSchoolingFish mob) {
|
| 20 | return reducedTickDelay(200 + mob.getRandom().nextInt(200) % 20);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public boolean canUse() {
|
| 25 | if (this.mob.hasFollowers()) {
|
| 26 | return false;
|
| 27 | } else if (this.mob.isFollower()) {
|
| 28 | return true;
|
| 29 | } else if (this.nextStartTick > 0) {
|
| 30 | this.nextStartTick--;
|
| 31 | return false;
|
| 32 | } else {
|
| 33 | this.nextStartTick = this.nextStartTick(this.mob);
|
| 34 | Predicate<AbstractSchoolingFish> predicate = fish -> fish.canBeFollowed() || !fish.isFollower();
|
| 35 | List<? extends AbstractSchoolingFish> leadersWithSpaceOrNotFollowers = this.mob
|
| 36 | .level()
|
| 37 | .getEntitiesOfClass((Class<? extends AbstractSchoolingFish>)this.mob.getClass(), this.mob.getBoundingBox().inflate(8.0, 8.0, 8.0), predicate);
|
| 38 | AbstractSchoolingFish leader = DataFixUtils.orElse(leadersWithSpaceOrNotFollowers.stream().filter(AbstractSchoolingFish::canBeFollowed).findAny(), this.mob);
|
| 39 | leader.addFollowers(leadersWithSpaceOrNotFollowers.stream().filter(fish -> !fish.isFollower()));
|
| 40 | return this.mob.isFollower();
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public boolean canContinueToUse() {
|
| 46 | return this.mob.isFollower() && this.mob.inRangeOfLeader();
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void start() {
|
| 51 | this.timeToRecalcPath = 0;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public void stop() {
|
| 56 | this.mob.stopFollowing();
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public void tick() {
|
| 61 | if (--this.timeToRecalcPath <= 0) {
|
| 62 | this.timeToRecalcPath = this.adjustedTickDelay(10);
|
| 63 | this.mob.pathToLeader();
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|