Solved onPlayerInteract(PlayerInteractEvent event)

Discussion in 'Plugin Development' started by Pixwarron, Jul 2, 2014.

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

    Pixwarron

    Hello, each time a player left clicks with an empty hand I get a NullPointerException at line 5
    this does not happen on leftclicks with a item or block in their hand and rightclicks are fine aswell :(



    Code:java
    1. @EventHandler
    2. public void onPlayerInterract(PlayerInteractEvent event) {
    3. ItemStack is = event.getItem();
    4. Player p = event.getPlayer();
    5. String name = is.getItemMeta().getDisplayName();
    6.  
    7. if (p.getItemInHand().getType() == Material.AIR
    8. || p.getItemInHand() == null) {
    9. return;
    10. }
    11. if (!(p.getItemInHand().hasItemMeta() && p.getItemInHand()
    12. .getItemMeta().hasDisplayName())) {
    13. return;
    14. }
    15. if (name.equalsIgnoreCase(ChatColor.RED + "Choose a Class")
    16. && event.getAction() == Action.RIGHT_CLICK_AIR) {
    17. classGUI(event.getPlayer());
    18. } else {
    19. return;
    20. }
    21. }
     
  2. Thats because the item doesn't have item meta. You need to test this at first:
    Code:Java
    1.  
    2. if (is.hasItemMeta()) {
    3.  
     
  3. Offline

    Pixwarron

    This is not working :(

    Code:java
    1. @EventHandler
    2. public void onPlayerInterract(PlayerInteractEvent event) {
    3. ItemStack is = event.getItem();
    4. Player p = event.getPlayer();
    5.  
    6. if (is.hasItemMeta()) {
    7. String name = is.getItemMeta().getDisplayName();
    8.  
    9. if (p.getItemInHand().getType() == Material.AIR
    10. || p.getItemInHand() == null) {
    11. return;
    12. }
    13. if (!(p.getItemInHand().hasItemMeta() && p.getItemInHand()
    14. .getItemMeta().hasDisplayName())) {
    15. return;
    16. }
    17. if (name.equalsIgnoreCase(ChatColor.RED + "Choose a Class")
    18. && event.getAction() == Action.RIGHT_CLICK_AIR) {
    19. classGUI(event.getPlayer());
    20. } else {
    21. return;
    22. }
    23. }
    24. }
     
  4. Offline

    61352151511

    Try declaring Player p = event.getPlayer() before the itemstack then for the itemstack use

    ItemStack is = p.getInventory().getItemInHand();
     
  5. Offline

    61352151511

    I'm not sure :/ That's the way I normally do it and when I put that in eclipse I didn't get any errors, didn't think about him checking to see if there was an item in the hand or not.
     
    AdamQpzm likes this.
  6. Offline

    Traks

    Check if the item in the player's hand is null before checking if it's type is air and before getting it's display name. By the way, instead of continuously invoking p.getItemInHand(), just use the variable is you created. You could also use p instead of event.getPlayer() on line 17.

    FisheyLP ItemStack#getItemMeta() only returns null when the item's type is air. There is no real need to ItemStack#hasItemMeta() in my opinion.
     
  7. Offline

    Shevchik

    Something is really wrong with the code.
    Check if item is not air and has item meta and display name first.
     
  8. Offline

    AoH_Ruthless

    Pixwarron
    The issue has nothing to do with null item-meta. The displayname is null (i.e default item name = null displayname). You need a null check on the displayname. While a null-check on itemmeta is ok, it's unwarranted in my opinion, to reiterate what Traks said.
     
  9. Offline

    tommyhoogstra

    Code:java
    1. if(!p.getItemInHand().hasItemMeta()) return;
    2. if(p.getItemInHand().getType() == Material.AIR){
    3.  
    4. }


    Start off with that.

    Also, to check for a specific display name, you have to have a .getDisplayName()

    e.g.
    Code:java
    1. if(p.getItemInHand().getDisplayName().contains("Kit Selector"){
    2.  
    3.  
    4. //code
    5.  
    6. }
     
  10. Offline

    Pixwarron

    Thank you very much, I cleaned up the code and this is the final product :D

    Code:java
    1. if (!(p.getItemInHand().hasItemMeta()))
    2. return;
    3. if (p.getItemInHand().getType() == Material.AIR)
    4. return;
    5. String name = is.getItemMeta().getDisplayName();
    6. if (name.equalsIgnoreCase(ChatColor.RED + "Choose a Class")
    7. && event.getAction() == Action.RIGHT_CLICK_AIR) {
    8. classGUI(event.getPlayer());
    9. } else {
    10. return;
    11. }
    12.  
    13. }
     
  11. Offline

    tommyhoogstra

    Add some comments of your own in your code, so you can look back at a later date if needed.
     
Thread Status:
Not open for further replies.

Share This Page