Advertisement
Zapfox

Untitled

Dec 4th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.90 KB | None | 0 0
  1. package orescape.mod.entity;
  2.  
  3.  
  4. import java.util.List;
  5.  
  6. import javax.annotation.Nullable;
  7.  
  8. import com.google.common.base.Predicate;
  9. import com.google.common.base.Predicates;
  10.  
  11. import net.minecraft.block.Block;
  12. import net.minecraft.block.material.Material;
  13. import net.minecraft.block.state.IBlockState;
  14. import net.minecraft.enchantment.EnchantmentHelper;
  15. import net.minecraft.entity.Entity;
  16. import net.minecraft.entity.EntityLivingBase;
  17. import net.minecraft.entity.IProjectile;
  18. import net.minecraft.entity.MoverType;
  19. import net.minecraft.entity.monster.EntityEnderman;
  20. import net.minecraft.entity.player.EntityPlayer;
  21. import net.minecraft.entity.player.EntityPlayerMP;
  22. import net.minecraft.entity.projectile.EntityArrow;
  23. import net.minecraft.init.Enchantments;
  24. import net.minecraft.init.SoundEvents;
  25. import net.minecraft.item.ItemStack;
  26. import net.minecraft.nbt.NBTTagCompound;
  27. import net.minecraft.network.datasync.DataParameter;
  28. import net.minecraft.network.datasync.DataSerializers;
  29. import net.minecraft.network.datasync.EntityDataManager;
  30. import net.minecraft.network.play.server.SPacketChangeGameState;
  31. import net.minecraft.util.DamageSource;
  32. import net.minecraft.util.EntitySelectors;
  33. import net.minecraft.util.EnumParticleTypes;
  34. import net.minecraft.util.ResourceLocation;
  35. import net.minecraft.util.datafix.DataFixer;
  36. import net.minecraft.util.math.AxisAlignedBB;
  37. import net.minecraft.util.math.BlockPos;
  38. import net.minecraft.util.math.MathHelper;
  39. import net.minecraft.util.math.RayTraceResult;
  40. import net.minecraft.util.math.Vec3d;
  41. import net.minecraft.world.World;
  42. import net.minecraftforge.fml.relauncher.Side;
  43. import net.minecraftforge.fml.relauncher.SideOnly;
  44.  
  45. public abstract class BronzeArrow extends EntityArrow implements IProjectile
  46. {
  47. private static final Predicate<Entity> ARROW_TARGETS = Predicates.and(EntitySelectors.NOT_SPECTATING, EntitySelectors.IS_ALIVE, new Predicate<Entity>()
  48. {
  49. public boolean apply(@Nullable Entity p_apply_1_)
  50. {
  51. return p_apply_1_.canBeCollidedWith();
  52. }
  53. });
  54. private static final DataParameter<Byte> CRITICAL = EntityDataManager.<Byte>createKey(BronzeArrow.class, DataSerializers.BYTE);
  55. private int xTile;
  56. private int yTile;
  57. private int zTile;
  58. private Block inTile;
  59. private int inData;
  60. protected boolean inGround;
  61. protected int timeInGround;
  62. /** 1 if the player can pick up the arrow */
  63. public BronzeArrow.PickupStatus pickupStatus;
  64. /** Seems to be some sort of timer for animating an arrow. */
  65. public int arrowShake;
  66. /** The owner of this arrow. */
  67. public Entity shootingEntity;
  68. private int ticksInGround;
  69. private int ticksInAir;
  70. private double damage;
  71. /** The amount of knockback an arrow applies when it hits a mob. */
  72. private int knockbackStrength;
  73.  
  74. public BronzeArrow(World worldIn)
  75. {
  76. super(worldIn);
  77. this.xTile = -1;
  78. this.yTile = -1;
  79. this.zTile = -1;
  80. this.pickupStatus = BronzeArrow.PickupStatus.DISALLOWED;
  81. this.damage = 0.5D;
  82. this.setSize(0.5F, 0.5F);
  83. }
  84.  
  85. public BronzeArrow(World worldIn, double x, double y, double z)
  86. {
  87. this(worldIn);
  88. this.setPosition(x, y, z);
  89. }
  90.  
  91. public BronzeArrow(World worldIn, EntityLivingBase shooter)
  92. {
  93. this(worldIn, shooter.posX, shooter.posY + (double)shooter.getEyeHeight() - 0.10000000149011612D, shooter.posZ);
  94. this.shootingEntity = shooter;
  95.  
  96. if (shooter instanceof EntityPlayer)
  97. {
  98. this.pickupStatus = BronzeArrow.PickupStatus.DISALLOWED;
  99. }
  100. }
  101.  
  102. public void onUpdate()
  103. {
  104. super.onUpdate();
  105.  
  106. if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
  107. {
  108. float f = MathHelper.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
  109. this.rotationYaw = (float)(MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));
  110. this.rotationPitch = (float)(MathHelper.atan2(this.motionY, (double)f) * (180D / Math.PI));
  111. this.prevRotationYaw = this.rotationYaw;
  112. this.prevRotationPitch = this.rotationPitch;
  113. }
  114.  
  115. BlockPos blockpos = new BlockPos(this.xTile, this.yTile, this.zTile);
  116. IBlockState iblockstate = this.world.getBlockState(blockpos);
  117. Block block = iblockstate.getBlock();
  118.  
  119. if (iblockstate.getMaterial() != Material.AIR)
  120. {
  121. AxisAlignedBB axisalignedbb = iblockstate.getCollisionBoundingBox(this.world, blockpos);
  122.  
  123. if (axisalignedbb != Block.NULL_AABB && axisalignedbb.offset(blockpos).contains(new Vec3d(this.posX, this.posY, this.posZ)))
  124. {
  125. this.inGround = true;
  126. }
  127. }
  128.  
  129. if (this.arrowShake > 0)
  130. {
  131. --this.arrowShake;
  132. }
  133.  
  134. if (this.inGround)
  135. {
  136. int j = block.getMetaFromState(iblockstate);
  137.  
  138. if ((block != this.inTile || j != this.inData) && !this.world.collidesWithAnyBlock(this.getEntityBoundingBox().grow(0.05D)))
  139. {
  140. this.inGround = false;
  141. this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
  142. this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
  143. this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
  144. this.ticksInGround = 0;
  145. this.ticksInAir = 0;
  146. }
  147. else
  148. {
  149. ++this.ticksInGround;
  150.  
  151. if (this.ticksInGround >= 20)
  152. {
  153. this.setDead();
  154. }
  155. }
  156.  
  157. ++this.timeInGround;
  158. }
  159. else
  160. {
  161. this.timeInGround = 0;
  162. ++this.ticksInAir;
  163. Vec3d vec3d1 = new Vec3d(this.posX, this.posY, this.posZ);
  164. Vec3d vec3d = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  165. RayTraceResult raytraceresult = this.world.rayTraceBlocks(vec3d1, vec3d, false, true, false);
  166. vec3d1 = new Vec3d(this.posX, this.posY, this.posZ);
  167. vec3d = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  168.  
  169. if (this.getIsCritical())
  170. {
  171. for (int k = 0; k < 4; ++k)
  172. {
  173. this.world.spawnParticle(EnumParticleTypes.CRIT, this.posX + this.motionX * (double)k / 4.0D, this.posY + this.motionY * (double)k / 4.0D, this.posZ + this.motionZ * (double)k / 4.0D, -this.motionX, -this.motionY + 0.2D, -this.motionZ);
  174. }
  175. }
  176.  
  177. this.posX += this.motionX;
  178. this.posY += this.motionY;
  179. this.posZ += this.motionZ;
  180. float f4 = MathHelper.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
  181. this.rotationYaw = (float)(MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));
  182.  
  183. for (this.rotationPitch = (float)(MathHelper.atan2(this.motionY, (double)f4) * (180D / Math.PI)); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
  184. {
  185. ;
  186. }
  187.  
  188. while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
  189. {
  190. this.prevRotationPitch += 360.0F;
  191. }
  192.  
  193. while (this.rotationYaw - this.prevRotationYaw < -180.0F)
  194. {
  195. this.prevRotationYaw -= 360.0F;
  196. }
  197.  
  198. while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
  199. {
  200. this.prevRotationYaw += 360.0F;
  201. }
  202.  
  203. this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
  204. this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
  205. float f1 = 0.99F;
  206. float f2 = 0.05F;
  207.  
  208. if (this.isInWater())
  209. {
  210. for (int i = 0; i < 4; ++i)
  211. {
  212. float f3 = 0.25F;
  213. this.world.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * 0.25D, this.posY - this.motionY * 0.25D, this.posZ - this.motionZ * 0.25D, this.motionX, this.motionY, this.motionZ);
  214. }
  215.  
  216. f1 = 0.6F;
  217. }
  218.  
  219. if (this.isWet())
  220. {
  221. this.extinguish();
  222. }
  223.  
  224. this.motionX *= (double)f1;
  225. this.motionY *= (double)f1;
  226. this.motionZ *= (double)f1;
  227.  
  228. if (!this.hasNoGravity())
  229. {
  230. this.motionY -= 0.05000000074505806D;
  231. }
  232.  
  233. this.setPosition(this.posX, this.posY, this.posZ);
  234. this.doBlockCollisions();
  235. }
  236. }
  237.  
  238. /**
  239. * Called when the arrow hits a block or an entity
  240. */
  241. protected void onHit(RayTraceResult raytraceResultIn)
  242. {
  243. Entity entity = raytraceResultIn.entityHit;
  244.  
  245. if (entity != null)
  246. {
  247. float f = MathHelper.sqrt(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
  248. int i = (int)this.damage;
  249.  
  250. if (this.getIsCritical())
  251. {
  252. i += this.rand.nextInt(i / 2 + 2);
  253. }
  254.  
  255. DamageSource damagesource;
  256.  
  257. if (this.shootingEntity == null)
  258. {
  259. damagesource = DamageSource.causeArrowDamage(this, this);
  260. }
  261. else
  262. {
  263. damagesource = DamageSource.causeArrowDamage(this, this.shootingEntity);
  264. }
  265.  
  266. if (this.isBurning() && !(entity instanceof EntityEnderman))
  267. {
  268. entity.setFire(5);
  269. }
  270.  
  271. if (entity.attackEntityFrom(damagesource, (float)i))
  272. {
  273. if (entity instanceof EntityLivingBase)
  274. {
  275. EntityLivingBase entitylivingbase = (EntityLivingBase)entity;
  276.  
  277. if (!this.world.isRemote)
  278. {
  279. entitylivingbase.setArrowCountInEntity(entitylivingbase.getArrowCountInEntity() + 1);
  280. }
  281.  
  282. if (this.knockbackStrength > 0)
  283. {
  284. float f1 = MathHelper.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
  285.  
  286. if (f1 > 0.0F)
  287. {
  288. entitylivingbase.addVelocity(this.motionX * (double)this.knockbackStrength * 0.6000000238418579D / (double)f1, 0.1D, this.motionZ * (double)this.knockbackStrength * 0.6000000238418579D / (double)f1);
  289. }
  290. }
  291.  
  292. if (this.shootingEntity instanceof EntityLivingBase)
  293. {
  294. EnchantmentHelper.applyThornEnchantments(entitylivingbase, this.shootingEntity);
  295. EnchantmentHelper.applyArthropodEnchantments((EntityLivingBase)this.shootingEntity, entitylivingbase);
  296. }
  297.  
  298. this.arrowHit(entitylivingbase);
  299.  
  300. if (this.shootingEntity != null && entitylivingbase != this.shootingEntity && entitylivingbase instanceof EntityPlayer && this.shootingEntity instanceof EntityPlayerMP)
  301. {
  302. ((EntityPlayerMP)this.shootingEntity).connection.sendPacket(new SPacketChangeGameState(6, 0.0F));
  303. }
  304. }
  305.  
  306. this.playSound(SoundEvents.ENTITY_ARROW_HIT, 1.0F, 1.2F / (this.rand.nextFloat() * 0.2F + 0.9F));
  307.  
  308. if (!(entity instanceof EntityEnderman))
  309. {
  310. this.setDead();
  311. }
  312. }
  313. else
  314. {
  315. this.motionX *= -0.10000000149011612D;
  316. this.motionY *= -0.10000000149011612D;
  317. this.motionZ *= -0.10000000149011612D;
  318. this.rotationYaw += 180.0F;
  319. this.prevRotationYaw += 180.0F;
  320. this.ticksInAir = 0;
  321.  
  322. if (!this.world.isRemote && this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ < 0.0010000000474974513D)
  323. {
  324.  
  325. this.setDead();
  326. }
  327. }
  328. }
  329. else
  330. {
  331. BlockPos blockpos = raytraceResultIn.getBlockPos();
  332. this.xTile = blockpos.getX();
  333. this.yTile = blockpos.getY();
  334. this.zTile = blockpos.getZ();
  335. IBlockState iblockstate = this.world.getBlockState(blockpos);
  336. this.inTile = iblockstate.getBlock();
  337. this.inData = this.inTile.getMetaFromState(iblockstate);
  338. this.motionX = (double)((float)(raytraceResultIn.hitVec.x - this.posX));
  339. this.motionY = (double)((float)(raytraceResultIn.hitVec.y - this.posY));
  340. this.motionZ = (double)((float)(raytraceResultIn.hitVec.z - this.posZ));
  341. float f2 = MathHelper.sqrt(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
  342. this.posX -= this.motionX / (double)f2 * 0.05000000074505806D;
  343. this.posY -= this.motionY / (double)f2 * 0.05000000074505806D;
  344. this.posZ -= this.motionZ / (double)f2 * 0.05000000074505806D;
  345. this.playSound(SoundEvents.ENTITY_ARROW_HIT, 1.0F, 1.2F / (this.rand.nextFloat() * 0.2F + 0.9F));
  346. this.inGround = true;
  347. this.arrowShake = 7;
  348. this.setIsCritical(false);
  349.  
  350. if (iblockstate.getMaterial() != Material.AIR)
  351. {
  352. this.inTile.onEntityCollidedWithBlock(this.world, blockpos, iblockstate, this);
  353. }
  354. }
  355. }
  356.  
  357. /**
  358. * Tries to move the entity towards the specified location.
  359. */
  360. public void move(MoverType type, double x, double y, double z)
  361. {
  362. super.move(type, x, y, z);
  363.  
  364. if (this.inGround)
  365. {
  366. this.xTile = MathHelper.floor(this.posX);
  367. this.yTile = MathHelper.floor(this.posY);
  368. this.zTile = MathHelper.floor(this.posZ);
  369. }
  370. }
  371.  
  372. protected void arrowHit(EntityLivingBase living)
  373. {
  374. }
  375.  
  376. @Nullable
  377. protected Entity findEntityOnPath(Vec3d start, Vec3d end)
  378. {
  379. Entity entity = null;
  380. List<Entity> list = this.world.getEntitiesInAABBexcluding(this, this.getEntityBoundingBox().expand(this.motionX, this.motionY, this.motionZ).grow(1.0D), ARROW_TARGETS);
  381. double d0 = 0.0D;
  382.  
  383. for (int i = 0; i < list.size(); ++i)
  384. {
  385. Entity entity1 = list.get(i);
  386.  
  387. if (entity1 != this.shootingEntity || this.ticksInAir >= 5)
  388. {
  389. AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow(0.30000001192092896D);
  390. RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(start, end);
  391.  
  392. if (raytraceresult != null)
  393. {
  394. double d1 = start.squareDistanceTo(raytraceresult.hitVec);
  395.  
  396. if (d1 < d0 || d0 == 0.0D)
  397. {
  398. entity = entity1;
  399. d0 = d1;
  400. }
  401. }
  402. }
  403. }
  404.  
  405. return entity;
  406. }
  407.  
  408. public static void registerFixesArrow(DataFixer fixer, String name)
  409. {
  410. }
  411.  
  412. public static void registerFixesArrow(DataFixer fixer)
  413. {
  414. registerFixesArrow(fixer, "Arrow");
  415. }
  416.  
  417. /**
  418. * (abstract) Protected helper method to write subclass entity data to NBT.
  419. */
  420. public void writeEntityToNBT(NBTTagCompound compound)
  421. {
  422. compound.setInteger("xTile", this.xTile);
  423. compound.setInteger("yTile", this.yTile);
  424. compound.setInteger("zTile", this.zTile);
  425. compound.setShort("life", (short)this.ticksInGround);
  426. ResourceLocation resourcelocation = Block.REGISTRY.getNameForObject(this.inTile);
  427. compound.setString("inTile", resourcelocation == null ? "" : resourcelocation.toString());
  428. compound.setByte("inData", (byte)this.inData);
  429. compound.setByte("shake", (byte)this.arrowShake);
  430. compound.setByte("inGround", (byte)(this.inGround ? 1 : 0));
  431. compound.setByte("pickup", (byte)this.pickupStatus.ordinal());
  432. compound.setDouble("damage", this.damage);
  433. compound.setBoolean("crit", this.getIsCritical());
  434. }
  435.  
  436. /**
  437. * (abstract) Protected helper method to read subclass entity data from NBT.
  438. */
  439. public void readEntityFromNBT(NBTTagCompound compound)
  440. {
  441. this.xTile = compound.getInteger("xTile");
  442. this.yTile = compound.getInteger("yTile");
  443. this.zTile = compound.getInteger("zTile");
  444. this.ticksInGround = compound.getShort("life");
  445.  
  446. if (compound.hasKey("inTile", 8))
  447. {
  448. this.inTile = Block.getBlockFromName(compound.getString("inTile"));
  449. }
  450. else
  451. {
  452. this.inTile = Block.getBlockById(compound.getByte("inTile") & 255);
  453. }
  454.  
  455. this.inData = compound.getByte("inData") & 255;
  456. this.arrowShake = compound.getByte("shake") & 255;
  457. this.inGround = compound.getByte("inGround") == 1;
  458.  
  459. if (compound.hasKey("damage", 99))
  460. {
  461. this.damage = compound.getDouble("damage");
  462. }
  463.  
  464. if (compound.hasKey("pickup", 99))
  465. {
  466. this.pickupStatus = BronzeArrow.PickupStatus.getByOrdinal(compound.getByte("pickup"));
  467. }
  468. else if (compound.hasKey("player", 99))
  469. {
  470. this.pickupStatus = compound.getBoolean("player") ? BronzeArrow.PickupStatus.ALLOWED : BronzeArrow.PickupStatus.DISALLOWED;
  471. }
  472.  
  473. this.setIsCritical(compound.getBoolean("crit"));
  474. }
  475.  
  476. /**
  477. * Called by a player entity when they collide with an entity
  478. */
  479. public void onCollideWithPlayer(EntityPlayer entityIn)
  480. {
  481.  
  482. {
  483. this.setDead();
  484. }
  485. }
  486.  
  487.  
  488. protected abstract ItemStack getArrowStack();
  489.  
  490. /**
  491. * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
  492. * prevent them from trampling crops
  493. */
  494. protected boolean canTriggerWalking()
  495. {
  496. return false;
  497. }
  498.  
  499. public void setDamage(double damageIn)
  500. {
  501. this.damage = damageIn;
  502. }
  503.  
  504. public double getDamage()
  505. {
  506. return this.damage;
  507. }
  508.  
  509. /**
  510. * Sets the amount of knockback the arrow applies when it hits a mob.
  511. */
  512. public void setKnockbackStrength(int knockbackStrengthIn)
  513. {
  514. this.knockbackStrength = knockbackStrengthIn;
  515. }
  516.  
  517. /**
  518. * Returns true if it's possible to attack this entity with an item.
  519. */
  520. public boolean canBeAttackedWithItem()
  521. {
  522. return false;
  523. }
  524.  
  525. public float getEyeHeight()
  526. {
  527. return 0.0F;
  528. }
  529.  
  530. /**
  531. * Whether the arrow has a stream of critical hit particles flying behind it.
  532. */
  533. public void setIsCritical(boolean critical)
  534. {
  535. byte b0 = ((Byte)this.dataManager.get(CRITICAL)).byteValue();
  536.  
  537. if (critical)
  538. {
  539. this.dataManager.set(CRITICAL, Byte.valueOf((byte)(b0 | 1)));
  540. }
  541. else
  542. {
  543. this.dataManager.set(CRITICAL, Byte.valueOf((byte)(b0 & -2)));
  544. }
  545. }
  546.  
  547. /**
  548. * Whether the arrow has a stream of critical hit particles flying behind it.
  549. */
  550. public boolean getIsCritical()
  551. {
  552. byte b0 = ((Byte)this.dataManager.get(CRITICAL)).byteValue();
  553. return (b0 & 1) != 0;
  554. }
  555.  
  556. public void setEnchantmentEffectsFromEntity(EntityLivingBase p_190547_1_, float p_190547_2_)
  557. {
  558. int i = EnchantmentHelper.getMaxEnchantmentLevel(Enchantments.POWER, p_190547_1_);
  559. int j = EnchantmentHelper.getMaxEnchantmentLevel(Enchantments.PUNCH, p_190547_1_);
  560. this.setDamage((double)(p_190547_2_ * 2.0F) + this.rand.nextGaussian() * 0.25D + (double)((float)this.world.getDifficulty().getDifficultyId() * 0.11F));
  561.  
  562. if (i > 0)
  563. {
  564. this.setDamage(this.getDamage() + (double)i * 0.5D + 0.5D);
  565. }
  566.  
  567. if (j > 0)
  568. {
  569. this.setKnockbackStrength(j);
  570. }
  571.  
  572. if (EnchantmentHelper.getMaxEnchantmentLevel(Enchantments.FLAME, p_190547_1_) > 0)
  573. {
  574. this.setFire(100);
  575. }
  576. }
  577.  
  578. public static enum PickupStatus
  579. {
  580. ALLOWED,
  581. DISALLOWED;
  582.  
  583.  
  584.  
  585. public static BronzeArrow.PickupStatus getByOrdinal(int ordinal)
  586. {
  587. if (ordinal < 0 || ordinal > values().length)
  588. {
  589. ordinal = 0;
  590. }
  591.  
  592. return values()[ordinal];
  593. }
  594. }
  595. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement