Skip to content

Enchantment Data Generation

Maksym Uimanov edited this page Feb 10, 2026 · 1 revision

Enchantment Data Generation

The @GenerateEnchantment annotation is a data generation annotation that automatically creates enchantment definitions for Minecraft mods using the Temporal API. It provides a structured approach to defining enchantments without manual JSON configuration by using description classes.

Responsibilities / Purpose

  • Automatic data generation: Creates enchantment definitions without manual JSON files
  • Description-based configuration: Uses EnchantmentDescription classes to define enchantment properties
  • Type safety: Ensures compile-time validation of enchantment configurations
  • Integration with data providers: Seamlessly integrates with NeoForge's enchantment data generation system

How it works

  1. Annotation processing: The annotation is processed by GenerateEnchantmentStrategy during the data generation phase
  2. Field extraction: The strategy extracts the ResourceKey<Enchantment> from the annotated field
  3. Description instantiation: Creates an instance of the specified EnchantmentDescription class
  4. Builder registration: Registers the enchantment description with ApiEnchantmentProvider for data generation

How to use / Configure

Step 1: Create an EnchantmentDescription implementation

public class CustomEnchantmentDescription implements EnchantmentDescription {
    @Override
    public Enchantment.Builder build(HolderGetter<Enchantment> enchantments, HolderGetter<Item> items) {
        return Enchantment.enchantment(Enchantment.definition(
                items.getOrThrow(ItemTags.SWORD_ENCHANTABLE),
                5,  // max level
                2,  // anvil cost
                Enchantment.dynamicCost(5, 20),   // min cost
                Enchantment.dynamicCost(55, 20),  // max cost
                2,  // min level for treasure
                EquipmentSlotGroup.MAINHAND))
                        .withEffect(
                                EnchantmentEffectComponents.POST_ATTACK, 
                                EnchantmentTarget.ATTACKER, 
                                EnchantmentTarget.VICTIM, 
                                new CustomEnchantmentEffect()
                        );
    }
}

Step 2: Apply the annotation to a ResourceKey field

public final class ModEnchantments {
    @GenerateEnchantment(CustomEnchantmentDescription.class)
    public static final ResourceKey<Enchantment> CUSTOM_ENCHANTMENT = 
        ResourceUtils.createKey(Registries.ENCHANTMENT, "custom_enchantment");
}

Annotation parameters

  • value (Class<? extends EnchantmentDescription>): The description class that defines the enchantment's properties and effects

Extension points

The annotation integrates with the Temporal API's strategy system:

  • Strategy implementation: GenerateEnchantmentStrategy handles the processing logic
  • Provider system: Uses ApiEnchantmentProvider for data generation
  • Description interface: EnchantmentDescription provides a flexible way to define enchantment properties

When to use

Use @GenerateEnchantment when:

  • Creating custom enchantments for your mod
  • Defining enchantments with complex effects and properties
  • Automating data generation for enchantment content
  • Ensuring consistent enchantment configuration across your mod

The annotation is particularly useful for mods with multiple enchantments, as it centralizes enchantment logic and eliminates manual JSON file management while providing full access to Minecraft's enchantment builder API.

Clone this wiki locally