I did this on Forge 47.1.44
first, I made ModDamageTypes interface by copying net.minecraft.world.damagesource.DamageTypes
public interface ModDamageTypes {
ResourceKey<DamageType> DEHYDRATION = ResourceKey.create(Registries.DAMAGE_TYPE,
new ResourceLocation(MyModCommon.MODID, "dehydration")
);
ResourceKey<DamageType> BEACON_BEAM = ResourceKey.create(Registries.DAMAGE_TYPE,
new ResourceLocation(MymodCommon.MODID, "beacon_beam")
);
}
And then I made a ModDamageSources class by copying net.minecraft.world.damagesource.DamageSources
public class ModDamageSources {
private final Registry<DamageType> damageTypes;
private final DamageSource dehydration;
private final DamageSource beaconBeam;
public ModDamageSources(RegistryAccess pRegistry) {
this.damageTypes = pRegistry.registryOrThrow(Registries.DAMAGE_TYPE);
this.dehydration = this.source(ModDamageTypes.DEHYDRATION);
this.beaconBeam = this.source(ModDamageTypes.BEACON_BEAM);
}
private DamageSource source(ResourceKey<DamageType> pDamageTypeKey) {
return new DamageSource(this.damageTypes.getHolderOrThrow(pDamageTypeKey));
}
private DamageSource source(ResourceKey<DamageType> pDamageTypeKey, @Nullable Entity pEntity) {
return new DamageSource(this.damageTypes.getHolderOrThrow(pDamageTypeKey), pEntity);
}
private DamageSource source(ResourceKey<DamageType> pDamageTypeKey, @Nullable Entity pCausingEntity, @Nullable Entity pDirectEntity) {
return new DamageSource(this.damageTypes.getHolderOrThrow(pDamageTypeKey), pCausingEntity, pDirectEntity);
}
public DamageSource dehydration() {
return this.dehydration;
}
public DamageSource beaconBeam() {
return this.beaconBeam;
}
}
In ModCommonEvents class
@SubscribeEvent
public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
if(event.side == LogicalSide.SERVER && !event.player.isCreative()) {
event.player.getCapability(PlayerThirstProvider.PLAYER_THIRST)
.ifPresent(thirst -> {
if (thirst.getThirst() > 0 && event.player.getRandom().nextFloat() < 0.005f) {
thirst.subThirst(1);
ModNetworking.sendToPlayer(
new ThirstDataSyncS2CPacket(thirst.getThirst()),
() -> (ServerPlayer)event.player
);
}
if (thirst.getThirst() == 0 && event.player.getRandom().nextFloat() < 0.005f) {
DamageSource dehydration =
new ModDamageSources(event.player.level().registryAccess()).dehydration();
event.player.hurt(dehydration, 2f);
}
});
}
}
Also, in src/main/resources/data/mymod/damage_type/
there needs to be dehydration.json and beacon_beam.json
{
"exhaustion": 0.1,
"message_id": "dehydration",
"scaling": "never"
}
{
"exhaustion": 0.1,
"message_id": "beacon_beam",
"scaling": "never"
}
And this will work in dev env
copying vanilla minecraft can always get me somewhere, but I am pretty sure this is not the proper way to do this.