enchantment.modid.enchantname
) is what appears as the book name.This is an old revision of the document!
Table of Contents
Adding Enchantments
To add enchantments to your mod, you'll need to:
- create a class that extends
Enchantment
or another existing Enchantment (such asDamageEnchantment
) - 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
:
public class WrathEnchantment extends DamageEnchantment { public WrathEnchantment(Weight weight, int typeIndex, EquipmentSlot... slots) { super(weight, typeIndex, slots) } @Override public float getAttackDamage(int level, EntityGroup group) { // .... } @Override public int getMinimumPower(int int_1) { // .... } @Override public int getMaximumLevel() { // .... } @Override public boolean differs(Enchantment enchantment) { // .... } }
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.