Inventory not updating on p.setItemInHand()

Discussion in 'Plugin Development' started by caldabeast, Dec 26, 2012.

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

    caldabeast

    I am attempting to make it so that when you right-click with a certain item in your hand, it adds a certain amount of health to your player's health. Everything is working properly, except that it will not remove the item from their inventory. Code follows:

    Code:java
    1. if(!doHunger){
    2. int amountToHeal = f.getHealthBoost();
    3. int health = p.getHealth() + amountToHeal;
    4. if(health > 20) health = 20;
    5. p.setHealth(health);
    6. System.out.println(p.getItemInHand());
    7. p.setItemInHand(new ItemStack(Material.AIR));
    8. p.updateInventory();
    9. System.out.println(p.getItemInHand());
    10. }
    output to console:
    Code:
     
    23:23:29 [INFO] ItemStack{GRILLED_PORK x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=Instant-Eat Cooked Pork, lore=[Instantly heals 3.0 Hearts,  0.9729892635818012], enchants={DIG_SPEED=1}}}
    23:23:29 [INFO] ItemStack{AIR x 0}
     
    
    The output i what I expected. It sets the item to air.
    However, the output does not match what happens in-game. The player's inventory does not update and the GRILLED_PORK remains in the inventory.

    Any ideas on why this would occur?

    [comment redacted]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    fireblast709

    what about setting the item using the inventory, like setting the slot yourself
     
  3. try using Player.updateInventory() to see if it works after that (methode is depredated, but still useable)
    If it works, then bukkit/minecraft didn't notice inventory was updated
    if it wont work, then you made a mistake
     
  4. Offline

    fireblast709

    He already does that in the OP
     
  5. Offline

    william9518

    try logging it instead
    getLogger().info()
    instead of
    System.out.println
    I need to see what that says after
     
  6. Offline

    tommycake50

    you dont take items like that T.T
    p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    Problem solved or no?
     
  7. Offline

    caldabeast

    Setting it to air works for me because I made it so it wouldn't stack :p
    I'll try the line you gave really fast, though.

    That does not work.
    I still get the same result using this following, new code.
    Code:java
    1.  
    2. p.setHealth(health);
    3. getLogger().info(p.getItemInHand().toString());
    4. item.setAmount(item.getAmount() - 1);
    5. p.setItemInHand(item);
    6. p.updateInventory();
    7. getLogger().info(p.getItemInHand().toString());
    8.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  8. Offline

    fireblast709

    if the amount is 1 and you have to remove 1 of it, you should set it to AIR
     
  9. Offline

    caldabeast

    The output to console says that it has been set to air, but they player's inventory doesn't get updated, even though I use p.updateInventory()
     
  10. Offline

    ImDeJay

    have you tried to just remove the pork from the players hand

    example:

    Code:
    ItemStack rItem = new ItemStack(320, 1);
     
    player.getInventory().removeItem(rItem);
    player.updateInventory();
     
  11. Offline

    tommycake50

    so does it change just not update?
    or does it not change?
    is this in an event? if so what is the event priority?
     
  12. Offline

    caldabeast

    It is in an event, and everything in the event works *except* removing the item from their inventory.
    It is supposed to run onPlayerInteract, check if right-click, check if a food item, and if so, heal the player accordingly and then consume the item. The last step is the problem
     
  13. Offline

    Intangir

    are you on 1.4.6, i am noticing a seperate issue trying to update the count in hand while processing a block place.. it doesn't work
     
  14. Offline

    Cowboys1919

    Yes I think this is a bug. Trying to set stuff inside of RIGHT_CLICK_BLOCK was giving me problems but immediately worked when i changed it to LEFT_CLICK_BLOCK
     
Thread Status:
Not open for further replies.

Share This Page