Solved How to get rid of InventoryCreative error

Discussion in 'Plugin Development' started by michael566, Oct 24, 2014.

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

    michael566

    Hi so I have the following code:

    Code:java
    1. @EventHandler
    2. public void onInventorymove(InventoryClickEvent e) {
    3. if (e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.GOLD + "Quest Pickaxe")) {
    4. e.setCancelled(true);
    5. }
    6.  
    7. }


    If I click or put a block in my inventory I get a NPE.

    I tried doing something like this:

    Code:java
    1. if (e.getCurrentItem() == null) {
    2. reutrn;
    3. }


    but it still gives me and error.
    Any suggestions?
     
  2. Offline

    WesJD

    michael566
    Try this:
    Code:java
    1. public void onClick(InventoryClickEvent e) {
    2. ItemStack clicked = e.getCurrentItem();
    3. String cName = clicked.getItemMeta().getDisplayName();
    4. if(clicked.getType() == Material.IRON_PICKAXE && cName.equals(ChatColor.RED + "Quest Pickaxe") {
    5. e.setCancelled(true);
    6. }
    7. }


    Also, have you tried putting in some debug messages?

    EDIT:

    Could you show us the NPE?
     
  3. Offline

    Unica

    Code:java
    1. if(theItem == null || theItem.getType() == Material.AIR) return;
    2. if(!(theItem.hasItemMeta() || theItem.getItemMeta().hasDisplayName())) return;


    If you're using multiple plugins / events, don't return, use
    Code:java
    1. if(item != null && item.getType() == Material.YOURITEM){
    2. if(item.hasItemMeta() && item.getItemMeta().hasDisplayName()){
    3. //stuff
    4. }
    5. }
     
  4. Offline

    michael566

    WesJD Thanks it worked. :)
     
  5. Offline

    Unica

    WesJD

    You're making a variable w/o checking if the item actual has a DisplayName lol.
    And you're guessing the player doesn't click outside of the inventory, look through your variables, because they don't solve anything. He's getting an NPE because he doesn't checks if the item has a displayname.

    fireblast709

    Typo :rolleyes:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 27, 2017
  6. Offline

    fireblast709

    Unica the first example needs to be &&, not || :p.
    Code:java
    1. if(!stack.hasItemMeta() || !stack.getItemMeta().hasDisplayName())
    becomes
    Code:java
    1. if(!(stack.hasItemMeta() && stack.getItemMeta().hasDisplayName()))
    Your method will try to check hasDisplayName() with no ItemMeta, since if(false || x) could still be true if x is true, hence Java will still check x.

    (for a more in-depth explanation, you are applying De Morgan's laws here)
     
  7. Offline

    WesJD

    Unica
    He wanted to check when a player clicked on an item with a certain name, that's what I gave him. Other things you stated might be needed in the future of what he does, but he doesn't state them to be needed here?
     
  8. Offline

    fireblast709

    WesJD your version will throw a neat NullPointerException if the player clicks on an empty slot, or an ItemStack without name.
     
  9. Offline

    WesJD

    fireblast709
    Oh, I'll fix it in a bit, currently I'm on mobile.

    michael566
    If you're able to, use Unica 's examples to create a fix.

    Fix(Using Unica 's code):
    Code:java
    1. @EventHandler
    2. public void onClick(InventoryClickEvene e) {
    3. ItemStack clicked = e.getCurrentItem();
    4. if(clicked != null && clicked.getType() == Material.IRON_PICKAXE) {
    5. if(clicked.hasItemMeta() && clicked.getItemMeta().hasDisplayName() {
    6. if(ChatColor.stripColor(clicked.getItemMeta().getDisplayName()).equals("Quest Pickaxe")) {
    7. e.setCancelled(true);
    8. }
    9. }
    10. }
    11. }
     
Thread Status:
Not open for further replies.

Share This Page