Test whether player has item in hand.

Discussion in 'Plugin Development' started by WiseHollow, Apr 20, 2013.

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

    WiseHollow

    I cant seem to figure out the correct way to see if a player's hand is empty.

    Code:
    if (damager.getInventory().getItemInHand() == new ItemStack(Material.AIR))
    {
        // Empty
    }
    This doesn't seem to work.. any ideas?
     
  2. Don't compare objects with '==', only primitives and enums can be used with that reliably.

    Best way would be:
    Code:
    ItemStack item = // set item in hand here
    
    if(item == null || item.getTypeId() == 0)
    {
        // no item in hand
    }
     
  3. Offline

    3ShotGod

    This is what I used to determine if the player has a CERTAIN item. I'd imagine it would work about the same way.

    Code:
                        if(player.getInventory().getItemInHand().getType().equals(Material.DIAMOND_SWORD)){
     
  4. Offline

    ccrama

    3ShotGod thats exactly the same as doing .getType() == Material.DIAMOND_SWORD. As Digi said above, check if it's null first then continue with more primitive comparisons
     
  5. Offline

    Niknea

    3ShotGod Try something like this
    PHP:
    ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    if(
    player.getInventory().getItemInHand().equals(sword)
    {
    // Code
    }
    Not as efficient as what they are suggesting, as it has one extra line of code, but a bit more "noob friendly".
     
Thread Status:
Not open for further replies.

Share This Page