How can i get the enchantments of the iteminhand ?
You need to use the data api.
This gets the item that a player is holding. Checks to see if an item is present in the player’s hand. Gets the optional enchantment data. And checks if there are enchantments on the item.
Optional<ItemStack> optional = player.getItemInHand();
// Check if the player is holding an item
if(optional.isPresent()) {
ItemStack stack = optional.get();
// Get the enchantment data, if present
Optional<EnchantmentData> data = stack.getData(EnchantmentData.class);
if(data.isPresent()) {
// We know there are enchantments, so it is safe to get the enchantment data
EnchantmentData enchantmentData = data.get();
player.sendMessage(Texts.of("There are enchantments!"));
} else {
player.sendMessage(Texts.of("No enchantments"));
}
}
OK and how can i check if an enchantment like Sharpness is there?
almost, @intronate67 .
EnchantmentData
is a map of Enchatment
to an integer (enchant level). Therefore you’d want to use:
enchantmentData.get(Enchantments.SHARPNESS).isPresent()
To try and get the sharpness enchant type from the item, and see if it exists.