| 1 | package net.minecraft.world.entity.animal.fish;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.stream.Stream;
|
| 5 | import net.minecraft.world.DifficultyInstance;
|
| 6 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 7 | import net.minecraft.world.entity.EntityType;
|
| 8 | import net.minecraft.world.entity.SpawnGroupData;
|
| 9 | import net.minecraft.world.entity.ai.goal.FollowFlockLeaderGoal;
|
| 10 | import net.minecraft.world.level.Level;
|
| 11 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public abstract class AbstractSchoolingFish extends AbstractFish {
|
| 15 | private @Nullable AbstractSchoolingFish leader;
|
| 16 | private int schoolSize = 1;
|
| 17 |
|
| 18 | public AbstractSchoolingFish(final EntityType<? extends AbstractSchoolingFish> type, final Level level) {
|
| 19 | super(type, level);
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | protected void registerGoals() {
|
| 24 | super.registerGoals();
|
| 25 | this.goalSelector.addGoal(5, new FollowFlockLeaderGoal(this));
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public int getMaxSpawnClusterSize() {
|
| 30 | return this.getMaxSchoolSize();
|
| 31 | }
|
| 32 |
|
| 33 | public int getMaxSchoolSize() {
|
| 34 | return super.getMaxSpawnClusterSize();
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | protected boolean canRandomSwim() {
|
| 39 | return !this.isFollower();
|
| 40 | }
|
| 41 |
|
| 42 | public boolean isFollower() {
|
| 43 | return this.leader != null && this.leader.isAlive();
|
| 44 | }
|
| 45 |
|
| 46 | public AbstractSchoolingFish startFollowing(final AbstractSchoolingFish leader) {
|
| 47 | this.leader = leader;
|
| 48 | leader.addFollower();
|
| 49 | return leader;
|
| 50 | }
|
| 51 |
|
| 52 | public void stopFollowing() {
|
| 53 | this.leader.removeFollower();
|
| 54 | this.leader = null;
|
| 55 | }
|
| 56 |
|
| 57 | private void addFollower() {
|
| 58 | this.schoolSize++;
|
| 59 | }
|
| 60 |
|
| 61 | private void removeFollower() {
|
| 62 | this.schoolSize--;
|
| 63 | }
|
| 64 |
|
| 65 | public boolean canBeFollowed() {
|
| 66 | return this.hasFollowers() && this.schoolSize < this.getMaxSchoolSize();
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void tick() {
|
| 71 | super.tick();
|
| 72 | if (this.hasFollowers() && this.level().getRandom().nextInt(200) == 1) {
|
| 73 | List<? extends AbstractFish> neighbors = this.level()
|
| 74 | .getEntitiesOfClass((Class<? extends AbstractFish>)this.getClass(), this.getBoundingBox().inflate(8.0, 8.0, 8.0));
|
| 75 | if (neighbors.size() <= 1) {
|
| 76 | this.schoolSize = 1;
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | public boolean hasFollowers() {
|
| 82 | return this.schoolSize > 1;
|
| 83 | }
|
| 84 |
|
| 85 | public boolean inRangeOfLeader() {
|
| 86 | return this.distanceToSqr(this.leader) <= 121.0;
|
| 87 | }
|
| 88 |
|
| 89 | public void pathToLeader() {
|
| 90 | if (this.isFollower()) {
|
| 91 | this.getNavigation().moveTo(this.leader, 1.0);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | public void addFollowers(final Stream<? extends AbstractSchoolingFish> abstractSchoolingFishStream) {
|
| 96 | abstractSchoolingFishStream.limit(this.getMaxSchoolSize() - this.schoolSize).filter(f -> f != this).forEach(otherFish -> otherFish.startFollowing(this));
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 101 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 102 | ) {
|
| 103 | super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 104 | if (groupData == null) {
|
| 105 | groupData = new AbstractSchoolingFish.SchoolSpawnGroupData(this);
|
| 106 | } else {
|
| 107 | this.startFollowing(((AbstractSchoolingFish.SchoolSpawnGroupData)groupData).leader);
|
| 108 | }
|
| 109 |
|
| 110 | return groupData;
|
| 111 | }
|
| 112 |
|
| 113 | public static class SchoolSpawnGroupData implements SpawnGroupData {
|
| 114 | public final AbstractSchoolingFish leader;
|
| 115 |
|
| 116 | public SchoolSpawnGroupData(final AbstractSchoolingFish leader) {
|
| 117 | this.leader = leader;
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|