| 1 | package net.minecraft.world.entity.raid;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import it.unimi.dsi.fastutil.ints.Int2LongOpenHashMap;
|
| 5 | import java.util.EnumSet;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.registries.Registries;
|
| 12 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 13 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 14 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.sounds.SoundEvent;
|
| 17 | import net.minecraft.world.DifficultyInstance;
|
| 18 | import net.minecraft.world.damagesource.DamageSource;
|
| 19 | import net.minecraft.world.entity.Entity;
|
| 20 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 21 | import net.minecraft.world.entity.EntityType;
|
| 22 | import net.minecraft.world.entity.EntityTypes;
|
| 23 | import net.minecraft.world.entity.EquipmentSlot;
|
| 24 | import net.minecraft.world.entity.LivingEntity;
|
| 25 | import net.minecraft.world.entity.SpawnGroupData;
|
| 26 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 27 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 28 | import net.minecraft.world.entity.ai.goal.PathfindToRaidGoal;
|
| 29 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 30 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 31 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 32 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 33 | import net.minecraft.world.entity.item.ItemEntity;
|
| 34 | import net.minecraft.world.entity.monster.PatrollingMonster;
|
| 35 | import net.minecraft.world.entity.monster.illager.AbstractIllager;
|
| 36 | import net.minecraft.world.item.ItemStack;
|
| 37 | import net.minecraft.world.level.Level;
|
| 38 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 39 | import net.minecraft.world.level.pathfinder.Path;
|
| 40 | import net.minecraft.world.level.storage.ValueInput;
|
| 41 | import net.minecraft.world.level.storage.ValueOutput;
|
| 42 | import net.minecraft.world.phys.Vec3;
|
| 43 | import org.jspecify.annotations.Nullable;
|
| 44 |
|
| 45 | public abstract class Raider extends PatrollingMonster {
|
| 46 | protected static final EntityDataAccessor<Boolean> IS_CELEBRATING = SynchedEntityData.defineId(Raider.class, EntityDataSerializers.BOOLEAN);
|
| 47 | private static final Predicate<ItemEntity> ALLOWED_ITEMS = e -> !e.hasPickUpDelay()
|
| 48 | && e.isAlive()
|
| 49 | && ItemStack.matches(e.getItem(), Raid.getOminousBannerInstance(e.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)));
|
| 50 | private static final int DEFAULT_WAVE = 0;
|
| 51 | private static final boolean DEFAULT_CAN_JOIN_RAID = false;
|
| 52 | protected @Nullable Raid raid;
|
| 53 | private int wave = 0;
|
| 54 | private boolean canJoinRaid = false;
|
| 55 | private int ticksOutsideRaid;
|
| 56 |
|
| 57 | protected Raider(final EntityType<? extends Raider> type, final Level level) {
|
| 58 | super(type, level);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected void registerGoals() {
|
| 63 | super.registerGoals();
|
| 64 | this.goalSelector.addGoal(1, new Raider.ObtainRaidLeaderBannerGoal<>(this));
|
| 65 | this.goalSelector.addGoal(3, new PathfindToRaidGoal<>(this));
|
| 66 | this.goalSelector.addGoal(4, new Raider.RaiderMoveThroughVillageGoal(this, 1.05F, 1));
|
| 67 | this.goalSelector.addGoal(5, new Raider.RaiderCelebration(this));
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 72 | super.defineSynchedData(entityData);
|
| 73 | entityData.define(IS_CELEBRATING, false);
|
| 74 | }
|
| 75 |
|
| 76 | public abstract void applyRaidBuffs(final ServerLevel level, final int wave, final boolean isCaptain);
|
| 77 |
|
| 78 | public boolean canJoinRaid() {
|
| 79 | return this.canJoinRaid;
|
| 80 | }
|
| 81 |
|
| 82 | public void setCanJoinRaid(final boolean canJoinRaid) {
|
| 83 | this.canJoinRaid = canJoinRaid;
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public void aiStep() {
|
| 88 | if (this.level() instanceof ServerLevel level && this.isAlive()) {
|
| 89 | Raid currentRaid = this.getCurrentRaid();
|
| 90 | if (this.canJoinRaid()) {
|
| 91 | if (currentRaid == null) {
|
| 92 | if (this.level().getGameTime() % 20L == 0L) {
|
| 93 | Raid nearbyRaid = level.getRaidAt(this.blockPosition());
|
| 94 | if (nearbyRaid != null && Raids.canJoinRaid(this)) {
|
| 95 | nearbyRaid.joinRaid(level, nearbyRaid.getGroupsSpawned(), this, null, true);
|
| 96 | }
|
| 97 | }
|
| 98 | } else {
|
| 99 | LivingEntity target = this.getTarget();
|
| 100 | if (target != null && (target.is(EntityTypes.PLAYER) || target.is(EntityTypes.IRON_GOLEM))) {
|
| 101 | this.noActionTime = 0;
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | super.aiStep();
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | protected void updateNoActionTime() {
|
| 112 | this.noActionTime += 2;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void die(final DamageSource source) {
|
| 117 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 118 | Entity killer = source.getEntity();
|
| 119 | Raid raidWhenKilled = this.getCurrentRaid();
|
| 120 | if (raidWhenKilled != null) {
|
| 121 | if (this.isPatrolLeader()) {
|
| 122 | raidWhenKilled.removeLeader(this.getWave());
|
| 123 | }
|
| 124 |
|
| 125 | if (killer != null && killer.is(EntityTypes.PLAYER)) {
|
| 126 | raidWhenKilled.addHeroOfTheVillage(killer);
|
| 127 | }
|
| 128 |
|
| 129 | raidWhenKilled.removeFromRaid(serverLevel, this, false);
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | super.die(source);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public boolean canJoinPatrol() {
|
| 138 | return !this.hasActiveRaid();
|
| 139 | }
|
| 140 |
|
| 141 | public void setCurrentRaid(final @Nullable Raid raid) {
|
| 142 | this.raid = raid;
|
| 143 | }
|
| 144 |
|
| 145 | public @Nullable Raid getCurrentRaid() {
|
| 146 | return this.raid;
|
| 147 | }
|
| 148 |
|
| 149 | public boolean isCaptain() {
|
| 150 | ItemStack banner = this.getItemBySlot(EquipmentSlot.HEAD);
|
| 151 | boolean hasCaptainBanner = !banner.isEmpty()
|
| 152 | && ItemStack.matches(banner, Raid.getOminousBannerInstance(this.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)));
|
| 153 | boolean patrolLeader = this.isPatrolLeader();
|
| 154 | return hasCaptainBanner && patrolLeader;
|
| 155 | }
|
| 156 |
|
| 157 | public boolean hasRaid() {
|
| 158 | return !(this.level() instanceof ServerLevel serverLevel) ? false : this.getCurrentRaid() != null || serverLevel.getRaidAt(this.blockPosition()) != null;
|
| 159 | }
|
| 160 |
|
| 161 | public boolean hasActiveRaid() {
|
| 162 | return this.getCurrentRaid() != null && this.getCurrentRaid().isActive();
|
| 163 | }
|
| 164 |
|
| 165 | public void setWave(final int wave) {
|
| 166 | this.wave = wave;
|
| 167 | }
|
| 168 |
|
| 169 | public int getWave() {
|
| 170 | return this.wave;
|
| 171 | }
|
| 172 |
|
| 173 | public boolean isCelebrating() {
|
| 174 | return this.entityData.get(IS_CELEBRATING);
|
| 175 | }
|
| 176 |
|
| 177 | public void setCelebrating(final boolean celebrating) {
|
| 178 | this.entityData.set(IS_CELEBRATING, celebrating);
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 183 | super.addAdditionalSaveData(output);
|
| 184 | output.putInt("Wave", this.wave);
|
| 185 | output.putBoolean("CanJoinRaid", this.canJoinRaid);
|
| 186 | if (this.raid != null && this.level() instanceof ServerLevel level) {
|
| 187 | level.getRaids().getId(this.raid).ifPresent(id -> output.putInt("RaidId", id));
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 193 | super.readAdditionalSaveData(input);
|
| 194 | this.wave = input.getIntOr("Wave", 0);
|
| 195 | this.canJoinRaid = input.getBooleanOr("CanJoinRaid", false);
|
| 196 | if (this.level() instanceof ServerLevel level) {
|
| 197 | input.getInt("RaidId").ifPresent(raidId -> {
|
| 198 | this.raid = level.getRaids().get(raidId);
|
| 199 | if (this.raid != null) {
|
| 200 | this.raid.addWaveMob(level, this.wave, this, false);
|
| 201 | if (this.isPatrolLeader()) {
|
| 202 | this.raid.setLeader(this.wave, this);
|
| 203 | }
|
| 204 | }
|
| 205 | });
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | protected void pickUpItem(final ServerLevel level, final ItemEntity entity) {
|
| 211 | ItemStack itemStack = entity.getItem();
|
| 212 | boolean hasRaidLeader = this.hasActiveRaid() && this.getCurrentRaid().getLeader(this.getWave()) != null;
|
| 213 | if (this.hasActiveRaid()
|
| 214 | && !hasRaidLeader
|
| 215 | && ItemStack.matches(itemStack, Raid.getOminousBannerInstance(this.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)))) {
|
| 216 | EquipmentSlot slot = EquipmentSlot.HEAD;
|
| 217 | ItemStack current = this.getItemBySlot(slot);
|
| 218 | double dropChance = this.getDropChances().byEquipment(slot);
|
| 219 | if (!current.isEmpty() && Math.max(this.random.nextFloat() - 0.1F, 0.0F) < dropChance) {
|
| 220 | this.spawnAtLocation(level, current);
|
| 221 | }
|
| 222 |
|
| 223 | this.onItemPickup(entity);
|
| 224 | this.setItemSlot(slot, itemStack);
|
| 225 | this.take(entity, itemStack.getCount());
|
| 226 | entity.discard();
|
| 227 | this.getCurrentRaid().setLeader(this.getWave(), this);
|
| 228 | this.setPatrolLeader(true);
|
| 229 | } else {
|
| 230 | super.pickUpItem(level, entity);
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | @Override
|
| 235 | public boolean removeWhenFarAway(final double distSqr) {
|
| 236 | return this.getCurrentRaid() == null ? super.removeWhenFarAway(distSqr) : false;
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public boolean requiresCustomPersistence() {
|
| 241 | return super.requiresCustomPersistence() || this.getCurrentRaid() != null;
|
| 242 | }
|
| 243 |
|
| 244 | public int getTicksOutsideRaid() {
|
| 245 | return this.ticksOutsideRaid;
|
| 246 | }
|
| 247 |
|
| 248 | public void setTicksOutsideRaid(final int ticksOutsideRaid) {
|
| 249 | this.ticksOutsideRaid = ticksOutsideRaid;
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 254 | if (this.hasActiveRaid()) {
|
| 255 | this.getCurrentRaid().updateBossbar();
|
| 256 | }
|
| 257 |
|
| 258 | return super.hurtServer(level, source, damage);
|
| 259 | }
|
| 260 |
|
| 261 | @Override
|
| 262 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 263 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 264 | ) {
|
| 265 | this.setCanJoinRaid(!this.is(EntityTypes.WITCH) || spawnReason != EntitySpawnReason.NATURAL);
|
| 266 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 267 | }
|
| 268 |
|
| 269 | public abstract SoundEvent getCelebrateSound();
|
| 270 |
|
| 271 | protected static class HoldGroundAttackGoal extends Goal {
|
| 272 | private final Raider mob;
|
| 273 | private final float hostileRadiusSqr;
|
| 274 | public final TargetingConditions shoutTargeting = TargetingConditions.forNonCombat().range(8.0).ignoreLineOfSight().ignoreInvisibilityTesting();
|
| 275 |
|
| 276 | public HoldGroundAttackGoal(final AbstractIllager mob, final float hostileRadius) {
|
| 277 | this.mob = mob;
|
| 278 | this.hostileRadiusSqr = hostileRadius * hostileRadius;
|
| 279 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 280 | }
|
| 281 |
|
| 282 | @Override
|
| 283 | public boolean canUse() {
|
| 284 | LivingEntity lastHurtByMob = this.mob.getLastHurtByMob();
|
| 285 | return this.mob.getCurrentRaid() == null
|
| 286 | && this.mob.isPatrolling()
|
| 287 | && this.mob.getTarget() != null
|
| 288 | && !this.mob.isAggressive()
|
| 289 | && (lastHurtByMob == null || !lastHurtByMob.is(EntityTypes.PLAYER));
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | public void start() {
|
| 294 | super.start();
|
| 295 | this.mob.getNavigation().stop();
|
| 296 |
|
| 297 | for (Raider entity : getServerLevel(this.mob)
|
| 298 | .getNearbyEntities(Raider.class, this.shoutTargeting, this.mob, this.mob.getBoundingBox().inflate(8.0, 8.0, 8.0))) {
|
| 299 | entity.setTarget(this.mob.getTarget());
|
| 300 | }
|
| 301 | }
|
| 302 |
|
| 303 | @Override
|
| 304 | public void stop() {
|
| 305 | super.stop();
|
| 306 | LivingEntity target = this.mob.getTarget();
|
| 307 | if (target != null) {
|
| 308 | for (Raider entity : getServerLevel(this.mob)
|
| 309 | .getNearbyEntities(Raider.class, this.shoutTargeting, this.mob, this.mob.getBoundingBox().inflate(8.0, 8.0, 8.0))) {
|
| 310 | entity.setTarget(target);
|
| 311 | entity.setAggressive(true);
|
| 312 | }
|
| 313 |
|
| 314 | this.mob.setAggressive(true);
|
| 315 | }
|
| 316 | }
|
| 317 |
|
| 318 | @Override
|
| 319 | public boolean requiresUpdateEveryTick() {
|
| 320 | return true;
|
| 321 | }
|
| 322 |
|
| 323 | @Override
|
| 324 | public void tick() {
|
| 325 | LivingEntity target = this.mob.getTarget();
|
| 326 | if (target != null) {
|
| 327 | if (this.mob.distanceToSqr(target) > this.hostileRadiusSqr) {
|
| 328 | this.mob.getLookControl().setLookAt(target, 30.0F, 30.0F);
|
| 329 | if (this.mob.random.nextInt(50) == 0) {
|
| 330 | this.mob.playAmbientSound();
|
| 331 | }
|
| 332 | } else {
|
| 333 | this.mob.setAggressive(true);
|
| 334 | }
|
| 335 |
|
| 336 | super.tick();
|
| 337 | }
|
| 338 | }
|
| 339 | }
|
| 340 |
|
| 341 | public class ObtainRaidLeaderBannerGoal<T extends Raider> extends Goal {
|
| 342 | private final T mob;
|
| 343 | private Int2LongOpenHashMap unreachableBannerCache = new Int2LongOpenHashMap();
|
| 344 | private @Nullable Path pathToBanner;
|
| 345 | private @Nullable ItemEntity pursuedBannerItemEntity;
|
| 346 |
|
| 347 | public ObtainRaidLeaderBannerGoal(final T mob) {
|
| 348 | this.mob = mob;
|
| 349 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 350 | }
|
| 351 |
|
| 352 | @Override
|
| 353 | public boolean canUse() {
|
| 354 | if (this.cannotPickUpBanner()) {
|
| 355 | return false;
|
| 356 | }
|
| 357 |
|
| 358 | Int2LongOpenHashMap tempCache = new Int2LongOpenHashMap();
|
| 359 | double followRange = Raider.this.getAttributeValue(Attributes.FOLLOW_RANGE);
|
| 360 |
|
| 361 | for (ItemEntity banner : this.mob
|
| 362 | .level()
|
| 363 | .getEntitiesOfClass(ItemEntity.class, this.mob.getBoundingBox().inflate(followRange, 8.0, followRange), Raider.ALLOWED_ITEMS)) {
|
| 364 | long unreachableUntilTime = this.unreachableBannerCache.getOrDefault(banner.getId(), Long.MIN_VALUE);
|
| 365 | if (Raider.this.level().getGameTime() < unreachableUntilTime) {
|
| 366 | tempCache.put(banner.getId(), unreachableUntilTime);
|
| 367 | } else {
|
| 368 | Path path = this.mob.getNavigation().createPath(banner, 1);
|
| 369 | if (path != null && path.canReach()) {
|
| 370 | this.pathToBanner = path;
|
| 371 | this.pursuedBannerItemEntity = banner;
|
| 372 | return true;
|
| 373 | }
|
| 374 |
|
| 375 | tempCache.put(banner.getId(), Raider.this.level().getGameTime() + 600L);
|
| 376 | }
|
| 377 | }
|
| 378 |
|
| 379 | this.unreachableBannerCache = tempCache;
|
| 380 | return false;
|
| 381 | }
|
| 382 |
|
| 383 | @Override
|
| 384 | public boolean canContinueToUse() {
|
| 385 | if (this.pursuedBannerItemEntity == null || this.pathToBanner == null) {
|
| 386 | return false;
|
| 387 | } else if (this.pursuedBannerItemEntity.isRemoved()) {
|
| 388 | return false;
|
| 389 | } else {
|
| 390 | return this.pathToBanner.isDone() ? false : !this.cannotPickUpBanner();
|
| 391 | }
|
| 392 | }
|
| 393 |
|
| 394 | private boolean cannotPickUpBanner() {
|
| 395 | if (!this.mob.hasActiveRaid()) {
|
| 396 | return true;
|
| 397 | }
|
| 398 |
|
| 399 | if (this.mob.getCurrentRaid().isOver()) {
|
| 400 | return true;
|
| 401 | }
|
| 402 |
|
| 403 | if (!this.mob.canBeLeader()) {
|
| 404 | return true;
|
| 405 | }
|
| 406 |
|
| 407 | if (ItemStack.matches(
|
| 408 | this.mob.getItemBySlot(EquipmentSlot.HEAD), Raid.getOminousBannerInstance(this.mob.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN))
|
| 409 | )) {
|
| 410 | return true;
|
| 411 | }
|
| 412 |
|
| 413 | Raider leader = Raider.this.raid.getLeader(this.mob.getWave());
|
| 414 | return leader != null && leader.isAlive();
|
| 415 | }
|
| 416 |
|
| 417 | @Override
|
| 418 | public void start() {
|
| 419 | this.mob.getNavigation().moveTo(this.pathToBanner, 1.15F);
|
| 420 | }
|
| 421 |
|
| 422 | @Override
|
| 423 | public void stop() {
|
| 424 | this.pathToBanner = null;
|
| 425 | this.pursuedBannerItemEntity = null;
|
| 426 | }
|
| 427 |
|
| 428 | @Override
|
| 429 | public void tick() {
|
| 430 | if (this.pursuedBannerItemEntity != null && this.pursuedBannerItemEntity.closerThan(this.mob, 1.414)) {
|
| 431 | this.mob.pickUpItem(getServerLevel(Raider.this.level()), this.pursuedBannerItemEntity);
|
| 432 | }
|
| 433 | }
|
| 434 | }
|
| 435 |
|
| 436 | public class RaiderCelebration extends Goal {
|
| 437 | private final Raider mob;
|
| 438 |
|
| 439 | public RaiderCelebration(final Raider mob) {
|
| 440 | this.mob = mob;
|
| 441 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 442 | }
|
| 443 |
|
| 444 | @Override
|
| 445 | public boolean canUse() {
|
| 446 | Raid currentRaid = this.mob.getCurrentRaid();
|
| 447 | return this.mob.isAlive() && this.mob.getTarget() == null && currentRaid != null && currentRaid.isLoss();
|
| 448 | }
|
| 449 |
|
| 450 | @Override
|
| 451 | public void start() {
|
| 452 | this.mob.setCelebrating(true);
|
| 453 | super.start();
|
| 454 | }
|
| 455 |
|
| 456 | @Override
|
| 457 | public void stop() {
|
| 458 | this.mob.setCelebrating(false);
|
| 459 | super.stop();
|
| 460 | }
|
| 461 |
|
| 462 | @Override
|
| 463 | public void tick() {
|
| 464 | if (!this.mob.isSilent() && this.mob.random.nextInt(this.adjustedTickDelay(100)) == 0) {
|
| 465 | Raider.this.makeSound(Raider.this.getCelebrateSound());
|
| 466 | }
|
| 467 |
|
| 468 | if (!this.mob.isPassenger() && this.mob.random.nextInt(this.adjustedTickDelay(50)) == 0) {
|
| 469 | this.mob.getJumpControl().jump();
|
| 470 | }
|
| 471 |
|
| 472 | super.tick();
|
| 473 | }
|
| 474 | }
|
| 475 |
|
| 476 | private static class RaiderMoveThroughVillageGoal extends Goal {
|
| 477 | private final Raider raider;
|
| 478 | private final double speedModifier;
|
| 479 | private BlockPos poiPos;
|
| 480 | private final List<BlockPos> visited = Lists.newArrayList();
|
| 481 | private final int distanceToPoi;
|
| 482 | private boolean stuck;
|
| 483 |
|
| 484 | public RaiderMoveThroughVillageGoal(final Raider mob, final double speedModifier, final int distanceToPoi) {
|
| 485 | this.raider = mob;
|
| 486 | this.speedModifier = speedModifier;
|
| 487 | this.distanceToPoi = distanceToPoi;
|
| 488 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 489 | }
|
| 490 |
|
| 491 | @Override
|
| 492 | public boolean canUse() {
|
| 493 | this.updateVisited();
|
| 494 | return this.isValidRaid() && this.hasSuitablePoi() && this.raider.getTarget() == null;
|
| 495 | }
|
| 496 |
|
| 497 | private boolean isValidRaid() {
|
| 498 | return this.raider.hasActiveRaid() && !this.raider.getCurrentRaid().isOver();
|
| 499 | }
|
| 500 |
|
| 501 | private boolean hasSuitablePoi() {
|
| 502 | ServerLevel level = (ServerLevel)this.raider.level();
|
| 503 | BlockPos pos = this.raider.blockPosition();
|
| 504 | Optional<BlockPos> homePos = level.getPoiManager()
|
| 505 | .getRandom(p -> p.is(PoiTypes.HOME), this::hasNotVisited, PoiManager.Occupancy.ANY, pos, 48, this.raider.random);
|
| 506 | if (homePos.isEmpty()) {
|
| 507 | return false;
|
| 508 | }
|
| 509 |
|
| 510 | this.poiPos = homePos.get().immutable();
|
| 511 | return true;
|
| 512 | }
|
| 513 |
|
| 514 | @Override
|
| 515 | public boolean canContinueToUse() {
|
| 516 | return this.raider.getNavigation().isDone()
|
| 517 | ? false
|
| 518 | : this.raider.getTarget() == null && !this.poiPos.closerToCenterThan(this.raider.position(), this.raider.getBbWidth() + this.distanceToPoi) && !this.stuck;
|
| 519 | }
|
| 520 |
|
| 521 | @Override
|
| 522 | public void stop() {
|
| 523 | if (this.poiPos.closerToCenterThan(this.raider.position(), this.distanceToPoi)) {
|
| 524 | this.visited.add(this.poiPos);
|
| 525 | }
|
| 526 | }
|
| 527 |
|
| 528 | @Override
|
| 529 | public void start() {
|
| 530 | super.start();
|
| 531 | this.raider.setNoActionTime(0);
|
| 532 | this.raider.getNavigation().moveTo(this.poiPos.getX(), this.poiPos.getY(), this.poiPos.getZ(), this.speedModifier);
|
| 533 | this.stuck = false;
|
| 534 | }
|
| 535 |
|
| 536 | @Override
|
| 537 | public void tick() {
|
| 538 | if (this.raider.getNavigation().isDone()) {
|
| 539 | Vec3 poiVec = Vec3.atBottomCenterOf(this.poiPos);
|
| 540 | Vec3 nextPos = DefaultRandomPos.getPosTowards(this.raider, 16, 7, poiVec, (float) (Math.PI / 10));
|
| 541 | if (nextPos == null) {
|
| 542 | nextPos = DefaultRandomPos.getPosTowards(this.raider, 8, 7, poiVec, (float) (Math.PI / 2));
|
| 543 | }
|
| 544 |
|
| 545 | if (nextPos == null) {
|
| 546 | this.stuck = true;
|
| 547 | return;
|
| 548 | }
|
| 549 |
|
| 550 | this.raider.getNavigation().moveTo(nextPos.x, nextPos.y, nextPos.z, this.speedModifier);
|
| 551 | }
|
| 552 | }
|
| 553 |
|
| 554 | private boolean hasNotVisited(final BlockPos poi) {
|
| 555 | for (BlockPos visitedPoi : this.visited) {
|
| 556 | if (Objects.equals(poi, visitedPoi)) {
|
| 557 | return false;
|
| 558 | }
|
| 559 | }
|
| 560 |
|
| 561 | return true;
|
| 562 | }
|
| 563 |
|
| 564 | private void updateVisited() {
|
| 565 | if (this.visited.size() > 2) {
|
| 566 | this.visited.remove(0);
|
| 567 | }
|
| 568 | }
|
| 569 | }
|
| 570 | }
|
| 571 |
|