Solved Weird PlayerInteractEvent error

Discussion in 'Plugin Development' started by MrJossy, May 13, 2015.

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

    MrJossy

    Hey! So... Recently I've been having a lot of problems with the PlayerInteractEvent. Check my code below (example because this happens in all of the "things" I do with this event):

    Code:
    if(a == Action.LEFT_CLICK_AIR || a == Action.LEFT_CLICK_BLOCK){
                if(e.getItem().getType() == Material.DIAMOND_SWORD){
                    p.launchProjectile(Arrow.class, p.getLocation().getDirection().multiply(10));
                    players.add(p.getName());
                }
            }
        }
    Now, my console error happens everytime that my item is not a diamond sword though I tried this:

    Code:
        if(a != Action.LEFT_CLICK_AIR || a != Action.LEFT_CLICK_BLOCK){
           if(e.getItem().getType() == Material.DIAMOND_SWORD){
             return;
           }
         } else if(a == Action.LEFT_CLICK_AIR || a == Action.LEFT_CLICK_BLOCK){
           /* My stuff here */
         }
    
    This bug is very annoying because everytime I do something in minecraft the stack trace shows up in the console (btw it's a Null Pointer Exception).

    Want the Console error? There you go:
    http://pastebin.com/YUAvxdDJ

    Line 44 hu?
    Code:
    if(e.getItem().getType() == Material.DIAMOND_SWORD){ 
    Thanks for reading!
     
  2. Offline

    87pen

    Code:
       public void onPlayerInteract(PlayerInteractEvent event){
           if(event.getItem() != null){
               if(event.getItem().equals(new ItemStack(Material.BOOK))){
                   //Code Here
               }
           }
    }
    You need to check if the Item is null because Minecraft does not return air if your holding nothing it returns null.
     
  3. Offline

    Gater12

    @MrJossy
    Check if the item is null.
     
  4. @MrJossy Another thing: Your logic is completely off here:

    This line is basically "if the action isn't left click air, or isn't left click block, then do something." There's no situation in which this can be false. If the action was LEFT_CLICK_AIR, then the second condition evaluate to true. If the action is LEFT_CLICK_BLOCK, then the first evaluate to true. If it's anything else, then both evaluate to true (except only the first will run due to short-circuit).
     
  5. Offline

    MrJossy

    Would it stay something like this? @87pen @Gater12 @AdamQpzm
    Code:
    if(a == Action.LEFT_CLICK_AIR || a == Action.LEFT_CLICK_BLOCK){
                if(e.getItem().getType() == null){
                    return;
                } else {
                    if(e.getItem().getType() == Material.DIAMOND_SWORD){
                        p.launchProjectile(Arrow.class, p.getLocation().getDirection().multiply(10));
                        players.add(p.getName());
                    }
                }
            }
        }
     
  6. @MrJossy Looks good to me. Technically, because of the return statement, you don't need the "else" there - the return will stop the method executing further. I'd remove it, because it's always nice to remove excess code/indentation. But that's personal preference, what you have should work :)
     
  7. Offline

    MrJossy

    @AdamQpzm Thanks! But when I left click the air with my hand it prints the stack trace again!
     
  8. @MrJossy Whoops, I read it wrong, sorry - it's not the getType() that will be null, it's the item itself - so you want to check e.getItem() not e.getItem().getType() :)
     
    MrJossy likes this.
  9. Offline

    MrJossy

    Thank you so much @AdamQpzm Really helped me a lot!

    ~Solved.
     
Thread Status:
Not open for further replies.

Share This Page