| 1 | package net.minecraft.world.entity.projectile;
|
| 2 |
|
| 3 | import net.minecraft.core.particles.ParticleTypes;
|
| 4 | import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
| 5 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.world.damagesource.DamageSource;
|
| 9 | import net.minecraft.world.entity.Entity;
|
| 10 | import net.minecraft.world.entity.EntityType;
|
| 11 | import net.minecraft.world.entity.EntityTypes;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.animal.equine.Llama;
|
| 14 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 17 | import net.minecraft.world.phys.BlockHitResult;
|
| 18 | import net.minecraft.world.phys.EntityHitResult;
|
| 19 | import net.minecraft.world.phys.HitResult;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 |
|
| 22 | public class LlamaSpit extends Projectile {
|
| 23 | public LlamaSpit(final EntityType<? extends LlamaSpit> type, final Level level) {
|
| 24 | super(type, level);
|
| 25 | }
|
| 26 |
|
| 27 | public LlamaSpit(final Level level, final Llama owner) {
|
| 28 | this(EntityTypes.LLAMA_SPIT, level);
|
| 29 | this.setOwner(owner);
|
| 30 | this.setPos(
|
| 31 | owner.getX() - (owner.getBbWidth() + 1.0F) * 0.5 * Mth.sin(owner.yBodyRot * (float) (Math.PI / 180.0)),
|
| 32 | owner.getEyeY() - 0.1F,
|
| 33 | owner.getZ() + (owner.getBbWidth() + 1.0F) * 0.5 * Mth.cos(owner.yBodyRot * (float) (Math.PI / 180.0))
|
| 34 | );
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | protected double getDefaultGravity() {
|
| 39 | return 0.06;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public void tick() {
|
| 44 | super.tick();
|
| 45 | Vec3 movement = this.getDeltaMovement();
|
| 46 | HitResult hitResult = ProjectileUtil.getHitResultOnMoveVector(this, this::canHitEntity);
|
| 47 | this.hitTargetOrDeflectSelf(hitResult);
|
| 48 | double x = this.getX() + movement.x;
|
| 49 | double y = this.getY() + movement.y;
|
| 50 | double z = this.getZ() + movement.z;
|
| 51 | this.updateRotation();
|
| 52 | boolean touchesNoAir = this.level().findBlocksIn(this.getBoundingBox()).filterState(BlockBehaviour.BlockStateBase::isAir).noneMatched();
|
| 53 | if (touchesNoAir) {
|
| 54 | this.discard();
|
| 55 | } else if (this.isInWater()) {
|
| 56 | this.discard();
|
| 57 | } else {
|
| 58 | this.setDeltaMovement(movement.scale(this.getAirDrag()));
|
| 59 | this.applyGravity();
|
| 60 | this.setPos(x, y, z);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected float getAirDrag() {
|
| 66 | return 0.99F;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected void onHitEntity(final EntityHitResult hitResult) {
|
| 71 | super.onHitEntity(hitResult);
|
| 72 | if (this.getOwner() instanceof LivingEntity livingOwner) {
|
| 73 | Entity target = hitResult.getEntity();
|
| 74 | DamageSource damageSource = this.damageSources().spit(this, livingOwner);
|
| 75 | if (this.level() instanceof ServerLevel serverLevel && target.hurtServer(serverLevel, damageSource, 1.0F)) {
|
| 76 | EnchantmentHelper.doPostAttackEffects(serverLevel, target, damageSource);
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected void onHitBlock(final BlockHitResult hitResult) {
|
| 83 | super.onHitBlock(hitResult);
|
| 84 | if (!this.level().isClientSide()) {
|
| 85 | this.discard();
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void recreateFromPacket(final ClientboundAddEntityPacket packet) {
|
| 95 | super.recreateFromPacket(packet);
|
| 96 | Vec3 movement = packet.getMovement();
|
| 97 |
|
| 98 | for (int i = 0; i < 7; i++) {
|
| 99 | double k = 0.4 + 0.1 * i;
|
| 100 | this.level().addParticle(ParticleTypes.SPIT, this.getX(), this.getY(), this.getZ(), movement.x * k, movement.y, movement.z * k);
|
| 101 | }
|
| 102 |
|
| 103 | this.setDeltaMovement(movement);
|
| 104 | }
|
| 105 | }
|
| 106 |
|