Getting the Type of an ItemStack?

How would I get the type of an ItemStack?

I want to check if an item that a player is holding is a sword. So far, I have this.

ItemStack sword = gameRegistry.createItemBuilder()
        .itemType(ItemTypes.IRON_SWORD)
        .build();

if (p.getItemInHand().equals(sword)) {
    // do this
}

But the reason why I need an item type instead of an ItemStack is because if the player is holding an ENCHANTED sword, then “p.getItemInHand().equals(sword)” will return FALSE because a normal iron sword ItemStack does not equal an enchanted iron sword ItemStack.

I want to get the ItemType instead so a non-enchanted iron sword and enchanted iron CAN be equal.

You’d want to use ItemStack.getItem() == ItemTypes.IRON_SWORD

use itemStack.getItem().equals(ItemTypes.IRON_SWORD)

== may be possible, but it’s a bad practise. Better get used to equals()

Also, we have some nice ItemStackComparators that you can use to compare two ItemStacks for various things (including just checking for ItemType equality).