Advertisement
Zapfox

Untitled

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