User Tools

Site Tools


tutorial:enchantments

This is an old revision of the document!


Adding Enchantments

To add enchantments to your mod, you'll need to:

  • create a class that extends Enchantment or another existing Enchantment (such as DamageEnchantment)
  • register your enchantment
  • add custom functionality or mechanics if needed
  • add translations for your enchantment 1)

Enchantments can either have custom functionality implemented separately (such as smelting ores mined) or can use already existing mechanics (such as DamageEnchantment), which are applied when appropriate.

Creating Enchantment Class

We'll be creating an enchantment called Wrath, which adds an extra 1.5 points of damage, per level, to a melee weapon. The easiest way to do this is by creating a class that extends DamageEnchantment:

  1. public class WrathEnchantment extends DamageEnchantment
  2. {
  3. public WrathEnchantment(Weight weight, int typeIndex, EquipmentSlot... slots)
  4. {
  5. super(weight, typeIndex, slots)
  6. }
  7.  
  8. @Override
  9. public float getAttackDamage(int level, EntityGroup group)
  10. {
  11. // ....
  12. }
  13.  
  14. @Override
  15. public int getMinimumPower(int int_1)
  16. {
  17. // ....
  18. }
  19.  
  20. @Override
  21. public int getMaximumLevel()
  22. {
  23. // ....
  24. }
  25.  
  26. @Override
  27. public boolean differs(Enchantment enchantment)
  28. {
  29. // ....
  30. }
  31. }

getAttackDamage, as you would expect, is the additional damage provided by the enchantment. level is the level of the enchantment:

@Override
public float getAttackDamage(int level, EntityGroup group)
{
    return level * 1.5f;
}

getMinimumPower is the minimum level required to get the enchant in an enchanting table. We'll set it to 1, so you can get it at any level:

@Override
public int getMinimumPower(int int_1)
{
    return 1;;
}

getMaximumLevel is the number of tiers the enchantment has. 2)

@Override
public int getMaximumLevel()
{
    return 5;
}

Finally, differs is how you isolate enchantments and prevent them from being on the same tool. As an example, sharpness can't be used with smite. We'll prevent our enchant from being combined with sharpness.

@Override
public boolean differs(Enchantment enchantment)
{
    return super.differs(enchantment) && enchantment != Enchantments.SHARPNESS;
}

Registering Enchantment

Registering enchantments follows the same process as usual:

private static Enchantment WRATH;
 
@Override
public void onInitialize()
{
    WRATH = Registry.register(
        Registry.ENCHANTMENT,
	new Identifier("tutorial", "wrath"),
	new WrathEnchantment(
	    Enchantment.Weight.VERY_RARE,
	    0,
	    EquipmentSlot.MAINHAND
	)
    );
}

This registers our enchantment under the namespace tutorial:wrath, sets it as a very rare enchantment, and only allows it on main hand tools.

Adding Translations & Testing

You'll need to add a translation to your enchantment as well. Head over to your mod lang file and add a new entry:

{
    "enchantment.tutorial.wrath": "Wrath"
}

If you go in-game, you should be able to enchant main hand weapons with your new enchant:

1)
When you register enchantments, books are automatically added to the game for each level. The translated name of the enchantment (enchantment.modid.enchantname) is what appears as the book name.
2)
Enchantments with more than a single tier will have roman numerals after the name to show the level. If the enchantment only has a single level, nothing is added.
tutorial/enchantments.1562098982.txt.gz · Last modified: 2019/07/02 20:23 by draylar