setAmount()?

Discussion in 'Plugin Development' started by RightLegRed, Jan 17, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Archelaus

    I've been trying to edit the amount of items in a stack. But so far having no luck. I found the "setAmount()" method not to work. Could someone please verify this or tell me if I'm using it wrong?

    I'm basically doing this:

    Code:
    players.getInventory().getItemInHand().setAmount(64)
    --- merged: Jan 17, 2011 11:06 PM ---
    Nevermind. I fixed it myself with:
    Code:
                                ItemStack oldType = new ItemStack(MATERIAL,AMOUNT);
                                players.getInventory().setItemInHand(oldType);
     
  2. For anyone that has this problem:
    Code:
    e.getItem().getItemStack().setAmount(amount);
    e.getItem().setItemStack(e.getItem().getItemStack()); 
    After changing the amount you have to set the stack as itself (I guess it updates this way).
     
  3. Offline

    Ivan

  4. If they did the below it would work without having to create a new stack and manually setting all properties of it.
    players.getInventory().getItemInHand().getItemStack().setAmount(64);
    players.getInventory().getItemInHand().setItemStack(players.getInventory().getItemInHand().getItemStack());
     
  5. Offline

    lycano

    adventuretc getItemInHand returns a new instance of the object held in hand. Thats why you would have to re-set the item held in hand after set the amount via setAmount.

    This is quite useful as you can get the ItemStack modify it without updating the held item and when you are done with it you can then decide "do i want to update the item in hand?" on your own. Otherwise the held Item would flicker or behave strangely. Also it would cause problems when other events are running like "InteractEvent".

    Source:
    See CraftInventoryPlayer#L27
    See CraftItemStack#L60
     
    adventuretc likes this.
Thread Status:
Not open for further replies.

Share This Page