Removing item in hand

Discussion in 'Spigot Plugin Development' started by Epixel, Feb 20, 2017.

  1. I'm trying to remove the item a player has in his hand. However, if he has 2 diamond swords, and he is holding 1 of them, it will remove all of them. How can I make it so it only removes the one he has selected. I'm using 1.7.10.
    Code (Text):

                        player.getInventory().remove(player.getItemInHand());
     
  2. Remove the slot?
     
    • Like Like x 2
  3. It sounds like you're running a for loop that goes through and removes the items. Post this section of your code, it may be as simple as adding a break; statement after the sword is removed so that it won't continue to search the inventory!


    Sent from my iPhone using Tapatalk
     
  4. Code (Text):
    p.setItemInHand(null);
     
    • Like Like x 3
  5. Set item in hand should only affect that one specific item stack in that one specific slot.

    Have you tried:
    ItemStack sword = p.getInventory().getItemInHand();

    p.getInventory().remove(sword);
    ?


    Sent from my iPhone using Tapatalk
     
  6. Test this:
    player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount()-1);
     
  7. This is what you do if the amount is > 1, but if the items amount is 1, this method won't work.
    Can you read? You posted the EXACT same method the OP used.
     
  8. Apparently not; for some reason whilst using tapatalk I didn't notice that he posted any code at all. However; It makes little sense as to why his code would be removing ALL swords instead of the one in hand.. Seems like the issue we should be addressing as to why that is happening.

    Decrementing the amount isn't the right approach either. There is either a problem elsewhere in the OP's code or spigot is taking every item out of his inventory that matches the itemInHand() itemstack which seems like its the wrong thing to do.

    OP what version of spigot are you running?
     
  9. p.getInventory.setItemInhand(new ItemStack(Material.Air));

    Somethin like that ;)
     
  10. Hi, I know this is an old thread, but I wanted to give another response since it still hasn't a definitive response, I needed the same thing and did this (p is the player)


    if (p.getInventory().getItemInHand().getAmount() == 1) {
    p.getInventory().setItemInHand(new ItemStack(Material.AIR));
    } else {
    p.getInventory().getItemInHand().setAmount(p.getInventory().getItemInHand().getAmount()-1);
    }