#How to add enchantments to items in 1.21 ?
17 messages · Page 1 of 1 (latest)
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.
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.
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
So can I still change the creative inventory or crafting results by enchanting the ItemStack?
Or is this method invalid like adding default enchantments during registration?
If it still works, how should the value of RegistryEntry<Enchantment> type be written?
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.
That's okay, because it's a question I asked for him. Other than that, thanks for the answer, I'll try later on.
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();
If I want to write enchantments into the crafting recipe, can I only do it by modifying the components?
This seems to circumvent the new limitations of addEnchantment method in 1.21.
I can't find any usable examples of how to add a Enchantment component to a itemStack.
Which method should I use to add enchantment components?
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.
ohh I see it now do in datapack maybe cuz it now dynamic register for enchantment
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.