Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.hsyyid.starwarsbattlecraft.items.firearms;
- import io.github.hsyyid.starwarsbattlecraft.entities.EntityBullet;
- import net.minecraft.creativetab.CreativeTabs;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.world.World;
- import java.lang.reflect.Constructor;
- public class ItemFirearm extends Item
- {
- public Item ammoItem;
- public int bulletDamage;
- public int clipRounds;
- public int remainingBullets;
- public Class<EntityBullet> bulletClass;
- public ItemFirearm(Item ammoItem, int bulletDamage, int clipRounds, Class<EntityBullet> bulletClass)
- {
- this.ammoItem = ammoItem;
- this.bulletDamage = bulletDamage;
- this.clipRounds = clipRounds;
- this.bulletClass = bulletClass;
- this.setCreativeTab(CreativeTabs.tabCombat);
- this.setMaxStackSize(1);
- this.setMaxDamage(1000);
- }
- public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
- {
- if (playerIn.capabilities.isCreativeMode || this.canDamageAmmo(worldIn, playerIn))
- {
- worldIn.playSoundAtEntity(playerIn, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
- if (!worldIn.isRemote)
- {
- try
- {
- if (bulletClass.getConstructor() != null)
- {
- Constructor<EntityBullet> bulletConstructor = bulletClass.getConstructor(new Class[] { World.class, EntityLivingBase.class });
- EntityBullet bullet = bulletConstructor.newInstance(new Object[] { worldIn, (EntityLivingBase) playerIn });
- bullet.damage = this.bulletDamage;
- worldIn.spawnEntityInWorld(bullet);
- itemStackIn.damageItem(1, playerIn);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- return itemStackIn;
- }
- return itemStackIn;
- }
- public boolean canDamageAmmo(World worldIn, EntityPlayer playerIn)
- {
- if (this.remainingBullets > 0)
- {
- this.remainingBullets--;
- return true;
- }
- else if (playerIn.inventory.hasItem(this.ammoItem))
- {
- for (ItemStack itemStack : playerIn.inventory.mainInventory)
- {
- if (itemStack != null && itemStack.getItem() != null && Item.getIdFromItem(this.ammoItem) == Item.getIdFromItem(itemStack.getItem()))
- {
- playerIn.inventory.consumeInventoryItem(itemStack.getItem());
- this.remainingBullets = clipRounds;
- // TODO: If wanted, add Reload Animation Here.
- this.remainingBullets--;
- return true;
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement