Guest User

Untitled

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