How to make a sword that sets targets on fire (Minecraft mod)
A sword’s hurtEnemy(stack, target, attacker) runs on the server after a hit lands, with the mob that was hit. Set the target on fire there (target.igniteForSeconds(n) on 1.20.5 and newer, setSecondsOnFire before), then return super.hurtEnemy so the sword still loses durability. The same method works for any on-hit effect: slowness, lightning, knockback. The sword is a SwordItem up to 1.21.4; from 1.21.5 it is a plain Item that is made a sword through its properties.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 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.
- Make a sword class with your tier and attributes (extend SwordItem, or a plain Item on 1.21.5 and newer).
- Override hurtEnemy and set the target on fire.
- Return super.hurtEnemy(…) so the sword still wears down.
- Register the item, use the item/handheld model and add it to the #minecraft:swords tag (not on 1.16.5 and older).
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 |
|---|---|---|
| seconds | 5 |
How long the target burns, in seconds. |
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 and NeoForge 1.20.1 to 1.20.4, Fabric 1.16.5 to 1.20, Forge 1.17.1 to 1.20.1
Section titled “Fabric and NeoForge 1.20.1 to 1.20.4, Fabric 1.16.5 to 1.20, Forge 1.17.1 to 1.20.1”Uses:
SwordItem.hurtEnemyEntity.setSecondsOnFireTiers
src/main/java/com/example/examplemod/item/EmberBladeItem.java
package com.example.examplemod.item;
import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.SwordItem;import net.minecraft.world.item.Tiers;
// Ember Blade: an iron sword that sets what it hits on fire for 5 seconds. hurtEnemy runs on the server after a hit lands.public class EmberBladeItem extends SwordItem { public EmberBladeItem(Item.Properties properties) { super(Tiers.IRON, 3, -2.4F, properties); }
@Override public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) { target.setSecondsOnFire(5); return super.hurtEnemy(stack, target, attacker); }}In src/main/resources/data/minecraft/tags/items/swords.json, add this to the list at /values:
"examplemod:ember_blade"It’s registered like any other item, as EmberBladeItem: see a new item.
Fabric and NeoForge 1.20.5 to 1.20.6
Section titled “Fabric and NeoForge 1.20.5 to 1.20.6”Uses:
SwordItem.hurtEnemyEntity.igniteForSecondsSwordItem.createAttributesTiers
src/main/java/com/example/examplemod/item/EmberBladeItem.java
package com.example.examplemod.item;
import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.SwordItem;import net.minecraft.world.item.Tiers;
// Ember Blade: an iron sword that sets what it hits on fire for 5 seconds. hurtEnemy runs on the server after a hit lands.public class EmberBladeItem extends SwordItem { public EmberBladeItem(Item.Properties properties) { super(Tiers.IRON, properties.attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F))); }
@Override public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) { target.igniteForSeconds(5); return super.hurtEnemy(stack, target, attacker); }}In src/main/resources/data/minecraft/tags/items/swords.json, add this to the list at /values:
"examplemod:ember_blade"It’s registered like any other item, as EmberBladeItem: see a new item.
Fabric and NeoForge 1.21 to 1.21.1
Section titled “Fabric and NeoForge 1.21 to 1.21.1”Uses:
SwordItem.hurtEnemyEntity.igniteForSecondsSwordItem.createAttributesTiers
src/main/java/com/example/examplemod/item/EmberBladeItem.java
package com.example.examplemod.item;
import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.SwordItem;import net.minecraft.world.item.Tiers;
// Ember Blade: an iron sword that sets what it hits on fire for 5 seconds. hurtEnemy runs on the server after a hit lands.public class EmberBladeItem extends SwordItem { public EmberBladeItem(Item.Properties properties) { super(Tiers.IRON, properties.attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F))); }
@Override public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) { target.igniteForSeconds(5); return super.hurtEnemy(stack, target, attacker); }}In src/main/resources/data/minecraft/tags/item/swords.json, add this to the list at /values:
"examplemod:ember_blade"It’s registered like any other item, as EmberBladeItem: see a new item.
Fabric and NeoForge 1.21.2 to 1.21.4
Section titled “Fabric and NeoForge 1.21.2 to 1.21.4”Uses:
SwordItem.hurtEnemyEntity.igniteForSecondsToolMaterial
src/main/java/com/example/examplemod/item/EmberBladeItem.java
package com.example.examplemod.item;
import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.SwordItem;import net.minecraft.world.item.ToolMaterial;
// Ember Blade: an iron sword that sets what it hits on fire for 5 seconds. hurtEnemy runs on the server after a hit lands.public class EmberBladeItem extends SwordItem { public EmberBladeItem(Item.Properties properties) { super(ToolMaterial.IRON, 3, -2.4F, properties); }
@Override public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) { target.igniteForSeconds(5); return super.hurtEnemy(stack, target, attacker); }}In src/main/resources/data/minecraft/tags/item/swords.json, add this to the list at /values:
"examplemod:ember_blade"It’s registered like any other item, as EmberBladeItem: see a new item.
Fabric and NeoForge 1.21.5 to 26.3
Section titled “Fabric and NeoForge 1.21.5 to 26.3”Uses:
Item.hurtEnemyEntity.igniteForSecondsItem.Properties.swordToolMaterial
Since 1.21.5 there is no SwordItem: Item.Properties#sword(material, damage, speed) makes a plain Item a sword, and hurtEnemy is overridden on Item.
src/main/java/com/example/examplemod/item/EmberBladeItem.java
package com.example.examplemod.item;
import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.ToolMaterial;
// Ember Blade: an iron sword that sets what it hits on fire for 5 seconds. hurtEnemy runs on the server after a hit lands.public class EmberBladeItem extends Item { public EmberBladeItem(Item.Properties properties) { super(properties.sword(ToolMaterial.IRON, 3, -2.4F)); }
@Override public void hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) { target.igniteForSeconds(5); super.hurtEnemy(stack, target, attacker); }}In src/main/resources/data/minecraft/tags/item/swords.json, add this to the list at /values:
"examplemod:ember_blade"It’s registered like any other item, as EmberBladeItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
SwordItem.hurtEnemyLivingEntity.setSecondsOnFireItemTier
The minecraft:swords and minecraft:pickaxes item tags do not exist in 1.16.5: the item class (SwordItem, PickaxeItem) decides what it can do.
src/main/java/com/example/examplemod/item/EmberBladeItem.java
package com.example.examplemod.item;
import net.minecraft.entity.LivingEntity;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.item.SwordItem;import net.minecraft.item.ItemTier;
// Ember Blade: an iron sword that sets what it hits on fire for 5 seconds. hurtEnemy runs on the server after a hit lands.public class EmberBladeItem extends SwordItem { public EmberBladeItem(Item.Properties properties) { super(ItemTier.IRON, 3, -2.4F, properties); }
@Override public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) { target.setSecondsOnFire(5); return super.hurtEnemy(stack, target, attacker); }}It’s registered like any other item, as EmberBladeItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
ItemSword.hurtEnemyEntityLivingBase.setSecondsOnFireToolMaterial
In 1.12.2 the class extends ItemSword and the fire is target.setFire(seconds).
src/main/java/com/example/examplemod/item/EmberBladeItem.java
package com.example.examplemod.item;
import net.minecraft.entity.EntityLivingBase;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.item.ItemSword;
// Ember Blade: an iron sword that sets what it hits on fire for 5 seconds. hitEntity runs on the server after a hit lands.public class EmberBladeItem extends ItemSword { public EmberBladeItem() { super(Item.ToolMaterial.IRON); }
@Override public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) { target.setFire(5); return super.hitEntity(stack, target, attacker); }}It’s registered like any other item, as EmberBladeItem: see a new item.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/items/custom-tools
- https://docs.neoforged.net/docs/1.21.1/items/tools
Related how-tos
Section titled “Related how-tos”- A pickaxe
- A piece of armour
- A sword
- An item that does something when used
- An item that gives a potion effect
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.