Guest User

Full Class [ToolBow.java]

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