| 1 | package net.minecraft.world.entity.monster.warden;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.Collections;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.function.BiConsumer;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.particles.BlockParticleOption;
|
| 11 | import net.minecraft.core.particles.ParticleTypes;
|
| 12 | import net.minecraft.network.protocol.Packet;
|
| 13 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 14 | import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
| 15 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 16 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 17 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 18 | import net.minecraft.server.level.ServerEntity;
|
| 19 | import net.minecraft.server.level.ServerLevel;
|
| 20 | import net.minecraft.sounds.SoundEvent;
|
| 21 | import net.minecraft.sounds.SoundEvents;
|
| 22 | import net.minecraft.tags.DamageTypeTags;
|
| 23 | import net.minecraft.tags.GameEventTags;
|
| 24 | import net.minecraft.tags.TagKey;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.util.RandomSource;
|
| 27 | import net.minecraft.util.Unit;
|
| 28 | import net.minecraft.util.profiling.Profiler;
|
| 29 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 30 | import net.minecraft.world.DifficultyInstance;
|
| 31 | import net.minecraft.world.damagesource.DamageSource;
|
| 32 | import net.minecraft.world.effect.MobEffectInstance;
|
| 33 | import net.minecraft.world.effect.MobEffectUtil;
|
| 34 | import net.minecraft.world.effect.MobEffects;
|
| 35 | import net.minecraft.world.entity.AnimationState;
|
| 36 | import net.minecraft.world.entity.Entity;
|
| 37 | import net.minecraft.world.entity.EntityDimensions;
|
| 38 | import net.minecraft.world.entity.EntityEvent;
|
| 39 | import net.minecraft.world.entity.EntitySelector;
|
| 40 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 41 | import net.minecraft.world.entity.EntityType;
|
| 42 | import net.minecraft.world.entity.EntityTypes;
|
| 43 | import net.minecraft.world.entity.LivingEntity;
|
| 44 | import net.minecraft.world.entity.Pose;
|
| 45 | import net.minecraft.world.entity.SpawnGroupData;
|
| 46 | import net.minecraft.world.entity.ai.Brain;
|
| 47 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 48 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 49 | import net.minecraft.world.entity.ai.behavior.warden.SonicBoom;
|
| 50 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 51 | import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
| 52 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 53 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 54 | import net.minecraft.world.entity.monster.Monster;
|
| 55 | import net.minecraft.world.entity.player.Player;
|
| 56 | import net.minecraft.world.level.Explosion;
|
| 57 | import net.minecraft.world.level.Level;
|
| 58 | import net.minecraft.world.level.LevelReader;
|
| 59 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 60 | import net.minecraft.world.level.block.RenderShape;
|
| 61 | import net.minecraft.world.level.block.state.BlockState;
|
| 62 | import net.minecraft.world.level.gameevent.DynamicGameEventListener;
|
| 63 | import net.minecraft.world.level.gameevent.EntityPositionSource;
|
| 64 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 65 | import net.minecraft.world.level.gameevent.PositionSource;
|
| 66 | import net.minecraft.world.level.gameevent.vibrations.VibrationSystem;
|
| 67 | import net.minecraft.world.level.pathfinder.Node;
|
| 68 | import net.minecraft.world.level.pathfinder.PathFinder;
|
| 69 | import net.minecraft.world.level.pathfinder.PathType;
|
| 70 | import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
|
| 71 | import net.minecraft.world.level.storage.ValueInput;
|
| 72 | import net.minecraft.world.level.storage.ValueOutput;
|
| 73 | import net.minecraft.world.phys.Vec3;
|
| 74 | import org.jetbrains.annotations.Contract;
|
| 75 | import org.jspecify.annotations.Nullable;
|
| 76 |
|
| 77 | public class Warden extends Monster implements VibrationSystem {
|
| 78 | private static final int VIBRATION_COOLDOWN_TICKS = 40;
|
| 79 | private static final int TIME_TO_USE_MELEE_UNTIL_SONIC_BOOM = 200;
|
| 80 | private static final int MAX_HEALTH = 500;
|
| 81 | private static final float MOVEMENT_SPEED_WHEN_FIGHTING = 0.3F;
|
| 82 | private static final float KNOCKBACK_RESISTANCE = 1.0F;
|
| 83 | private static final float ATTACK_KNOCKBACK = 1.5F;
|
| 84 | private static final int ATTACK_DAMAGE = 30;
|
| 85 | private static final int FOLLOW_RANGE = 24;
|
| 86 | private static final EntityDataAccessor<Integer> CLIENT_ANGER_LEVEL = SynchedEntityData.defineId(Warden.class, EntityDataSerializers.INT);
|
| 87 | private static final int DARKNESS_DISPLAY_LIMIT = 200;
|
| 88 | private static final int DARKNESS_DURATION = 260;
|
| 89 | private static final int DARKNESS_RADIUS = 20;
|
| 90 | private static final int DARKNESS_INTERVAL = 120;
|
| 91 | private static final int ANGERMANAGEMENT_TICK_DELAY = 20;
|
| 92 | private static final int DEFAULT_ANGER = 35;
|
| 93 | private static final int PROJECTILE_ANGER = 10;
|
| 94 | private static final int ON_HURT_ANGER_BOOST = 20;
|
| 95 | private static final int RECENT_PROJECTILE_TICK_THRESHOLD = 100;
|
| 96 | private static final int TOUCH_COOLDOWN_TICKS = 20;
|
| 97 | private static final int DIGGING_PARTICLES_AMOUNT = 30;
|
| 98 | private static final float DIGGING_PARTICLES_DURATION = 4.5F;
|
| 99 | private static final float DIGGING_PARTICLES_OFFSET = 0.7F;
|
| 100 | private static final int PROJECTILE_ANGER_DISTANCE = 30;
|
| 101 | private static final Brain.Provider<Warden> BRAIN_PROVIDER = Brain.provider(
|
| 102 | List.of(MemoryModuleType.NEAREST_VISIBLE_NEMESIS, MemoryModuleType.RECENT_PROJECTILE, MemoryModuleType.TOUCH_COOLDOWN, MemoryModuleType.VIBRATION_COOLDOWN),
|
| 103 | List.of(SensorType.NEAREST_PLAYERS, SensorType.WARDEN_ENTITY_SENSOR),
|
| 104 | WardenAi::getActivities
|
| 105 | );
|
| 106 | private int tendrilAnimation;
|
| 107 | private int tendrilAnimationO;
|
| 108 | private int heartAnimation;
|
| 109 | private int heartAnimationO;
|
| 110 | public final AnimationState roarAnimationState = new AnimationState();
|
| 111 | public final AnimationState sniffAnimationState = new AnimationState();
|
| 112 | public final AnimationState emergeAnimationState = new AnimationState();
|
| 113 | public final AnimationState diggingAnimationState = new AnimationState();
|
| 114 | public final AnimationState attackAnimationState = new AnimationState();
|
| 115 | public final AnimationState sonicBoomAnimationState = new AnimationState();
|
| 116 | private final DynamicGameEventListener<VibrationSystem.Listener> dynamicGameEventListener;
|
| 117 | private final VibrationSystem.User vibrationUser;
|
| 118 | private VibrationSystem.Data vibrationData;
|
| 119 | private AngerManagement angerManagement = new AngerManagement(this::canTargetEntity, Collections.emptyList());
|
| 120 |
|
| 121 | public Warden(final EntityType<? extends Monster> type, final Level level) {
|
| 122 | super(type, level);
|
| 123 | this.vibrationUser = new Warden.VibrationUser();
|
| 124 | this.vibrationData = new VibrationSystem.Data();
|
| 125 | this.dynamicGameEventListener = new DynamicGameEventListener<>(new VibrationSystem.Listener(this));
|
| 126 | this.xpReward = 5;
|
| 127 | this.getNavigation().setCanFloat(true);
|
| 128 | this.setPathfindingMalus(PathType.UNPASSABLE_RAIL, 0.0F);
|
| 129 | this.setPathfindingMalus(PathType.DAMAGING, 8.0F);
|
| 130 | this.setPathfindingMalus(PathType.POWDER_SNOW, 8.0F);
|
| 131 | this.setPathfindingMalus(PathType.LAVA, 8.0F);
|
| 132 | this.setPathfindingMalus(PathType.FIRE, 0.0F);
|
| 133 | this.setPathfindingMalus(PathType.FIRE_IN_NEIGHBOR, 0.0F);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public Packet<ClientGamePacketListener> getAddEntityPacket(final ServerEntity serverEntity) {
|
| 138 | return new ClientboundAddEntityPacket(this, serverEntity, this.hasPose(Pose.EMERGING) ? 1 : 0);
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | public void recreateFromPacket(final ClientboundAddEntityPacket packet) {
|
| 143 | super.recreateFromPacket(packet);
|
| 144 | if (packet.getData() == 1) {
|
| 145 | this.setPose(Pose.EMERGING);
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public boolean checkSpawnObstruction(final LevelReader level) {
|
| 151 | return super.checkSpawnObstruction(level) && level.noCollision(this, this.getType().getDimensions().makeBoundingBox(this.position()));
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
|
| 156 | return 0.0F;
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | public boolean isInvulnerableTo(final ServerLevel level, final DamageSource source) {
|
| 161 | return this.isDiggingOrEmerging() && !source.is(DamageTypeTags.BYPASSES_INVULNERABILITY) ? true : super.isInvulnerableTo(level, source);
|
| 162 | }
|
| 163 |
|
| 164 | private boolean isDiggingOrEmerging() {
|
| 165 | return this.hasPose(Pose.DIGGING) || this.hasPose(Pose.EMERGING);
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | protected boolean canRide(final Entity vehicle) {
|
| 170 | return false;
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public float getSecondsToDisableBlocking() {
|
| 175 | return 5.0F;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | protected float nextStep() {
|
| 180 | return this.moveDist + 0.55F;
|
| 181 | }
|
| 182 |
|
| 183 | public static AttributeSupplier.Builder createAttributes() {
|
| 184 | return Monster.createMonsterAttributes()
|
| 185 | .add(Attributes.MAX_HEALTH, 500.0)
|
| 186 | .add(Attributes.MOVEMENT_SPEED, 0.3F)
|
| 187 | .add(Attributes.KNOCKBACK_RESISTANCE, 1.0)
|
| 188 | .add(Attributes.ATTACK_KNOCKBACK, 1.5)
|
| 189 | .add(Attributes.ATTACK_DAMAGE, 30.0)
|
| 190 | .add(Attributes.FOLLOW_RANGE, 24.0);
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | public boolean dampensVibrations() {
|
| 195 | return true;
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | protected float getSoundVolume() {
|
| 200 | return 4.0F;
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | protected @Nullable SoundEvent getAmbientSound() {
|
| 205 | return !this.hasPose(Pose.ROARING) && !this.isDiggingOrEmerging() ? this.getAngerLevel().getAmbientSound() : null;
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 210 | return SoundEvents.WARDEN_HURT;
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | protected SoundEvent getDeathSound() {
|
| 215 | return SoundEvents.WARDEN_DEATH;
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 220 | this.playSound(SoundEvents.WARDEN_STEP, 10.0F, 1.0F);
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | public boolean doHurtTarget(final ServerLevel level, final Entity target) {
|
| 225 | level.broadcastEntityEvent(this, (byte)4);
|
| 226 | this.playSound(SoundEvents.WARDEN_ATTACK_IMPACT, 10.0F, this.getVoicePitch());
|
| 227 | SonicBoom.setCooldown(this, 40);
|
| 228 | return super.doHurtTarget(level, target);
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 233 | super.defineSynchedData(entityData);
|
| 234 | entityData.define(CLIENT_ANGER_LEVEL, 0);
|
| 235 | }
|
| 236 |
|
| 237 | public int getClientAngerLevel() {
|
| 238 | return this.entityData.get(CLIENT_ANGER_LEVEL);
|
| 239 | }
|
| 240 |
|
| 241 | private void syncClientAngerLevel() {
|
| 242 | this.entityData.set(CLIENT_ANGER_LEVEL, this.getActiveAnger());
|
| 243 | }
|
| 244 |
|
| 245 | @Override
|
| 246 | public void tick() {
|
| 247 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 248 | VibrationSystem.Ticker.tick(serverLevel, this.vibrationData, this.vibrationUser);
|
| 249 | if (this.isPersistenceRequired() || this.requiresCustomPersistence()) {
|
| 250 | WardenAi.setDigCooldown(this);
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | super.tick();
|
| 255 | if (this.level().isClientSide()) {
|
| 256 | if (this.tickCount % this.getHeartBeatDelay() == 0) {
|
| 257 | this.heartAnimation = 10;
|
| 258 | if (!this.isSilent()) {
|
| 259 | this.level().playLocalSound(this.getX(), this.getY(), this.getZ(), SoundEvents.WARDEN_HEARTBEAT, this.getSoundSource(), 5.0F, this.getVoicePitch(), false);
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | this.tendrilAnimationO = this.tendrilAnimation;
|
| 264 | if (this.tendrilAnimation > 0) {
|
| 265 | this.tendrilAnimation--;
|
| 266 | }
|
| 267 |
|
| 268 | this.heartAnimationO = this.heartAnimation;
|
| 269 | if (this.heartAnimation > 0) {
|
| 270 | this.heartAnimation--;
|
| 271 | }
|
| 272 |
|
| 273 | switch (this.getPose()) {
|
| 274 | case EMERGING:
|
| 275 | this.clientDiggingParticles(this.emergeAnimationState);
|
| 276 | break;
|
| 277 | case DIGGING:
|
| 278 | this.clientDiggingParticles(this.diggingAnimationState);
|
| 279 | }
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | protected void customServerAiStep(final ServerLevel level) {
|
| 285 | ProfilerFiller profiler = Profiler.get();
|
| 286 | profiler.push("wardenBrain");
|
| 287 | this.getBrain().tick(level, this);
|
| 288 | profiler.pop();
|
| 289 | super.customServerAiStep(level);
|
| 290 | if ((this.tickCount + this.getId()) % 120 == 0) {
|
| 291 | applyDarknessAround(level, this.position(), this, 20);
|
| 292 | }
|
| 293 |
|
| 294 | if (this.tickCount % 20 == 0) {
|
| 295 | this.angerManagement.tick(level, this::canTargetEntity);
|
| 296 | this.syncClientAngerLevel();
|
| 297 | }
|
| 298 |
|
| 299 | WardenAi.updateActivity(this.getBrain());
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 304 | if (id == 4) {
|
| 305 | this.roarAnimationState.stop();
|
| 306 | this.attackAnimationState.start(this.tickCount);
|
| 307 | } else if (id == 61) {
|
| 308 | this.tendrilAnimation = 10;
|
| 309 | } else if (id == 62) {
|
| 310 | this.sonicBoomAnimationState.start(this.tickCount);
|
| 311 | } else {
|
| 312 | super.handleEntityEvent(id);
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | private int getHeartBeatDelay() {
|
| 317 | float anger = (float)this.getClientAngerLevel() / AngerLevel.ANGRY.getMinimumAnger();
|
| 318 | return 40 - Mth.floor(Mth.clamp(anger, 0.0F, 1.0F) * 30.0F);
|
| 319 | }
|
| 320 |
|
| 321 | public float getTendrilAnimation(final float a) {
|
| 322 | return Mth.lerp(a, this.tendrilAnimationO, this.tendrilAnimation) / 10.0F;
|
| 323 | }
|
| 324 |
|
| 325 | public float getHeartAnimation(final float a) {
|
| 326 | return Mth.lerp(a, this.heartAnimationO, this.heartAnimation) / 10.0F;
|
| 327 | }
|
| 328 |
|
| 329 | private void clientDiggingParticles(final AnimationState state) {
|
| 330 | if ((float)state.getTimeInMillis(this.tickCount) < 4500.0F) {
|
| 331 | RandomSource random = this.getRandom();
|
| 332 | BlockState stateBelow = this.getBlockStateOn();
|
| 333 | if (stateBelow.getRenderShape() != RenderShape.INVISIBLE) {
|
| 334 | for (int i = 0; i < 30; i++) {
|
| 335 | double xx = this.getX() + Mth.randomBetween(random, -0.7F, 0.7F);
|
| 336 | double yy = this.getY();
|
| 337 | double zz = this.getZ() + Mth.randomBetween(random, -0.7F, 0.7F);
|
| 338 | this.level().addParticle(new BlockParticleOption(ParticleTypes.BLOCK, stateBelow), xx, yy, zz, 0.0, 0.0, 0.0);
|
| 339 | }
|
| 340 | }
|
| 341 | }
|
| 342 | }
|
| 343 |
|
| 344 | @Override
|
| 345 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 346 | if (DATA_POSE.equals(accessor)) {
|
| 347 | switch (this.getPose()) {
|
| 348 | case EMERGING:
|
| 349 | this.emergeAnimationState.start(this.tickCount);
|
| 350 | break;
|
| 351 | case DIGGING:
|
| 352 | this.diggingAnimationState.start(this.tickCount);
|
| 353 | break;
|
| 354 | case ROARING:
|
| 355 | this.roarAnimationState.start(this.tickCount);
|
| 356 | break;
|
| 357 | case SNIFFING:
|
| 358 | this.sniffAnimationState.start(this.tickCount);
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | super.onSyncedDataUpdated(accessor);
|
| 363 | }
|
| 364 |
|
| 365 | @Override
|
| 366 | public boolean ignoreExplosion(final Explosion explosion) {
|
| 367 | return this.isDiggingOrEmerging();
|
| 368 | }
|
| 369 |
|
| 370 | @Override
|
| 371 | protected Brain<Warden> makeBrain(final Brain.Packed packedBrain) {
|
| 372 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 373 | }
|
| 374 |
|
| 375 | @Override
|
| 376 | public Brain<Warden> getBrain() {
|
| 377 | return super.getBrain();
|
| 378 | }
|
| 379 |
|
| 380 | @Override
|
| 381 | public void updateDynamicGameEventListener(final BiConsumer<DynamicGameEventListener<?>, ServerLevel> action) {
|
| 382 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 383 | action.accept(this.dynamicGameEventListener, serverLevel);
|
| 384 | }
|
| 385 | }
|
| 386 |
|
| 387 | @Override
|
| 388 | public boolean canAttack(final LivingEntity target) {
|
| 389 | return this.canTargetEntity(target);
|
| 390 | }
|
| 391 |
|
| 392 | @Contract("null->false")
|
| 393 | public boolean canTargetEntity(final @Nullable Entity entity) {
|
| 394 | return entity instanceof LivingEntity livingEntity
|
| 395 | && this.level() == entity.level()
|
| 396 | && EntitySelector.NO_CREATIVE_OR_SPECTATOR.test(entity)
|
| 397 | && !this.isAlliedTo(entity)
|
| 398 | && !livingEntity.is(EntityTypes.ARMOR_STAND)
|
| 399 | && !livingEntity.is(EntityTypes.WARDEN)
|
| 400 | && !livingEntity.isInvulnerable()
|
| 401 | && !livingEntity.isDeadOrDying()
|
| 402 | && this.level().getWorldBorder().isWithinBounds(livingEntity.getBoundingBox());
|
| 403 | }
|
| 404 |
|
| 405 | public static void applyDarknessAround(final ServerLevel level, final Vec3 position, final @Nullable Entity source, final int darknessRadius) {
|
| 406 | MobEffectInstance darkness = new MobEffectInstance(MobEffects.DARKNESS, 260, 0, false, false);
|
| 407 | MobEffectUtil.addEffectToPlayersAround(level, source, position, darknessRadius, darkness, 200);
|
| 408 | }
|
| 409 |
|
| 410 | @Override
|
| 411 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 412 | super.addAdditionalSaveData(output);
|
| 413 | output.store("anger", AngerManagement.codec(this::canTargetEntity), this.angerManagement);
|
| 414 | output.store("listener", VibrationSystem.Data.CODEC, this.vibrationData);
|
| 415 | }
|
| 416 |
|
| 417 | @Override
|
| 418 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 419 | super.readAdditionalSaveData(input);
|
| 420 | this.angerManagement = input.read("anger", AngerManagement.codec(this::canTargetEntity))
|
| 421 | .orElseGet(() -> new AngerManagement(this::canTargetEntity, Collections.emptyList()));
|
| 422 | this.syncClientAngerLevel();
|
| 423 | this.vibrationData = input.read("listener", VibrationSystem.Data.CODEC).orElseGet(VibrationSystem.Data::new);
|
| 424 | }
|
| 425 |
|
| 426 | private void playListeningSound() {
|
| 427 | if (!this.hasPose(Pose.ROARING)) {
|
| 428 | this.playSound(this.getAngerLevel().getListeningSound(), 10.0F, this.getVoicePitch());
|
| 429 | }
|
| 430 | }
|
| 431 |
|
| 432 | public AngerLevel getAngerLevel() {
|
| 433 | return AngerLevel.byAnger(this.getActiveAnger());
|
| 434 | }
|
| 435 |
|
| 436 | private int getActiveAnger() {
|
| 437 | return this.angerManagement.getActiveAnger(this.getTarget());
|
| 438 | }
|
| 439 |
|
| 440 | public void clearAnger(final Entity entity) {
|
| 441 | this.angerManagement.clearAnger(entity);
|
| 442 | }
|
| 443 |
|
| 444 | public void increaseAngerAt(final @Nullable Entity entity) {
|
| 445 | this.increaseAngerAt(entity, 35, true);
|
| 446 | }
|
| 447 |
|
| 448 | @VisibleForTesting
|
| 449 | public void increaseAngerAt(final @Nullable Entity entity, final int amount, final boolean playSound) {
|
| 450 | if (!this.isNoAi() && this.canTargetEntity(entity)) {
|
| 451 | WardenAi.setDigCooldown(this);
|
| 452 | boolean maybeSwitchTarget = !(this.getTarget() instanceof Player);
|
| 453 | int newAnger = this.angerManagement.increaseAnger(entity, amount);
|
| 454 | if (entity instanceof Player && maybeSwitchTarget && AngerLevel.byAnger(newAnger).isAngry()) {
|
| 455 | this.getBrain().eraseMemory(MemoryModuleType.ATTACK_TARGET);
|
| 456 | }
|
| 457 |
|
| 458 | if (playSound) {
|
| 459 | this.playListeningSound();
|
| 460 | }
|
| 461 | }
|
| 462 | }
|
| 463 |
|
| 464 | public Optional<LivingEntity> getEntityAngryAt() {
|
| 465 | return this.getAngerLevel().isAngry() ? this.angerManagement.getActiveEntity() : Optional.empty();
|
| 466 | }
|
| 467 |
|
| 468 | @Override
|
| 469 | public @Nullable LivingEntity getTarget() {
|
| 470 | return this.getTargetFromBrain();
|
| 471 | }
|
| 472 |
|
| 473 | @Override
|
| 474 | public boolean removeWhenFarAway(final double distSqr) {
|
| 475 | return false;
|
| 476 | }
|
| 477 |
|
| 478 | @Override
|
| 479 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 480 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 481 | ) {
|
| 482 | this.getBrain().setMemoryWithExpiry(MemoryModuleType.DIG_COOLDOWN, Unit.INSTANCE, 1200L);
|
| 483 | if (spawnReason == EntitySpawnReason.TRIGGERED) {
|
| 484 | this.setPose(Pose.EMERGING);
|
| 485 | this.getBrain().setMemoryWithExpiry(MemoryModuleType.IS_EMERGING, Unit.INSTANCE, WardenAi.EMERGE_DURATION);
|
| 486 | this.playSound(SoundEvents.WARDEN_AGITATED, 5.0F, 1.0F);
|
| 487 | }
|
| 488 |
|
| 489 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 490 | }
|
| 491 |
|
| 492 | @Override
|
| 493 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 494 | boolean wasHurt = super.hurtServer(level, source, damage);
|
| 495 | if (!this.isNoAi() && !this.isDiggingOrEmerging()) {
|
| 496 | Entity attacker = source.getEntity();
|
| 497 | this.increaseAngerAt(attacker, AngerLevel.ANGRY.getMinimumAnger() + 20, false);
|
| 498 | if (this.brain.getMemory(MemoryModuleType.ATTACK_TARGET).isEmpty()
|
| 499 | && attacker instanceof LivingEntity livingAttacker
|
| 500 | && (source.isDirect() || this.closerThan(livingAttacker, 5.0))) {
|
| 501 | this.setAttackTarget(livingAttacker);
|
| 502 | }
|
| 503 | }
|
| 504 |
|
| 505 | return wasHurt;
|
| 506 | }
|
| 507 |
|
| 508 | public void setAttackTarget(final LivingEntity target) {
|
| 509 | this.getBrain().eraseMemory(MemoryModuleType.ROAR_TARGET);
|
| 510 | this.getBrain().setMemory(MemoryModuleType.ATTACK_TARGET, target);
|
| 511 | this.getBrain().eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 512 | SonicBoom.setCooldown(this, 200);
|
| 513 | }
|
| 514 |
|
| 515 | @Override
|
| 516 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 517 | EntityDimensions dimensions = super.getDefaultDimensions(pose);
|
| 518 | return this.isDiggingOrEmerging() ? EntityDimensions.fixed(dimensions.width(), 1.0F) : dimensions;
|
| 519 | }
|
| 520 |
|
| 521 | @Override
|
| 522 | public boolean isPushable() {
|
| 523 | return !this.isDiggingOrEmerging() && super.isPushable();
|
| 524 | }
|
| 525 |
|
| 526 | @Override
|
| 527 | protected void doPush(final Entity entity) {
|
| 528 | if (!this.isNoAi() && !this.getBrain().hasMemoryValue(MemoryModuleType.TOUCH_COOLDOWN)) {
|
| 529 | this.getBrain().setMemoryWithExpiry(MemoryModuleType.TOUCH_COOLDOWN, Unit.INSTANCE, 20L);
|
| 530 | this.increaseAngerAt(entity);
|
| 531 | WardenAi.setDisturbanceLocation(this, entity.blockPosition());
|
| 532 | }
|
| 533 |
|
| 534 | super.doPush(entity);
|
| 535 | }
|
| 536 |
|
| 537 | @VisibleForTesting
|
| 538 | public AngerManagement getAngerManagement() {
|
| 539 | return this.angerManagement;
|
| 540 | }
|
| 541 |
|
| 542 | @Override
|
| 543 | protected PathNavigation createNavigation(final Level level) {
|
| 544 | return new GroundPathNavigation(this, level) {
|
| 545 | @Override
|
| 546 | protected PathFinder createPathFinder(final int maxVisitedNodes) {
|
| 547 | this.nodeEvaluator = new WalkNodeEvaluator();
|
| 548 | return new PathFinder(this.nodeEvaluator, maxVisitedNodes) {
|
| 549 | @Override
|
| 550 | protected float distance(final Node from, final Node to) {
|
| 551 | return from.distanceToXZ(to);
|
| 552 | }
|
| 553 | };
|
| 554 | }
|
| 555 | };
|
| 556 | }
|
| 557 |
|
| 558 | @Override
|
| 559 | public VibrationSystem.Data getVibrationData() {
|
| 560 | return this.vibrationData;
|
| 561 | }
|
| 562 |
|
| 563 | @Override
|
| 564 | public VibrationSystem.User getVibrationUser() {
|
| 565 | return this.vibrationUser;
|
| 566 | }
|
| 567 |
|
| 568 | private class VibrationUser implements VibrationSystem.User {
|
| 569 | private static final int GAME_EVENT_LISTENER_RANGE = 16;
|
| 570 | private final PositionSource positionSource = new EntityPositionSource(Warden.this, Warden.this.getEyeHeight());
|
| 571 |
|
| 572 | @Override
|
| 573 | public int getListenerRadius() {
|
| 574 | return 16;
|
| 575 | }
|
| 576 |
|
| 577 | @Override
|
| 578 | public PositionSource getPositionSource() {
|
| 579 | return this.positionSource;
|
| 580 | }
|
| 581 |
|
| 582 | @Override
|
| 583 | public TagKey<GameEvent> getListenableEvents() {
|
| 584 | return GameEventTags.WARDEN_CAN_LISTEN;
|
| 585 | }
|
| 586 |
|
| 587 | @Override
|
| 588 | public boolean canTriggerAvoidVibration() {
|
| 589 | return true;
|
| 590 | }
|
| 591 |
|
| 592 | @Override
|
| 593 | public boolean canReceiveVibration(final ServerLevel level, final BlockPos pos, final Holder<GameEvent> event, final GameEvent.Context context) {
|
| 594 | return !Warden.this.isNoAi()
|
| 595 | && !Warden.this.isDeadOrDying()
|
| 596 | && !Warden.this.getBrain().hasMemoryValue(MemoryModuleType.VIBRATION_COOLDOWN)
|
| 597 | && !Warden.this.isDiggingOrEmerging()
|
| 598 | && level.getWorldBorder().isWithinBounds(pos)
|
| 599 | ? !(context.sourceEntity() instanceof LivingEntity livingEntity && !Warden.this.canTargetEntity(livingEntity))
|
| 600 | : false;
|
| 601 | }
|
| 602 |
|
| 603 | @Override
|
| 604 | public void onReceiveVibration(
|
| 605 | final ServerLevel level,
|
| 606 | final BlockPos pos,
|
| 607 | final Holder<GameEvent> event,
|
| 608 | final @Nullable Entity sourceEntity,
|
| 609 | final @Nullable Entity projectileOwner,
|
| 610 | final float receivingDistance
|
| 611 | ) {
|
| 612 | if (!Warden.this.isDeadOrDying()) {
|
| 613 | Warden.this.brain.setMemoryWithExpiry(MemoryModuleType.VIBRATION_COOLDOWN, Unit.INSTANCE, 40L);
|
| 614 | level.broadcastEntityEvent(Warden.this, (byte)61);
|
| 615 | Warden.this.playSound(SoundEvents.WARDEN_TENDRIL_CLICKS, 5.0F, Warden.this.getVoicePitch());
|
| 616 | BlockPos suspiciousPos = pos;
|
| 617 | if (projectileOwner != null) {
|
| 618 | if (Warden.this.closerThan(projectileOwner, 30.0)) {
|
| 619 | if (Warden.this.getBrain().hasMemoryValue(MemoryModuleType.RECENT_PROJECTILE)) {
|
| 620 | if (Warden.this.canTargetEntity(projectileOwner)) {
|
| 621 | suspiciousPos = projectileOwner.blockPosition();
|
| 622 | }
|
| 623 |
|
| 624 | Warden.this.increaseAngerAt(projectileOwner);
|
| 625 | } else {
|
| 626 | Warden.this.increaseAngerAt(projectileOwner, 10, true);
|
| 627 | }
|
| 628 | }
|
| 629 |
|
| 630 | Warden.this.getBrain().setMemoryWithExpiry(MemoryModuleType.RECENT_PROJECTILE, Unit.INSTANCE, 100L);
|
| 631 | } else {
|
| 632 | Warden.this.increaseAngerAt(sourceEntity);
|
| 633 | }
|
| 634 |
|
| 635 | if (!Warden.this.getAngerLevel().isAngry()) {
|
| 636 | Optional<LivingEntity> activeEntity = Warden.this.angerManagement.getActiveEntity();
|
| 637 | if (projectileOwner != null || activeEntity.isEmpty() || activeEntity.get() == sourceEntity) {
|
| 638 | WardenAi.setDisturbanceLocation(Warden.this, suspiciousPos);
|
| 639 | }
|
| 640 | }
|
| 641 | }
|
| 642 | }
|
| 643 | }
|
| 644 | }
|
| 645 |
|