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:

public float getAttackDamage(int level, EntityGroup group)
{
    return level * 1.5f;
}
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.
tutorial/enchantments.1562098084.txt.gz · Last modified: 2019/07/02 20:08 by draylar