Advertisement
Camellias_

ItemRevolver.java

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