Help - How check if an item has an specific enchantment.

Buddelbubi

Member
Hello. I've made an Job Plugin. It includes "Miner".
Now Player use an Pickaxe enchanted with Silk touch, to get unlimited Money.
I add: if(p.getInventory().getItemInHand().hasEnchantments()) .... It worked but now every enchantment is blocked.
How check if the Item in players hand isn't enchanted with silktouch.

Sorry my bad english, i'm german
 

RealAstro

New Member
Item item = Item.get(Item.DIAMOND_PICKAXE, 0, 1);

if (player.getInventory().getItemInHand().equals(item) && item.hasEnchantments()) {
 

Leonidius

Member
You should get all of the enchantments applied to the item, iterate over them and check if there is a Silk Touch enchantment among them. To do so, compare each enchanment's ID to the ID of Silk Touch. It should look a little something like this:

Java:
for (Enchantment e : item.getEnchantments()) {
    if (e.getId() == Enchantment.ID_SILK_TOUCH) {
        // do something
    }
}
 
Top