Jump to content

Recommended Posts

Posted (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 by Adil Yilan
Posted

@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;
		}

 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.