Zapfox

Untitled

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