Adil Yilan Posted January 19, 2022 Posted January 19, 2022 (edited) Hi, I am trying to determine if item has specific enchant. I have tried with EnchantmentHelper, however statement is not returning true for item that is enchanted: // Get item in main hand. ItemStack item = player.getMainHandItem(); // Get list of enchantements on item. Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(item); // IF: Timber is enchanted on item. if(enchantments.containsKey(ToolEnchantments.TIMBER.get())) { // Mine the block at the position. mineBlock(world, blockPos, item); } My guess is that simple object comparison won't do? What would be proper way to check if enchantment is present on item? This is how ToolEnchantments.TIMBER is defined: public final class ToolEnchantments { public static DeferredRegister<Enchantment> REGISTRY; public static RegistryObject<Enchantment> BLOOM; public static RegistryObject<Enchantment> TIMBER; public static void register(IEventBus eventBus) { REGISTRY = DeferredRegister.create(ForgeRegistries.ENCHANTMENTS, ExperimentalMod.MODID); REGISTRY.register("bloom", () -> new BloomEnchantment()); REGISTRY.register("timber", () -> new TimberEnchantment()); REGISTRY.register(eventBus); } } Edited January 19, 2022 by Adil Yilan Quote
Adil Yilan Posted January 21, 2022 Author Posted January 21, 2022 @diesieben07 Thank you for your hint on DeferredRegister, you were right, that was the reason why it didn't work. However, I've changed to use getItemEnchantmentLevel function instead, and now enchantment is working great // Get item in main hand. ItemStack item = player.getMainHandItem(); // Get level of enchantment on item. int level = EnchantmentHelper.getItemEnchantmentLevel(ToolEnchantments.TIMBER.get(), item); // IF: Enchantement level is not at least 1; if (level < 1) { return; } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.