Solved Checking if item in hand isn't null

Discussion in 'Plugin Development' started by OPsteffOP, Mar 13, 2016.

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

    OPsteffOP

    Hello,

    I'm trying to check if the item in the players hand isn't null.

    I'm using this:
    Code:
    if(p.getItemInHand() != null){
    It doesn't work & it doesn't give any errors!

    Can someone help me please,
    - OPsteffOP
     
  2. Offline

    87pen

    What are you trying to achieve and what is the problem?
     
  3. Offline

    mine-care

    @OPsteffOP what the piece of code you provided does, is exactly that.

    "Doesnt work" is very broad, mabe you need to tell us what the behaviour is, and when it happends and mabe provide some more code.
    Laslty, if it doesnt error, how do you know it doesnt work because of the null check?
     
  4. Offline

    Zombie_Striker

    @OPsteffOP
    Can you explain what "doesn't work" means? How do you know it does not work?

    double ninja'd
     
  5. Offline

    OPsteffOP

    I'm trying to check if the item in the players hand isn't null (nothing in his hand).
    I'm using this:
    Code:
    if(p.getItemInHand() != null){
    But if I'm doing to command in-game with nothing in my hand it DOESN'T give the error message:
    Code:
    }else{
    p.sendMessage(ChatColor.RED + "You don't have an item in your hand!");
    }
    How can I fix that it will give the error message if my hand is empty?
    - OPsteffOP
     
    Last edited: Mar 13, 2016
  6. Offline

    ChristianWP3579

    Try this:

    Code:
    if (p.getItemInHand().getType() != null) {
        // code
    } else {
        // code
    }
     
  7. Offline

    OPsteffOP

    Still doesn't work :/
    But thanks for trying to help me!
     
  8. @OPsteffOP
    Did you register your command in plugin.yml? I can't see anything obvious that would be wrong in your code, you could try checking if the item's type isn't AIR.
    Code:
    if(item != null && item.getType() != Material.AIR) {
        //
    }
     
    OPsteffOP likes this.
  9. Offline

    OPsteffOP

    OMG yeh! I forgot to check if the type isn't AIR!
    Thank you very much mate! :)
     
  10. Offline

    ChristianWP3579

    Perhaps this method will work:

    Code:
    ItemStack item = p.getItemInHand();
        
    if (item != null || item.getTypeId() != 0) {
        // code
    } else {
        // code
    }
    --Edit--

    Nvm, I didn't realize the problem was solved until after I posted.
     
Thread Status:
Not open for further replies.

Share This Page