Advertisement
Superfreek

Code CustomFishingHook

Feb 27th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.99 KB | None | 0 0
  1. package fishingadventure.entity.projectile;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.BlockLiquid;
  5. import net.minecraft.block.material.Material;
  6. import net.minecraft.block.state.IBlockState;
  7. import net.minecraft.entity.Entity;
  8. import net.minecraft.entity.MoverType;
  9. import net.minecraft.entity.item.EntityItem;
  10. import net.minecraft.entity.item.EntityXPOrb;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.init.SoundEvents;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.stats.StatList;
  16. import net.minecraft.util.math.BlockPos;
  17. import net.minecraft.util.math.MathHelper;
  18. import net.minecraft.world.World;
  19. import net.minecraft.world.WorldServer;
  20. import net.minecraftforge.common.MinecraftForge;
  21. import net.minecraftforge.event.entity.player.ItemFishedEvent;
  22. import net.minecraftforge.fml.relauncher.Side;
  23. import net.minecraftforge.fml.relauncher.SideOnly;
  24. import net.minecraft.entity.projectile.EntityFishHook;
  25.  
  26. import fishingadventure.core.Items;
  27. import fishingadventure.core.MaterialManager;
  28. import fishingadventure.core.ParticlesManager;
  29. import fishingadventure.entity.item.EntityFireproofItem;
  30. import fishingadventure.core.CatchManager;
  31.  
  32. import java.util.ArrayList;
  33. import java.util.List;
  34.  
  35. /**
  36. * Superfreek's Adventure
  37. *
  38. * @author Superfreek
  39. * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
  40. */
  41. public class EntityCustomFishHook extends AbstractFishHook {
  42.  
  43. protected boolean inGround;
  44. protected int ticksInGround;
  45. protected int ticksInAir;
  46. protected int ticksCatchable;
  47. protected int ticksCaughtDelay;
  48. protected int ticksCatchableDelay;
  49. protected float fishApproachAngle;
  50. private EntityCustomFishHook.State currentState;
  51.  
  52. public EntityCustomFishHook(World world) {
  53. super(world);
  54. }
  55.  
  56. @SideOnly(Side.CLIENT)
  57. public EntityCustomFishHook(World world, EntityPlayer player, double x, double y, double z) {
  58. super(world, player, x, y, z);
  59. }
  60.  
  61. public EntityCustomFishHook(World world, EntityPlayer player) {
  62. super(world, player);
  63. }
  64.  
  65. @Override
  66. public void onUpdate() {
  67. if (!this.world.isRemote) {
  68. this.setFlag(6, this.isGlowing());
  69. }
  70.  
  71. this.onEntityUpdate();
  72.  
  73. if (this.getAngler() == null) {
  74. this.setDead();
  75. } else if (this.world.isRemote || !this.shouldStopFishing()) {
  76. if (this.inGround) {
  77. this.ticksInGround++;
  78.  
  79. if (this.ticksInGround >= 1200) {
  80. this.setDead();
  81. return;
  82. }
  83. }
  84.  
  85. float f = 0;
  86. BlockPos pos = new BlockPos(this);
  87. IBlockState state = this.world.getBlockState(pos);
  88. boolean isInLiquid = MaterialManager.MATERIAL_SET.contains(state.getMaterial());
  89.  
  90. if (state.getMaterial() == Material.LAVA && !this.isImmuneToFire) {
  91. this.setDead();
  92. }
  93. if (isInLiquid) {
  94. f = BlockLiquid.getBlockLiquidHeight(state, this.world, pos);
  95. }
  96.  
  97. if (this.currentState == State.FLYING) {
  98. if (this.caughtEntity != null) {
  99. this.motionX = 0;
  100. this.motionY = 0;
  101. this.motionZ = 0;
  102. this.currentState = State.HOOKED_IN_ENTITY;
  103. return;
  104. }
  105.  
  106. if (f > 0) {
  107. this.motionX *= 0.3;
  108. this.motionY *= 0.2;
  109. this.motionZ *= 0.3;
  110. this.currentState = State.BOBBING;
  111. return;
  112. }
  113.  
  114. if (!this.world.isRemote) {
  115. this.checkCollision;
  116. }
  117.  
  118. if (!this.inGround && !this.onGround && !this.collidedHorizontally) {
  119. this.ticksInAir++;
  120. } else {
  121. this.ticksInAir = 0;
  122. this.motionX = 0;
  123. this.motionY = 0;
  124. this.motionZ = 0;
  125. }
  126. } else {
  127. if (this.currentState == State.HOOKED_IN_ENTITY) {
  128. if (this.caughtEntity != null) {
  129. if (this.caughtEntity.isDead) {
  130. this.caughtEntity = null;
  131. this.currentState = State.FLYING;
  132. } else {
  133. this.posX = this.caughtEntity.posX;
  134. double d2 = this.caughtEntity.height;
  135. this.posY = this.caughtEntity.getEntityBoundingBox().minY + d2 * 0.8;
  136. this.posZ = this.caughtEntity.posZ;
  137. this.setPosition(this.posX, this.posY, this.posZ);
  138. }
  139. }
  140.  
  141. return;
  142. }
  143.  
  144. if (this.currentState == State.BOBBING) {
  145. this.motionX *= 0.9;
  146. this.motionZ *= 0.9;
  147. double d0 = this.posY + this.motionY - pos.getY() - f;
  148.  
  149. if (Math.abs(d0) < 0.01) {
  150. d0 += Math.signum(d0) * 0.1;
  151. }
  152.  
  153. this.motionY -= d0 * this.rand.nextFloat() * 0.2;
  154.  
  155. if (!this.world.isRemote && f > 0) {
  156. this.catchingFish(pos);
  157. }
  158. }
  159. }
  160.  
  161. if (!isInLiquid) {
  162. this.motionY -= 0.03;
  163. }
  164.  
  165. this.move(MoverType.SELF, this.motionX, this.motionY, this.motionZ);
  166. this.updateRotation();
  167.  
  168. this.motionX *= 0.92;
  169. this.motionY *= 0.92;
  170. this.motionZ *= 0.92;
  171. this.setPosition(this.posX, this.posY, this.posZ);
  172. }
  173. }
  174.  
  175. protected boolean shouldStopFishing() {
  176. ItemStack mainStack = this.getAngler().getHeldItemMainhand();
  177. ItemStack offStack = this.getAngler().getHeldItemOffhand();
  178. boolean flag = isFishingPoleStack(mainStack);
  179. boolean flag1 = isFishingPoleStack(offStack);
  180.  
  181. if (!this.getAngler().isDead && this.getAngler().isEntityAlive() && (flag || flag1) && this.getDistanceSq(this.getAngler()) <= 1024) {
  182. return false;
  183. } else {
  184. this.setDead();
  185. return true;
  186. }
  187. }
  188.  
  189. protected void catchingFish(BlockPos pos) {
  190. WorldServer worldserver = (WorldServer) this.world;
  191. int i = 1;
  192. BlockPos blockpos = pos.up();
  193.  
  194. if (this.rand.nextFloat() < 0.25 && this.world.isRainingAt(blockpos)) {
  195. i++;
  196. }
  197.  
  198. if (this.rand.nextFloat() < 0.5 && !this.world.canSeeSky(blockpos)) {
  199. i--;
  200. }
  201.  
  202. if (this.ticksCatchable > 0) {
  203. this.ticksCatchable--;
  204.  
  205. if (this.ticksCatchable <= 0) {
  206. this.ticksCaughtDelay = 0;
  207. this.ticksCatchableDelay = 0;
  208. } else {
  209. this.motionY -= 0.2 * this.rand.nextFloat() * this.rand.nextFloat();
  210. }
  211. } else if (this.ticksCatchableDelay > 0) {
  212. this.ticksCatchableDelay -= i;
  213.  
  214. int minY = MathHelper.floor(this.getEntityBoundingBox().minY);
  215. Block liquidBlock = worldserver.getBlockState(new BlockPos(this.posX, minY, this.posZ)).getBlock();
  216. if (this.ticksCatchableDelay > 0) {
  217. this.fishApproachAngle = (float) (this.fishApproachAngle + this.rand.nextGaussian() * 4);
  218. float angle = this.fishApproachAngle * 0.0175F;
  219. float sin = MathHelper.sin(angle);
  220. float cos = MathHelper.cos(angle);
  221. double xPos = this.posX + sin * this.ticksCatchableDelay * 0.1;
  222. double yPos = minY + 1;
  223. double zPos = this.posZ + cos * this.ticksCatchableDelay * 0.1;
  224.  
  225. if (MaterialManager.MATERIAL_SET.contains(worldserver.getBlockState(new BlockPos(xPos, minY, zPos)).getMaterial())) {
  226. if (this.rand.nextFloat() < 0.15) {
  227. ParticlesManager.INSTANCE.getBubbleParticles(liquidBlock).spawn(worldserver, xPos, yPos - 0.1, zPos, 1, sin, 0.1, cos, 0);
  228. }
  229.  
  230. float zOffset = sin * 0.04F;
  231. float xOffset = cos * 0.04F;
  232. ParticlesManager.INSTANCE.getWakeParticles(liquidBlock).spawn(worldserver, xPos, yPos, zPos, 0, xOffset, 0.01, -zOffset, 1);
  233. ParticlesManager.INSTANCE.getWakeParticles(liquidBlock).spawn(worldserver, xPos, yPos, zPos, 0, -xOffset, 0.01, zOffset, 1);
  234. }
  235. } else {
  236. this.motionY = -0.4 * MathHelper.nextFloat(this.rand, 0.6F, 1);
  237. this.playSound(SoundEvents.ENTITY_BOBBER_SPLASH, 0.25F, 1 + (this.rand.nextFloat() - this.rand.nextFloat()) * 0.4F);
  238. double d3 = this.getEntityBoundingBox().minY + 0.5;
  239.  
  240. ParticlesManager.INSTANCE.getBubbleParticles(liquidBlock).spawn(worldserver, this.posX, d3, this.posZ, (int) (1 + this.width * 20), this.width, 0, this.width, 0.2);
  241. ParticlesManager.INSTANCE.getWakeParticles(liquidBlock).spawn(worldserver, this.posX, d3, this.posZ, (int) (1 + this.width * 20), this.width, 0, this.width, 0.2);
  242. this.ticksCatchable = MathHelper.getInt(this.rand, 20, 40);
  243. }
  244. } else if (this.ticksCaughtDelay > 0) {
  245. this.ticksCaughtDelay -= i;
  246. float f5 = 0.15F;
  247.  
  248. if (this.ticksCaughtDelay < 20) {
  249. f5 = f5 + (20 - this.ticksCaughtDelay) * 0.05F;
  250. } else if (this.ticksCaughtDelay < 40) {
  251. f5 = f5 + (40 - this.ticksCaughtDelay) * 0.02F;
  252. } else if (this.ticksCaughtDelay < 60) {
  253. f5 = f5 + (60 - this.ticksCaughtDelay) * 0.01F;
  254. }
  255.  
  256. if (this.rand.nextFloat() < f5) {
  257. float f6 = MathHelper.nextFloat(this.rand, 0, 360) * 0.017453292F;
  258. float f7 = MathHelper.nextFloat(this.rand, 25, 60);
  259. int minY = MathHelper.floor(this.getEntityBoundingBox().minY);
  260. double xPos = this.posX + MathHelper.sin(f6) * f7 * 0.1;
  261. double yPos = minY + 1;
  262. double zPos = this.posZ + MathHelper.cos(f6) * f7 * 0.1;
  263.  
  264. if (MaterialManager.MATERIAL_SET.contains(worldserver.getBlockState(new BlockPos(xPos, minY, zPos)).getMaterial())) {
  265. ParticlesManager.INSTANCE.getSplashParticles(worldserver.getBlockState(new BlockPos(this.posX, minY, this.posZ)).getBlock()).spawn(worldserver, this.rand, xPos, yPos, zPos);
  266. }
  267. }
  268.  
  269. if (this.ticksCaughtDelay <= 0) {
  270. this.fishApproachAngle = MathHelper.nextFloat(this.rand, 0, 360);
  271. this.ticksCatchableDelay = MathHelper.getInt(this.rand, 20, 80);
  272. }
  273. } else {
  274. this.ticksCaughtDelay = MathHelper.getInt(this.rand, 100, 600);
  275. this.ticksCaughtDelay -= this.lureSpeed * 20 * 5;
  276. }
  277. }
  278.  
  279. @Override
  280. protected void doWaterSplashEffect() {
  281. Entity entity = this.isBeingRidden() && this.getControllingPassenger() != null ? this.getControllingPassenger() : this;
  282. float f = (entity == this) ? 0.2F : 0.9F;
  283. float f1 = MathHelper.sqrt(entity.motionX * entity.motionX * 0.2 + entity.motionY * entity.motionY + entity.motionZ * entity.motionZ * 0.2) * f;
  284.  
  285. if (f1 > 1) {
  286. f1 = 1;
  287. }
  288.  
  289. this.playSound(this.getSplashSound(), f1, 1 + (this.rand.nextFloat() - this.rand.nextFloat()) * 0.4F);
  290.  
  291. if (!this.world.isRemote) {
  292. float minY = (float) MathHelper.floor(this.getEntityBoundingBox().minY);
  293. Block liquidBlock = this.world.getBlockState(new BlockPos(this.posX, minY, this.posZ)).getBlock();
  294. for (int i = 0; i < 1 + this.width * 20; i++) {
  295. float f3 = (this.rand.nextFloat() * 2 - 1) * this.width;
  296. float f4 = (this.rand.nextFloat() * 2 - 1) * this.width;
  297. ParticlesManager.INSTANCE.getBubbleParticles(liquidBlock).spawn((WorldServer) this.world, this.posX + f3, minY + 1, this.posZ + f4,
  298. 1, this.motionX, this.motionY - this.rand.nextFloat() * 0.2F, this.motionZ, 0);
  299. }
  300.  
  301. for (int j = 0; j < 1 + this.width * 20; j++) {
  302. float f5 = (this.rand.nextFloat() * 2 - 1) * this.width;
  303. float f6 = (this.rand.nextFloat() * 2 - 1) * this.width;
  304.  
  305. ParticlesManager.INSTANCE.getSplashParticles(liquidBlock).spawn((WorldServer) this.world, this.rand, this.posX + f5, minY + 1, this.posZ + f6);
  306. }
  307. }
  308. }
  309.  
  310. @Override
  311. public int handleHookRetraction() {
  312. if (!this.world.isRemote && this.getAngler() != null) {
  313. int i = 0;
  314.  
  315. ItemFishedEvent event = null;
  316. if (this.caughtEntity != null) {
  317. this.bringInHookedEntity();
  318. this.world.setEntityState(this, (byte) 31);
  319. i = this.caughtEntity instanceof EntityItem ? 3 : 5;
  320. } else if (this.ticksCatchable > 0) {
  321. List<ItemStack> result = this.getCatch();
  322. event = new ItemFishedEvent(result, this.inGround ? 2 : 1, this);
  323. MinecraftForge.EVENT_BUS.post(event);
  324.  
  325. if (event.isCanceled()) {
  326. this.setDead();
  327. return event.getRodDamage();
  328. }
  329.  
  330. for (ItemStack stack : result) {
  331. EntityItem entityitem = getCatchEntityItem(stack);
  332. double d0 = this.getAngler().posX - this.posX;
  333. double d1 = this.getAngler().posY - this.posY;
  334. double d2 = this.getAngler().posZ - this.posZ;
  335. double d3 = MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
  336.  
  337. entityitem.motionX = d0 * 0.1;
  338. entityitem.motionY = d1 * 0.1 + MathHelper.sqrt(d3) * 0.08;
  339. entityitem.motionZ = d2 * 0.1;
  340. this.world.spawnEntity(entityitem);
  341. this.getAngler().world.spawnEntity(new EntityXPOrb(this.getAngler().world, this.getAngler().posX, this.getAngler().posY + 0.5, this.getAngler().posZ + 0.5, this.rand.nextInt(6) + 1));
  342. Item item = stack.getItem();
  343.  
  344. if (item == net.minecraft.init.Items.FISH || item == net.minecraft.init.Items.COOKED_FISH || item == Items.FISH) {
  345. this.getAngler().addStat(StatList.FISH_CAUGHT, 1);
  346. }
  347. }
  348.  
  349. i = 1;
  350. }
  351.  
  352. if (this.inGround) {
  353. i = 2;
  354. }
  355.  
  356. this.setDead();
  357. return event == null ? i : event.getRodDamage();
  358. } else {
  359. return 0;
  360. }
  361. }
  362.  
  363. protected EntityItem getCatchEntityItem(ItemStack stack) {
  364. return new EntityItem(this.world, this.posX, this.posY + 0.5, this.posZ, stack);
  365. }
  366.  
  367. protected EntityItem getFireproofCatchEntityItem(ItemStack stack) {
  368. return new EntityFireproofItem(this.world, this.posX, this.posY + 0.5, this.posZ, stack);
  369. }
  370.  
  371. protected List<ItemStack> getCatch() {
  372. List<ItemStack> result = new ArrayList<>(1);
  373. Block liquidBlock = this.world.getBlockState(new BlockPos(this.posX, MathHelper.floor(this.getEntityBoundingBox().minY), this.posZ)).getBlock();
  374.  
  375. List<ItemStack> tempList = CatchManager.INSTANCE.getCatch(liquidBlock)
  376. .getCatch(world, this.getPosition(), (this.luck + this.getAngler().getLuck()) * 1.5F);
  377. result.add(tempList.get(this.rand.nextInt(tempList.size())));
  378.  
  379. return result;
  380. }
  381. static enum State
  382. {
  383. FLYING,
  384. HOOKED_IN_ENTITY,
  385. BOBBING;
  386. }
  387. }
  388.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement