| 1 | package net.minecraft.world.entity.projectile.hurtingprojectile;
|
| 2 |
|
| 3 | import net.minecraft.core.particles.ParticleOptions;
|
| 4 | import net.minecraft.core.particles.ParticleTypes;
|
| 5 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.world.damagesource.DamageSource;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.entity.EntityType;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.projectile.Projectile;
|
| 12 | import net.minecraft.world.entity.projectile.ProjectileUtil;
|
| 13 | import net.minecraft.world.level.ClipContext;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 | import net.minecraft.world.level.storage.ValueInput;
|
| 16 | import net.minecraft.world.level.storage.ValueOutput;
|
| 17 | import net.minecraft.world.phys.HitResult;
|
| 18 | import net.minecraft.world.phys.Vec3;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public abstract class AbstractHurtingProjectile extends Projectile {
|
| 22 | public static final double INITAL_ACCELERATION_POWER = 0.1;
|
| 23 | public static final double DEFLECTION_SCALE = 0.5;
|
| 24 | public double accelerationPower = 0.1;
|
| 25 |
|
| 26 | protected AbstractHurtingProjectile(final EntityType<? extends AbstractHurtingProjectile> type, final Level level) {
|
| 27 | super(type, level);
|
| 28 | }
|
| 29 |
|
| 30 | protected AbstractHurtingProjectile(
|
| 31 | final EntityType<? extends AbstractHurtingProjectile> type, final double x, final double y, final double z, final Level level
|
| 32 | ) {
|
| 33 | this(type, level);
|
| 34 | this.setPos(x, y, z);
|
| 35 | }
|
| 36 |
|
| 37 | public AbstractHurtingProjectile(
|
| 38 | final EntityType<? extends AbstractHurtingProjectile> type, final double x, final double y, final double z, final Vec3 direction, final Level level
|
| 39 | ) {
|
| 40 | this(type, level);
|
| 41 | this.snapTo(x, y, z, this.getYRot(), this.getXRot());
|
| 42 | this.reapplyPosition();
|
| 43 | this.assignDirectionalMovement(direction, this.accelerationPower);
|
| 44 | }
|
| 45 |
|
| 46 | public AbstractHurtingProjectile(final EntityType<? extends AbstractHurtingProjectile> type, final LivingEntity mob, final Vec3 direction, final Level level) {
|
| 47 | this(type, mob.getX(), mob.getY(), mob.getZ(), direction, level);
|
| 48 | this.setOwner(mob);
|
| 49 | this.setRot(mob.getYRot(), mob.getXRot());
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 58 | double size = this.getBoundingBox().getSize() * 4.0;
|
| 59 | if (Double.isNaN(size)) {
|
| 60 | size = 4.0;
|
| 61 | }
|
| 62 |
|
| 63 | size *= 64.0;
|
| 64 | return distance < size * size;
|
| 65 | }
|
| 66 |
|
| 67 | protected ClipContext.Block getClipType() {
|
| 68 | return ClipContext.Block.COLLIDER;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public void tick() {
|
| 73 | Entity owner = this.getOwner();
|
| 74 | this.applyInertia();
|
| 75 | if (this.level().isClientSide() || (owner == null || !owner.isRemoved()) && this.level().hasChunkAt(this.blockPosition())) {
|
| 76 | HitResult hitResult = ProjectileUtil.getHitResultOnMoveVector(this, this::canHitEntity, this.getClipType());
|
| 77 | Vec3 newPosition;
|
| 78 | if (hitResult.getType() != HitResult.Type.MISS) {
|
| 79 | newPosition = hitResult.getLocation();
|
| 80 | } else {
|
| 81 | newPosition = this.position().add(this.getDeltaMovement());
|
| 82 | }
|
| 83 |
|
| 84 | ProjectileUtil.rotateTowardsMovement(this, 0.2F);
|
| 85 | this.setPos(newPosition);
|
| 86 | this.applyEffectsFromBlocks();
|
| 87 | super.tick();
|
| 88 | if (this.shouldBurn()) {
|
| 89 | this.igniteForSeconds(1.0F);
|
| 90 | }
|
| 91 |
|
| 92 | if (hitResult.getType() != HitResult.Type.MISS && this.isAlive()) {
|
| 93 | this.hitTargetOrDeflectSelf(hitResult);
|
| 94 | }
|
| 95 |
|
| 96 | this.createParticleTrail();
|
| 97 | } else {
|
| 98 | this.discard();
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private void applyInertia() {
|
| 103 | Vec3 movement = this.getDeltaMovement();
|
| 104 | Vec3 position = this.position();
|
| 105 | float inertia;
|
| 106 | if (this.isInWater()) {
|
| 107 | for (int i = 0; i < 4; i++) {
|
| 108 | float s = 0.25F;
|
| 109 | this.level()
|
| 110 | .addParticle(
|
| 111 | ParticleTypes.BUBBLE, position.x - movement.x * 0.25, position.y - movement.y * 0.25, position.z - movement.z * 0.25, movement.x, movement.y, movement.z
|
| 112 | );
|
| 113 | }
|
| 114 |
|
| 115 | inertia = this.getLiquidInertia();
|
| 116 | } else {
|
| 117 | inertia = this.getInertia();
|
| 118 | }
|
| 119 |
|
| 120 | this.setDeltaMovement(movement.add(movement.normalize().scale(this.accelerationPower)).scale(inertia));
|
| 121 | }
|
| 122 |
|
| 123 | private void createParticleTrail() {
|
| 124 | ParticleOptions trailParticle = this.getTrailParticle();
|
| 125 | Vec3 position = this.position();
|
| 126 | if (trailParticle != null) {
|
| 127 | this.level().addParticle(trailParticle, position.x, position.y + 0.5, position.z, 0.0, 0.0, 0.0);
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 133 | return false;
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | protected boolean canHitEntity(final Entity entity) {
|
| 138 | return super.canHitEntity(entity) && !entity.noPhysics;
|
| 139 | }
|
| 140 |
|
| 141 | protected boolean shouldBurn() {
|
| 142 | return true;
|
| 143 | }
|
| 144 |
|
| 145 | protected @Nullable ParticleOptions getTrailParticle() {
|
| 146 | return ParticleTypes.SMOKE;
|
| 147 | }
|
| 148 |
|
| 149 | protected float getInertia() {
|
| 150 | return 0.95F;
|
| 151 | }
|
| 152 |
|
| 153 | protected float getLiquidInertia() {
|
| 154 | return 0.8F;
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 159 | super.addAdditionalSaveData(output);
|
| 160 | output.putDouble("acceleration_power", this.accelerationPower);
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 165 | super.readAdditionalSaveData(input);
|
| 166 | this.accelerationPower = input.getDoubleOr("acceleration_power", 0.1);
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public float getLightLevelDependentMagicValue() {
|
| 171 | return 1.0F;
|
| 172 | }
|
| 173 |
|
| 174 | private void assignDirectionalMovement(final Vec3 direction, final double speed) {
|
| 175 | this.setDeltaMovement(direction.normalize().scale(speed));
|
| 176 | this.needsSync = true;
|
| 177 | }
|
| 178 |
|
| 179 | @Override
|
| 180 | protected void onDeflection(final boolean byAttack) {
|
| 181 | super.onDeflection(byAttack);
|
| 182 | if (byAttack) {
|
| 183 | this.accelerationPower = 0.1;
|
| 184 | } else {
|
| 185 | this.accelerationPower *= 0.5;
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 |
|