| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.registries.Registries;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.DifficultyInstance;
|
| 9 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 10 | import net.minecraft.world.entity.EntityType;
|
| 11 | import net.minecraft.world.entity.EquipmentSlot;
|
| 12 | import net.minecraft.world.entity.SpawnGroupData;
|
| 13 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 14 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 15 | import net.minecraft.world.entity.raid.Raid;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.LevelAccessor;
|
| 18 | import net.minecraft.world.level.LightLayer;
|
| 19 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 20 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 21 | import net.minecraft.world.level.storage.ValueInput;
|
| 22 | import net.minecraft.world.level.storage.ValueOutput;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public abstract class PatrollingMonster extends Monster {
|
| 27 | private static final boolean DEFAULT_PATROL_LEADER = false;
|
| 28 | private static final boolean DEFAULT_PATROLLING = false;
|
| 29 | private @Nullable BlockPos patrolTarget;
|
| 30 | private boolean patrolLeader = false;
|
| 31 | private boolean patrolling = false;
|
| 32 |
|
| 33 | protected PatrollingMonster(final EntityType<? extends PatrollingMonster> type, final Level level) {
|
| 34 | super(type, level);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | protected void registerGoals() {
|
| 39 | super.registerGoals();
|
| 40 | this.goalSelector.addGoal(4, new PatrollingMonster.LongDistancePatrolGoal<>(this, 0.7, 0.595));
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 45 | super.addAdditionalSaveData(output);
|
| 46 | output.storeNullable("patrol_target", BlockPos.CODEC, this.patrolTarget);
|
| 47 | output.putBoolean("PatrolLeader", this.patrolLeader);
|
| 48 | output.putBoolean("Patrolling", this.patrolling);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 53 | super.readAdditionalSaveData(input);
|
| 54 | this.patrolTarget = input.read("patrol_target", BlockPos.CODEC).orElse(null);
|
| 55 | this.patrolLeader = input.getBooleanOr("PatrolLeader", false);
|
| 56 | this.patrolling = input.getBooleanOr("Patrolling", false);
|
| 57 | }
|
| 58 |
|
| 59 | public boolean canBeLeader() {
|
| 60 | return true;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 65 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 66 | ) {
|
| 67 | if (spawnReason != EntitySpawnReason.PATROL
|
| 68 | && spawnReason != EntitySpawnReason.EVENT
|
| 69 | && spawnReason != EntitySpawnReason.STRUCTURE
|
| 70 | && level.getRandom().nextFloat() < 0.06F
|
| 71 | && this.canBeLeader()) {
|
| 72 | this.patrolLeader = true;
|
| 73 | }
|
| 74 |
|
| 75 | if (this.isPatrolLeader()) {
|
| 76 | this.setItemSlot(EquipmentSlot.HEAD, Raid.getOminousBannerInstance(this.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)));
|
| 77 | this.setDropChance(EquipmentSlot.HEAD, 2.0F);
|
| 78 | }
|
| 79 |
|
| 80 | if (spawnReason == EntitySpawnReason.PATROL) {
|
| 81 | this.patrolling = true;
|
| 82 | }
|
| 83 |
|
| 84 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 85 | }
|
| 86 |
|
| 87 | public static boolean checkPatrollingMonsterSpawnRules(
|
| 88 | final EntityType<? extends PatrollingMonster> type,
|
| 89 | final LevelAccessor level,
|
| 90 | final EntitySpawnReason spawnReason,
|
| 91 | final BlockPos pos,
|
| 92 | final RandomSource random
|
| 93 | ) {
|
| 94 | return level.getBrightness(LightLayer.BLOCK, pos) > 8 ? false : checkAnyLightMonsterSpawnRules(type, level, spawnReason, pos, random);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean removeWhenFarAway(final double distSqr) {
|
| 99 | return !this.patrolling || distSqr > 16384.0;
|
| 100 | }
|
| 101 |
|
| 102 | public void setPatrolTarget(final BlockPos target) {
|
| 103 | this.patrolTarget = target;
|
| 104 | this.patrolling = true;
|
| 105 | }
|
| 106 |
|
| 107 | public @Nullable BlockPos getPatrolTarget() {
|
| 108 | return this.patrolTarget;
|
| 109 | }
|
| 110 |
|
| 111 | public boolean hasPatrolTarget() {
|
| 112 | return this.patrolTarget != null;
|
| 113 | }
|
| 114 |
|
| 115 | public void setPatrolLeader(final boolean isLeader) {
|
| 116 | this.patrolLeader = isLeader;
|
| 117 | this.patrolling = true;
|
| 118 | }
|
| 119 |
|
| 120 | public boolean isPatrolLeader() {
|
| 121 | return this.patrolLeader;
|
| 122 | }
|
| 123 |
|
| 124 | public boolean canJoinPatrol() {
|
| 125 | return true;
|
| 126 | }
|
| 127 |
|
| 128 | public void findPatrolTarget() {
|
| 129 | this.patrolTarget = this.blockPosition().offset(-500 + this.random.nextInt(1000), 0, -500 + this.random.nextInt(1000));
|
| 130 | this.patrolling = true;
|
| 131 | }
|
| 132 |
|
| 133 | protected boolean isPatrolling() {
|
| 134 | return this.patrolling;
|
| 135 | }
|
| 136 |
|
| 137 | protected void setPatrolling(final boolean value) {
|
| 138 | this.patrolling = value;
|
| 139 | }
|
| 140 |
|
| 141 | public static class LongDistancePatrolGoal<T extends PatrollingMonster> extends Goal {
|
| 142 | private static final int NAVIGATION_FAILED_COOLDOWN = 200;
|
| 143 | private final T mob;
|
| 144 | private final double speedModifier;
|
| 145 | private final double leaderSpeedModifier;
|
| 146 | private long cooldownUntil;
|
| 147 |
|
| 148 | public LongDistancePatrolGoal(final T mob, final double speedModifier, final double leaderSpeedModifier) {
|
| 149 | this.mob = mob;
|
| 150 | this.speedModifier = speedModifier;
|
| 151 | this.leaderSpeedModifier = leaderSpeedModifier;
|
| 152 | this.cooldownUntil = -1L;
|
| 153 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public boolean canUse() {
|
| 158 | boolean isOnCooldown = this.mob.level().getGameTime() < this.cooldownUntil;
|
| 159 | return this.mob.isPatrolling() && this.mob.getTarget() == null && !this.mob.hasControllingPassenger() && this.mob.hasPatrolTarget() && !isOnCooldown;
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | public void start() {
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public void stop() {
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public void tick() {
|
| 172 | boolean patrolLeader = this.mob.isPatrolLeader();
|
| 173 | PathNavigation navigation = this.mob.getNavigation();
|
| 174 | if (navigation.isDone()) {
|
| 175 | List<PatrollingMonster> companions = this.findPatrolCompanions();
|
| 176 | if (this.mob.isPatrolling() && companions.isEmpty()) {
|
| 177 | this.mob.setPatrolling(false);
|
| 178 | } else if (patrolLeader && this.mob.getPatrolTarget().closerToCenterThan(this.mob.position(), 10.0)) {
|
| 179 | this.mob.findPatrolTarget();
|
| 180 | } else {
|
| 181 | Vec3 longDistanceTarget = Vec3.atBottomCenterOf(this.mob.getPatrolTarget());
|
| 182 | Vec3 selfVector = this.mob.position();
|
| 183 | Vec3 distance = selfVector.subtract(longDistanceTarget);
|
| 184 | longDistanceTarget = distance.yRot(90.0F).scale(0.4).add(longDistanceTarget);
|
| 185 | Vec3 moveTarget = longDistanceTarget.subtract(selfVector).normalize().scale(10.0).add(selfVector);
|
| 186 | BlockPos pathTarget = BlockPos.containing(moveTarget);
|
| 187 | pathTarget = this.mob.level().getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, pathTarget);
|
| 188 | if (!navigation.moveTo(pathTarget.getX(), pathTarget.getY(), pathTarget.getZ(), patrolLeader ? this.leaderSpeedModifier : this.speedModifier)) {
|
| 189 | this.moveRandomly();
|
| 190 | this.cooldownUntil = this.mob.level().getGameTime() + 200L;
|
| 191 | } else if (patrolLeader) {
|
| 192 | for (PatrollingMonster companion : companions) {
|
| 193 | companion.setPatrolTarget(pathTarget);
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | private List<PatrollingMonster> findPatrolCompanions() {
|
| 201 | return this.mob
|
| 202 | .level()
|
| 203 | .getEntitiesOfClass(PatrollingMonster.class, this.mob.getBoundingBox().inflate(16.0), mob -> mob.canJoinPatrol() && !mob.is(this.mob));
|
| 204 | }
|
| 205 |
|
| 206 | private boolean moveRandomly() {
|
| 207 | RandomSource random = this.mob.getRandom();
|
| 208 | BlockPos pathTarget = this.mob
|
| 209 | .level()
|
| 210 | .getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, this.mob.blockPosition().offset(-8 + random.nextInt(16), 0, -8 + random.nextInt(16)));
|
| 211 | return this.mob.getNavigation().moveTo(pathTarget.getX(), pathTarget.getY(), pathTarget.getZ(), this.speedModifier);
|
| 212 | }
|
| 213 | }
|
| 214 | }
|
| 215 |
|