Jump to content

Add Enchant to a custom sword


Shreyas R

Recommended Posts

 Hello, I've been trying to do this for the past 3 days with no luck😑 So what I'm trying to do is to add sharpness to a custom sword I made and this is my code👇

package com.shreyas.exmod.item.custom;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SwordItem;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;


public class DivineExcaliburItem extends SwordItem {
    public DivineExcaliburItem(Tier tier, int i, float v, Properties properties) {
        super(tier, i, v, properties);
    }

    @Override
    public boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantment) {
        return enchantment == Enchantments.SHARPNESS || super.canApplyAtEnchantingTable(stack, enchantment);
    }

    @Override
    public boolean isFoil(ItemStack ignored) {
        return false; // never looks enchanted
    }
    
}

And I have no idea how to add the echant to it. Thanks in advance!

Link to comment
Share on other sites

9 hours ago, Shreyas R said:

 Hello, I've been trying to do this for the past 3 days with no luck😑 So what I'm trying to do is to add sharpness to a custom sword I made and this is my code👇

 

To add it in the creative menu, you have to apply the enchantment before passing it through #accept. To have it on craft, you need to add the enchantment to the resulting nbt.

Link to comment
Share on other sites

3 hours ago, Shreyas R said:

So what is the code for enchanting

It's an nbt, so whatever the serialized nbt is. You can probably search it on the Internet. As for the helpers, there is EnchantmentUtils iirc.

3 hours ago, Shreyas R said:

where is the #accept?

Inside CreativeModeTabEvent$BuildContents. Make sure to check the tab you are adding to via #getTab.

Link to comment
Share on other sites

  • 1 month later...

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.