Skip to content

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.

  1. Write the damage type JSON.
  2. Add the death message to the lang file (the how-to does this).
  3. Add the type to tags such as bypasses_armor if it should ignore armor.
  4. Look the type up in the damage type registry, build a DamageSource and hurt an entity.

The example uses these values. Change them in the code.

Setting In the example What it does
exhaustion 0.1 Hunger the hit costs.

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_TYPE
  • DamageSource
  • DamageTypes

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"
}

Uses:

  • Registries.DAMAGE_TYPE
  • DamageSource
  • DamageTypes

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"
}

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.