| 1 | package net.minecraft.world.entity.projectile;
|
| 2 |
|
| 3 | import net.minecraft.core.particles.ParticleTypes;
|
| 4 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 5 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 6 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.world.damagesource.DamageSource;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.entity.EntityType;
|
| 13 | import net.minecraft.world.entity.EntityTypes;
|
| 14 | import net.minecraft.world.entity.item.ItemEntity;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.Items;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.storage.ValueInput;
|
| 19 | import net.minecraft.world.level.storage.ValueOutput;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class EyeOfEnder extends Entity implements ItemSupplier {
|
| 24 | private static final float MIN_CAMERA_DISTANCE_SQUARED = 12.25F;
|
| 25 | private static final float TOO_FAR_SIGNAL_HEIGHT = 8.0F;
|
| 26 | private static final float TOO_FAR_DISTANCE = 12.0F;
|
| 27 | private static final EntityDataAccessor<ItemStack> DATA_ITEM_STACK = SynchedEntityData.defineId(EyeOfEnder.class, EntityDataSerializers.ITEM_STACK);
|
| 28 | private @Nullable Vec3 target;
|
| 29 | private int life;
|
| 30 | private boolean surviveAfterDeath;
|
| 31 |
|
| 32 | public EyeOfEnder(final EntityType<? extends EyeOfEnder> type, final Level level) {
|
| 33 | super(type, level);
|
| 34 | }
|
| 35 |
|
| 36 | public EyeOfEnder(final Level level, final double x, final double y, final double z) {
|
| 37 | this(EntityTypes.EYE_OF_ENDER, level);
|
| 38 | this.setPos(x, y, z);
|
| 39 | }
|
| 40 |
|
| 41 | public void setItem(final ItemStack source) {
|
| 42 | if (source.isEmpty()) {
|
| 43 | this.getEntityData().set(DATA_ITEM_STACK, this.getDefaultItem());
|
| 44 | } else {
|
| 45 | this.getEntityData().set(DATA_ITEM_STACK, source.copyWithCount(1));
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public ItemStack getItem() {
|
| 51 | return this.getEntityData().get(DATA_ITEM_STACK);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 56 | entityData.define(DATA_ITEM_STACK, this.getDefaultItem());
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 61 | if (this.tickCount < 2 && distance < 12.25) {
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | double size = this.getBoundingBox().getSize() * 4.0;
|
| 66 | if (Double.isNaN(size)) {
|
| 67 | size = 4.0;
|
| 68 | }
|
| 69 |
|
| 70 | size *= 64.0;
|
| 71 | return distance < size * size;
|
| 72 | }
|
| 73 |
|
| 74 | public void signalTo(final Vec3 target) {
|
| 75 | Vec3 delta = target.subtract(this.position());
|
| 76 | double horizontalDistance = delta.horizontalDistance();
|
| 77 | if (horizontalDistance > 12.0) {
|
| 78 | this.target = this.position().add(delta.x / horizontalDistance * 12.0, 8.0, delta.z / horizontalDistance * 12.0);
|
| 79 | } else {
|
| 80 | this.target = target;
|
| 81 | }
|
| 82 |
|
| 83 | this.life = 0;
|
| 84 | this.surviveAfterDeath = this.random.nextInt(5) > 0;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public void tick() {
|
| 89 | super.tick();
|
| 90 | Vec3 newPosition = this.position().add(this.getDeltaMovement());
|
| 91 | if (!this.level().isClientSide() && this.target != null) {
|
| 92 | this.setDeltaMovement(updateDeltaMovement(this.getDeltaMovement(), newPosition, this.target));
|
| 93 | }
|
| 94 |
|
| 95 | if (this.level().isClientSide()) {
|
| 96 | Vec3 particleOrigin = newPosition.subtract(this.getDeltaMovement().scale(0.25));
|
| 97 | this.spawnParticles(particleOrigin, this.getDeltaMovement());
|
| 98 | }
|
| 99 |
|
| 100 | this.setPos(newPosition);
|
| 101 | if (!this.level().isClientSide()) {
|
| 102 | this.life++;
|
| 103 | if (this.life > 80 && !this.level().isClientSide()) {
|
| 104 | this.playSound(SoundEvents.ENDER_EYE_DEATH, 1.0F, 1.0F);
|
| 105 | this.discard();
|
| 106 | if (this.surviveAfterDeath) {
|
| 107 | this.level().addFreshEntity(new ItemEntity(this.level(), this.getX(), this.getY(), this.getZ(), this.getItem()));
|
| 108 | } else {
|
| 109 | this.level().levelEvent(2003, this.blockPosition(), 0);
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | private void spawnParticles(final Vec3 origin, final Vec3 movement) {
|
| 116 | if (this.isInWater()) {
|
| 117 | for (int i = 0; i < 4; i++) {
|
| 118 | this.level().addParticle(ParticleTypes.BUBBLE, origin.x, origin.y, origin.z, movement.x, movement.y, movement.z);
|
| 119 | }
|
| 120 | } else {
|
| 121 | this.level()
|
| 122 | .addParticle(
|
| 123 | ParticleTypes.PORTAL,
|
| 124 | origin.x + this.random.nextDouble() * 0.6 - 0.3,
|
| 125 | origin.y - 0.5,
|
| 126 | origin.z + this.random.nextDouble() * 0.6 - 0.3,
|
| 127 | movement.x,
|
| 128 | movement.y,
|
| 129 | movement.z
|
| 130 | );
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | private static Vec3 updateDeltaMovement(final Vec3 oldMovement, final Vec3 position, final Vec3 target) {
|
| 135 | Vec3 horizontalDelta = new Vec3(target.x - position.x, 0.0, target.z - position.z);
|
| 136 | double horizontalLength = horizontalDelta.length();
|
| 137 | double wantedSpeed = Mth.lerp(0.0025, oldMovement.horizontalDistance(), horizontalLength);
|
| 138 | double movementY = oldMovement.y;
|
| 139 | if (horizontalLength < 1.0) {
|
| 140 | wantedSpeed *= 0.8;
|
| 141 | movementY *= 0.8;
|
| 142 | }
|
| 143 |
|
| 144 | double wantedMovementY = position.y - oldMovement.y < target.y ? 1.0 : -1.0;
|
| 145 | return horizontalDelta.scale(wantedSpeed / horizontalLength).add(0.0, movementY + (wantedMovementY - movementY) * 0.015, 0.0);
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 150 | output.store("Item", ItemStack.CODEC, this.getItem());
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 155 | this.setItem(input.read("Item", ItemStack.CODEC).orElse(this.getDefaultItem()));
|
| 156 | }
|
| 157 |
|
| 158 | private ItemStack getDefaultItem() {
|
| 159 | return new ItemStack(Items.ENDER_EYE);
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | public float getLightLevelDependentMagicValue() {
|
| 164 | return 1.0F;
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public boolean isAttackable() {
|
| 169 | return false;
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 174 | return false;
|
| 175 | }
|
| 176 | }
|
| 177 |
|