How to define a damage type in a Minecraft mod
Since 1.19.4 a damage type is a data file in data/<modid>/damage_type. It names the death message (message_id, with the lang key death.attack.<message_id>), how much hunger the hit costs (exhaustion) and how it scales with difficulty. Armor and enchantment rules come from tags: add the type to #minecraft:bypasses_armor, #minecraft:is_fire and so on. In code you make a DamageSource from the type’s holder and hurt an entity with it (entity.hurt(source, amount), or hurtServer(level, source, amount) on 1.21.2 and newer).
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.19.4 to 26.3 on Fabric; Minecraft 1.19.4 to 1.20.1 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Write the damage type JSON.
- Add the death message to the lang file (the how-to does this).
- Add the type to tags such as bypasses_armor if it should ignore armor.
- Look the type up in the damage type registry, build a DamageSource and hurt an entity.
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 |
|---|---|---|
| exhaustion | 0.1 |
Hunger the hit costs. |
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.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.19.4 to 1.20.3, Fabric and NeoForge 1.21.2 to 26.3
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.19.4 to 1.20.3, Fabric and NeoForge 1.21.2 to 26.3”Uses:
Registries.DAMAGE_TYPEDamageSourceDamageTypes
Use it in code with new DamageSource(level.registryAccess().registryOrThrow(Registries.DAMAGE_TYPE).getHolderOrThrow(ResourceKey.create(Registries.DAMAGE_TYPE, ResourceLocation.fromNamespaceAndPath(“<modid>”, “<name>”)))). The lang line is the death message.
src/main/resources/data/examplemod/damage_type/shadow_burn.json
{ "message_id": "shadow_burn", "exhaustion": 0.1, "scaling": "when_caused_by_living_non_player"}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "death.attack.shadow_burn": "%1$s was hurt by Shadow Burn"}Forge 1.19.4 to 1.20.1
Section titled “Forge 1.19.4 to 1.20.1”Uses:
Registries.DAMAGE_TYPEDamageSourceDamageTypes
Use it in code with new DamageSource(level.registryAccess().registryOrThrow(Registries.DAMAGE_TYPE).getHolderOrThrow(ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(“<modid>”, “<name>”)))). The lang line is the death message.
src/main/resources/data/examplemod/damage_type/shadow_burn.json
{ "message_id": "shadow_burn", "exhaustion": 0.1, "scaling": "when_caused_by_living_non_player"}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "death.attack.shadow_burn": "%1$s was hurt by Shadow Burn"}Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/resources/server/damagetypes
- https://docs.neoforged.net/docs/1.21.1/resources/server/
- https://docs.fabricmc.net/develop/
Related how-tos
Section titled “Related how-tos”- An enchantment that heals you (lifesteal)
- A sword that sets targets on fire
- Make players immune to a kind of damage
- 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.