| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.EnumSet;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.core.particles.ParticleTypes;
|
| 9 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 10 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 11 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.sounds.SoundEvent;
|
| 15 | import net.minecraft.sounds.SoundEvents;
|
| 16 | import net.minecraft.tags.BlockTags;
|
| 17 | import net.minecraft.tags.DamageTypeTags;
|
| 18 | import net.minecraft.tags.FluidTags;
|
| 19 | import net.minecraft.tags.PotionTags;
|
| 20 | import net.minecraft.util.BlockUtil;
|
| 21 | import net.minecraft.util.Mth;
|
| 22 | import net.minecraft.util.RandomSource;
|
| 23 | import net.minecraft.util.TimeUtil;
|
| 24 | import net.minecraft.util.valueproviders.UniformInt;
|
| 25 | import net.minecraft.world.damagesource.DamageSource;
|
| 26 | import net.minecraft.world.entity.Entity;
|
| 27 | import net.minecraft.world.entity.EntityReference;
|
| 28 | import net.minecraft.world.entity.EntityType;
|
| 29 | import net.minecraft.world.entity.LivingEntity;
|
| 30 | import net.minecraft.world.entity.NeutralMob;
|
| 31 | import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
| 32 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 33 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 34 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 35 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 36 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 37 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 38 | import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
|
| 39 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 40 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 41 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 42 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 43 | import net.minecraft.world.entity.ai.goal.target.ResetUniversalAngerTargetGoal;
|
| 44 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 45 | import net.minecraft.world.entity.player.Player;
|
| 46 | import net.minecraft.world.entity.projectile.throwableitemprojectile.AbstractThrownPotion;
|
| 47 | import net.minecraft.world.item.ItemStack;
|
| 48 | import net.minecraft.world.item.Items;
|
| 49 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 50 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 51 | import net.minecraft.world.item.enchantment.providers.VanillaEnchantmentProviders;
|
| 52 | import net.minecraft.world.level.ClipContext;
|
| 53 | import net.minecraft.world.level.Level;
|
| 54 | import net.minecraft.world.level.LevelReader;
|
| 55 | import net.minecraft.world.level.block.Block;
|
| 56 | import net.minecraft.world.level.block.Blocks;
|
| 57 | import net.minecraft.world.level.block.state.BlockState;
|
| 58 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 59 | import net.minecraft.world.level.gamerules.GameRules;
|
| 60 | import net.minecraft.world.level.pathfinder.PathType;
|
| 61 | import net.minecraft.world.level.storage.ValueInput;
|
| 62 | import net.minecraft.world.level.storage.ValueOutput;
|
| 63 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 64 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 65 | import net.minecraft.world.phys.AABB;
|
| 66 | import net.minecraft.world.phys.BlockHitResult;
|
| 67 | import net.minecraft.world.phys.Vec3;
|
| 68 | import org.jspecify.annotations.Nullable;
|
| 69 |
|
| 70 | public class Enderman extends Monster implements NeutralMob {
|
| 71 | private static final Identifier SPEED_MODIFIER_ATTACKING_ID = Identifier.withDefaultNamespace("attacking");
|
| 72 | private static final AttributeModifier SPEED_MODIFIER_ATTACKING = new AttributeModifier(
|
| 73 | SPEED_MODIFIER_ATTACKING_ID, 0.15F, AttributeModifier.Operation.ADD_VALUE
|
| 74 | );
|
| 75 | public static final int MAX_TELEPORT_RADIUS = 32;
|
| 76 | public static final int MAX_TELEPORT_DIAMETER = 64;
|
| 77 | private static final int DELAY_BETWEEN_CREEPY_STARE_SOUND = 400;
|
| 78 | private static final int MIN_DEAGGRESSION_TIME = 600;
|
| 79 | private static final EntityDataAccessor<Optional<BlockState>> DATA_CARRY_STATE = SynchedEntityData.defineId(
|
| 80 | Enderman.class, EntityDataSerializers.OPTIONAL_BLOCK_STATE
|
| 81 | );
|
| 82 | private static final EntityDataAccessor<Boolean> DATA_CREEPY = SynchedEntityData.defineId(Enderman.class, EntityDataSerializers.BOOLEAN);
|
| 83 | private static final EntityDataAccessor<Boolean> DATA_STARED_AT = SynchedEntityData.defineId(Enderman.class, EntityDataSerializers.BOOLEAN);
|
| 84 | private int lastStareSound = Integer.MIN_VALUE;
|
| 85 | private int targetChangeTime;
|
| 86 | private static final UniformInt PERSISTENT_ANGER_TIME = TimeUtil.rangeOfSeconds(20, 39);
|
| 87 | private long persistentAngerEndTime;
|
| 88 | private @Nullable EntityReference<LivingEntity> persistentAngerTarget;
|
| 89 |
|
| 90 | public Enderman(final EntityType<? extends Enderman> type, final Level level) {
|
| 91 | super(type, level);
|
| 92 | this.setPathfindingMalus(PathType.WATER, -1.0F);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected void registerGoals() {
|
| 97 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 98 | this.goalSelector.addGoal(1, new Enderman.EndermanFreezeWhenLookedAt(this));
|
| 99 | this.goalSelector.addGoal(2, new MeleeAttackGoal(this, 1.0, false));
|
| 100 | this.goalSelector.addGoal(7, new WaterAvoidingRandomStrollGoal(this, 1.0, 0.0F));
|
| 101 | this.goalSelector.addGoal(8, new LookAtPlayerGoal(this, Player.class, 8.0F));
|
| 102 | this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
|
| 103 | this.goalSelector.addGoal(10, new Enderman.EndermanLeaveBlockGoal(this));
|
| 104 | this.goalSelector.addGoal(11, new Enderman.EndermanTakeBlockGoal(this));
|
| 105 | this.targetSelector.addGoal(1, new Enderman.EndermanLookForPlayerGoal(this, this::isAngryAt));
|
| 106 | this.targetSelector.addGoal(2, new HurtByTargetGoal(this));
|
| 107 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Endermite.class, true, false));
|
| 108 | this.targetSelector.addGoal(4, new ResetUniversalAngerTargetGoal<>(this, false));
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
|
| 113 | return 0.0F;
|
| 114 | }
|
| 115 |
|
| 116 | public static AttributeSupplier.Builder createAttributes() {
|
| 117 | return Monster.createMonsterAttributes()
|
| 118 | .add(Attributes.MAX_HEALTH, 40.0)
|
| 119 | .add(Attributes.MOVEMENT_SPEED, 0.3F)
|
| 120 | .add(Attributes.ATTACK_DAMAGE, 7.0)
|
| 121 | .add(Attributes.FOLLOW_RANGE, 64.0)
|
| 122 | .add(Attributes.STEP_HEIGHT, 1.0);
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public void setTarget(final @Nullable LivingEntity target) {
|
| 127 | super.setTarget(target);
|
| 128 | AttributeInstance movementSpeed = this.getAttribute(Attributes.MOVEMENT_SPEED);
|
| 129 | if (target == null) {
|
| 130 | this.targetChangeTime = 0;
|
| 131 | this.entityData.set(DATA_CREEPY, false);
|
| 132 | this.entityData.set(DATA_STARED_AT, false);
|
| 133 | movementSpeed.removeModifier(SPEED_MODIFIER_ATTACKING_ID);
|
| 134 | } else {
|
| 135 | this.targetChangeTime = this.tickCount;
|
| 136 | this.entityData.set(DATA_CREEPY, true);
|
| 137 | if (!movementSpeed.hasModifier(SPEED_MODIFIER_ATTACKING_ID)) {
|
| 138 | movementSpeed.addTransientModifier(SPEED_MODIFIER_ATTACKING);
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 145 | super.defineSynchedData(entityData);
|
| 146 | entityData.define(DATA_CARRY_STATE, Optional.empty());
|
| 147 | entityData.define(DATA_CREEPY, false);
|
| 148 | entityData.define(DATA_STARED_AT, false);
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public void startPersistentAngerTimer() {
|
| 153 | this.setTimeToRemainAngry(PERSISTENT_ANGER_TIME.sample(this.random));
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public void setPersistentAngerEndTime(final long endTime) {
|
| 158 | this.persistentAngerEndTime = endTime;
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public long getPersistentAngerEndTime() {
|
| 163 | return this.persistentAngerEndTime;
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public void setPersistentAngerTarget(final @Nullable EntityReference<LivingEntity> persistentAngerTarget) {
|
| 168 | this.persistentAngerTarget = persistentAngerTarget;
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public @Nullable EntityReference<LivingEntity> getPersistentAngerTarget() {
|
| 173 | return this.persistentAngerTarget;
|
| 174 | }
|
| 175 |
|
| 176 | public void playStareSound() {
|
| 177 | if (this.tickCount >= this.lastStareSound + 400) {
|
| 178 | this.lastStareSound = this.tickCount;
|
| 179 | if (!this.isSilent()) {
|
| 180 | this.level().playLocalSound(this.getX(), this.getEyeY(), this.getZ(), SoundEvents.ENDERMAN_STARE, this.getSoundSource(), 2.5F, 1.0F, false);
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 187 | if (DATA_CREEPY.equals(accessor) && this.hasBeenStaredAt() && this.level().isClientSide()) {
|
| 188 | this.playStareSound();
|
| 189 | }
|
| 190 |
|
| 191 | super.onSyncedDataUpdated(accessor);
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 196 | super.addAdditionalSaveData(output);
|
| 197 | BlockState blockState = this.getCarriedBlock();
|
| 198 | if (blockState != null) {
|
| 199 | output.store("carriedBlockState", BlockState.CODEC, blockState);
|
| 200 | }
|
| 201 |
|
| 202 | this.addPersistentAngerSaveData(output);
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 207 | super.readAdditionalSaveData(input);
|
| 208 | this.setCarriedBlock(input.read("carriedBlockState", BlockState.CODEC).filter(blockState -> !blockState.isAir()).orElse(null));
|
| 209 | this.readPersistentAngerSaveData(this.level(), input);
|
| 210 | }
|
| 211 |
|
| 212 | private boolean isBeingStaredBy(final Player player) {
|
| 213 | return !LivingEntity.PLAYER_NOT_WEARING_DISGUISE_ITEM.test(player) ? false : this.isLookingAtMe(player, 0.025, true, false, this.getEyeY());
|
| 214 | }
|
| 215 |
|
| 216 | @Override
|
| 217 | public void aiStep() {
|
| 218 | if (this.level().isClientSide()) {
|
| 219 | for (int i = 0; i < 2; i++) {
|
| 220 | this.level()
|
| 221 | .addParticle(
|
| 222 | ParticleTypes.PORTAL,
|
| 223 | this.getRandomX(0.5),
|
| 224 | this.getRandomY() - 0.25,
|
| 225 | this.getRandomZ(0.5),
|
| 226 | (this.random.nextDouble() - 0.5) * 2.0,
|
| 227 | -this.random.nextDouble(),
|
| 228 | (this.random.nextDouble() - 0.5) * 2.0
|
| 229 | );
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | this.jumping = false;
|
| 234 | if (!this.level().isClientSide()) {
|
| 235 | this.updatePersistentAnger((ServerLevel)this.level(), true);
|
| 236 | }
|
| 237 |
|
| 238 | super.aiStep();
|
| 239 | }
|
| 240 |
|
| 241 | @Override
|
| 242 | public boolean isSensitiveToWater() {
|
| 243 | return true;
|
| 244 | }
|
| 245 |
|
| 246 | @Override
|
| 247 | protected void customServerAiStep(final ServerLevel level) {
|
| 248 | if (level.isBrightOutside() && this.tickCount >= this.targetChangeTime + 600) {
|
| 249 | float br = this.getLightLevelDependentMagicValue();
|
| 250 | if (br > 0.5F && level.canSeeSky(this.blockPosition()) && this.random.nextFloat() * 30.0F < (br - 0.4F) * 2.0F) {
|
| 251 | this.setTarget(null);
|
| 252 | this.teleport();
|
| 253 | }
|
| 254 | }
|
| 255 |
|
| 256 | super.customServerAiStep(level);
|
| 257 | }
|
| 258 |
|
| 259 | protected boolean teleport() {
|
| 260 | if (!this.level().isClientSide() && this.isAlive()) {
|
| 261 | double xx = this.getX() + (this.random.nextDouble() - 0.5) * 64.0;
|
| 262 | double yy = this.getY() + (this.random.nextInt(64) - 32);
|
| 263 | double zz = this.getZ() + (this.random.nextDouble() - 0.5) * 64.0;
|
| 264 | return this.teleport(xx, yy, zz);
|
| 265 | } else {
|
| 266 | return false;
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | private boolean teleportTowards(final Entity entity) {
|
| 271 | Vec3 dir = new Vec3(this.getX() - entity.getX(), this.getY(0.5) - entity.getEyeY(), this.getZ() - entity.getZ());
|
| 272 | dir = dir.normalize();
|
| 273 | double d = 16.0;
|
| 274 | double xx = this.getX() + (this.random.nextDouble() - 0.5) * 8.0 - dir.x * 16.0;
|
| 275 | double yy = this.getY() + (this.random.nextInt(16) - 8) - dir.y * 16.0;
|
| 276 | double zz = this.getZ() + (this.random.nextDouble() - 0.5) * 8.0 - dir.z * 16.0;
|
| 277 | return this.teleport(xx, yy, zz);
|
| 278 | }
|
| 279 |
|
| 280 | @VisibleForTesting
|
| 281 | public boolean teleport(final double x, final double y, final double z) {
|
| 282 | if (this.isPassenger()) {
|
| 283 | return false;
|
| 284 | }
|
| 285 |
|
| 286 | Vec3 oldPos = this.position();
|
| 287 | boolean result = this.randomTeleport(x, y, z, true, BlockTags.ENDERMAN_DOES_NOT_TELEPORT_TO);
|
| 288 | if (result) {
|
| 289 | Level level = this.level();
|
| 290 | level.gameEvent(GameEvent.TELEPORT, oldPos, GameEvent.Context.of(this));
|
| 291 | if (!this.isSilent()) {
|
| 292 | BlockPos oldBlockPos = BlockPos.containing(oldPos);
|
| 293 | int packedDiff = BlockUtil.clampedPackDifferenceInPosition(oldBlockPos, this.blockPosition(), 127, 127, 127);
|
| 294 | level.levelEvent(2018, oldBlockPos, packedDiff);
|
| 295 | level.playSound(null, this.xo, this.yo, this.zo, SoundEvents.ENDERMAN_TELEPORT, this.getSoundSource(), 1.0F, 1.0F);
|
| 296 | this.playSound(SoundEvents.ENDERMAN_TELEPORT, 1.0F, 1.0F);
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | return result;
|
| 301 | }
|
| 302 |
|
| 303 | @Override
|
| 304 | public boolean canRandomlyTeleportTo(final double x, final double y, final double z) {
|
| 305 | BlockPos posBelow = BlockPos.containing(x, y, z).below();
|
| 306 | BlockState stateBelow = this.level().getBlockState(posBelow);
|
| 307 | return !stateBelow.getFluidState().is(FluidTags.WATER);
|
| 308 | }
|
| 309 |
|
| 310 | private void repeatedlyTryToTeleport() {
|
| 311 | if (!this.isPassenger()) {
|
| 312 | for (int i = 0; i < 64; i++) {
|
| 313 | if (this.teleport()) {
|
| 314 | return;
|
| 315 | }
|
| 316 | }
|
| 317 | }
|
| 318 | }
|
| 319 |
|
| 320 | @Override
|
| 321 | protected SoundEvent getAmbientSound() {
|
| 322 | return this.isCreepy() ? SoundEvents.ENDERMAN_SCREAM : SoundEvents.ENDERMAN_AMBIENT;
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 327 | return SoundEvents.ENDERMAN_HURT;
|
| 328 | }
|
| 329 |
|
| 330 | @Override
|
| 331 | protected SoundEvent getDeathSound() {
|
| 332 | return SoundEvents.ENDERMAN_DEATH;
|
| 333 | }
|
| 334 |
|
| 335 | @Override
|
| 336 | protected void dropCustomDeathLoot(final ServerLevel level, final DamageSource source, final boolean killedByPlayer) {
|
| 337 | super.dropCustomDeathLoot(level, source, killedByPlayer);
|
| 338 | BlockState carryingBlock = this.getCarriedBlock();
|
| 339 | if (carryingBlock != null) {
|
| 340 | ItemStack fakeTool = new ItemStack(Items.DIAMOND_AXE);
|
| 341 | EnchantmentHelper.enchantItemFromProvider(
|
| 342 | fakeTool, level.registryAccess(), VanillaEnchantmentProviders.ENDERMAN_LOOT_DROP, level.getCurrentDifficultyAt(this.blockPosition()), this.getRandom()
|
| 343 | );
|
| 344 | LootParams.Builder params = new LootParams.Builder((ServerLevel)this.level())
|
| 345 | .withParameter(LootContextParams.ORIGIN, this.position())
|
| 346 | .withParameter(LootContextParams.TOOL, fakeTool)
|
| 347 | .withOptionalParameter(LootContextParams.THIS_ENTITY, this);
|
| 348 |
|
| 349 | for (ItemStack itemStack : carryingBlock.getDrops(params)) {
|
| 350 | this.spawnAtLocation(level, itemStack);
|
| 351 | }
|
| 352 | }
|
| 353 | }
|
| 354 |
|
| 355 | public void setCarriedBlock(final @Nullable BlockState carryingBlock) {
|
| 356 | this.entityData.set(DATA_CARRY_STATE, Optional.ofNullable(carryingBlock));
|
| 357 | }
|
| 358 |
|
| 359 | public @Nullable BlockState getCarriedBlock() {
|
| 360 | return this.entityData.get(DATA_CARRY_STATE).orElse(null);
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 365 | if (this.isInvulnerableTo(level, source)) {
|
| 366 | return false;
|
| 367 | }
|
| 368 |
|
| 369 | AbstractThrownPotion thrownPotion = source.getDirectEntity() instanceof AbstractThrownPotion potion ? potion : null;
|
| 370 | if (source.is(DamageTypeTags.IS_PROJECTILE) && !this.isPassenger()) {
|
| 371 | this.repeatedlyTryToTeleport();
|
| 372 | return false;
|
| 373 | }
|
| 374 |
|
| 375 | if (thrownPotion != null) {
|
| 376 | boolean hurtWithCleanWater = this.hurtWithCleanWater(level, source, thrownPotion, damage);
|
| 377 | this.repeatedlyTryToTeleport();
|
| 378 | return hurtWithCleanWater;
|
| 379 | }
|
| 380 |
|
| 381 | boolean result = super.hurtServer(level, source, damage);
|
| 382 | if (!(source.getEntity() instanceof LivingEntity) && this.random.nextInt(10) != 0) {
|
| 383 | this.teleport();
|
| 384 | }
|
| 385 |
|
| 386 | return result;
|
| 387 | }
|
| 388 |
|
| 389 | private boolean hurtWithCleanWater(final ServerLevel level, final DamageSource source, final AbstractThrownPotion thrownPotion, final float damage) {
|
| 390 | ItemStack potionItemStack = thrownPotion.getItem();
|
| 391 | PotionContents potionContents = potionItemStack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY);
|
| 392 | return potionContents.is(PotionTags.HURTS_WATER_SENSITIVE_ENTITIES) ? super.hurtServer(level, source, damage) : false;
|
| 393 | }
|
| 394 |
|
| 395 | @Override
|
| 396 | public boolean projectileReceivesSideEffectsOnHit(final boolean wasHurt) {
|
| 397 | return wasHurt;
|
| 398 | }
|
| 399 |
|
| 400 | public boolean isCreepy() {
|
| 401 | return this.entityData.get(DATA_CREEPY);
|
| 402 | }
|
| 403 |
|
| 404 | public boolean hasBeenStaredAt() {
|
| 405 | return this.entityData.get(DATA_STARED_AT);
|
| 406 | }
|
| 407 |
|
| 408 | public void setBeingStaredAt() {
|
| 409 | this.entityData.set(DATA_STARED_AT, true);
|
| 410 | }
|
| 411 |
|
| 412 | @Override
|
| 413 | public boolean requiresCustomPersistence() {
|
| 414 | return super.requiresCustomPersistence() || this.getCarriedBlock() != null;
|
| 415 | }
|
| 416 |
|
| 417 | private static class EndermanFreezeWhenLookedAt extends Goal {
|
| 418 | private final Enderman enderman;
|
| 419 | private @Nullable LivingEntity target;
|
| 420 |
|
| 421 | public EndermanFreezeWhenLookedAt(final Enderman enderman) {
|
| 422 | this.enderman = enderman;
|
| 423 | this.setFlags(EnumSet.of(Goal.Flag.JUMP, Goal.Flag.MOVE));
|
| 424 | }
|
| 425 |
|
| 426 | @Override
|
| 427 | public boolean canUse() {
|
| 428 | this.target = this.enderman.getTarget();
|
| 429 | if (this.target instanceof Player playerTarget) {
|
| 430 | double dist = this.target.distanceToSqr(this.enderman);
|
| 431 | return dist > 256.0 ? false : this.enderman.isBeingStaredBy(playerTarget);
|
| 432 | } else {
|
| 433 | return false;
|
| 434 | }
|
| 435 | }
|
| 436 |
|
| 437 | @Override
|
| 438 | public void start() {
|
| 439 | this.enderman.getNavigation().stop();
|
| 440 | }
|
| 441 |
|
| 442 | @Override
|
| 443 | public void tick() {
|
| 444 | this.enderman.getLookControl().setLookAt(this.target.getX(), this.target.getEyeY(), this.target.getZ());
|
| 445 | }
|
| 446 | }
|
| 447 |
|
| 448 | private static class EndermanLeaveBlockGoal extends Goal {
|
| 449 | private final Enderman enderman;
|
| 450 |
|
| 451 | public EndermanLeaveBlockGoal(final Enderman enderman) {
|
| 452 | this.enderman = enderman;
|
| 453 | }
|
| 454 |
|
| 455 | @Override
|
| 456 | public boolean canUse() {
|
| 457 | if (this.enderman.getCarriedBlock() == null) {
|
| 458 | return false;
|
| 459 | } else {
|
| 460 | return !getServerLevel(this.enderman).getGameRules().get(GameRules.MOB_GRIEFING) ? false : this.enderman.getRandom().nextInt(reducedTickDelay(2000)) == 0;
|
| 461 | }
|
| 462 | }
|
| 463 |
|
| 464 | @Override
|
| 465 | public void tick() {
|
| 466 | RandomSource random = this.enderman.getRandom();
|
| 467 | Level level = this.enderman.level();
|
| 468 | int xt = Mth.floor(this.enderman.getX() - 1.0 + random.nextDouble() * 2.0);
|
| 469 | int yt = Mth.floor(this.enderman.getY() + random.nextDouble() * 2.0);
|
| 470 | int zt = Mth.floor(this.enderman.getZ() - 1.0 + random.nextDouble() * 2.0);
|
| 471 | BlockPos pos = new BlockPos(xt, yt, zt);
|
| 472 | BlockState targetState = level.getBlockState(pos);
|
| 473 | BlockPos below = pos.below();
|
| 474 | BlockState belowState = level.getBlockState(below);
|
| 475 | BlockState carried = this.enderman.getCarriedBlock();
|
| 476 | if (carried != null) {
|
| 477 | carried = Block.updateFromNeighbourShapes(carried, this.enderman.level(), pos);
|
| 478 | if (this.canPlaceBlock(level, pos, carried, targetState, belowState, below)) {
|
| 479 | level.setBlockAndUpdate(pos, carried);
|
| 480 | level.gameEvent(GameEvent.BLOCK_PLACE, pos, GameEvent.Context.of(this.enderman, carried));
|
| 481 | this.enderman.setCarriedBlock(null);
|
| 482 | }
|
| 483 | }
|
| 484 | }
|
| 485 |
|
| 486 | private boolean canPlaceBlock(
|
| 487 | final Level level, final BlockPos pos, final BlockState carried, final BlockState targetState, final BlockState belowState, final BlockPos below
|
| 488 | ) {
|
| 489 | return targetState.isAir()
|
| 490 | && !belowState.isAir()
|
| 491 | && !belowState.is(Blocks.BEDROCK)
|
| 492 | && belowState.isCollisionShapeFullBlock(level, below)
|
| 493 | && carried.canSurvive(level, pos)
|
| 494 | && level.getEntities(this.enderman, AABB.unitCubeFromLowerCorner(Vec3.atLowerCornerOf(pos))).isEmpty();
|
| 495 | }
|
| 496 | }
|
| 497 |
|
| 498 | private static class EndermanLookForPlayerGoal extends NearestAttackableTargetGoal<Player> {
|
| 499 | private final Enderman enderman;
|
| 500 | private @Nullable Player pendingTarget;
|
| 501 | private int aggroTime;
|
| 502 | private int teleportTime;
|
| 503 | private final TargetingConditions startAggroTargetConditions;
|
| 504 | private final TargetingConditions continueAggroTargetConditions = TargetingConditions.forCombat().ignoreLineOfSight();
|
| 505 | private final TargetingConditions.Selector isAngerInducing;
|
| 506 |
|
| 507 | public EndermanLookForPlayerGoal(final Enderman enderman, final TargetingConditions.@Nullable Selector isAngryAt) {
|
| 508 | super(enderman, Player.class, 10, false, false, isAngryAt);
|
| 509 | this.enderman = enderman;
|
| 510 | this.isAngerInducing = (target, level) -> (enderman.isBeingStaredBy((Player)target) || enderman.isAngryAt(target, level))
|
| 511 | && !enderman.hasIndirectPassenger(target);
|
| 512 | this.startAggroTargetConditions = TargetingConditions.forCombat().range(this.getFollowDistance()).selector(this.isAngerInducing);
|
| 513 | }
|
| 514 |
|
| 515 | @Override
|
| 516 | public boolean canUse() {
|
| 517 | this.pendingTarget = getServerLevel(this.enderman).getNearestPlayer(this.startAggroTargetConditions.range(this.getFollowDistance()), this.enderman);
|
| 518 | return this.pendingTarget != null;
|
| 519 | }
|
| 520 |
|
| 521 | @Override
|
| 522 | public void start() {
|
| 523 | this.aggroTime = this.adjustedTickDelay(5);
|
| 524 | this.teleportTime = 0;
|
| 525 | this.enderman.setBeingStaredAt();
|
| 526 | }
|
| 527 |
|
| 528 | @Override
|
| 529 | public void stop() {
|
| 530 | this.pendingTarget = null;
|
| 531 | super.stop();
|
| 532 | }
|
| 533 |
|
| 534 | @Override
|
| 535 | public boolean canContinueToUse() {
|
| 536 | if (this.pendingTarget != null) {
|
| 537 | if (!this.isAngerInducing.test(this.pendingTarget, getServerLevel(this.enderman))) {
|
| 538 | return false;
|
| 539 | }
|
| 540 |
|
| 541 | this.enderman.lookAt(this.pendingTarget, 10.0F, 10.0F);
|
| 542 | return true;
|
| 543 | } else {
|
| 544 | if (this.target != null) {
|
| 545 | if (this.enderman.hasIndirectPassenger(this.target)) {
|
| 546 | return false;
|
| 547 | }
|
| 548 |
|
| 549 | if (this.continueAggroTargetConditions.test(getServerLevel(this.enderman), this.enderman, this.target)) {
|
| 550 | return true;
|
| 551 | }
|
| 552 | }
|
| 553 |
|
| 554 | return super.canContinueToUse();
|
| 555 | }
|
| 556 | }
|
| 557 |
|
| 558 | @Override
|
| 559 | public void tick() {
|
| 560 | if (this.enderman.getTarget() == null) {
|
| 561 | super.setTarget(null);
|
| 562 | }
|
| 563 |
|
| 564 | if (this.pendingTarget != null) {
|
| 565 | if (--this.aggroTime <= 0) {
|
| 566 | this.target = this.pendingTarget;
|
| 567 | this.pendingTarget = null;
|
| 568 | super.start();
|
| 569 | }
|
| 570 | } else {
|
| 571 | if (this.target != null && !this.enderman.isPassenger()) {
|
| 572 | if (this.enderman.isBeingStaredBy((Player)this.target)) {
|
| 573 | if (this.target.distanceToSqr(this.enderman) < 16.0) {
|
| 574 | this.enderman.teleport();
|
| 575 | }
|
| 576 |
|
| 577 | this.teleportTime = 0;
|
| 578 | } else if (this.target.distanceToSqr(this.enderman) > 256.0
|
| 579 | && this.teleportTime++ >= this.adjustedTickDelay(30)
|
| 580 | && this.enderman.teleportTowards(this.target)) {
|
| 581 | this.teleportTime = 0;
|
| 582 | }
|
| 583 | }
|
| 584 |
|
| 585 | super.tick();
|
| 586 | }
|
| 587 | }
|
| 588 | }
|
| 589 |
|
| 590 | private static class EndermanTakeBlockGoal extends Goal {
|
| 591 | private final Enderman enderman;
|
| 592 |
|
| 593 | public EndermanTakeBlockGoal(final Enderman enderman) {
|
| 594 | this.enderman = enderman;
|
| 595 | }
|
| 596 |
|
| 597 | @Override
|
| 598 | public boolean canUse() {
|
| 599 | if (this.enderman.getCarriedBlock() != null) {
|
| 600 | return false;
|
| 601 | } else {
|
| 602 | return !getServerLevel(this.enderman).getGameRules().get(GameRules.MOB_GRIEFING) ? false : this.enderman.getRandom().nextInt(reducedTickDelay(20)) == 0;
|
| 603 | }
|
| 604 | }
|
| 605 |
|
| 606 | @Override
|
| 607 | public void tick() {
|
| 608 | RandomSource random = this.enderman.getRandom();
|
| 609 | Level level = this.enderman.level();
|
| 610 | int xt = Mth.floor(this.enderman.getX() - 2.0 + random.nextDouble() * 4.0);
|
| 611 | int yt = Mth.floor(this.enderman.getY() + random.nextDouble() * 3.0);
|
| 612 | int zt = Mth.floor(this.enderman.getZ() - 2.0 + random.nextDouble() * 4.0);
|
| 613 | BlockPos pos = new BlockPos(xt, yt, zt);
|
| 614 | BlockState blockState = level.getBlockState(pos);
|
| 615 | Vec3 from = new Vec3(this.enderman.getBlockX() + 0.5, yt + 0.5, this.enderman.getBlockZ() + 0.5);
|
| 616 | Vec3 to = new Vec3(xt + 0.5, yt + 0.5, zt + 0.5);
|
| 617 | BlockHitResult result = level.clip(new ClipContext(from, to, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, this.enderman));
|
| 618 | boolean reachable = result.getBlockPos().equals(pos);
|
| 619 | if (blockState.is(BlockTags.ENDERMAN_HOLDABLE) && reachable) {
|
| 620 | level.removeBlock(pos, false);
|
| 621 | level.gameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Context.of(this.enderman, blockState));
|
| 622 | this.enderman.setCarriedBlock(blockState.getBlock().defaultBlockState());
|
| 623 | }
|
| 624 | }
|
| 625 | }
|
| 626 | }
|
| 627 |
|