Remove item in hand

Discussion in 'Plugin Development' started by mike546378, Jul 16, 2014.

Thread Status:
Not open for further replies.
  1. This should be really simple, right?.. What I am trying to do is make it so if the player clicks with a Baked Potato it removes 1 from the ItemStack. The issue comes when there is only 1 item left;
    Code:java
    1. item.setAmount(Stackgrenade.getAmount() - 1);

    no longer works at that point. So I added an if statement to check if there is only 1 item left and remove it. This is the part that is confusing me, it just refuses to work. The code is being executed but the potato just stays there (sometimes it will be removed for a tick and then reappear).
    Here is most of the methods I have tried to make this work, all failed;
    Code:java
    1. item.setAmount(0);

    Code:java
    1. ev.getPlayer.getInventory.setItemInHand(null);

    Code:java
    1. ev.getPlayer.getInventory.setItemInHand(new ItemStack(Material.AIR));

    Code:java
    1. Inventory inv = ev.getPlayer().getInventory();
    2. inv.setItem(ev.getPlayer().getInventory().getHeldItemSlot(), new ItemStack(Material.AIR));
    3. ev.getPlayer().getInventory().clear();
    4. ev.getPlayer().getInventory().setContents(inv.getContents());

    Along with a few other versions of the above code. I have also tried using ev.getPlayer.updateInventory(); after each of them with no luck.
    Any help is greatly appreciated, I just don't know where to go from here.
     
  2. Offline

    JustinsCool15

    I've always used this, where p is the player and pi is the player inventory.

    Code:java
    1. if(p.getItemInHand().getAmount() == 1){
    2. pi.setItem(pi.getHeldItemSlot(), new ItemStack(Material.AIR, 0));
    3. }else{
    4. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    5. }
     
  3. JustinsCool15 That code works perfectly for left clicking with the item but it refuses to remove the last item if you right click with it for some reason. Any ideas? Code I am working with is:
    Code:java
    1. @EventHandler
    2. public void grenade(final PlayerInteractEvent ev){
    3. if(ev.getItem() != null && ev.getPlayer().getItemInHand() != null && GameBase.gameStarted)
    4. if(ev.getItem().getType() == Material.BAKED_POTATO){
    5. ItemStack Stackgrenade = ev.getItem();
    6. if(Stackgrenade.getAmount() == 1){
    7. ev.getPlayer().getInventory().setItem(ev.getPlayer().getInventory().getHeldItemSlot(), new ItemStack(Material.AIR));
    8. }else{
    9. Stackgrenade.setAmount(Stackgrenade.getAmount() - 1);
    10. }



    EDIT:
    It seems that the issue is with the item being edible. Just tried with an ink sack and that worked perfectly
     
  4. Offline

    Niknea

    As I can see, you haven't checked the action, add the following
    Code:
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK{
    //Code
    }
    
    Also, for future times, there is also a isEdible(); method.
     
  5. Offline

    Msrules123

    You could have also made a simple command in the directory that would have been activated, that clears an item, if it is an amount of one. Only the server could execute this code, and it would simply remove the item in hand.
     
Thread Status:
Not open for further replies.

Share This Page