EntityItemProjectile.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package me.km.entities;
  2. import me.km.Server;
  3. import net.minecraft.block.Blocks;
  4. import net.minecraft.entity.Entity;
  5. import net.minecraft.entity.LivingEntity;
  6. import net.minecraft.entity.item.ItemEntity;
  7. import net.minecraft.entity.projectile.ProjectileHelper;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.particles.ParticleTypes;
  10. import net.minecraft.util.DamageSource;
  11. import net.minecraft.util.math.AxisAlignedBB;
  12. import net.minecraft.util.math.BlockRayTraceResult;
  13. import net.minecraft.util.math.EntityRayTraceResult;
  14. import net.minecraft.util.math.MathHelper;
  15. import net.minecraft.util.math.RayTraceContext;
  16. import net.minecraft.util.math.RayTraceResult;
  17. import net.minecraft.util.math.vector.Vector3d;
  18. public class EntityItemProjectile extends ItemEntity {
  19. private LivingEntity owner;
  20. private boolean hit = false;
  21. private final float damage;
  22. private Entity ignoreEntity = null;
  23. private int ignoreTime = 0;
  24. public EntityItemProjectile(LivingEntity thrower, ItemStack stack, float damage) {
  25. super(thrower.world, thrower.getPosX(), thrower.getPosY() + thrower.getEyeHeight() - 0.1d, thrower.getPosZ(), stack);
  26. this.owner = thrower;
  27. super.setInfinitePickupDelay();
  28. this.damage = damage;
  29. }
  30. public EntityItemProjectile(LivingEntity thrower, ItemStack stack) {
  31. this(thrower, stack, -1);
  32. }
  33. public LivingEntity getItemThrower() {
  34. return owner;
  35. }
  36. public void setHeadingFromThrower(Entity thrower, float rotationPitchIn, float rotationYawIn, float pitchOffset, float velocity, float inaccuracy) {
  37. float f = -MathHelper.sin(rotationYawIn * ((float) Math.PI / 180.0f)) * MathHelper.cos(rotationPitchIn * ((float) Math.PI / 180.0f));
  38. float f1 = -MathHelper.sin((rotationPitchIn + pitchOffset) * ((float) Math.PI / 180.0f));
  39. float f2 = MathHelper.cos(rotationYawIn * ((float) Math.PI / 180.0f)) * MathHelper.cos(rotationPitchIn * ((float) Math.PI / 180.0f));
  40. shoot(f, f1, f2, velocity, inaccuracy);
  41. Vector3d vec3d = thrower.getMotion();
  42. this.setMotion(this.getMotion().add(vec3d.x, thrower.isOnGround() ? 0.0D : vec3d.y, vec3d.z));
  43. }
  44. public void shoot(double x, double y, double z, float velocity, float inaccuracy) {
  45. Vector3d vec3d = new Vector3d(x, y, z)
  46. .normalize()
  47. .add(rand.nextGaussian() * 0.0075 * inaccuracy, rand.nextGaussian() * 0.0075 * inaccuracy, rand.nextGaussian() * 0.0075 * inaccuracy)
  48. .scale(velocity);
  49. setMotion(vec3d);
  50. float f1 = MathHelper.sqrt(horizontalMag(vec3d));
  51. this.rotationYaw = (float) (MathHelper.atan2(x, z) * (180.0 / Math.PI));
  52. this.rotationPitch = (float) (MathHelper.atan2(y, f1) * (180.0 / Math.PI));
  53. this.prevRotationYaw = this.rotationYaw;
  54. this.prevRotationPitch = this.rotationPitch;
  55. }
  56. private void onImpact(RayTraceResult ray) {
  57. if(damage == -1) {
  58. switch(ray.getType()) {
  59. case ENTITY:
  60. Server.scriptEvents.onEntityItemProjectileHit(this, owner, this.getItem(), ((EntityRayTraceResult) ray).getEntity(), null);
  61. break;
  62. case BLOCK:
  63. Server.scriptEvents.onEntityItemProjectileHit(this, owner, this.getItem(), null, ((BlockRayTraceResult) ray).getPos());
  64. break;
  65. }
  66. } else if(ray.getType() == RayTraceResult.Type.ENTITY) {
  67. ((EntityRayTraceResult) ray).getEntity().attackEntityFrom(DamageSource.causeThrownDamage(this, owner), damage);
  68. }
  69. }
  70. @Override
  71. public void tick() {
  72. super.tick();
  73. if(hit) {
  74. return;
  75. }
  76. AxisAlignedBB axisalignedbb = this.getBoundingBox().expand(this.getMotion()).grow(2.0D);
  77. for(Entity entity : this.world.getEntitiesInAABBexcluding(this, axisalignedbb, (p) -> !p.isSpectator() && p.canBeCollidedWith())) {
  78. if(entity == this.ignoreEntity) {
  79. this.ignoreTime++;
  80. break;
  81. }
  82. if(this.owner != null && this.ticksExisted < 2 && this.ignoreEntity == null) {
  83. this.ignoreEntity = entity;
  84. this.ignoreTime = 3;
  85. break;
  86. }
  87. }
  88. RayTraceResult ray = ProjectileHelper.func_234618_a_(this, this::func_230298_a_);
  89. if(this.ignoreEntity != null && this.ignoreTime-- <= 0) {
  90. this.ignoreEntity = null;
  91. }
  92. if(ray.getType() != RayTraceResult.Type.MISS) {
  93. if(ray.getType() == RayTraceResult.Type.BLOCK && this.world.getBlockState(((BlockRayTraceResult) ray).getPos()).getBlock() == Blocks.NETHER_PORTAL) {
  94. this.setPortal(((BlockRayTraceResult) ray).getPos());
  95. } else {
  96. this.onImpact(ray);
  97. hit = true;
  98. this.setDefaultPickupDelay();
  99. }
  100. }
  101. Vector3d vec3d = this.getMotion();
  102. setRawPosition(getPosX() + vec3d.x, getPosY() + vec3d.y, getPosZ() + vec3d.z);
  103. float f = MathHelper.sqrt(horizontalMag(vec3d));
  104. this.rotationYaw = (float) (MathHelper.atan2(vec3d.x, vec3d.z) * (double) (180F / (float) Math.PI));
  105. for(this.rotationPitch = (float) (MathHelper.atan2(vec3d.y, (double) f) * (double) (180F / (float) Math.PI)); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F) {
  106. }
  107. while(this.rotationPitch - this.prevRotationPitch >= 180.0F) {
  108. this.prevRotationPitch += 360.0F;
  109. }
  110. while(this.rotationYaw - this.prevRotationYaw < -180.0F) {
  111. this.prevRotationYaw -= 360.0F;
  112. }
  113. while(this.rotationYaw - this.prevRotationYaw >= 180.0F) {
  114. this.prevRotationYaw += 360.0F;
  115. }
  116. this.rotationPitch = MathHelper.lerp(0.2F, this.prevRotationPitch, this.rotationPitch);
  117. this.rotationYaw = MathHelper.lerp(0.2F, this.prevRotationYaw, this.rotationYaw);
  118. float f1;
  119. if(this.isInWater()) {
  120. for(int i = 0; i < 4; ++i) {
  121. this.world.addParticle(ParticleTypes.BUBBLE, getPosX() - vec3d.x * 0.25D, getPosY() - vec3d.y * 0.25D, getPosZ() - vec3d.z * 0.25D, vec3d.x, vec3d.y, vec3d.z);
  122. }
  123. f1 = 0.8F;
  124. } else {
  125. f1 = 0.99F;
  126. }
  127. this.setMotion(vec3d.scale((double) f1));
  128. if(!this.hasNoGravity()) {
  129. Vector3d vec3d1 = this.getMotion();
  130. this.setMotion(vec3d1.x, vec3d1.y - 0.03, vec3d1.z);
  131. }
  132. this.setPosition(getPosX(), getPosY(), getPosZ());
  133. }
  134. protected boolean func_230298_a_(Entity ent) {
  135. if(!ent.isSpectator() && ent.isAlive() && ent.canBeCollidedWith()) {
  136. Entity other = this.getItemThrower();
  137. return other == null || ent != other || !other.isRidingSameEntity(ent);
  138. }
  139. return false;
  140. }
  141. }