I did it this way:
I have a class with all the Damage Types I need to declare and then in the bootstrap method I register them
public static final ResourceKey<DamageType> SPEAR = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation("mod_id", "spear"));
public static void bootstrap(BootstapContext<DamageType> context) { context.register(SPEAR, new DamageType("spear", 0.1F)); }
Then, where I need to use that damage type, I can call it like this
DamageSource damagesource = new DamageSource(this.level.registryAccess().registryOrThrow(Registries.DAMAGE_TYPE).getHolderOrThrow(MyDamageTypes.SPEAR), this, entity1 == null ? this : entity1);
In this case I'm providing 2 more arguments to the constructor since I need them, but these depends on the type of damage. What I'm trying to mimic here is something similar to the trident damage, so that's why I need those parameters. Anyawy the key here is how you retrieve the DamageType
this.level.registryAccess().registryOrThrow(Registries.DAMAGE_TYPE).getHolderOrThrow(MyDamageTypes.SPEAR)
Essentially we get the RegistryAccess instance from a Level (world) instance (in this case the Level instance of the entity I'm calling this in). From here we get the Damage Type registry and from here we get the DamageType we want providing a Resouce Key (in this case we provide oure resource key).
This method is similar to how vanilla/forge implements it, you can see the vanilla game damage sources inside the DamageSources class
After all this you need the JSON files inside your mod's assets data folder.
Under <your_mod_id> you need to add a new folder called "damage_type". Inside you need to add a json file named like the damage type you're adding. In the example above I'm adding a "spear" damage type, so I create a "spear.json" file in which I specify the damage type properties
{
"exhaustion": 0.1,
"message_id": "spear",
"scaling": "when_caused_by_living_non_player"
}
This may vary based on the damage type you're trying to add/mimic and of course on your needs.
You may also want to add your damage type to the minecraft tags, if there is one that fits your new damage type, under minecraft/tags/damage_type.
In my case my new damage is a projectile type one, so I added the "is_projectile.json" file
{
"replace": false,
"values": [
"mod_id:spear"
]
}