Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

  • Author

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;
    }

  • Author

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.

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.