Guest User

EntityObsidianBoat

a guest
Feb 1st, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.96 KB | None | 0 0
  1. package com.obsidian.mod.entity;
  2.  
  3. import java.util.List;
  4.  
  5. import com.obsidian.mod.init.MyItems;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.entity.Entity;
  10. import net.minecraft.entity.EntityLivingBase;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.init.Blocks;
  13. import net.minecraft.init.Items;
  14. import net.minecraft.item.Item;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.util.AxisAlignedBB;
  17. import net.minecraft.util.BlockPos;
  18. import net.minecraft.util.DamageSource;
  19. import net.minecraft.util.EntityDamageSourceIndirect;
  20. import net.minecraft.util.EnumParticleTypes;
  21. import net.minecraft.util.MathHelper;
  22. import net.minecraft.world.World;
  23. import net.minecraftforge.fml.relauncher.Side;
  24. import net.minecraftforge.fml.relauncher.SideOnly;
  25.  
  26. public class EntityObsidianBoat extends Entity
  27. {
  28. private boolean isBoatEmpty;
  29. private double speedMultiplier;
  30. private int boatPosRotationIncrements;
  31. private double boatX;
  32. private double boatY;
  33. private double boatZ;
  34. private double boatYaw;
  35. private double boatPitch;
  36. @SideOnly(Side.CLIENT)
  37. private double velocityX;
  38. @SideOnly(Side.CLIENT)
  39. private double velocityY;
  40. @SideOnly(Side.CLIENT)
  41. private double velocityZ;
  42.  
  43. public EntityObsidianBoat(World worldIn)
  44. {
  45. super(worldIn);
  46. this.isBoatEmpty = true;
  47. this.speedMultiplier = 0.07D;
  48. this.preventEntitySpawning = true;
  49. this.setSize(1.5F, 0.6F);
  50. }
  51.  
  52. protected boolean canTriggerWalking()
  53. {
  54. return false;
  55. }
  56.  
  57. protected void entityInit()
  58. {
  59. this.dataWatcher.addObject(17, new Integer(0));
  60. this.dataWatcher.addObject(18, new Integer(1));
  61. this.dataWatcher.addObject(19, new Float(0.0F));
  62. }
  63.  
  64. public AxisAlignedBB getCollisionBox(Entity entityIn)
  65. {
  66. return entityIn.getEntityBoundingBox();
  67. }
  68.  
  69. public AxisAlignedBB getBoundingBox()
  70. {
  71. return this.getEntityBoundingBox();
  72. }
  73.  
  74. public boolean canBePushed()
  75. {
  76. return true;
  77. }
  78.  
  79. public EntityObsidianBoat(World worldIn, double i, double j, double k)
  80. {
  81. this(worldIn);
  82. this.setPosition(i, j, k);
  83. this.motionX = 0.0D;
  84. this.motionY = 0.0D;
  85. this.motionZ = 0.0D;
  86. this.prevPosX = i;
  87. this.prevPosY = j;
  88. this.prevPosZ = k;
  89. }
  90.  
  91. public double getMountedYOffset()
  92. {
  93. return (double)this.height * 0.0D - 0.30000001192092896D;
  94. }
  95.  
  96. public boolean attackEntityFrom(DamageSource source, float amount)
  97. {
  98. if (this.isEntityInvulnerable(source))
  99. {
  100. return false;
  101. }
  102. else if (!this.worldObj.isRemote && !this.isDead)
  103. {
  104. if (this.riddenByEntity != null && this.riddenByEntity == source.getEntity() && source instanceof EntityDamageSourceIndirect)
  105. {
  106. return false;
  107. }
  108. else
  109. {
  110. this.setForwardDirection(-this.getForwardDirection());
  111. this.setTimeSinceHit(10);
  112. this.setDamageTaken(this.getDamageTaken() + amount * 10.0F);
  113. this.setBeenAttacked();
  114. boolean flag = source.getEntity() instanceof EntityPlayer && ((EntityPlayer)source.getEntity()).capabilities.isCreativeMode;
  115.  
  116. if (flag || this.getDamageTaken() > 40.0F)
  117. {
  118. if (this.riddenByEntity != null)
  119. {
  120. this.riddenByEntity.mountEntity(this);
  121. }
  122.  
  123. if (!flag)
  124. {
  125. this.dropItemWithOffset(MyItems.itemObsidianBoat, 1, 0.0F);
  126. }
  127.  
  128. this.setDead();
  129. }
  130.  
  131. return true;
  132. }
  133. }
  134. else
  135. {
  136. return true;
  137. }
  138. }
  139.  
  140. @SideOnly(Side.CLIENT)
  141. public void performHurtAnimation()
  142. {
  143. this.setForwardDirection(-this.getForwardDirection());
  144. this.setTimeSinceHit(10);
  145. this.setDamageTaken(this.getDamageTaken() * 11.0F);
  146. }
  147.  
  148. public boolean canBeCollidedWith()
  149. {
  150. return !this.isDead;
  151. }
  152.  
  153. @SideOnly(Side.CLIENT)
  154. public void func_180426_a(double p_180426_1_, double p_180426_3_, double p_180426_5_, float p_180426_7_, float p_180426_8_, int p_180426_9_, boolean p_180426_10_)
  155. {
  156. if (p_180426_10_ && this.riddenByEntity != null)
  157. {
  158. this.prevPosX = this.posX = p_180426_1_;
  159. this.prevPosY = this.posY = p_180426_3_;
  160. this.prevPosZ = this.posZ = p_180426_5_;
  161. this.rotationYaw = p_180426_7_;
  162. this.rotationPitch = p_180426_8_;
  163. this.boatPosRotationIncrements = 0;
  164. this.setPosition(p_180426_1_, p_180426_3_, p_180426_5_);
  165. this.motionX = this.velocityX = 0.0D;
  166. this.motionY = this.velocityY = 0.0D;
  167. this.motionZ = this.velocityZ = 0.0D;
  168. }
  169. else
  170. {
  171. if (this.isBoatEmpty)
  172. {
  173. this.boatPosRotationIncrements = p_180426_9_ + 5;
  174. }
  175. else
  176. {
  177. double d3 = p_180426_1_ - this.posX;
  178. double d4 = p_180426_3_ - this.posY;
  179. double d5 = p_180426_5_ - this.posZ;
  180. double d6 = d3 * d3 + d4 * d4 + d5 * d5;
  181.  
  182. if (d6 <= 1.0D)
  183. {
  184. return;
  185. }
  186.  
  187. this.boatPosRotationIncrements = 3;
  188. }
  189.  
  190. this.boatX = p_180426_1_;
  191. this.boatY = p_180426_3_;
  192. this.boatZ = p_180426_5_;
  193. this.boatYaw = (double)p_180426_7_;
  194. this.boatPitch = (double)p_180426_8_;
  195. this.motionX = this.velocityX;
  196. this.motionY = this.velocityY;
  197. this.motionZ = this.velocityZ;
  198. }
  199. }
  200.  
  201. @SideOnly(Side.CLIENT)
  202. public void setVelocity(double x, double y, double z)
  203. {
  204. this.velocityX = this.motionX = x;
  205. this.velocityY = this.motionY = y;
  206. this.velocityZ = this.motionZ = z;
  207. }
  208.  
  209. public void onUpdate()
  210. {
  211. super.onUpdate();
  212.  
  213. if (this.getTimeSinceHit() > 0)
  214. {
  215. this.setTimeSinceHit(this.getTimeSinceHit() - 1);
  216. }
  217.  
  218. if (this.getDamageTaken() > 0.0F)
  219. {
  220. this.setDamageTaken(this.getDamageTaken() - 1.0F);
  221. }
  222.  
  223. this.prevPosX = this.posX;
  224. this.prevPosY = this.posY;
  225. this.prevPosZ = this.posZ;
  226. byte b0 = 5;
  227. double d0 = 0.0D;
  228.  
  229. for (int i = 0; i < b0; ++i)
  230. {
  231. double d1 = this.getEntityBoundingBox().minY + (this.getEntityBoundingBox().maxY - this.getEntityBoundingBox().minY) * (double)(i + 0) / (double)b0 - 0.125D;
  232. double d3 = this.getEntityBoundingBox().minY + (this.getEntityBoundingBox().maxY - this.getEntityBoundingBox().minY) * (double)(i + 1) / (double)b0 - 0.125D;
  233. AxisAlignedBB axisalignedbb = new AxisAlignedBB(this.getEntityBoundingBox().minX, d1, this.getEntityBoundingBox().minZ, this.getEntityBoundingBox().maxX, d3, this.getEntityBoundingBox().maxZ);
  234.  
  235. if (this.worldObj.isAABBInMaterial(axisalignedbb, Material.lava))
  236. {
  237. d0 += 1.0D / (double)b0;
  238. }
  239. }
  240.  
  241. double d9 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
  242. double d2;
  243. double d4;
  244. int j;
  245.  
  246. if (d9 > 0.2975D)
  247. {
  248. d2 = Math.cos((double)this.rotationYaw * Math.PI / 180.0D);
  249. d4 = Math.sin((double)this.rotationYaw * Math.PI / 180.0D);
  250.  
  251. for (j = 0; (double)j < 1.0D + d9 * 60.0D; ++j)
  252. {
  253. double d5 = (double)(this.rand.nextFloat() * 2.0F - 1.0F);
  254. double d6 = (double)(this.rand.nextInt(2) * 2 - 1) * 0.7D;
  255. double d7;
  256. double d8;
  257.  
  258. if (this.rand.nextBoolean())
  259. {
  260. d7 = this.posX - d2 * d5 * 0.8D + d4 * d6;
  261. d8 = this.posZ - d4 * d5 * 0.8D - d2 * d6;
  262. this.worldObj.spawnParticle(EnumParticleTypes.FLAME, d7, this.posY - 0.125D, d8, this.motionX, this.motionY, this.motionZ, new int[0]);
  263. }
  264. else
  265. {
  266. d7 = this.posX + d2 + d4 * d5 * 0.7D;
  267. d8 = this.posZ + d4 - d2 * d5 * 0.7D;
  268. this.worldObj.spawnParticle(EnumParticleTypes.FLAME, d7, this.posY - 0.125D, d8, this.motionX, this.motionY, this.motionZ, new int[0]);
  269. }
  270. }
  271. }
  272.  
  273. double d10;
  274. double d11;
  275.  
  276. if (this.worldObj.isRemote && this.isBoatEmpty)
  277. {
  278. if (this.boatPosRotationIncrements > 0)
  279. {
  280. d2 = this.posX + (this.boatX - this.posX) / (double)this.boatPosRotationIncrements;
  281. d4 = this.posY + (this.boatY - this.posY) / (double)this.boatPosRotationIncrements;
  282. d10 = this.posZ + (this.boatZ - this.posZ) / (double)this.boatPosRotationIncrements;
  283. d11 = MathHelper.wrapAngleTo180_double(this.boatYaw - (double)this.rotationYaw);
  284. this.rotationYaw = (float)((double)this.rotationYaw + d11 / (double)this.boatPosRotationIncrements);
  285. this.rotationPitch = (float)((double)this.rotationPitch + (this.boatPitch - (double)this.rotationPitch) / (double)this.boatPosRotationIncrements);
  286. --this.boatPosRotationIncrements;
  287. this.setPosition(d2, d4, d10);
  288. this.setRotation(this.rotationYaw, this.rotationPitch);
  289. }
  290. else
  291. {
  292. d2 = this.posX + this.motionX;
  293. d4 = this.posY + this.motionY;
  294. d10 = this.posZ + this.motionZ;
  295. this.setPosition(d2, d4, d10);
  296.  
  297. if (this.onGround)
  298. {
  299. this.motionX *= 0.5D;
  300. this.motionY *= 0.5D;
  301. this.motionZ *= 0.5D;
  302. }
  303.  
  304. this.motionX *= 0.9900000095367432D;
  305. this.motionY *= 0.949999988079071D;
  306. this.motionZ *= 0.9900000095367432D;
  307. }
  308. }
  309. else
  310. {
  311. if (d0 < 1.0D)
  312. {
  313. d2 = d0 * 2.0D - 1.0D;
  314. this.motionY += 0.03999999910593033D * d2;
  315. }
  316. else
  317. {
  318. if (this.motionY < 0.0D)
  319. {
  320. this.motionY /= 2.0D;
  321. }
  322.  
  323. this.motionY += 0.007000000216066837D;
  324. }
  325.  
  326. if (this.riddenByEntity instanceof EntityLivingBase)
  327. {
  328. EntityLivingBase entitylivingbase = (EntityLivingBase)this.riddenByEntity;
  329. float f = this.riddenByEntity.rotationYaw + -entitylivingbase.moveStrafing * 90.0F;
  330. this.motionX += -Math.sin((double)(f * (float)Math.PI / 180.0F)) * this.speedMultiplier * (double)entitylivingbase.moveForward * 0.05000000074505806D;
  331. this.motionZ += Math.cos((double)(f * (float)Math.PI / 180.0F)) * this.speedMultiplier * (double)entitylivingbase.moveForward * 0.05000000074505806D;
  332. }
  333.  
  334. d2 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
  335.  
  336. if (d2 > 0.35D)
  337. {
  338. d4 = 0.35D / d2;
  339. this.motionX *= d4;
  340. this.motionZ *= d4;
  341. d2 = 0.35D;
  342. }
  343.  
  344. if (d2 > d9 && this.speedMultiplier < 0.35D)
  345. {
  346. this.speedMultiplier += (0.35D - this.speedMultiplier) / 35.0D;
  347.  
  348. if (this.speedMultiplier > 0.35D)
  349. {
  350. this.speedMultiplier = 0.35D;
  351. }
  352. }
  353. else
  354. {
  355. this.speedMultiplier -= (this.speedMultiplier - 0.07D) / 35.0D;
  356.  
  357. if (this.speedMultiplier < 0.07D)
  358. {
  359. this.speedMultiplier = 0.07D;
  360. }
  361. }
  362.  
  363. int l;
  364.  
  365. for (l = 0; l < 4; ++l)
  366. {
  367. int i1 = MathHelper.floor_double(this.posX + ((double)(l % 2) - 0.5D) * 0.8D);
  368. j = MathHelper.floor_double(this.posZ + ((double)(l / 2) - 0.5D) * 0.8D);
  369.  
  370. for (int j1 = 0; j1 < 2; ++j1)
  371. {
  372. int k = MathHelper.floor_double(this.posY) + j1;
  373. BlockPos blockpos = new BlockPos(i1, k, j);
  374. Block block = this.worldObj.getBlockState(blockpos).getBlock();
  375.  
  376. if (block == Blocks.snow_layer)
  377. {
  378. this.worldObj.setBlockToAir(blockpos);
  379. this.isCollidedHorizontally = false;
  380. }
  381. else if (block == Blocks.waterlily)
  382. {
  383. this.worldObj.destroyBlock(blockpos, true);
  384. this.isCollidedHorizontally = false;
  385. }
  386. }
  387. }
  388.  
  389. if (this.onGround)
  390. {
  391. this.motionX *= 0.5D;
  392. this.motionY *= 0.5D;
  393. this.motionZ *= 0.5D;
  394. }
  395.  
  396. this.moveEntity(this.motionX, this.motionY, this.motionZ);
  397.  
  398. if (this.isCollidedHorizontally && d9 > 0.2D)
  399. {
  400. if (!this.worldObj.isRemote && !this.isDead)
  401. {
  402. this.setDead();
  403.  
  404. for (l = 0; l < 3; ++l)
  405. {
  406. this.dropItemWithOffset(Item.getItemFromBlock(Blocks.obsidian), 1, 0.0F);
  407. }
  408.  
  409. for (l = 0; l < 2; ++l)
  410. {
  411. this.dropItemWithOffset(null, 1, 0.0F);
  412. }
  413. }
  414. }
  415. else
  416. {
  417. this.motionX *= 0.9900000095367432D;
  418. this.motionY *= 0.949999988079071D;
  419. this.motionZ *= 0.9900000095367432D;
  420. }
  421.  
  422. this.rotationPitch = 0.0F;
  423. d4 = (double)this.rotationYaw;
  424. d10 = this.prevPosX - this.posX;
  425. d11 = this.prevPosZ - this.posZ;
  426.  
  427. if (d10 * d10 + d11 * d11 > 0.001D)
  428. {
  429. d4 = (double)((float)(Math.atan2(d11, d10) * 180.0D / Math.PI));
  430. }
  431.  
  432. double d12 = MathHelper.wrapAngleTo180_double(d4 - (double)this.rotationYaw);
  433.  
  434. if (d12 > 20.0D)
  435. {
  436. d12 = 20.0D;
  437. }
  438.  
  439. if (d12 < -20.0D)
  440. {
  441. d12 = -20.0D;
  442. }
  443.  
  444. this.rotationYaw = (float)((double)this.rotationYaw + d12);
  445. this.setRotation(this.rotationYaw, this.rotationPitch);
  446.  
  447. if (!this.worldObj.isRemote)
  448. {
  449. List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(0.20000000298023224D, 0.0D, 0.20000000298023224D));
  450.  
  451. if (list != null && !list.isEmpty())
  452. {
  453. for (int k1 = 0; k1 < list.size(); ++k1)
  454. {
  455. Entity entity = (Entity)list.get(k1);
  456.  
  457. if (entity != this.riddenByEntity && entity.canBePushed() && entity instanceof EntityObsidianBoat)
  458. {
  459. entity.applyEntityCollision(this);
  460. }
  461. }
  462. }
  463.  
  464. if (this.riddenByEntity != null && this.riddenByEntity.isDead)
  465. {
  466. this.riddenByEntity = null;
  467. }
  468. }
  469. }
  470. }
  471.  
  472. public void updateRiderPosition()
  473. {
  474. if (this.riddenByEntity != null)
  475. {
  476. double d0 = Math.cos((double)this.rotationYaw * Math.PI / 180.0D) * 0.4D;
  477. double d1 = Math.sin((double)this.rotationYaw * Math.PI / 180.0D) * 0.4D;
  478. this.riddenByEntity.setPosition(this.posX + d0, this.posY + this.getMountedYOffset() + this.riddenByEntity.getYOffset(), this.posZ + d1);
  479. }
  480. }
  481.  
  482. protected void writeEntityToNBT(NBTTagCompound tagCompound) {}
  483.  
  484. protected void readEntityFromNBT(NBTTagCompound tagCompund) {}
  485.  
  486. public boolean interactFirst(EntityPlayer playerIn)
  487. {
  488. if (this.riddenByEntity != null && this.riddenByEntity instanceof EntityPlayer && this.riddenByEntity != playerIn)
  489. {
  490. return true;
  491. }
  492. else
  493. {
  494. if (!this.worldObj.isRemote)
  495. {
  496. playerIn.mountEntity(this);
  497. }
  498.  
  499. return true;
  500. }
  501. }
  502.  
  503. protected void func_180433_a(double p_180433_1_, boolean p_180433_3_, Block p_180433_4_, BlockPos p_180433_5_)
  504. {
  505. if (p_180433_3_)
  506. {
  507. if (this.fallDistance > 3.0F)
  508. {
  509. this.fall(this.fallDistance, 1.0F);
  510.  
  511. if (!this.worldObj.isRemote && !this.isDead)
  512. {
  513. this.setDead();
  514. int i;
  515.  
  516. for (i = 0; i < 3; ++i)
  517. {
  518. this.dropItemWithOffset(Item.getItemFromBlock(Blocks.obsidian), 1, 0.0F);
  519. }
  520.  
  521. for (i = 0; i < 2; ++i)
  522. {
  523. this.dropItemWithOffset(Items.stick, 1, 0.0F);
  524. }
  525. }
  526.  
  527. this.fallDistance = 0.0F;
  528. }
  529. }
  530. else if (this.worldObj.getBlockState((new BlockPos(this)).down()).getBlock().getMaterial() != Material.lava && p_180433_1_ < 0.0D)
  531. {
  532. this.fallDistance = (float)((double)this.fallDistance - p_180433_1_);
  533. }
  534. }
  535.  
  536. public void setDamageTaken(float p_70266_1_)
  537. {
  538. this.dataWatcher.updateObject(19, Float.valueOf(p_70266_1_));
  539. }
  540.  
  541. public float getDamageTaken()
  542. {
  543. return this.dataWatcher.getWatchableObjectFloat(19);
  544. }
  545.  
  546. public void setTimeSinceHit(int p_70265_1_)
  547. {
  548. this.dataWatcher.updateObject(17, Integer.valueOf(p_70265_1_));
  549. }
  550.  
  551. public int getTimeSinceHit()
  552. {
  553. return this.dataWatcher.getWatchableObjectInt(17);
  554. }
  555.  
  556. public void setForwardDirection(int p_70269_1_)
  557. {
  558. this.dataWatcher.updateObject(18, Integer.valueOf(p_70269_1_));
  559. }
  560.  
  561. public int getForwardDirection()
  562. {
  563. return this.dataWatcher.getWatchableObjectInt(18);
  564. }
  565.  
  566. @SideOnly(Side.CLIENT)
  567. public void setIsBoatEmpty(boolean p_70270_1_)
  568. {
  569. this.isBoatEmpty = p_70270_1_;
  570. }
  571. }
Advertisement
Add Comment
Please, Sign In to add comment