How to make an enchantment that heals you (lifesteal) (Minecraft mod)
Since 1.21 an enchantment is a data file, and its built-in effects cover damage, protection and the like. For a new effect such as lifesteal, write the enchantment file with no effects and add the behaviour in code: listen for the damage event, look up the attacker’s level of your enchantment on the held item and heal by that much. Fabric API has ServerLivingEntityEvents.AFTER_DAMAGE; NeoForge has LivingDamageEvent.Post. Before 1.21 an enchantment is a Java object instead, and its post-attack method does the work.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.19.2 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Write the enchantment JSON with an empty effects object (before 1.21, write the Enchantment class instead).
- Add its name to the lang file (the how-to does this).
- Listen for the damage event for your loader (not needed where the enchantment class has the post-attack hook).
- Look up the enchantment level on the attacker’s item and call heal.
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| max | 3 |
The highest level. |
| heal | 1 |
Health restored per level, in half hearts. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric 1.21.1
Section titled “Fabric 1.21.1”Uses:
ServerLivingEntityEvents.AFTER_DAMAGEEnchantmentHelper.getItemEnchantmentLevelRegistries.ENCHANTMENTLivingEntity.heal
src/main/resources/data/examplemod/enchantment/vampiric.json
{ "description": { "translate": "enchantment.examplemod.vampiric" }, "supported_items": "#minecraft:enchantable/sword", "primary_items": "#minecraft:enchantable/sword", "weight": 3, "max_level": 3, "min_cost": { "base": 15, "per_level_above_first": 10 }, "max_cost": { "base": 45, "per_level_above_first": 10 }, "anvil_cost": 6, "slots": [ "mainhand" ], "effects": {}}src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentHelper;
// The enchantment examplemod:vampiric is a data file; this class gives it its effect: the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { private VampiricLifesteal() { }
// Called from the mod's onInitialize. public static void register() { ResourceKey<Enchantment> key = ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath("examplemod", "vampiric")); ServerLivingEntityEvents.AFTER_DAMAGE.register((entity, source, baseDamage, damageTaken, blocked) -> { if (source.getEntity() instanceof LivingEntity attacker && !blocked) { int level = attacker.level().registryAccess().registryOrThrow(Registries.ENCHANTMENT).getHolder(key) .map(holder -> EnchantmentHelper.getItemEnchantmentLevel(holder, attacker.getMainHandItem())).orElse(0); if (level > 0) { attacker.heal(level * 1.0F); } } }); }}In your main mod class, inside onInitialize:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Fabric 1.19.3 to 1.20.4
Section titled “Fabric 1.19.3 to 1.20.4”Uses:
EnchantmentEnchantmentCategory.WEAPONBuiltInRegistries.ENCHANTMENTServerLivingEntityEvents.ALLOW_DAMAGEEnchantmentHelper.getItemEnchantmentLevelLivingEntity.heal
Before 1.21 an enchantment is a Java object registered in BuiltInRegistries.ENCHANTMENT (here a Rarity, an EnchantmentCategory and the slots it works in), not a data file, so there is no JSON here. Fabric API has no AFTER_DAMAGE yet: ALLOW_DAMAGE runs before the damage is applied and always returns true.
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.Registry;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EquipmentSlot;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentCategory;import net.minecraft.world.item.enchantment.EnchantmentHelper;
// Before 1.21 an enchantment is a registered object, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { public static final Enchantment ENCHANTMENT = Registry.register(BuiltInRegistries.ENCHANTMENT, new ResourceLocation("examplemod", "vampiric"), new Enchantment(Enchantment.Rarity.UNCOMMON, EnchantmentCategory.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND}) { @Override public int getMaxLevel() { return 3; } });
private VampiricLifesteal() { }
// Called from the mod's onInitialize. public static void register() { ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> { if (source.getEntity() instanceof LivingEntity attacker) { int level = EnchantmentHelper.getItemEnchantmentLevel(ENCHANTMENT, attacker.getMainHandItem()); if (level > 0) { attacker.heal(level * 1.0F); } } return true; }); }}In your main mod class, inside onInitialize:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Fabric 1.20.5 to 1.20.6
Section titled “Fabric 1.20.5 to 1.20.6”Uses:
Enchantment.definitionBuiltInRegistries.ENCHANTMENTServerLivingEntityEvents.ALLOW_DAMAGEEnchantmentHelper.getItemEnchantmentLevelLivingEntity.heal
Before 1.21 an enchantment is a Java object registered in BuiltInRegistries.ENCHANTMENT, not a data file, so there is no JSON here. Fabric API has no AFTER_DAMAGE yet: ALLOW_DAMAGE runs before the damage is applied and always returns true.
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.Registry;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.resources.ResourceLocation;import net.minecraft.tags.ItemTags;import net.minecraft.world.entity.EquipmentSlot;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentHelper;
// Before 1.21 an enchantment is a registered object, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { public static final Enchantment ENCHANTMENT = Registry.register(BuiltInRegistries.ENCHANTMENT, new ResourceLocation("examplemod", "vampiric"), new Enchantment(Enchantment.definition(ItemTags.SWORD_ENCHANTABLE, 3, 3, Enchantment.dynamicCost(15, 10), Enchantment.dynamicCost(45, 10), 6, EquipmentSlot.MAINHAND)) { });
private VampiricLifesteal() { }
// Called from the mod's onInitialize. public static void register() { ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> { if (source.getEntity() instanceof LivingEntity attacker) { int level = EnchantmentHelper.getItemEnchantmentLevel(ENCHANTMENT, attacker.getMainHandItem()); if (level > 0) { attacker.heal(level * 1.0F); } } return true; }); }}In your main mod class, inside onInitialize:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Fabric 1.21
Section titled “Fabric 1.21”Uses:
ServerLivingEntityEvents.ALLOW_DAMAGEEnchantmentHelper.getItemEnchantmentLevelRegistries.ENCHANTMENTLivingEntity.heal
Fabric API 1.21 has no AFTER_DAMAGE yet: this uses ALLOW_DAMAGE, which runs before the damage is applied and always returns true.
src/main/resources/data/examplemod/enchantment/vampiric.json
{ "description": { "translate": "enchantment.examplemod.vampiric" }, "supported_items": "#minecraft:enchantable/sword", "primary_items": "#minecraft:enchantable/sword", "weight": 3, "max_level": 3, "min_cost": { "base": 15, "per_level_above_first": 10 }, "max_cost": { "base": 45, "per_level_above_first": 10 }, "anvil_cost": 6, "slots": [ "mainhand" ], "effects": {}}src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentHelper;
// The enchantment examplemod:vampiric is a data file; this class gives it its effect: the attacker heals 1.0F per level when a hit is about to land.public final class VampiricLifesteal { private VampiricLifesteal() { }
// Called from the mod's onInitialize. public static void register() { ResourceKey<Enchantment> key = ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath("examplemod", "vampiric")); ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> { if (source.getEntity() instanceof LivingEntity attacker ) { int level = attacker.level().registryAccess().registryOrThrow(Registries.ENCHANTMENT).getHolder(key) .map(holder -> EnchantmentHelper.getItemEnchantmentLevel(holder, attacker.getMainHandItem())).orElse(0); if (level > 0) { attacker.heal(level * 1.0F); } } return true; }); }}In your main mod class, inside onInitialize:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}NeoForge 1.21 to 1.21.1
Section titled “NeoForge 1.21 to 1.21.1”Uses:
LivingDamageEvent.PostEnchantmentHelper.getItemEnchantmentLevelRegistries.ENCHANTMENTLivingEntity.heal
src/main/resources/data/examplemod/enchantment/vampiric.json
{ "description": { "translate": "enchantment.examplemod.vampiric" }, "supported_items": "#minecraft:enchantable/sword", "primary_items": "#minecraft:enchantable/sword", "weight": 3, "max_level": 3, "min_cost": { "base": 15, "per_level_above_first": 10 }, "max_cost": { "base": 45, "per_level_above_first": 10 }, "anvil_cost": 6, "slots": [ "mainhand" ], "effects": {}}src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentHelper;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.living.LivingDamageEvent;
// The enchantment examplemod:vampiric is a data file; this class gives it its effect: the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { private VampiricLifesteal() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(VampiricLifesteal::onDamage); }
private static void onDamage(LivingDamageEvent.Post event) { ResourceKey<Enchantment> key = ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath("examplemod", "vampiric")); if (event.getSource().getEntity() instanceof LivingEntity attacker) { int level = attacker.level().registryAccess().registryOrThrow(Registries.ENCHANTMENT).getHolder(key) .map(holder -> EnchantmentHelper.getItemEnchantmentLevel(holder, attacker.getMainHandItem())).orElse(0); if (level > 0) { attacker.heal(level * 1.0F); } } }}In your main mod class, inside <init>:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}NeoForge 1.20.5 to 1.20.6
Section titled “NeoForge 1.20.5 to 1.20.6”Uses:
Enchantment.definitionEnchantment.doPostAttackRegistries.ENCHANTMENTLivingEntity.heal
Before 1.21 an enchantment is a Java object registered in the enchantment registry, not a data file, so there is no JSON here. doPostAttack is the hook for the wearer hitting something, so no event is needed.
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.core.registries.Registries;import net.minecraft.tags.ItemTags;import net.minecraft.world.entity.Entity;import net.minecraft.world.entity.EquipmentSlot;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;
// Before 1.21 an enchantment is a registered object, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { public static final DeferredRegister<Enchantment> ENCHANTMENTS = DeferredRegister.create(Registries.ENCHANTMENT, ExampleMod.MOD_ID);
public static final DeferredHolder<Enchantment, Enchantment> VAMPIRIC = ENCHANTMENTS.register("vampiric", LifestealEnchantment::new);
private VampiricLifesteal() { }
// Called from the mod's constructor. public static void register(IEventBus modBus) { ENCHANTMENTS.register(modBus); }
private static final class LifestealEnchantment extends Enchantment { LifestealEnchantment() { super(Enchantment.definition(ItemTags.SWORD_ENCHANTABLE, 3, 3, Enchantment.dynamicCost(15, 10), Enchantment.dynamicCost(45, 10), 6, EquipmentSlot.MAINHAND)); }
// Runs on the server when the wearer of the enchanted item hits something. @Override public void doPostAttack(LivingEntity attacker, Entity target, int level) { attacker.heal(level * 1.0F); } }}In your main mod class, inside <init>:
VampiricLifesteal.register(modEventBus);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}NeoForge 1.20.2 to 1.20.4
Section titled “NeoForge 1.20.2 to 1.20.4”Uses:
Enchantment.doPostAttackRegistries.ENCHANTMENTEnchantmentCategory.WEAPONLivingEntity.heal
Before 1.21 an enchantment is a Java object registered in the enchantment registry (here a Rarity, an EnchantmentCategory and the slots it works in), not a data file, so there is no JSON here. doPostAttack is the hook for the wearer hitting something, so no event is needed.
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.core.registries.Registries;import net.minecraft.world.entity.Entity;import net.minecraft.world.entity.EquipmentSlot;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentCategory;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;
// Before 1.21 an enchantment is a registered object, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { public static final DeferredRegister<Enchantment> ENCHANTMENTS = DeferredRegister.create(Registries.ENCHANTMENT, ExampleMod.MOD_ID);
public static final DeferredHolder<Enchantment, Enchantment> VAMPIRIC = ENCHANTMENTS.register("vampiric", LifestealEnchantment::new);
private VampiricLifesteal() { }
// Called from the mod's constructor. public static void register(IEventBus modBus) { ENCHANTMENTS.register(modBus); }
private static final class LifestealEnchantment extends Enchantment { LifestealEnchantment() { super(Rarity.UNCOMMON, EnchantmentCategory.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND}); }
@Override public int getMaxLevel() { return 3; }
// Runs on the server when the wearer of the enchanted item hits something. @Override public void doPostAttack(LivingEntity attacker, Entity target, int level) { attacker.heal(level * 1.0F); } }}In your main mod class, inside <init>:
VampiricLifesteal.register(modEventBus);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Fabric 1.21.2 to 1.21.10
Section titled “Fabric 1.21.2 to 1.21.10”Uses:
ServerLivingEntityEvents.AFTER_DAMAGEEnchantmentHelper.getItemEnchantmentLevelRegistries.ENCHANTMENTLivingEntity.heal
src/main/resources/data/examplemod/enchantment/vampiric.json
{ "description": { "translate": "enchantment.examplemod.vampiric" }, "supported_items": "#minecraft:enchantable/sword", "primary_items": "#minecraft:enchantable/sword", "weight": 3, "max_level": 3, "min_cost": { "base": 15, "per_level_above_first": 10 }, "max_cost": { "base": 45, "per_level_above_first": 10 }, "anvil_cost": 6, "slots": [ "mainhand" ], "effects": {}}src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentHelper;
// The enchantment examplemod:vampiric is a data file; this class gives it its effect: the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { private VampiricLifesteal() { }
// Called from the mod's onInitialize. public static void register() { ResourceKey<Enchantment> key = ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath("examplemod", "vampiric")); ServerLivingEntityEvents.AFTER_DAMAGE.register((entity, source, baseDamage, damageTaken, blocked) -> { if (source.getEntity() instanceof LivingEntity attacker && !blocked) { int level = attacker.level().registryAccess().lookupOrThrow(Registries.ENCHANTMENT).get(key) .map(holder -> EnchantmentHelper.getItemEnchantmentLevel(holder, attacker.getMainHandItem())).orElse(0); if (level > 0) { attacker.heal(level * 1.0F); } } }); }}In your main mod class, inside onInitialize:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}NeoForge 1.21.2 to 1.21.10
Section titled “NeoForge 1.21.2 to 1.21.10”Uses:
LivingDamageEvent.PostEnchantmentHelper.getItemEnchantmentLevelRegistries.ENCHANTMENTLivingEntity.heal
src/main/resources/data/examplemod/enchantment/vampiric.json
{ "description": { "translate": "enchantment.examplemod.vampiric" }, "supported_items": "#minecraft:enchantable/sword", "primary_items": "#minecraft:enchantable/sword", "weight": 3, "max_level": 3, "min_cost": { "base": 15, "per_level_above_first": 10 }, "max_cost": { "base": 45, "per_level_above_first": 10 }, "anvil_cost": 6, "slots": [ "mainhand" ], "effects": {}}src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentHelper;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.living.LivingDamageEvent;
// The enchantment examplemod:vampiric is a data file; this class gives it its effect: the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { private VampiricLifesteal() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(VampiricLifesteal::onDamage); }
private static void onDamage(LivingDamageEvent.Post event) { ResourceKey<Enchantment> key = ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath("examplemod", "vampiric")); if (event.getSource().getEntity() instanceof LivingEntity attacker) { int level = attacker.level().registryAccess().lookupOrThrow(Registries.ENCHANTMENT).get(key) .map(holder -> EnchantmentHelper.getItemEnchantmentLevel(holder, attacker.getMainHandItem())).orElse(0); if (level > 0) { attacker.heal(level * 1.0F); } } }}In your main mod class, inside <init>:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}NeoForge 1.21.11 to 26.3
Section titled “NeoForge 1.21.11 to 26.3”Uses:
LivingDamageEvent.PostEnchantmentHelper.getItemEnchantmentLevelRegistries.ENCHANTMENTLivingEntity.heal
src/main/resources/data/examplemod/enchantment/vampiric.json
{ "description": { "translate": "enchantment.examplemod.vampiric" }, "supported_items": "#minecraft:enchantable/sword", "primary_items": "#minecraft:enchantable/sword", "weight": 3, "max_level": 3, "min_cost": { "base": 15, "per_level_above_first": 10 }, "max_cost": { "base": 45, "per_level_above_first": 10 }, "anvil_cost": 6, "slots": [ "mainhand" ], "effects": {}}src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.Identifier;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentHelper;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.living.LivingDamageEvent;
// The enchantment examplemod:vampiric is a data file; this class gives it its effect: the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { private VampiricLifesteal() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(VampiricLifesteal::onDamage); }
private static void onDamage(LivingDamageEvent.Post event) { ResourceKey<Enchantment> key = ResourceKey.create(Registries.ENCHANTMENT, Identifier.fromNamespaceAndPath("examplemod", "vampiric")); if (event.getSource().getEntity() instanceof LivingEntity attacker) { int level = attacker.level().registryAccess().lookupOrThrow(Registries.ENCHANTMENT).get(key) .map(holder -> EnchantmentHelper.getItemEnchantmentLevel(holder, attacker.getMainHandItem())).orElse(0); if (level > 0) { attacker.heal(level * 1.0F); } } }}In your main mod class, inside <init>:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Fabric 1.21.11 to 26.3
Section titled “Fabric 1.21.11 to 26.3”Uses:
ServerLivingEntityEvents.AFTER_DAMAGEEnchantmentHelper.getItemEnchantmentLevelRegistries.ENCHANTMENTLivingEntity.heal
src/main/resources/data/examplemod/enchantment/vampiric.json
{ "description": { "translate": "enchantment.examplemod.vampiric" }, "supported_items": "#minecraft:enchantable/sword", "primary_items": "#minecraft:enchantable/sword", "weight": 3, "max_level": 3, "min_cost": { "base": 15, "per_level_above_first": 10 }, "max_cost": { "base": 45, "per_level_above_first": 10 }, "anvil_cost": 6, "slots": [ "mainhand" ], "effects": {}}src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.Identifier;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentHelper;
// The enchantment examplemod:vampiric is a data file; this class gives it its effect: the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { private VampiricLifesteal() { }
// Called from the mod's onInitialize. public static void register() { ResourceKey<Enchantment> key = ResourceKey.create(Registries.ENCHANTMENT, Identifier.fromNamespaceAndPath("examplemod", "vampiric")); ServerLivingEntityEvents.AFTER_DAMAGE.register((entity, source, baseDamage, damageTaken, blocked) -> { if (source.getEntity() instanceof LivingEntity attacker && !blocked) { int level = attacker.level().registryAccess().lookupOrThrow(Registries.ENCHANTMENT).get(key) .map(holder -> EnchantmentHelper.getItemEnchantmentLevel(holder, attacker.getMainHandItem())).orElse(0); if (level > 0) { attacker.heal(level * 1.0F); } } }); }}In your main mod class, inside onInitialize:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Forge 1.17.1
Section titled “Forge 1.17.1”Uses:
Enchantment.doPostAttackForgeRegistries.ENCHANTMENTSEnchantmentCategory.WEAPONLivingEntity.heal
Before 1.21 an enchantment is a Java object registered in the enchantment registry (here a Rarity, an EnchantmentCategory and the slots it works in), not a data file, so there is no JSON here. doPostAttack is the hook for the wearer hitting something, so no event is needed.
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.world.entity.Entity;import net.minecraft.world.entity.EquipmentSlot;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentCategory;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.fmllegacy.RegistryObject;
// Before 1.21 an enchantment is a registered object, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { public static final DeferredRegister<Enchantment> ENCHANTMENTS = DeferredRegister.create(ForgeRegistries.ENCHANTMENTS, ExampleMod.MOD_ID);
public static final RegistryObject<Enchantment> VAMPIRIC = ENCHANTMENTS.register("vampiric", LifestealEnchantment::new);
private VampiricLifesteal() { }
// Called from the mod's constructor. public static void register() { ENCHANTMENTS.register(FMLJavaModLoadingContext.get().getModEventBus()); }
private static final class LifestealEnchantment extends Enchantment { LifestealEnchantment() { super(Rarity.UNCOMMON, EnchantmentCategory.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND}); }
@Override public int getMaxLevel() { return 3; }
// Runs on the server when the wearer of the enchanted item hits something. @Override public void doPostAttack(LivingEntity attacker, Entity target, int level) { attacker.heal(level * 1.0F); } }}In your main mod class, inside <init>:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Forge 1.18.2 to 1.20.1, NeoForge 1.20.1
Section titled “Forge 1.18.2 to 1.20.1, NeoForge 1.20.1”Uses:
Enchantment.doPostAttackForgeRegistries.ENCHANTMENTSEnchantmentCategory.WEAPONLivingEntity.heal
Before 1.21 an enchantment is a Java object registered in the enchantment registry (here a Rarity, an EnchantmentCategory and the slots it works in), not a data file, so there is no JSON here. doPostAttack is the hook for the wearer hitting something, so no event is needed.
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.world.entity.Entity;import net.minecraft.world.entity.EquipmentSlot;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentCategory;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.registries.RegistryObject;
// Before 1.21 an enchantment is a registered object, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { public static final DeferredRegister<Enchantment> ENCHANTMENTS = DeferredRegister.create(ForgeRegistries.ENCHANTMENTS, ExampleMod.MOD_ID);
public static final RegistryObject<Enchantment> VAMPIRIC = ENCHANTMENTS.register("vampiric", LifestealEnchantment::new);
private VampiricLifesteal() { }
// Called from the mod's constructor. public static void register() { ENCHANTMENTS.register(FMLJavaModLoadingContext.get().getModEventBus()); }
private static final class LifestealEnchantment extends Enchantment { LifestealEnchantment() { super(Rarity.UNCOMMON, EnchantmentCategory.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND}); }
@Override public int getMaxLevel() { return 3; }
// Runs on the server when the wearer of the enchanted item hits something. @Override public void doPostAttack(LivingEntity attacker, Entity target, int level) { attacker.heal(level * 1.0F); } }}In your main mod class, inside <init>:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Fabric 1.19.2
Section titled “Fabric 1.19.2”Uses:
EnchantmentEnchantmentCategory.WEAPONBuiltInRegistries.ENCHANTMENTServerLivingEntityEvents.ALLOW_DAMAGEEnchantmentHelper.getItemEnchantmentLevelLivingEntity.heal
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;import net.minecraft.core.Registry;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EquipmentSlot;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.enchantment.Enchantment;import net.minecraft.world.item.enchantment.EnchantmentCategory;import net.minecraft.world.item.enchantment.EnchantmentHelper;
// Before 1.21 an enchantment is a registered object, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { public static final Enchantment ENCHANTMENT = Registry.register(Registry.ENCHANTMENT, new ResourceLocation("examplemod", "vampiric"), new Enchantment(Enchantment.Rarity.UNCOMMON, EnchantmentCategory.WEAPON, new EquipmentSlot[] {EquipmentSlot.MAINHAND}) { @Override public int getMaxLevel() { return 3; } });
private VampiricLifesteal() { }
// Called from the mod's onInitialize. public static void register() { ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> { if (source.getEntity() instanceof LivingEntity attacker) { int level = EnchantmentHelper.getItemEnchantmentLevel(ENCHANTMENT, attacker.getMainHandItem()); if (level > 0) { attacker.heal(level * 1.0F); } } return true; }); }}In your main mod class, inside onInitialize:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
Enchantment.doPostAttackForgeRegistries.ENCHANTMENTSEnchantmentType.WEAPONLivingEntity.heal
In 1.16.5 an enchantment is a Java object registered in the enchantment registry (here a Rarity, an EnchantmentType and the slots it works in), not a data file, so there is no JSON here. doPostAttack is the hook for the wearer hitting something, so no event is needed.
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.entity.Entity;import net.minecraft.inventory.EquipmentSlotType;import net.minecraft.entity.LivingEntity;import net.minecraft.enchantment.Enchantment;import net.minecraft.enchantment.EnchantmentType;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.fml.RegistryObject;
// Before 1.21 an enchantment is a registered object, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.public final class VampiricLifesteal { public static final DeferredRegister<Enchantment> ENCHANTMENTS = DeferredRegister.create(ForgeRegistries.ENCHANTMENTS, ExampleMod.MOD_ID);
public static final RegistryObject<Enchantment> VAMPIRIC = ENCHANTMENTS.register("vampiric", LifestealEnchantment::new);
private VampiricLifesteal() { }
// Called from the mod's constructor. public static void register() { ENCHANTMENTS.register(FMLJavaModLoadingContext.get().getModEventBus()); }
private static final class LifestealEnchantment extends Enchantment { LifestealEnchantment() { super(Enchantment.Rarity.UNCOMMON, EnchantmentType.WEAPON, new EquipmentSlotType[] {EquipmentSlotType.MAINHAND}); }
@Override public int getMaxLevel() { return 3; }
// Runs on the server when the wearer of the enchanted item hits something. @Override public void doPostAttack(LivingEntity attacker, Entity target, int level) { attacker.heal(level * 1.0F); } }}In your main mod class, inside <init>:
VampiricLifesteal.register();In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
Enchantment.doPostAttackForgeRegistries.ENCHANTMENTSEnchantmentType.WEAPONEntityLivingBase.heal
In 1.12.2 an enchantment is a Java object (an Enchantment with a Rarity, an EnumEnchantmentType and the slots it works in) registered in a registry event, so there is no JSON here. Its post-attack hook runs when the wearer hits something, so no event is needed.
src/main/java/com/example/examplemod/event/VampiricLifesteal.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.enchantment.Enchantment;import net.minecraft.enchantment.EnumEnchantmentType;import net.minecraft.entity.Entity;import net.minecraft.entity.EntityLivingBase;import net.minecraft.inventory.EntityEquipmentSlot;import net.minecraft.util.ResourceLocation;import net.minecraftforge.event.RegistryEvent;import net.minecraftforge.fml.common.Mod;import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
// In 1.12.2 an enchantment is a Java object registered in the enchantment registry, not a data file. examplemod:vampiric is on swords; the attacker heals 1.0F per level when a hit lands.@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)public final class VampiricLifesteal { public static final Enchantment ENCHANTMENT = new LifestealEnchantment();
private VampiricLifesteal() { }
@SubscribeEvent public static void register(RegistryEvent.Register<Enchantment> event) { event.getRegistry().register(ENCHANTMENT); }
// Called from the mod's startup code; the event handler above does the work. public static void register() { }
private static final class LifestealEnchantment extends Enchantment { LifestealEnchantment() { super(Rarity.UNCOMMON, EnumEnchantmentType.WEAPON, new EntityEquipmentSlot[] {EntityEquipmentSlot.MAINHAND}); setName("examplemod.vampiric"); setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "vampiric")); }
@Override public int getMaxLevel() { return 3; }
// Runs on the server when the wearer of the enchanted item hits something. @Override public void onEntityDamaged(EntityLivingBase attacker, Entity target, int level) { attacker.heal(level * 1.0F); } }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "enchantment.examplemod.vampiric": "Vampiric"}Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/items/enchantments
- https://docs.fabricmc.net/1.21.1/develop/events
- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/entity/event/v1/ServerLivingEntityEvents.html
Related how-tos
Section titled “Related how-tos”- A mob that drops an item
- Make players immune to a kind of damage
- Define a damage type
- A new enchantment
- Define a new biome
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.