Advertisement
Zapfox

Untitled

Dec 4th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 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. System.out.println(this.findAmmo(entityplayer).toString());
  112.  
  113. int i = this.getMaxItemUseDuration(stack) - timeLeft;
  114. i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, entityplayer, i, !itemstack.isEmpty() || flag);
  115. if (i < 0) return;
  116.  
  117. if (!itemstack.isEmpty() || flag)
  118. {
  119. if (itemstack.isEmpty())
  120. {
  121. itemstack = new ItemStack(ItemInit.BRONZE_ARROW);
  122. }
  123.  
  124. float f = getArrowVelocity(i);
  125.  
  126. if ((double)f >= 0.1D)
  127. {
  128. boolean flag1 = entityplayer.capabilities.isCreativeMode || (itemstack.getItem() instanceof ArrowBronze && ((ArrowBronze) itemstack.getItem()).isInfinite(itemstack, stack, entityplayer));
  129.  
  130. if (!worldIn.isRemote)
  131. {
  132. ArrowBronze itemarrow = (ArrowBronze)(itemstack.getItem() instanceof ArrowBronze ? itemstack.getItem() : ItemInit.BRONZE_ARROW);
  133. BronzeArrow entityarrow = itemarrow.createArrow(worldIn, itemstack, entityplayer);
  134. entityarrow.shoot(entityplayer, entityplayer.rotationPitch, entityplayer.rotationYaw, 0.0F, f * 3.0F, 1.0F);
  135.  
  136. if (f == 1.0F)
  137. {
  138. entityarrow.setIsCritical(true);
  139. }
  140.  
  141. int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, stack);
  142.  
  143. if (j > 0)
  144. {
  145. entityarrow.setDamage(entityarrow.getDamage() + (double)j * 0.5D + 0.5D);
  146. }
  147.  
  148. int k = EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, stack);
  149.  
  150. if (k > 0)
  151. {
  152. entityarrow.setKnockbackStrength(k);
  153. }
  154.  
  155. worldIn.spawnEntity(entityarrow);
  156. }
  157.  
  158. 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);
  159.  
  160. if (!flag1 && !entityplayer.capabilities.isCreativeMode)
  161. {
  162. itemstack.shrink(1);
  163.  
  164. if (itemstack.isEmpty())
  165. {
  166. entityplayer.inventory.deleteStack(itemstack);
  167. }
  168. }
  169.  
  170. entityplayer.addStat(StatList.getObjectUseStats(this));
  171. }
  172. }
  173. }
  174. }
  175.  
  176. /**
  177. * Gets the velocity of the arrow entity from the bow's charge
  178. */
  179. public static float getArrowVelocity(int charge)
  180. {
  181. float f = (float)charge / 15.0F;
  182. f = (f * f + f * 2.0F) / 3.0F;
  183.  
  184. if (f > 1.0F)
  185. {
  186. f = 1.0F;
  187. }
  188.  
  189. return f;
  190. }
  191.  
  192. /**
  193. * How long it takes to use or consume an item
  194. */
  195. public int getMaxItemUseDuration(ItemStack stack)
  196. {
  197. return 72000;
  198. }
  199.  
  200. /**
  201. * returns the action that specifies what animation to play when the items is being used
  202. */
  203. public EnumAction getItemUseAction(ItemStack stack)
  204. {
  205. return EnumAction.BOW;
  206. }
  207.  
  208. /**
  209. * Called when the equipped item is right clicked.
  210. */
  211. public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
  212. {
  213. ItemStack itemstack = playerIn.getHeldItem(handIn);
  214. boolean flag = !this.findAmmo(playerIn).isEmpty();
  215.  
  216. ActionResult<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, worldIn, playerIn, handIn, flag);
  217. if (ret != null) return ret;
  218.  
  219. if (!playerIn.capabilities.isCreativeMode && !flag)
  220. {
  221. return flag ? new ActionResult(EnumActionResult.PASS, itemstack) : new ActionResult(EnumActionResult.FAIL, itemstack);
  222. }
  223. else
  224. {
  225. playerIn.setActiveHand(handIn);
  226. return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
  227. }
  228. }
  229.  
  230. /**
  231. * Return the enchantability factor of the item, most of the time is based on material.
  232. */
  233. public int getItemEnchantability()
  234. {
  235. return 0;
  236. }
  237.  
  238.  
  239. @Override
  240. public void registerModels()
  241. {
  242. OSMain.proxy.registerItemRenderer(this, 0, "inventory");
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement