Jump to content

Recommended Posts

Posted

Just a simple question, if I use an Item how can I get that to reduce the data value for another item which is somewhere in the players inventory?

    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
    	if(par3EntityPlayer.inventory.hasItem(SaberMod.blasterBattery.shiftedIndex))
    	{
    		EntityBlast blast = new EntityBlast(par2World, par3EntityPlayer);
    		if (!par2World.isRemote)
        	{
    			par2World.spawnEntityInWorld(blast);
        	}
    	}
        return par1ItemStack;
    }

I have that to check if the play has the Item, but I need another check to see if the item (SaberMod.blasterBattery)'s data value is above 0. How would I do this? I can't see a way to do it from par3EntityPlayer.inventory

Posted

Fixed it, possibly, maybe not the best method but it seems like it would work;

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
	boolean hasBattery = par3EntityPlayer.inventory
			.hasItemStack(new ItemStack(SaberMod.blasterBattery, 1, 1))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 2))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 3))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 4))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 5))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 6))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 7))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, )
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 9))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 10))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 11))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 12))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 13))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 14))
			|| par3EntityPlayer.inventory.hasItemStack(new ItemStack(
					SaberMod.blasterBattery, 1, 15));
    	if(hasBattery)
    	{
    		EntityBlast blast = new EntityBlast(par2World, par3EntityPlayer);
    		if (!par2World.isRemote)
        	{
    			par2World.spawnEntityInWorld(blast);
        	}
    	}
        return par1ItemStack;
    }

Posted

After yet more improvement I've come up with this, and it's tested to be working, although it is quite a large method for something I deem quite simple.

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
	boolean hasBattery;

	List<ItemStack> batteries;
	List<Integer> slots;
	batteries = new ArrayList();
	slots = new ArrayList();

	for(int x = 0; x < par3EntityPlayer.inventory.getSizeInventory(); x++)
	{
		if(par3EntityPlayer.inventory.getStackInSlot(x) != null && par3EntityPlayer.inventory.getStackInSlot(x).getItem().shiftedIndex == SaberMod.blasterBattery.shiftedIndex)
		{
			batteries.add(par3EntityPlayer.inventory.getStackInSlot(x));
			slots.add(x);
		}
	}

	hasBattery = batteries.size()!= 0;

    	if(hasBattery)
    	{
    		for(int x = 0; x < batteries.size(); x++)
    		{
    			if(batteries.get(x).getItemDamage() != 0)
    			{
    				EntityBlast blast = new EntityBlast(par2World, par3EntityPlayer);
    				if (!par2World.isRemote)
    				{
    					par2World.spawnEntityInWorld(blast);
    				}
    				par3EntityPlayer.inventory.getStackInSlot(x).setItemDamage(par3EntityPlayer.inventory.getStackInSlot(x).getItemDamage() - 1);
    				break;
    			}
    		}
    		
    	}
        return par1ItemStack;
    }

 

If people can reduce the size of the method down I would be very grateful.

Posted

Fixed it, here's a fix for future reference;

private ItemStack getInventoryFirstItemStack(EntityPlayer par2EntityPlayer)
    {
        for (int var1 = 0; var1 < par2EntityPlayer.inventory.mainInventory.length; ++var1)
        {
            if (par2EntityPlayer.inventory.mainInventory[var1] != null && par2EntityPlayer.inventory.mainInventory[var1].itemID == SaberMod.blasterBattery.shiftedIndex)
            {
            	if(par2EntityPlayer.inventory.getStackInSlot(var1).getItemDamage() > 0)
            	{
            		return par2EntityPlayer.inventory.getStackInSlot(var1);
            	}
            }
        }
        
        return null;
    }
    
    private void getSoundToPlay(EntityPlayer par2EntityPlayer)
    {
    	ItemStack stack = null;
        for (int var1 = 0; var1 < par2EntityPlayer.inventory.mainInventory.length; ++var1)
        {
            if (par2EntityPlayer.inventory.mainInventory[var1] != null && par2EntityPlayer.inventory.mainInventory[var1].itemID == SaberMod.blasterBattery.shiftedIndex)
            {
            		stack = par2EntityPlayer.inventory.getStackInSlot(var1);
            }
        }
        
        if(stack != null)
        {
        	if(stack.getItemDamage() > 0)
        	{
        		sendSoundPacket(4);
        	}
        	else
        	{
        		sendSoundPacket(3);
        	}
        }
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
    	if(getInventoryFirstItemStack(par3EntityPlayer) != null)
    	{
    		EntityBlast blast = new EntityBlast(par2World, par3EntityPlayer);
    		if (!par2World.isRemote)
    		{
    			par2World.spawnEntityInWorld(blast);
    		}
    		getSoundToPlay(par3EntityPlayer);
    		getInventoryFirstItemStack(par3EntityPlayer).setItemDamage(getInventoryFirstItemStack(par3EntityPlayer).getItemDamage() - 1);
    	}
    	return par1ItemStack;
    }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.