| 1 | package net.minecraft.world.entity.projectile;
|
| 2 |
|
| 3 | import net.minecraft.core.particles.ParticleTypes;
|
| 4 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.world.damagesource.DamageSource;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.entity.EntityEvent;
|
| 10 | import net.minecraft.world.entity.EntityReference;
|
| 11 | import net.minecraft.world.entity.EntityType;
|
| 12 | import net.minecraft.world.entity.EntityTypes;
|
| 13 | import net.minecraft.world.entity.LivingEntity;
|
| 14 | import net.minecraft.world.entity.TraceableEntity;
|
| 15 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.storage.ValueInput;
|
| 18 | import net.minecraft.world.level.storage.ValueOutput;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class EvokerFangs extends Entity implements TraceableEntity {
|
| 22 | public static final int ATTACK_DURATION = 20;
|
| 23 | public static final int LIFE_OFFSET = 2;
|
| 24 | public static final int ATTACK_TRIGGER_TICKS = 14;
|
| 25 | private static final int DEFAULT_WARMUP_DELAY = 0;
|
| 26 | private int warmupDelayTicks = 0;
|
| 27 | private boolean sentSpikeEvent;
|
| 28 | private int lifeTicks = 22;
|
| 29 | private boolean clientSideAttackStarted;
|
| 30 | private @Nullable EntityReference<LivingEntity> owner;
|
| 31 |
|
| 32 | public EvokerFangs(final EntityType<? extends EvokerFangs> type, final Level level) {
|
| 33 | super(type, level);
|
| 34 | }
|
| 35 |
|
| 36 | public EvokerFangs(
|
| 37 | final Level level, final double x, final double y, final double z, final float rotaionRadians, final int warmupDelayTicks, final LivingEntity owner
|
| 38 | ) {
|
| 39 | this(EntityTypes.EVOKER_FANGS, level);
|
| 40 | this.warmupDelayTicks = warmupDelayTicks;
|
| 41 | this.setOwner(owner);
|
| 42 | this.setYRot(rotaionRadians * (180.0F / (float)Math.PI));
|
| 43 | this.setPos(x, y, z);
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 48 | }
|
| 49 |
|
| 50 | public void setOwner(final @Nullable LivingEntity owner) {
|
| 51 | this.owner = EntityReference.of(owner);
|
| 52 | }
|
| 53 |
|
| 54 | public @Nullable LivingEntity getOwner() {
|
| 55 | return EntityReference.getLivingEntity(this.owner, this.level());
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 60 | this.warmupDelayTicks = input.getIntOr("Warmup", 0);
|
| 61 | this.owner = EntityReference.read(input, "Owner");
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 66 | output.putInt("Warmup", this.warmupDelayTicks);
|
| 67 | EntityReference.store(this.owner, output, "Owner");
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void tick() {
|
| 72 | super.tick();
|
| 73 | if (this.level().isClientSide()) {
|
| 74 | if (this.clientSideAttackStarted) {
|
| 75 | this.lifeTicks--;
|
| 76 | if (this.lifeTicks == 14) {
|
| 77 | for (int i = 0; i < 12; i++) {
|
| 78 | double x = this.getX() + (this.random.nextDouble() * 2.0 - 1.0) * this.getBbWidth() * 0.5;
|
| 79 | double y = this.getY() + 0.05 + this.random.nextDouble();
|
| 80 | double z = this.getZ() + (this.random.nextDouble() * 2.0 - 1.0) * this.getBbWidth() * 0.5;
|
| 81 | double xd = (this.random.nextDouble() * 2.0 - 1.0) * 0.3;
|
| 82 | double yd = 0.3 + this.random.nextDouble() * 0.3;
|
| 83 | double zd = (this.random.nextDouble() * 2.0 - 1.0) * 0.3;
|
| 84 | this.level().addParticle(ParticleTypes.CRIT, x, y + 1.0, z, xd, yd, zd);
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 | } else if (--this.warmupDelayTicks < 0) {
|
| 89 | if (this.warmupDelayTicks == -8) {
|
| 90 | for (LivingEntity entity : this.level().getEntitiesOfClass(LivingEntity.class, this.getBoundingBox().inflate(0.2, 0.0, 0.2))) {
|
| 91 | this.dealDamageTo(entity);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | if (!this.sentSpikeEvent) {
|
| 96 | this.level().broadcastEntityEvent(this, (byte)4);
|
| 97 | this.sentSpikeEvent = true;
|
| 98 | }
|
| 99 |
|
| 100 | if (--this.lifeTicks < 0) {
|
| 101 | this.discard();
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | private void dealDamageTo(final LivingEntity entity) {
|
| 107 | LivingEntity currentOwner = this.getOwner();
|
| 108 | if (entity.isAlive() && !entity.isInvulnerable() && entity != currentOwner) {
|
| 109 | if (currentOwner == null) {
|
| 110 | entity.hurt(this.damageSources().magic(), 6.0F);
|
| 111 | } else {
|
| 112 | if (currentOwner.isAlliedTo(entity)) {
|
| 113 | return;
|
| 114 | }
|
| 115 |
|
| 116 | DamageSource damageSource = this.damageSources().indirectMagic(this, currentOwner);
|
| 117 | if (this.level() instanceof ServerLevel serverLevel && entity.hurtServer(serverLevel, damageSource, 6.0F)) {
|
| 118 | EnchantmentHelper.doPostAttackEffects(serverLevel, entity, damageSource);
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 126 | super.handleEntityEvent(id);
|
| 127 | if (id == 4) {
|
| 128 | this.clientSideAttackStarted = true;
|
| 129 | if (!this.isSilent()) {
|
| 130 | this.level()
|
| 131 | .playLocalSound(
|
| 132 | this.getX(), this.getY(), this.getZ(), SoundEvents.EVOKER_FANGS_ATTACK, this.getSoundSource(), 1.0F, this.random.nextFloat() * 0.2F + 0.85F, false
|
| 133 | );
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | public float getAnimationProgress(final float a) {
|
| 139 | if (!this.clientSideAttackStarted) {
|
| 140 | return 0.0F;
|
| 141 | }
|
| 142 |
|
| 143 | int remainingLife = this.lifeTicks - 2;
|
| 144 | return remainingLife <= 0 ? 1.0F : 1.0F - (remainingLife - a) / 20.0F;
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 149 | return false;
|
| 150 | }
|
| 151 | }
|
| 152 |
|