Jump to content

Help with changing Stack Size


6269329

Recommended Posts

I am making a mod and I need the item to disappear when used. I have tried Durability and changing the stack size, but all the things I have found do nothing. Any help would be appreciated.

 

	
public boolean onItemUse(ItemStack itemStack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10)
    {
        if(player.getCurrentEquippedItem() != null)
        {
        	ItemStack currentItem = player.getCurrentEquippedItem();
         
        	if(currentItem.getItem() == this)
        	{
        		player.addPotionEffect((new PotionEffect(Potion.regeneration.getId(), 100, 1))); //4 seconds
        		int newStackSize = player.inventory.mainInventory[player.inventory.currentItem].stackSize - 1;
        		//itemStack.stackSize-- doesn't work either
        		currentItem.stackSize = currentItem.stackSize - 1;
        		
        	}
        }
        return true;
    }

Link to comment
Share on other sites

You look to be on the right track, I know I was going to ask this question on here, but with some playing around I figured it out myself.

 

here is the onItemRightClick from one of my instant consumable items.

 

@Override
public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) 
{
	int type = item.getItemDamage();
	ExtendPlayerRotManaStam props = ExtendPlayerRotManaStam.get(player);
	if (type == 0)
	{
		props.regenMana(40);					
		--item.stackSize;
	}
	else if (type == 1)
	{
		props.regenMana(10);
		--item.stackSize;
	}
	return item;
}

 

type is the item damage because this item has subtypes, but you have to return the altered stack, not just true;

 

and your code is a bit overkill, remember it passes its own instance in that method so no need to "getCurrentEquipedItem" and since you're using it no need to check if they are holding it (If you were making an onUpdate() effect and needed to hold it to activate them then yes you'd need to check)

 

and since a stacksize is a public int you can just use the "--" operator to decrease the value no need to go farther with getting the current stacksize - 1

 

 

Link to comment
Share on other sites

setting the stack size on items in inventory dosnt work, you need to change the item compleatly

 

however there is a function to decrease the size of a stack

player.inventory.decrStackSize(slotnumber, amounttodecreaseby);
[\code]

if you need to set the stack size do this

[code]
	ItemStack itm=player.inventory.getStackInSlot(slotnumber);
	itm.stackSize=newamount;
	player.inventory.setInventorySlotContents(slotnumber,itm.copy());
[\code]

notice the use of itm.copy() , thats makes a new stack

the reason you have to replace the item completely, is that it compares the new item with the existing one
so therefore if you try to put the item you took from the inventory back, it gets compared with itself, and dosnt get updated



Link to comment
Share on other sites

Thanks to Loon, I now have it working so that It will remove the item. However, for some reason it only works if the item is alone and not in an actual stack. Thanks for all the quick replies!

 

public boolean onItemUse(ItemStack itemStack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10)
    {
        if(player.getCurrentEquippedItem() != null)
        {
                ItemStack currentItem = player.getCurrentEquippedItem();
        	player.addPotionEffect((new PotionEffect(Potion.regeneration.getId(), 100, 1))); //4 seconds
        	player.inventory.decrStackSize(player.inventory.currentItem, 1);
        }
        return true;
    }

Link to comment
Share on other sites

nvm, I did the thing!

 


public boolean onItemUse(ItemStack itemStack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10)
    {
        if(player.getCurrentEquippedItem() != null)
        {

        	ItemStack currentItem = player.getCurrentEquippedItem();

        	player.addPotionEffect((new PotionEffect(Potion.regeneration.getId(), 100, 1))); //4 seconds of Regen II

        	if (currentItem.stackSize == 1)
        	{
        		player.inventory.decrStackSize(player.inventory.currentItem, 1);
        	}
        	else 
        	{
        		ItemStack itm=player.inventory.getStackInSlot(player.inventory.currentItem);
    		
        		player.inventory.decrStackSize(player.inventory.currentItem, 1);
    		
        		player.inventory.setInventorySlotContents(player.inventory.currentItem,itm.copy());
        	}
        }
        return true;
    }

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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