#How to add enchantments to items in 1.21 ?

17 messages · Page 1 of 1 (latest)

granite geyser
#

I want to enchant a custom item with sharpness 5, but in 1.21 the first parameter type in addEnchantment becomes RegistryEntry<Enchantment>. But in 1.20.x, you can just use RegistryKey<Enchantment>. I am clueless now, please help me, thanks a million!

idle tulip
#

Take a look at the EnchantmentScreenHandler, specifically its generateEnhancements method. You're gonna need access to the DynamicRegistryManager, which can be gotten from an instance of ServerWorld using world.getServer().getRegistryManager()

#

Because enchantments are data driven now, you're gonna have to look up the sharpness enchantment, make sure it exists, then apply it.

silver eagle
# idle tulip Take a look at the EnchantmentScreenHandler, specifically its `generateEnhanceme...

Does this mean that I can't set default enchantments for items when registering them?
My original design was to override the getDefaultStack method in the custom item class when registering items.
Then call the getDefaultStack method in the custom recipe generator or ItemGroup to replace itemStack(They all take ItemStack as a parameter).
This way, both the creative mode inventory and crafting can get items with default enchantments.

idle tulip
#

Yeah, that would be correct. You wouldn't have a registry key at the point of registering items.

#

You could hook into the selection within the creative mode inventory and attach it there

silver eagle
idle tulip
#

You're adding the recipe in json file, right?

#

Hold on, I went to go look at the implementation of RegistryKey. You might be able to get away with adding a RegistryEntry.Reference<Enchantment> using RegistryEntry.Reference.standalone

#

We should also move this chat elsewhere since it doesn't apply to the original question.

granite geyser
atomic fern
#

If you want to get by RegistryEntry You can try

DynamicRegistryManager dynamicRegistryManager = world.getRegistryManager();
RegistryEntry<Enchantment> enchant = dynamicRegistryManager.get(RegistryKeys.ENCHANTMENT).getEntry(Identifier.ofVanilla("name_of_vanilla_enchantment")).get();
silver eagle
atomic fern
silver eagle
# atomic fern like a own craft recipe for custom enchantment?

Yes, in previous versions, enchanting ItemStacks would allow you to add enchantment components to custom recipes.
But in 1.21, due to the change of the addEnchantment method, this no longer works. So I can only consider adding it through the component.

atomic fern
silver eagle
# atomic fern ohh I see it now do in datapack maybe cuz it now dynamic register for enchantmen...

Editing the json file directly is easy, but it will make the recipes not generated by RecipeProvider.
In addition, not modifying the enchantment components of the default item stack will also make it impossible to directly replace the enchanted items in the creative mode inventory through the getDefaultStack method.
If possible, I still hope to enchant the item stack in the getDefaultStack method, so that the methods in the creative mode inventory and the recipe provider can use the enchanted items as output results.