Checking specific itemMeta

Discussion in 'Plugin Development' started by thechrisanator, Apr 5, 2017.

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

    thechrisanator

    I want to check if a sword has a certain name, but not an enchantment.
    Ex: if sword has name "example" {
    //do something
    }
    So if the sword has that name, it will do something, right? But I want it to work even if the sword has an enchantment.
    So basically I want it to check for a name, and it dosnt care about anything else. How would I do that?
     
  2. Offline

    The_Spaceman

    Code:
    ItemStack itemstack = player.getInventory().getItemInMainHand();
    ItemMeta itemmeta = itemstack.getItemMeta();
    if (itemmeta.getDisplayName().equalsIgnoreCase("your name here")) {
         //do stuff
     }
    
    this will get the item in the main hand of the user. you can add that is MUST be a sword, but this will work with all items with that certain name
     
  3. Offline

    thechrisanator

    @The_Spaceman
    for future notice, you might want to add a
    HasDisplayName();
    before you to getDisplayName, because it will be null if there's no custom name, and it will throw a nullPointerException Error
     
Thread Status:
Not open for further replies.

Share This Page