Guest User

Untitled

a guest
Jan 12th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 KB | None | 0 0
  1. package top.mod.item;
  2. import javax.annotation.Nullable;
  3. import net.minecraft.creativetab.CreativeTabs;
  4. import net.minecraft.enchantment.EnchantmentHelper;
  5. import net.minecraft.entity.EntityLivingBase;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.entity.projectile.EntityArrow;
  8. import net.minecraft.init.Enchantments;
  9. import net.minecraft.init.Items;
  10. import net.minecraft.init.SoundEvents;
  11. import net.minecraft.item.EnumAction;
  12. import net.minecraft.item.IItemPropertyGetter;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemArrow;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.stats.StatList;
  17. import net.minecraft.util.ActionResult;
  18. import net.minecraft.util.EnumActionResult;
  19. import net.minecraft.util.EnumHand;
  20. import net.minecraft.util.ResourceLocation;
  21. import net.minecraft.util.SoundCategory;
  22. import net.minecraft.world.World;
  23. import net.minecraftforge.fml.relauncher.Side;
  24. import net.minecraftforge.fml.relauncher.SideOnly;
  25. public class ModBowIron extends Item
  26. {
  27. public ModBowIron()
  28. {
  29. this.maxStackSize = 1;
  30. this.setMaxDamage(634);
  31. this.setCreativeTab(CreativeTabs.COMBAT);
  32. this.addPropertyOverride(new ResourceLocation("pull"), new IItemPropertyGetter()
  33. {
  34. @SideOnly(Side.CLIENT)
  35. public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)
  36. {
  37. return entityIn == null ? 0.0F : (entityIn.getActiveItemStack().getItem() != Items.BOW ? 0.0F : (float)(stack.getMaxItemUseDuration() - entityIn.getItemInUseCount()) / 20.0F);
  38. }
  39. });
  40. this.addPropertyOverride(new ResourceLocation("pulling"), new IItemPropertyGetter()
  41. {
  42. @SideOnly(Side.CLIENT)
  43. public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)
  44. {
  45. return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
  46. }
  47. });
  48. }
  49. private ItemStack findAmmo(EntityPlayer player)
  50. {
  51. if (this.isArrow(player.getHeldItem(EnumHand.OFF_HAND)))
  52. {
  53. return player.getHeldItem(EnumHand.OFF_HAND);
  54. }
  55. else if (this.isArrow(player.getHeldItem(EnumHand.MAIN_HAND)))
  56. {
  57. return player.getHeldItem(EnumHand.MAIN_HAND);
  58. }
  59. else
  60. {
  61. for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
  62. {
  63. ItemStack itemstack = player.inventory.getStackInSlot(i);
  64. if (this.isArrow(itemstack))
  65. {
  66. return itemstack;
  67. }
  68. }
  69. return ItemStack.EMPTY;
  70. }
  71. }
  72. protected boolean isArrow(ItemStack stack)
  73. {
  74. return stack.getItem() instanceof ItemArrow;
  75. }
  76. /**
  77. * Called when the player stops using an Item (stops holding the right mouse button).
  78. */
  79. public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
  80. {
  81. if (entityLiving instanceof EntityPlayer)
  82. {
  83. EntityPlayer entityplayer = (EntityPlayer)entityLiving;
  84. boolean flag = entityplayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0;
  85. ItemStack itemstack = this.findAmmo(entityplayer);
  86. int i = this.getMaxItemUseDuration(stack) - timeLeft;
  87. i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, entityplayer, i, !itemstack.isEmpty() || flag);
  88. if (i < 0) return;
  89. if (!itemstack.isEmpty() || flag)
  90. {
  91. if (itemstack.isEmpty())
  92. {
  93. itemstack = new ItemStack(Items.ARROW);
  94. itemstack.damageItem(1, entityplayer);
  95. }
  96. float f = getArrowVelocity(i);
  97. if ((double)f >= 0.1D)
  98. {
  99. boolean flag1 = entityplayer.capabilities.isCreativeMode || (itemstack.getItem() instanceof ItemArrow && ((ItemArrow) itemstack.getItem()).isInfinite(itemstack, stack, entityplayer));
  100. if (!worldIn.isRemote)
  101. {
  102. EntityGrenade entitysnowball = new EntityGrenade(worldIn, entityplayer);
  103. entitysnowball.setHeadingFromThrower(entityplayer, entityplayer.rotationPitch, entityplayer.rotationYaw, 0.0F, 1.5F, 1.0F);
  104. worldIn.spawnEntity(entitysnowball);
  105. 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);
  106. if (!flag1 && !entityplayer.capabilities.isCreativeMode)
  107. {
  108. itemstack.shrink(1);
  109.  
  110. if (itemstack.isEmpty())
  111. {
  112. entityplayer.inventory.deleteStack(itemstack);
  113. }
  114. }
  115. entityplayer.addStat(StatList.getObjectUseStats(this));
  116. }
  117. }
  118. }
  119. }
  120.  
  121. }
  122. /**
  123. * Gets the velocity of the arrow entity from the bow's charge
  124. */
  125. public static float getArrowVelocity(int charge)
  126. {
  127. float f = (float)charge / 20.0F;
  128. f = (f * f + f * 2.0F) / 3.0F;
  129. if (f > 1.0F)
  130. {
  131. f = 1.0F;
  132. }
  133. return f;
  134. }
  135. /**
  136. * How long it takes to use or consume an item
  137. */
  138. public int getMaxItemUseDuration(ItemStack stack)
  139. {
  140. return 72000;
  141. }
  142. /**
  143. * returns the action that specifies what animation to play when the items is being used
  144. */
  145. public EnumAction getItemUseAction(ItemStack stack)
  146. {
  147. return EnumAction.BOW;
  148. }
  149. public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
  150. {
  151. ItemStack itemstack = playerIn.getHeldItem(handIn);
  152. boolean flag = !this.findAmmo(playerIn).isEmpty();
  153. ActionResult<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, worldIn, playerIn, handIn, flag);
  154. if (ret != null) return ret;
  155. if (!playerIn.capabilities.isCreativeMode && !flag)
  156. {
  157. return flag ? new ActionResult(EnumActionResult.PASS, itemstack) : new ActionResult(EnumActionResult.FAIL, itemstack);
  158. }
  159. else
  160. {
  161. playerIn.setActiveHand(handIn);
  162. return new ActionResult(EnumActionResult.SUCCESS, itemstack);
  163. }
  164. }
  165. /**
  166. * Return the enchantability factor of the item, most of the time is based on material.
  167. */
  168. public int getItemEnchantability()
  169. {
  170. return 1;
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment