Advertisement
9678Dash

ItemRepeater

Dec 5th, 2019
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. package com.dash.frmod.entity.uniqueToolClasses;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import net.minecraft.creativetab.CreativeTabs;
  6. import net.minecraft.enchantment.EnchantmentHelper;
  7. import net.minecraft.entity.EntityLivingBase;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.entity.projectile.EntityArrow;
  10. import net.minecraft.init.Enchantments;
  11. import net.minecraft.init.Items;
  12. import net.minecraft.init.SoundEvents;
  13. import net.minecraft.item.EnumAction;
  14. import net.minecraft.item.IItemPropertyGetter;
  15. import net.minecraft.item.Item;
  16. import net.minecraft.item.ItemArrow;
  17. import net.minecraft.item.ItemBow;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.stats.StatList;
  20. import net.minecraft.util.ActionResult;
  21. import net.minecraft.util.EnumActionResult;
  22. import net.minecraft.util.EnumHand;
  23. import net.minecraft.util.ResourceLocation;
  24. import net.minecraft.util.SoundCategory;
  25. import net.minecraft.world.World;
  26. import net.minecraftforge.fml.relauncher.Side;
  27. import net.minecraftforge.fml.relauncher.SideOnly;
  28.  
  29. public class ItemRepeater extends ItemBow
  30. {
  31. public ItemRepeater()
  32. {
  33. this.maxStackSize = 1;
  34. this.setMaxDamage(384);
  35. this.setCreativeTab(CreativeTabs.COMBAT);
  36. this.addPropertyOverride(new ResourceLocation("pull"), new IItemPropertyGetter()
  37. {
  38. @SideOnly(Side.CLIENT)
  39. public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)
  40. {
  41. if (entityIn == null)
  42. {
  43. return 0.0F;
  44. }
  45. else
  46. {
  47. return entityIn.getActiveItemStack().getItem() != Items.BOW ? 0.0F : (float)(stack.getMaxItemUseDuration() - entityIn.getItemInUseCount()) / 20.0F;
  48. }
  49. }
  50. });
  51. this.addPropertyOverride(new ResourceLocation("pulling"), new IItemPropertyGetter()
  52. {
  53. @SideOnly(Side.CLIENT)
  54. public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)
  55. {
  56. return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
  57. }
  58. });
  59. }
  60.  
  61. private ItemStack findAmmo(EntityPlayer player)
  62. {
  63. if (this.isArrow(player.getHeldItem(EnumHand.OFF_HAND)))
  64. {
  65. return player.getHeldItem(EnumHand.OFF_HAND);
  66. }
  67. else if (this.isArrow(player.getHeldItem(EnumHand.MAIN_HAND)))
  68. {
  69. return player.getHeldItem(EnumHand.MAIN_HAND);
  70. }
  71. else
  72. {
  73. for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
  74. {
  75. ItemStack itemstack = player.inventory.getStackInSlot(i);
  76.  
  77. if (this.isArrow(itemstack))
  78. {
  79. return itemstack;
  80. }
  81. }
  82.  
  83. return ItemStack.EMPTY;
  84. }
  85. }
  86.  
  87. protected boolean isArrow(ItemStack stack)
  88. {
  89. return stack.getItem() instanceof ItemArrow;
  90. }
  91.  
  92. /**
  93. * Called when the player stops using an Item (stops holding the right mouse button).
  94. */
  95. public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
  96. {
  97. if (entityLiving instanceof EntityPlayer)
  98. {
  99. EntityPlayer entityplayer = (EntityPlayer)entityLiving;
  100. boolean flag = entityplayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0;
  101. ItemStack itemstack = this.findAmmo(entityplayer);
  102.  
  103. int i = this.getMaxItemUseDuration(stack) - timeLeft;
  104. i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, entityplayer, i, !itemstack.isEmpty() || flag);
  105. if (i < 0) return;
  106.  
  107. if (!itemstack.isEmpty() || flag)
  108. {
  109. if (itemstack.isEmpty())
  110. {
  111. itemstack = new ItemStack(Items.ARROW);
  112. }
  113.  
  114. float f = getArrowVelocity(i);
  115.  
  116. if ((double)f >= 0.1D)
  117. {
  118. boolean flag1 = entityplayer.capabilities.isCreativeMode || (itemstack.getItem() instanceof ItemArrow && ((ItemArrow) itemstack.getItem()).isInfinite(itemstack, stack, entityplayer));
  119.  
  120. if (!worldIn.isRemote)
  121. {
  122. ItemArrow itemarrow = (ItemArrow)(itemstack.getItem() instanceof ItemArrow ? itemstack.getItem() : Items.ARROW);
  123. EntityArrow entityarrow = itemarrow.createArrow(worldIn, itemstack, entityplayer);
  124. entityarrow.shoot(entityplayer, entityplayer.rotationPitch, entityplayer.rotationYaw, 0.0F, f * 3.0F, 1.0F);
  125.  
  126. if (f == 1.0F)
  127. {
  128. entityarrow.setIsCritical(true);
  129. }
  130.  
  131. int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, stack);
  132.  
  133. if (j > 0)
  134. {
  135. entityarrow.setDamage(entityarrow.getDamage() + (double)j * 0.5D + 0.5D);
  136. }
  137.  
  138. int k = EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, stack);
  139.  
  140. if (k > 0)
  141. {
  142. entityarrow.setKnockbackStrength(k);
  143. }
  144.  
  145. if (EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAME, stack) > 0)
  146. {
  147. entityarrow.setFire(100);
  148. }
  149.  
  150. stack.damageItem(1, entityplayer);
  151.  
  152. if (flag1 || entityplayer.capabilities.isCreativeMode && (itemstack.getItem() == Items.SPECTRAL_ARROW || itemstack.getItem() == Items.TIPPED_ARROW))
  153. {
  154. entityarrow.pickupStatus = EntityArrow.PickupStatus.CREATIVE_ONLY;
  155. }
  156.  
  157. worldIn.spawnEntity(entityarrow);
  158. }
  159.  
  160. worldIn.playSound((EntityPlayer)null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);
  161.  
  162. if (!flag1 && !entityplayer.capabilities.isCreativeMode)
  163. {
  164. itemstack.shrink(1);
  165.  
  166. if (itemstack.isEmpty())
  167. {
  168. entityplayer.inventory.deleteStack(itemstack);
  169. }
  170. }
  171.  
  172. entityplayer.addStat(StatList.getObjectUseStats(this));
  173. }
  174. }
  175. }
  176. }
  177.  
  178. /**
  179. * Gets the velocity of the arrow entity from the bow's charge
  180. */
  181.  
  182. // THIS IS WHERE THE CODE MUST BE MODIFIED TO MATCH YOUR DESIRED SPEED
  183. public static float getArrowVelocity(int charge)
  184. {
  185. float f = (float)charge / 20.0F; //THIS IS THE EXACT LINE, 20.0 IS DEFAULT, 10.0 IS TWICE AS FAST.
  186. f = (f * f + f * 2.0F) / 3.0F;
  187.  
  188. if (f > 1.0F)
  189. {
  190. f = 1.0F;
  191. }
  192.  
  193. return f;
  194. }
  195.  
  196.  
  197.  
  198. /**
  199. * How long it takes to use or consume an item
  200. */
  201. public int getMaxItemUseDuration(ItemStack stack)
  202. {
  203. return 3600;
  204. }
  205.  
  206. /**
  207. * returns the action that specifies what animation to play when the items is being used
  208. */
  209. public EnumAction getItemUseAction(ItemStack stack)
  210. {
  211. return EnumAction.BOW;
  212. }
  213.  
  214. /**
  215. * Called when the equipped item is right clicked.
  216. */
  217. public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
  218. {
  219. ItemStack itemstack = playerIn.getHeldItem(handIn);
  220. boolean flag = !this.findAmmo(playerIn).isEmpty();
  221.  
  222. ActionResult<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, worldIn, playerIn, handIn, flag);
  223. if (ret != null) return ret;
  224.  
  225. if (!playerIn.capabilities.isCreativeMode && !flag)
  226. {
  227. return flag ? new ActionResult(EnumActionResult.PASS, itemstack) : new ActionResult(EnumActionResult.FAIL, itemstack);
  228. }
  229. else
  230. {
  231. playerIn.setActiveHand(handIn);
  232. return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
  233. }
  234. }
  235.  
  236. /**
  237. * Return the enchantability factor of the item, most of the time is based on material.
  238. */
  239. public int getItemEnchantability()
  240. {
  241. return 1;
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement