Solved Remove items in hand

Discussion in 'Plugin Development' started by MordorKing78, Jun 13, 2015.

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

    MordorKing78

    Well, I can't seem to figure it out because it works fine until I just have 1 item in my hand.. It wont remove the last..

    My code:

    Code:
    if(p.getItemInHand().getAmount() > 1) {
                          p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                    } else {
                        p.getItemInHand().setAmount(0);
                    }
     
  2. Offline

    567legodude

    @MordorKing78 I'll be honest, I've had trouble with removing things from players hand, took me many many tries, but this is what works:
    Whatever code completely removes the item from their hand, put that inside of a bukkit task that runs instantly, I don't know why, but for some reason that's pretty much the only reliable way to remove the item from their hand on an interact event.
     
  3. Offline

    MordorKing78

  4. Offline

    567legodude

    @MordorKing78 There are a couple of ways to run the task instantly, you could run a task with a delay of 0, or just run the task instantly, which is what I would do.
    Bukkit.getScheduler().runTask();
     
    MordorKing78 likes this.
  5. Offline

    MordorKing78

    @567legodude I tried it after a 4 tick delay but it wont work.. I also tried 0 ticks and 1 tick..

    EDIT:

    Code:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            public void run() {
                               if(p.getItemInHand().getAmount() > 1) {
                                    p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                               } else {
                                  p.getItemInHand().setAmount(0);
                               }
                            }
                        }, 0);
     
  6. Offline

    567legodude

    @MordorKing78 This is what I did to remove the item in their hand, I mean getItemInHand() is useful, but I like to use ItemStack[] when working with inventories:
    Code:
    Bukkit.getScheduler().runTask(plugin, new Runnable() {
        @Override
        public void run() {
            int slot = player.getInventory().getHeldItemSlot();
            ItemStack[] con = player.getInventory().getContents();
            con[slot] = new ItemStack(Material.AIR);
            player.getInventory().setContents(con);
            player.updateInventory(); // not required, but useful
        }
    });
     
    dsouzamatt likes this.
  7. player#.setItemInHand(Material.AIR) or is it new ItemStack(Material.AIR) forgot :p
     
  8. Just a suggestion, why do you check for more than one item in their hand?? Why not just check for more than 0 and then just have setting it to -1 what it was?
     
  9. Offline

    Zombie_Striker

    The problem is that you can't just remove an item by using .setAmount(). You would need to get the player's inventory and use the .remove(ItemStack) method.
     
  10. Offline

    Monkey_Swag

    Look at your logic. After your else statement, you set the amount of the item in the hand to 0. I can't have 0 apples. What you need to do is remove the item completely. What works for me is setting the item in hand to null
     
  11. Offline

    MordorKing78

    Thanks everyone for the help! Got it to work!
     
Thread Status:
Not open for further replies.

Share This Page