How to make a sword in a Minecraft mod
A sword is a tool item made of a tier (a ToolMaterial from 1.21.2), which sets its durability and enchantability, plus attack damage and speed attributes. Before 1.21.5 it is a SwordItem (ItemSword on 1.12.2); from 1.21.5 it is a plain Item made a sword through Item.Properties#sword. Its item model is held like a tool (item/handheld), and the #minecraft:swords tag, where the version has it, lets it take sword enchantments.
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 the sword with your tier and attributes (a class that extends SwordItem before 1.21.5, Item.Properties#sword after).
- Register it with your class.
- Use the item/handheld model parent.
- Add it to the #minecraft:swords item 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 |
|---|---|---|
| tier | IRON |
The vanilla tier it is made of: its durability, speed and what it can mine. |
| damage | 3 |
Attack damage on top of the tier’s (vanilla swords add 3). |
| speed | -2.4 |
Attack speed change (vanilla swords -2.4). |
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:
SwordItemTiersItemTags.SWORDS
src/main/java/com/example/examplemod/item/RubySwordItem.java
package com.example.examplemod.item;
import net.minecraft.world.item.Item;import net.minecraft.world.item.SwordItem;import net.minecraft.world.item.Tiers;
// Ruby Sword: a IRON sword; its damage and speed are on top of the tier's.public class RubySwordItem extends SwordItem { public RubySwordItem(Item.Properties properties) { super(Tiers.IRON, 3, -2.4F, properties); }}In src/main/resources/data/minecraft/tags/items/swords.json, add this to the list at /values:
"examplemod:ruby_sword"It’s registered like any other item, as RubySwordItem: 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:
SwordItemSwordItem.createAttributesTiersItem.Properties.attributesItemTags.SWORDS
src/main/java/com/example/examplemod/item/RubySwordItem.java
package com.example.examplemod.item;
import net.minecraft.world.item.Item;import net.minecraft.world.item.SwordItem;import net.minecraft.world.item.Tiers;
// Ruby Sword: a IRON sword; its damage and speed are on top of the tier's.public class RubySwordItem extends SwordItem { public RubySwordItem(Item.Properties properties) { super(Tiers.IRON, properties.attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F))); }}In src/main/resources/data/minecraft/tags/items/swords.json, add this to the list at /values:
"examplemod:ruby_sword"It’s registered like any other item, as RubySwordItem: 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:
SwordItemSwordItem.createAttributesTiersItem.Properties.attributesItemTags.SWORDS
src/main/java/com/example/examplemod/item/RubySwordItem.java
package com.example.examplemod.item;
import net.minecraft.world.item.Item;import net.minecraft.world.item.SwordItem;import net.minecraft.world.item.Tiers;
// Ruby Sword: a IRON sword; its damage and speed are on top of the tier's.public class RubySwordItem extends SwordItem { public RubySwordItem(Item.Properties properties) { super(Tiers.IRON, properties.attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F))); }}In src/main/resources/data/minecraft/tags/item/swords.json, add this to the list at /values:
"examplemod:ruby_sword"It’s registered like any other item, as RubySwordItem: 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:
SwordItemToolMaterialItemTags.SWORDS
src/main/java/com/example/examplemod/item/RubySwordItem.java
package com.example.examplemod.item;
import net.minecraft.world.item.Item;import net.minecraft.world.item.SwordItem;import net.minecraft.world.item.ToolMaterial;
// Ruby Sword: a IRON sword; its damage and speed are on top of the tier's.public class RubySwordItem extends SwordItem { public RubySwordItem(Item.Properties properties) { super(ToolMaterial.IRON, 3, -2.4F, properties); }}In src/main/resources/data/minecraft/tags/item/swords.json, add this to the list at /values:
"examplemod:ruby_sword"It’s registered like any other item, as RubySwordItem: 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.Properties.swordToolMaterialItemTags.SWORDS
Since 1.21.5 there is no SwordItem: Item.Properties#sword(material, damage, speed) makes a plain Item a sword.
src/main/java/com/example/examplemod/item/RubySwordItem.java
package com.example.examplemod.item;
import net.minecraft.world.item.Item;import net.minecraft.world.item.ToolMaterial;
// Ruby Sword: a IRON sword; its damage and speed are on top of the tier's.public class RubySwordItem extends Item { public RubySwordItem(Item.Properties properties) { super(properties.sword(ToolMaterial.IRON, 3, -2.4F)); }}In src/main/resources/data/minecraft/tags/item/swords.json, add this to the list at /values:
"examplemod:ruby_sword"It’s registered like any other item, as RubySwordItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
SwordItemItemTier
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/RubySwordItem.java
package com.example.examplemod.item;
import net.minecraft.item.Item;import net.minecraft.item.SwordItem;import net.minecraft.item.ItemTier;
// Ruby Sword: a IRON sword; its damage and speed are on top of the tier's.public class RubySwordItem extends SwordItem { public RubySwordItem(Item.Properties properties) { super(ItemTier.IRON, 3, -2.4F, properties); }}It’s registered like any other item, as RubySwordItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
ItemSwordToolMaterial
src/main/java/com/example/examplemod/item/RubySwordItem.java
package com.example.examplemod.item;
import com.google.common.collect.Multimap;import net.minecraft.entity.SharedMonsterAttributes;import net.minecraft.entity.ai.attributes.AttributeModifier;import net.minecraft.inventory.EntityEquipmentSlot;import net.minecraft.item.ItemSword;
// Ruby Sword: a IRON sword; its damage and speed are on top of the tier's.public class RubySwordItem extends ItemSword { public RubySwordItem() { super(ToolMaterial.IRON); }
// 1.12.2: damage and attack speed are attribute modifiers of the main hand; the material gives the base damage. @Override public Multimap<String, AttributeModifier> getItemAttributeModifiers(EntityEquipmentSlot slot) { Multimap<String, AttributeModifier> map = super.getItemAttributeModifiers(slot); if (slot == EntityEquipmentSlot.MAINHAND) { map.removeAll(SharedMonsterAttributes.ATTACK_DAMAGE.getName()); map.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", 3 + getAttackDamage(), 0)); map.removeAll(SharedMonsterAttributes.ATTACK_SPEED.getName()); map.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(ATTACK_SPEED_MODIFIER, "Weapon modifier", -2.4F, 0)); } return map; }}It’s registered like any other item, as RubySwordItem: 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”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.