| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.Vec3i;
|
| 8 | import net.minecraft.core.component.DataComponentGetter;
|
| 9 | import net.minecraft.core.component.DataComponentType;
|
| 10 | import net.minecraft.core.component.DataComponents;
|
| 11 | import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
| 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.resources.Identifier;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.sounds.SoundEvent;
|
| 18 | import net.minecraft.sounds.SoundEvents;
|
| 19 | import net.minecraft.sounds.SoundSource;
|
| 20 | import net.minecraft.tags.BlockTags;
|
| 21 | import net.minecraft.tags.DamageTypeTags;
|
| 22 | import net.minecraft.util.BlockUtil;
|
| 23 | import net.minecraft.util.Mth;
|
| 24 | import net.minecraft.world.Difficulty;
|
| 25 | import net.minecraft.world.DifficultyInstance;
|
| 26 | import net.minecraft.world.damagesource.DamageSource;
|
| 27 | import net.minecraft.world.entity.Entity;
|
| 28 | import net.minecraft.world.entity.EntityDimensions;
|
| 29 | import net.minecraft.world.entity.EntitySelector;
|
| 30 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 31 | import net.minecraft.world.entity.EntityType;
|
| 32 | import net.minecraft.world.entity.EntityTypes;
|
| 33 | import net.minecraft.world.entity.InterpolationHandler;
|
| 34 | import net.minecraft.world.entity.LivingEntity;
|
| 35 | import net.minecraft.world.entity.Mob;
|
| 36 | import net.minecraft.world.entity.MoverType;
|
| 37 | import net.minecraft.world.entity.Pose;
|
| 38 | import net.minecraft.world.entity.SpawnGroupData;
|
| 39 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 40 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 41 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 42 | import net.minecraft.world.entity.ai.control.BodyRotationControl;
|
| 43 | import net.minecraft.world.entity.ai.control.LookControl;
|
| 44 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 45 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 46 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 47 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 48 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 49 | import net.minecraft.world.entity.animal.golem.AbstractGolem;
|
| 50 | import net.minecraft.world.entity.player.Player;
|
| 51 | import net.minecraft.world.entity.projectile.ShulkerBullet;
|
| 52 | import net.minecraft.world.entity.projectile.arrow.AbstractArrow;
|
| 53 | import net.minecraft.world.item.DyeColor;
|
| 54 | import net.minecraft.world.level.Level;
|
| 55 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 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.storage.ValueInput;
|
| 60 | import net.minecraft.world.level.storage.ValueOutput;
|
| 61 | import net.minecraft.world.phys.AABB;
|
| 62 | import net.minecraft.world.phys.Vec3;
|
| 63 | import org.joml.Vector3f;
|
| 64 | import org.joml.Vector3fc;
|
| 65 | import org.jspecify.annotations.Nullable;
|
| 66 |
|
| 67 | public class Shulker extends AbstractGolem implements Enemy {
|
| 68 | private static final Identifier COVERED_ARMOR_MODIFIER_ID = Identifier.withDefaultNamespace("covered");
|
| 69 | private static final AttributeModifier COVERED_ARMOR_MODIFIER = new AttributeModifier(COVERED_ARMOR_MODIFIER_ID, 20.0, AttributeModifier.Operation.ADD_VALUE);
|
| 70 | protected static final EntityDataAccessor<Direction> DATA_ATTACH_FACE_ID = SynchedEntityData.defineId(Shulker.class, EntityDataSerializers.DIRECTION);
|
| 71 | protected static final EntityDataAccessor<Byte> DATA_PEEK_ID = SynchedEntityData.defineId(Shulker.class, EntityDataSerializers.BYTE);
|
| 72 | protected static final EntityDataAccessor<Byte> DATA_COLOR_ID = SynchedEntityData.defineId(Shulker.class, EntityDataSerializers.BYTE);
|
| 73 | private static final int TELEPORT_STEPS = 6;
|
| 74 | private static final byte NO_COLOR = 16;
|
| 75 | private static final byte DEFAULT_COLOR = 16;
|
| 76 | public static final int MAX_TELEPORT_DISTANCE = 8;
|
| 77 | private static final int OTHER_SHULKER_SCAN_RADIUS = 8;
|
| 78 | private static final int OTHER_SHULKER_LIMIT = 5;
|
| 79 | private static final float PEEK_PER_TICK = 0.05F;
|
| 80 | private static final byte DEFAULT_PEEK = 0;
|
| 81 | private static final Direction DEFAULT_ATTACH_FACE = Direction.DOWN;
|
| 82 | private static final Vector3fc FORWARD = Direction.SOUTH.getUnitVec3f();
|
| 83 | private static final float MAX_SCALE = 3.0F;
|
| 84 | private float currentPeekAmountO;
|
| 85 | private float currentPeekAmount;
|
| 86 | private @Nullable BlockPos clientOldAttachPosition;
|
| 87 | private int clientSideTeleportInterpolation;
|
| 88 | private static final float MAX_LID_OPEN = 1.0F;
|
| 89 |
|
| 90 | public Shulker(final EntityType<? extends Shulker> type, final Level level) {
|
| 91 | super(type, level);
|
| 92 | this.xpReward = 5;
|
| 93 | this.lookControl = new Shulker.ShulkerLookControl(this);
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | protected void registerGoals() {
|
| 98 | this.goalSelector.addGoal(1, new LookAtPlayerGoal(this, Player.class, 8.0F, 0.02F, true));
|
| 99 | this.goalSelector.addGoal(4, new Shulker.ShulkerAttackGoal());
|
| 100 | this.goalSelector.addGoal(7, new Shulker.ShulkerPeekGoal());
|
| 101 | this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
|
| 102 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this, this.getClass()).setAlertOthers());
|
| 103 | this.targetSelector.addGoal(2, new Shulker.ShulkerNearestAttackGoal(this));
|
| 104 | this.targetSelector.addGoal(3, new Shulker.ShulkerDefenseAttackGoal(this));
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected Entity.MovementEmission getMovementEmission() {
|
| 109 | return Entity.MovementEmission.NONE;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public SoundSource getSoundSource() {
|
| 114 | return SoundSource.HOSTILE;
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected SoundEvent getAmbientSound() {
|
| 119 | return SoundEvents.SHULKER_AMBIENT;
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public void playAmbientSound() {
|
| 124 | if (!this.isClosed()) {
|
| 125 | super.playAmbientSound();
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected SoundEvent getDeathSound() {
|
| 131 | return SoundEvents.SHULKER_DEATH;
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 136 | return this.isClosed() ? SoundEvents.SHULKER_HURT_CLOSED : SoundEvents.SHULKER_HURT;
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 141 | super.defineSynchedData(entityData);
|
| 142 | entityData.define(DATA_ATTACH_FACE_ID, DEFAULT_ATTACH_FACE);
|
| 143 | entityData.define(DATA_PEEK_ID, (byte)0);
|
| 144 | entityData.define(DATA_COLOR_ID, (byte)16);
|
| 145 | }
|
| 146 |
|
| 147 | public static AttributeSupplier.Builder createAttributes() {
|
| 148 | return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 30.0);
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | protected BodyRotationControl createBodyControl() {
|
| 153 | return new Shulker.ShulkerBodyRotationControl(this);
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 158 | super.readAdditionalSaveData(input);
|
| 159 | this.setAttachFace(input.read("AttachFace", Direction.LEGACY_ID_CODEC).orElse(DEFAULT_ATTACH_FACE));
|
| 160 | this.entityData.set(DATA_PEEK_ID, input.getByteOr("Peek", (byte)0));
|
| 161 | this.entityData.set(DATA_COLOR_ID, input.getByteOr("Color", (byte)16));
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 166 | super.addAdditionalSaveData(output);
|
| 167 | output.store("AttachFace", Direction.LEGACY_ID_CODEC, this.getAttachFace());
|
| 168 | output.putByte("Peek", this.entityData.get(DATA_PEEK_ID));
|
| 169 | output.putByte("Color", this.entityData.get(DATA_COLOR_ID));
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public void tick() {
|
| 174 | super.tick();
|
| 175 | if (!this.level().isClientSide() && !this.isPassenger() && !this.canStayAt(this.blockPosition(), this.getAttachFace())) {
|
| 176 | this.findNewAttachment();
|
| 177 | }
|
| 178 |
|
| 179 | if (this.updatePeekAmount()) {
|
| 180 | this.onPeekAmountChange();
|
| 181 | }
|
| 182 |
|
| 183 | if (this.level().isClientSide()) {
|
| 184 | if (this.clientSideTeleportInterpolation > 0) {
|
| 185 | this.clientSideTeleportInterpolation--;
|
| 186 | } else {
|
| 187 | this.clientOldAttachPosition = null;
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | private void findNewAttachment() {
|
| 193 | Direction attachmentDirection = this.findAttachableSurface(this.blockPosition());
|
| 194 | if (attachmentDirection != null) {
|
| 195 | this.setAttachFace(attachmentDirection);
|
| 196 | } else {
|
| 197 | this.teleportSomewhere();
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | protected AABB makeBoundingBox(final Vec3 position) {
|
| 203 | float physPeek = getPhysicalPeek(this.currentPeekAmount);
|
| 204 | Direction direction = this.getAttachFace().getOpposite();
|
| 205 | return getProgressAabb(this.getScale(), direction, physPeek, position);
|
| 206 | }
|
| 207 |
|
| 208 | private static float getPhysicalPeek(final float amount) {
|
| 209 | return 0.5F - Mth.sin((0.5F + amount) * (float) Math.PI) * 0.5F;
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 214 | EntityDimensions dimension = super.getDefaultDimensions(pose);
|
| 215 | if (this.getAttachFace() == Direction.DOWN && this.currentPeekAmount > 0.0F) {
|
| 216 | float heightScaleFactor = 1.0F + this.currentPeekAmount;
|
| 217 | return dimension.scale(1.0F, heightScaleFactor);
|
| 218 | } else {
|
| 219 | return dimension;
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | private boolean updatePeekAmount() {
|
| 224 | this.currentPeekAmountO = this.currentPeekAmount;
|
| 225 | float targetPeekAmount = this.getRawPeekAmount() * 0.01F;
|
| 226 | if (this.currentPeekAmount == targetPeekAmount) {
|
| 227 | return false;
|
| 228 | }
|
| 229 |
|
| 230 | if (this.currentPeekAmount > targetPeekAmount) {
|
| 231 | this.currentPeekAmount = Mth.clamp(this.currentPeekAmount - 0.05F, targetPeekAmount, 1.0F);
|
| 232 | } else {
|
| 233 | this.currentPeekAmount = Mth.clamp(this.currentPeekAmount + 0.05F, 0.0F, targetPeekAmount);
|
| 234 | }
|
| 235 |
|
| 236 | return true;
|
| 237 | }
|
| 238 |
|
| 239 | private void onPeekAmountChange() {
|
| 240 | this.reapplyPosition();
|
| 241 | this.refreshDimensions();
|
| 242 | float physicalPeek = getPhysicalPeek(this.currentPeekAmount);
|
| 243 | float physicalPeekOld = getPhysicalPeek(this.currentPeekAmountO);
|
| 244 | Direction direction = this.getAttachFace().getOpposite();
|
| 245 | float push = (physicalPeek - physicalPeekOld) * this.getScale();
|
| 246 | if (!(push <= 0.0F)) {
|
| 247 | for (Entity entity : this.level()
|
| 248 | .getEntities(
|
| 249 | this,
|
| 250 | getProgressDeltaAabb(this.getScale(), direction, physicalPeekOld, physicalPeek, this.position()),
|
| 251 | EntitySelector.NO_SPECTATORS.and(e -> !e.isPassengerOfSameVehicle(this))
|
| 252 | )) {
|
| 253 | if (!(entity instanceof Shulker) && !entity.noPhysics) {
|
| 254 | entity.move(MoverType.SHULKER, new Vec3(push * direction.getStepX(), push * direction.getStepY(), push * direction.getStepZ()));
|
| 255 | }
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | public static AABB getProgressAabb(final float size, final Direction direction, final float progressTo, final Vec3 position) {
|
| 261 | return getProgressDeltaAabb(size, direction, -1.0F, progressTo, position);
|
| 262 | }
|
| 263 |
|
| 264 | public static AABB getProgressDeltaAabb(final float size, final Direction direction, final float progressFrom, final float progressTo, final Vec3 position) {
|
| 265 | AABB boundsAtBottomCenter = new AABB(-size * 0.5, 0.0, -size * 0.5, size * 0.5, size, size * 0.5);
|
| 266 | double maxMovement = Math.max(progressFrom, progressTo);
|
| 267 | double minMovement = Math.min(progressFrom, progressTo);
|
| 268 | AABB aabb = boundsAtBottomCenter.expandTowards(
|
| 269 | direction.getStepX() * maxMovement * size, direction.getStepY() * maxMovement * size, direction.getStepZ() * maxMovement * size
|
| 270 | )
|
| 271 | .contract(
|
| 272 | -direction.getStepX() * (1.0 + minMovement) * size, -direction.getStepY() * (1.0 + minMovement) * size, -direction.getStepZ() * (1.0 + minMovement) * size
|
| 273 | );
|
| 274 | return aabb.move(position.x, position.y, position.z);
|
| 275 | }
|
| 276 |
|
| 277 | @Override
|
| 278 | public boolean startRiding(final Entity entity, final boolean force, final boolean sendEventAndTriggers) {
|
| 279 | if (this.level().isClientSide()) {
|
| 280 | this.clientOldAttachPosition = null;
|
| 281 | this.clientSideTeleportInterpolation = 0;
|
| 282 | }
|
| 283 |
|
| 284 | this.setAttachFace(Direction.DOWN);
|
| 285 | return super.startRiding(entity, force, sendEventAndTriggers);
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | public void stopRiding() {
|
| 290 | super.stopRiding();
|
| 291 | if (this.level().isClientSide()) {
|
| 292 | this.clientOldAttachPosition = this.blockPosition();
|
| 293 | }
|
| 294 |
|
| 295 | this.yBodyRotO = 0.0F;
|
| 296 | this.yBodyRot = 0.0F;
|
| 297 | }
|
| 298 |
|
| 299 | @Override
|
| 300 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 301 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 302 | ) {
|
| 303 | this.setYRot(0.0F);
|
| 304 | this.yHeadRot = this.getYRot();
|
| 305 | this.setOldPosAndRot();
|
| 306 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 307 | }
|
| 308 |
|
| 309 | @Override
|
| 310 | public void move(final MoverType moverType, final Vec3 delta) {
|
| 311 | if (moverType == MoverType.SHULKER_BOX) {
|
| 312 | this.teleportSomewhere();
|
| 313 | } else {
|
| 314 | super.move(moverType, delta);
|
| 315 | }
|
| 316 | }
|
| 317 |
|
| 318 | @Override
|
| 319 | public Vec3 getDeltaMovement() {
|
| 320 | return Vec3.ZERO;
|
| 321 | }
|
| 322 |
|
| 323 | @Override
|
| 324 | public void setDeltaMovement(final Vec3 deltaMovement) {
|
| 325 | }
|
| 326 |
|
| 327 | @Override
|
| 328 | public void setPos(final double x, final double y, final double z) {
|
| 329 | BlockPos oldPos = this.blockPosition();
|
| 330 | if (this.isPassenger()) {
|
| 331 | super.setPos(x, y, z);
|
| 332 | } else {
|
| 333 | super.setPos(Mth.floor(x) + 0.5, Mth.floor(y + 0.5), Mth.floor(z) + 0.5);
|
| 334 | }
|
| 335 |
|
| 336 | if (this.tickCount != 0) {
|
| 337 | BlockPos pos = this.blockPosition();
|
| 338 | if (!pos.equals(oldPos)) {
|
| 339 | this.entityData.set(DATA_PEEK_ID, (byte)0);
|
| 340 | this.needsSync = true;
|
| 341 | if (this.level().isClientSide() && !this.isPassenger() && !pos.equals(this.clientOldAttachPosition)) {
|
| 342 | this.clientOldAttachPosition = oldPos;
|
| 343 | this.clientSideTeleportInterpolation = 6;
|
| 344 | this.xOld = this.getX();
|
| 345 | this.yOld = this.getY();
|
| 346 | this.zOld = this.getZ();
|
| 347 | }
|
| 348 | }
|
| 349 | }
|
| 350 | }
|
| 351 |
|
| 352 | protected @Nullable Direction findAttachableSurface(final BlockPos target) {
|
| 353 | for (Direction direction : Direction.values()) {
|
| 354 | if (this.canStayAt(target, direction)) {
|
| 355 | return direction;
|
| 356 | }
|
| 357 | }
|
| 358 |
|
| 359 | return null;
|
| 360 | }
|
| 361 |
|
| 362 | private boolean canStayAt(final BlockPos target, final Direction face) {
|
| 363 | if (this.isPositionBlocked(target)) {
|
| 364 | return false;
|
| 365 | }
|
| 366 |
|
| 367 | Direction oppositeFace = face.getOpposite();
|
| 368 | if (!this.level().loadedAndEntityCanStandOnFace(target.relative(face), this, oppositeFace)) {
|
| 369 | return false;
|
| 370 | }
|
| 371 |
|
| 372 | AABB fullyOpened = getProgressAabb(this.getScale(), oppositeFace, 1.0F, Vec3.atBottomCenterOf(target)).deflate(1.0E-6);
|
| 373 | return this.level().noCollision(this, fullyOpened);
|
| 374 | }
|
| 375 |
|
| 376 | private boolean isPositionBlocked(final BlockPos target) {
|
| 377 | BlockState state = this.level().getBlockState(target);
|
| 378 | if (state.isAir()) {
|
| 379 | return false;
|
| 380 | }
|
| 381 |
|
| 382 | boolean movingPistonInOurCurrentPosition = state.is(Blocks.MOVING_PISTON) && target.equals(this.blockPosition());
|
| 383 | return !movingPistonInOurCurrentPosition;
|
| 384 | }
|
| 385 |
|
| 386 | protected boolean teleportSomewhere() {
|
| 387 | if (!this.isNoAi() && this.isAlive()) {
|
| 388 | BlockPos oldPos = this.blockPosition();
|
| 389 | Level level = this.level();
|
| 390 |
|
| 391 | for (int attempt = 0; attempt < 5; attempt++) {
|
| 392 | BlockPos target = oldPos.offset(
|
| 393 | Mth.randomBetweenInclusive(this.random, -8, 8), Mth.randomBetweenInclusive(this.random, -8, 8), Mth.randomBetweenInclusive(this.random, -8, 8)
|
| 394 | );
|
| 395 | if (target.getY() > level.getMinY()
|
| 396 | && level.isEmptyBlock(target)
|
| 397 | && level.getWorldBorder().isWithinBounds(target)
|
| 398 | && level.noCollision(this, new AABB(target).deflate(1.0E-6))) {
|
| 399 | Direction attachmentDirection = this.findAttachableSurface(target);
|
| 400 | if (attachmentDirection != null && !this.level().getBlockState(target.relative(attachmentDirection)).is(BlockTags.SHULKER_DOES_NOT_TELEPORT_TO)) {
|
| 401 | this.unRide();
|
| 402 | this.setAttachFace(attachmentDirection);
|
| 403 | this.playSound(SoundEvents.SHULKER_TELEPORT, 1.0F, 1.0F);
|
| 404 | this.setPos(target.getX() + 0.5, target.getY(), target.getZ() + 0.5);
|
| 405 | level.gameEvent(GameEvent.TELEPORT, oldPos, GameEvent.Context.of(this));
|
| 406 | int packedDiff = BlockUtil.packDifferenceInPosition(oldPos, this.blockPosition(), 8, 8, 8);
|
| 407 | level.levelEvent(2016, oldPos, packedDiff);
|
| 408 | this.entityData.set(DATA_PEEK_ID, (byte)0);
|
| 409 | this.setTarget(null);
|
| 410 | return true;
|
| 411 | }
|
| 412 | }
|
| 413 | }
|
| 414 |
|
| 415 | return false;
|
| 416 | } else {
|
| 417 | return false;
|
| 418 | }
|
| 419 | }
|
| 420 |
|
| 421 | @Override
|
| 422 | protected InterpolationHandler createInterpolationHandler() {
|
| 423 | return InterpolationHandler.NO_OP;
|
| 424 | }
|
| 425 |
|
| 426 | @Override
|
| 427 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 428 | if (this.isClosed()) {
|
| 429 | Entity directEntity = source.getDirectEntity();
|
| 430 | if (directEntity instanceof AbstractArrow) {
|
| 431 | return false;
|
| 432 | }
|
| 433 | }
|
| 434 |
|
| 435 | if (!super.hurtServer(level, source, damage)) {
|
| 436 | return false;
|
| 437 | }
|
| 438 |
|
| 439 | if (this.getHealth() < this.getMaxHealth() * 0.5 && this.random.nextInt(4) == 0) {
|
| 440 | this.teleportSomewhere();
|
| 441 | } else if (source.is(DamageTypeTags.IS_PROJECTILE)) {
|
| 442 | Entity directEntity = source.getDirectEntity();
|
| 443 | if (directEntity != null && directEntity.is(EntityTypes.SHULKER_BULLET)) {
|
| 444 | this.hitByShulkerBullet();
|
| 445 | }
|
| 446 | }
|
| 447 |
|
| 448 | return true;
|
| 449 | }
|
| 450 |
|
| 451 | private boolean isClosed() {
|
| 452 | return this.getRawPeekAmount() == 0;
|
| 453 | }
|
| 454 |
|
| 455 | private void hitByShulkerBullet() {
|
| 456 | Vec3 oldPosition = this.position();
|
| 457 | AABB oldAabb = this.getBoundingBox();
|
| 458 | if (!this.isClosed() && this.teleportSomewhere()) {
|
| 459 | int shulkerCount = this.level().getEntities(EntityTypes.SHULKER, oldAabb.inflate(8.0), Entity::isAlive).size();
|
| 460 | float failureChance = (shulkerCount - 1) / 5.0F;
|
| 461 | if (!(this.level().getRandom().nextFloat() < failureChance)) {
|
| 462 | Shulker baby = EntityTypes.SHULKER.create(this.level(), EntitySpawnReason.BREEDING);
|
| 463 | if (baby != null) {
|
| 464 | baby.setVariant(this.getVariant());
|
| 465 | baby.snapTo(oldPosition);
|
| 466 | this.level().addFreshEntity(baby);
|
| 467 | }
|
| 468 | }
|
| 469 | }
|
| 470 | }
|
| 471 |
|
| 472 | @Override
|
| 473 | public boolean canBeCollidedWith(final @Nullable Entity other) {
|
| 474 | return this.isAlive();
|
| 475 | }
|
| 476 |
|
| 477 | public Direction getAttachFace() {
|
| 478 | return this.entityData.get(DATA_ATTACH_FACE_ID);
|
| 479 | }
|
| 480 |
|
| 481 | private void setAttachFace(final Direction attachmentDirection) {
|
| 482 | this.entityData.set(DATA_ATTACH_FACE_ID, attachmentDirection);
|
| 483 | }
|
| 484 |
|
| 485 | @Override
|
| 486 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 487 | if (DATA_ATTACH_FACE_ID.equals(accessor)) {
|
| 488 | this.setBoundingBox(this.makeBoundingBox());
|
| 489 | }
|
| 490 |
|
| 491 | super.onSyncedDataUpdated(accessor);
|
| 492 | }
|
| 493 |
|
| 494 | private int getRawPeekAmount() {
|
| 495 | return this.entityData.get(DATA_PEEK_ID);
|
| 496 | }
|
| 497 |
|
| 498 | private void setRawPeekAmount(final int amount) {
|
| 499 | if (!this.level().isClientSide()) {
|
| 500 | this.getAttribute(Attributes.ARMOR).removeModifier(COVERED_ARMOR_MODIFIER_ID);
|
| 501 | if (amount == 0) {
|
| 502 | this.getAttribute(Attributes.ARMOR).addPermanentModifier(COVERED_ARMOR_MODIFIER);
|
| 503 | this.playSound(SoundEvents.SHULKER_CLOSE, 1.0F, 1.0F);
|
| 504 | this.gameEvent(GameEvent.CONTAINER_CLOSE);
|
| 505 | } else {
|
| 506 | this.playSound(SoundEvents.SHULKER_OPEN, 1.0F, 1.0F);
|
| 507 | this.gameEvent(GameEvent.CONTAINER_OPEN);
|
| 508 | }
|
| 509 | }
|
| 510 |
|
| 511 | this.entityData.set(DATA_PEEK_ID, (byte)amount);
|
| 512 | }
|
| 513 |
|
| 514 | public float getClientPeekAmount(final float a) {
|
| 515 | return Mth.lerp(a, this.currentPeekAmountO, this.currentPeekAmount);
|
| 516 | }
|
| 517 |
|
| 518 | @Override
|
| 519 | public void recreateFromPacket(final ClientboundAddEntityPacket packet) {
|
| 520 | super.recreateFromPacket(packet);
|
| 521 | this.yBodyRot = 0.0F;
|
| 522 | this.yBodyRotO = 0.0F;
|
| 523 | }
|
| 524 |
|
| 525 | @Override
|
| 526 | public int getMaxHeadXRot() {
|
| 527 | return 180;
|
| 528 | }
|
| 529 |
|
| 530 | @Override
|
| 531 | public int getMaxHeadYRot() {
|
| 532 | return 180;
|
| 533 | }
|
| 534 |
|
| 535 | @Override
|
| 536 | public void push(final Entity entity) {
|
| 537 | }
|
| 538 |
|
| 539 | public @Nullable Vec3 getRenderPosition(final float a) {
|
| 540 | if (this.clientOldAttachPosition != null && this.clientSideTeleportInterpolation > 0) {
|
| 541 | double scale = (this.clientSideTeleportInterpolation - a) / 6.0;
|
| 542 | scale *= scale;
|
| 543 | scale *= this.getScale();
|
| 544 | BlockPos currentPos = this.blockPosition();
|
| 545 | double ox = (currentPos.getX() - this.clientOldAttachPosition.getX()) * scale;
|
| 546 | double oy = (currentPos.getY() - this.clientOldAttachPosition.getY()) * scale;
|
| 547 | double oz = (currentPos.getZ() - this.clientOldAttachPosition.getZ()) * scale;
|
| 548 | return new Vec3(-ox, -oy, -oz);
|
| 549 | } else {
|
| 550 | return null;
|
| 551 | }
|
| 552 | }
|
| 553 |
|
| 554 | @Override
|
| 555 | protected float sanitizeScale(final float scale) {
|
| 556 | return Math.min(scale, 3.0F);
|
| 557 | }
|
| 558 |
|
| 559 | private void setVariant(final Optional<DyeColor> color) {
|
| 560 | this.entityData.set(DATA_COLOR_ID, color.<Byte>map(dyeColor -> (byte)dyeColor.getId()).orElse((byte)16));
|
| 561 | }
|
| 562 |
|
| 563 | public Optional<DyeColor> getVariant() {
|
| 564 | return Optional.ofNullable(this.getColor());
|
| 565 | }
|
| 566 |
|
| 567 | public @Nullable DyeColor getColor() {
|
| 568 | byte color = this.entityData.get(DATA_COLOR_ID);
|
| 569 | return color != 16 && color <= 15 ? DyeColor.byId(color) : null;
|
| 570 | }
|
| 571 |
|
| 572 | @Override
|
| 573 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 574 | return type == DataComponents.SHULKER_COLOR ? castComponentValue((DataComponentType<T>)type, this.getColor()) : super.get(type);
|
| 575 | }
|
| 576 |
|
| 577 | @Override
|
| 578 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 579 | this.applyImplicitComponentIfPresent(components, DataComponents.SHULKER_COLOR);
|
| 580 | super.applyImplicitComponents(components);
|
| 581 | }
|
| 582 |
|
| 583 | @Override
|
| 584 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 585 | if (type == DataComponents.SHULKER_COLOR) {
|
| 586 | this.setVariant(Optional.of(castComponentValue(DataComponents.SHULKER_COLOR, value)));
|
| 587 | return true;
|
| 588 | } else {
|
| 589 | return super.applyImplicitComponent(type, value);
|
| 590 | }
|
| 591 | }
|
| 592 |
|
| 593 | private class ShulkerAttackGoal extends Goal {
|
| 594 | private int attackTime;
|
| 595 |
|
| 596 | public ShulkerAttackGoal() {
|
| 597 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 598 | }
|
| 599 |
|
| 600 | @Override
|
| 601 | public boolean canUse() {
|
| 602 | LivingEntity target = Shulker.this.getTarget();
|
| 603 | return target != null && target.isAlive() ? Shulker.this.level().getDifficulty() != Difficulty.PEACEFUL : false;
|
| 604 | }
|
| 605 |
|
| 606 | @Override
|
| 607 | public void start() {
|
| 608 | this.attackTime = 20;
|
| 609 | Shulker.this.setRawPeekAmount(100);
|
| 610 | }
|
| 611 |
|
| 612 | @Override
|
| 613 | public void stop() {
|
| 614 | Shulker.this.setRawPeekAmount(0);
|
| 615 | }
|
| 616 |
|
| 617 | @Override
|
| 618 | public boolean requiresUpdateEveryTick() {
|
| 619 | return true;
|
| 620 | }
|
| 621 |
|
| 622 | @Override
|
| 623 | public void tick() {
|
| 624 | if (Shulker.this.level().getDifficulty() != Difficulty.PEACEFUL) {
|
| 625 | this.attackTime--;
|
| 626 | LivingEntity target = Shulker.this.getTarget();
|
| 627 | if (target != null) {
|
| 628 | Shulker.this.getLookControl().setLookAt(target, 180.0F, 180.0F);
|
| 629 | double distance = Shulker.this.distanceToSqr(target);
|
| 630 | if (distance < 400.0) {
|
| 631 | if (this.attackTime <= 0) {
|
| 632 | this.attackTime = 20 + Shulker.this.random.nextInt(10) * 20 / 2;
|
| 633 | Shulker.this.level().addFreshEntity(new ShulkerBullet(Shulker.this.level(), Shulker.this, target, Shulker.this.getAttachFace().getAxis()));
|
| 634 | Shulker.this.playSound(SoundEvents.SHULKER_SHOOT, 2.0F, (Shulker.this.random.nextFloat() - Shulker.this.random.nextFloat()) * 0.2F + 1.0F);
|
| 635 | }
|
| 636 | } else {
|
| 637 | Shulker.this.setTarget(null);
|
| 638 | }
|
| 639 |
|
| 640 | super.tick();
|
| 641 | }
|
| 642 | }
|
| 643 | }
|
| 644 | }
|
| 645 |
|
| 646 | private static class ShulkerBodyRotationControl extends BodyRotationControl {
|
| 647 | public ShulkerBodyRotationControl(final Mob mob) {
|
| 648 | super(mob);
|
| 649 | }
|
| 650 |
|
| 651 | @Override
|
| 652 | public void clientTick() {
|
| 653 | }
|
| 654 | }
|
| 655 |
|
| 656 | private static class ShulkerDefenseAttackGoal extends NearestAttackableTargetGoal<LivingEntity> {
|
| 657 | public ShulkerDefenseAttackGoal(final Shulker mob) {
|
| 658 | super(mob, LivingEntity.class, 10, true, false, (input, level) -> input instanceof Enemy);
|
| 659 | }
|
| 660 |
|
| 661 | @Override
|
| 662 | public boolean canUse() {
|
| 663 | return this.mob.getTeam() == null ? false : super.canUse();
|
| 664 | }
|
| 665 |
|
| 666 | @Override
|
| 667 | protected AABB getTargetSearchArea(final double followDistance) {
|
| 668 | Direction attachFace = ((Shulker)this.mob).getAttachFace();
|
| 669 | if (attachFace.getAxis() == Direction.Axis.X) {
|
| 670 | return this.mob.getBoundingBox().inflate(4.0, followDistance, followDistance);
|
| 671 | } else {
|
| 672 | return attachFace.getAxis() == Direction.Axis.Z
|
| 673 | ? this.mob.getBoundingBox().inflate(followDistance, followDistance, 4.0)
|
| 674 | : this.mob.getBoundingBox().inflate(followDistance, 4.0, followDistance);
|
| 675 | }
|
| 676 | }
|
| 677 | }
|
| 678 |
|
| 679 | private class ShulkerLookControl extends LookControl {
|
| 680 | public ShulkerLookControl(final Mob mob) {
|
| 681 | super(mob);
|
| 682 | }
|
| 683 |
|
| 684 | @Override
|
| 685 | protected void clampHeadRotationToBody() {
|
| 686 | }
|
| 687 |
|
| 688 | @Override
|
| 689 | protected Optional<Float> getYRotD() {
|
| 690 | Direction attachFace = Shulker.this.getAttachFace().getOpposite();
|
| 691 | Vector3f forward = attachFace.getRotation().transform(new Vector3f(Shulker.FORWARD));
|
| 692 | Vec3i upNormal = attachFace.getUnitVec3i();
|
| 693 | Vector3f right = new Vector3f(upNormal.getX(), upNormal.getY(), upNormal.getZ());
|
| 694 | right.cross(forward);
|
| 695 | double xd = this.wantedX - this.mob.getX();
|
| 696 | double yd = this.wantedY - this.mob.getEyeY();
|
| 697 | double zd = this.wantedZ - this.mob.getZ();
|
| 698 | Vector3f out = new Vector3f((float)xd, (float)yd, (float)zd);
|
| 699 | float deltaRight = right.dot(out);
|
| 700 | float deltaForward = forward.dot(out);
|
| 701 | return !(Math.abs(deltaRight) > 1.0E-5F) && !(Math.abs(deltaForward) > 1.0E-5F)
|
| 702 | ? Optional.empty()
|
| 703 | : Optional.of((float)(Mth.atan2(-deltaRight, deltaForward) * 180.0F / (float)Math.PI));
|
| 704 | }
|
| 705 |
|
| 706 | @Override
|
| 707 | protected Optional<Float> getXRotD() {
|
| 708 | return Optional.of(0.0F);
|
| 709 | }
|
| 710 | }
|
| 711 |
|
| 712 | private class ShulkerNearestAttackGoal extends NearestAttackableTargetGoal<Player> {
|
| 713 | public ShulkerNearestAttackGoal(final Shulker mob) {
|
| 714 | super(mob, Player.class, true);
|
| 715 | }
|
| 716 |
|
| 717 | @Override
|
| 718 | public boolean canUse() {
|
| 719 | return Shulker.this.level().getDifficulty() == Difficulty.PEACEFUL ? false : super.canUse();
|
| 720 | }
|
| 721 |
|
| 722 | @Override
|
| 723 | protected AABB getTargetSearchArea(final double followDistance) {
|
| 724 | Direction attachFace = ((Shulker)this.mob).getAttachFace();
|
| 725 | if (attachFace.getAxis() == Direction.Axis.X) {
|
| 726 | return this.mob.getBoundingBox().inflate(4.0, followDistance, followDistance);
|
| 727 | } else {
|
| 728 | return attachFace.getAxis() == Direction.Axis.Z
|
| 729 | ? this.mob.getBoundingBox().inflate(followDistance, followDistance, 4.0)
|
| 730 | : this.mob.getBoundingBox().inflate(followDistance, 4.0, followDistance);
|
| 731 | }
|
| 732 | }
|
| 733 | }
|
| 734 |
|
| 735 | private class ShulkerPeekGoal extends Goal {
|
| 736 | private int peekTime;
|
| 737 |
|
| 738 | @Override
|
| 739 | public boolean canUse() {
|
| 740 | return Shulker.this.getTarget() == null
|
| 741 | && Shulker.this.random.nextInt(reducedTickDelay(40)) == 0
|
| 742 | && Shulker.this.canStayAt(Shulker.this.blockPosition(), Shulker.this.getAttachFace());
|
| 743 | }
|
| 744 |
|
| 745 | @Override
|
| 746 | public boolean canContinueToUse() {
|
| 747 | return Shulker.this.getTarget() == null && this.peekTime > 0;
|
| 748 | }
|
| 749 |
|
| 750 | @Override
|
| 751 | public void start() {
|
| 752 | this.peekTime = this.adjustedTickDelay(20 * (1 + Shulker.this.random.nextInt(3)));
|
| 753 | Shulker.this.setRawPeekAmount(30);
|
| 754 | }
|
| 755 |
|
| 756 | @Override
|
| 757 | public void stop() {
|
| 758 | if (Shulker.this.getTarget() == null) {
|
| 759 | Shulker.this.setRawPeekAmount(0);
|
| 760 | }
|
| 761 | }
|
| 762 |
|
| 763 | @Override
|
| 764 | public void tick() {
|
| 765 | this.peekTime--;
|
| 766 | }
|
| 767 | }
|
| 768 | }
|
| 769 |
|