Advertisement
Guest User

Untitled

a guest
Oct 30th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package io.github.hsyyid.starwarsbattlecraft.items.firearms;
  2.  
  3. import io.github.hsyyid.starwarsbattlecraft.entities.EntityBullet;
  4. import net.minecraft.creativetab.CreativeTabs;
  5. import net.minecraft.entity.EntityLivingBase;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.item.Item;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.world.World;
  10.  
  11. import java.lang.reflect.Constructor;
  12.  
  13. public class ItemFirearm extends Item
  14. {
  15.     public Item ammoItem;
  16.     public int bulletDamage;
  17.     public int clipRounds;
  18.     public int remainingBullets;
  19.     public Class<EntityBullet> bulletClass;
  20.  
  21.     public ItemFirearm(Item ammoItem, int bulletDamage, int clipRounds, Class<EntityBullet> bulletClass)
  22.     {
  23.         this.ammoItem = ammoItem;
  24.         this.bulletDamage = bulletDamage;
  25.         this.clipRounds = clipRounds;
  26.         this.bulletClass = bulletClass;
  27.         this.setCreativeTab(CreativeTabs.tabCombat);
  28.         this.setMaxStackSize(1);
  29.         this.setMaxDamage(1000);
  30.     }
  31.  
  32.     public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
  33.     {
  34.         if (playerIn.capabilities.isCreativeMode || this.canDamageAmmo(worldIn, playerIn))
  35.         {
  36.             worldIn.playSoundAtEntity(playerIn, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
  37.  
  38.             if (!worldIn.isRemote)
  39.             {
  40.                 try
  41.                 {
  42.                     if (bulletClass.getConstructor() != null)
  43.                     {
  44.                         Constructor<EntityBullet> bulletConstructor = bulletClass.getConstructor(new Class[] { World.class, EntityLivingBase.class });
  45.                         EntityBullet bullet = bulletConstructor.newInstance(new Object[] { worldIn, (EntityLivingBase) playerIn });
  46.                         bullet.damage = this.bulletDamage;
  47.                         worldIn.spawnEntityInWorld(bullet);
  48.                         itemStackIn.damageItem(1, playerIn);
  49.                     }
  50.                 }
  51.                 catch (Exception e)
  52.                 {
  53.                     e.printStackTrace();
  54.                 }
  55.             }
  56.  
  57.             return itemStackIn;
  58.         }
  59.  
  60.         return itemStackIn;
  61.     }
  62.  
  63.     public boolean canDamageAmmo(World worldIn, EntityPlayer playerIn)
  64.     {
  65.         if (this.remainingBullets > 0)
  66.         {
  67.             this.remainingBullets--;
  68.             return true;
  69.         }
  70.         else if (playerIn.inventory.hasItem(this.ammoItem))
  71.         {
  72.             for (ItemStack itemStack : playerIn.inventory.mainInventory)
  73.             {
  74.                 if (itemStack != null && itemStack.getItem() != null && Item.getIdFromItem(this.ammoItem) == Item.getIdFromItem(itemStack.getItem()))
  75.                 {
  76.                     playerIn.inventory.consumeInventoryItem(itemStack.getItem());
  77.                     this.remainingBullets = clipRounds;
  78.                     // TODO: If wanted, add Reload Animation Here.
  79.                     this.remainingBullets--;
  80.                     return true;
  81.                 }
  82.             }
  83.         }
  84.  
  85.         return false;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement