Test For The Name Of A Picked-Up Item?

Discussion in 'Spigot Plugin Development' started by eli.musgrove9, May 20, 2015.

  1. I am trying to stop players from dropping an item named "Guide Book" but it is giving me a long error. I don't know how to get the name of the dropped item if this is incorrect. Any help?

    Code (Text):
    if (e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_RED + "Guide Book")) {
                e.setCancelled(true);
            }
     
  2. That "long error" message is propably the call-stack and the exception message, which is your computer literally telling you what's wrong. That's why you should always read it carefully, and include it in posts about coding problems here ;)

    That being said, the problem will most likely be that your code will produce a NullPointerException if any of the properties is null.
     
    • Agree Agree x 1
  3. I realize it is a NPE, just wondering how to test for the item name...
     
  4. Should probably remove the ChatColor.RED from the string. Also post the stack trace.
     
  5. The display name should indeed be the property you're looking for. If it isn't working, have you tried to output the result of that statement to the console to see what the expected value should be?
     
  6. This also doesn't work. I am using the 1.8 spigot api btw.
     
  7. In what way does outputting a debug message not work?
     
  8. It outputs the same error.
     
  9. Okay you should always check first that your item contains ItemMeta. If it doesn't then you will get an exception thrown that you don't have a big enough mitt to catch.
    Code (Text):
    if(e.getItemDrop().getItemStack().hasItemMeta()){ //we need this line to ensure that our item contains ItemMeta
        if (e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_RED + "Guide Book")) {
                e.setCancelled(true);
            }

    }
    You can't just assume that your "e" will have some meta data associated with it. If it doesnt then the getItemMeta() method will throw an exception.