Advertisement
Camellias_

ItemRevolver.java

Jul 6th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.52 KB | None | 0 0
  1. package items;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import camellias_.fallout.Reference;
  6. import net.minecraft.creativetab.CreativeTabs;
  7. import net.minecraft.enchantment.EnchantmentHelper;
  8. import net.minecraft.entity.EntityLivingBase;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.entity.projectile.EntityArrow;
  11. import net.minecraft.init.Enchantments;
  12. import net.minecraft.init.Items;
  13. import net.minecraft.init.SoundEvents;
  14. import net.minecraft.item.EnumAction;
  15. import net.minecraft.item.IItemPropertyGetter;
  16. import net.minecraft.item.Item;
  17. import net.minecraft.item.ItemArrow;
  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.  
  29. public class ItemRevolver extends Item {
  30.    
  31.     public ItemRevolver() {
  32.         setUnlocalizedName(Reference.FalloutItems.REVOLVER.getUnlocalizedName());
  33.         setRegistryName(Reference.FalloutItems.REVOLVER.getRegistryName());
  34.         maxStackSize = 1;
  35.         this.setMaxDamage(384);
  36.         this.setCreativeTab(CreativeTabs.COMBAT);
  37.  
  38.         {
  39.            
  40.         }
  41.     }
  42.  
  43.     private ItemStack findAmmo(EntityPlayer player)
  44.     {
  45.         if (this.isArrow(player.getHeldItem(EnumHand.OFF_HAND)))
  46.         {
  47.             return player.getHeldItem(EnumHand.OFF_HAND);
  48.         }
  49.         else if (this.isArrow(player.getHeldItem(EnumHand.MAIN_HAND)))
  50.         {
  51.             return player.getHeldItem(EnumHand.MAIN_HAND);
  52.         }
  53.         else
  54.         {
  55.             for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
  56.             {
  57.                 ItemStack itemstack = player.inventory.getStackInSlot(i);
  58.  
  59.                 if (this.isArrow(itemstack))
  60.                 {
  61.                     return itemstack;
  62.                 }
  63.             }
  64.  
  65.             return ItemStack.EMPTY;
  66.         }
  67.     }
  68.  
  69.     protected boolean isArrow(ItemStack stack)
  70.     {
  71.         return stack.getItem() instanceof ItemArrow;
  72.     }
  73.  
  74.     /**
  75.      * Called when the player stops using an Item (stops holding the right mouse button).
  76.      */
  77.     public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
  78.     {
  79.         if (entityLiving instanceof EntityPlayer)
  80.         {
  81.             EntityPlayer entityplayer = (EntityPlayer)entityLiving;
  82.             boolean flag = entityplayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0;
  83.             ItemStack itemstack = this.findAmmo(entityplayer);
  84.  
  85.             int i = this.getMaxItemUseDuration(stack) - timeLeft;
  86.             i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, entityplayer, i, !itemstack.isEmpty() || flag);
  87.             if (i < 0) return;
  88.  
  89.             if (!itemstack.isEmpty() || flag)
  90.             {
  91.                 if (itemstack.isEmpty())
  92.                 {
  93.                     itemstack = new ItemStack(Items.ARROW);
  94.                 }
  95.  
  96.                 float f = getArrowVelocity(i);
  97.  
  98.                 if ((double)f >= 0.1D)
  99.                 {
  100.                     boolean flag1 = entityplayer.capabilities.isCreativeMode || (itemstack.getItem() instanceof ItemArrow && ((ItemArrow) itemstack.getItem()).isInfinite(itemstack, stack, entityplayer));
  101.  
  102.                     if (!worldIn.isRemote)
  103.                     {
  104.                         ItemArrow itemarrow = (ItemArrow)((ItemArrow)(itemstack.getItem() instanceof ItemArrow ? itemstack.getItem() : Items.ARROW));
  105.                         EntityArrow entityarrow = itemarrow.createArrow(worldIn, itemstack, entityplayer);
  106.                         entityarrow.setAim(entityplayer, entityplayer.rotationPitch, entityplayer.rotationYaw, 0.0F, f * 3.0F, 1.0F);
  107.  
  108.                         if (f == 1.0F)
  109.                         {
  110.                             entityarrow.setIsCritical(true);
  111.                         }
  112.  
  113.                         stack.damageItem(0, entityplayer);
  114.  
  115.                         worldIn.spawnEntity(entityarrow);
  116.                     }
  117.  
  118.                     worldIn.playSound((EntityPlayer)null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.ENTITY_FIREWORK_LARGE_BLAST , SoundCategory.PLAYERS, 2.0F, 2.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.      * Gets the velocity of the arrow entity from the bow's charge
  138.      */
  139.     public static float getArrowVelocity(int charge)
  140.     {
  141.         float f = (float)charge / 0.0F;
  142.         f = (f * f + f * 3.0F) / 3.0F;
  143.  
  144.         if (f > 1.0F)
  145.         {
  146.             f = 1.0F;
  147.         }
  148.  
  149.         return f;
  150.     }
  151.  
  152.     /**
  153.      * How long it takes to use or consume an item
  154.      */
  155.     public int getMaxItemUseDuration(ItemStack stack)
  156.     {
  157.         return 72000;
  158.     }
  159.  
  160.     /**
  161.      * returns the action that specifies what animation to play when the items is being used
  162.      */
  163.     public EnumAction getItemUseAction(ItemStack stack)
  164.     {
  165.         return EnumAction.BOW;
  166.     }
  167.  
  168.     /**
  169.      * Called when the equipped item is right clicked.
  170.      */
  171.     public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
  172.     {
  173.         ItemStack itemstack = playerIn.getHeldItem(handIn);
  174.         boolean flag = !this.findAmmo(playerIn).isEmpty();
  175.  
  176.         ActionResult<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, worldIn, playerIn, handIn, flag);
  177.         if (ret != null) return ret;
  178.  
  179.         if (!playerIn.capabilities.isCreativeMode && !flag)
  180.         {
  181.             return flag ? new ActionResult(EnumActionResult.PASS, itemstack) : new ActionResult(EnumActionResult.FAIL, itemstack);
  182.         }
  183.         else
  184.         {
  185.             playerIn.setActiveHand(handIn);
  186.             return new ActionResult(EnumActionResult.SUCCESS, itemstack);
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement