38 lines
1.1 KiB
JavaScript

import { TRIGGER_TYPES } from "../constants.js";
import { handleTrigger } from "../engine/reaction-engine.js";
import { getBestTokenDocumentForActor } from "../utils/token-utils.js";
export function registerDnd5eDamageTrigger() {
Hooks.on("dnd5e.applyDamage", async (actor, amount, options = {}) => {
if (!game.user.isGM) return;
if (!actor) return;
const tokenDocument = options.token ?? options.tokenDocument ?? getBestTokenDocumentForActor(actor);
const damageTypes = extractDamageTypes(options);
await handleTrigger(TRIGGER_TYPES.DAMAGE_RECEIVED, {
actor,
targetActor: actor,
tokenDocument,
targetTokenDocument: tokenDocument,
targetToken: tokenDocument?.object ?? null,
amount: Number(amount) || 0,
damageAmount: Number(amount) || 0,
damageTypes,
options
});
});
}
function extractDamageTypes(options) {
const candidates = [
options.damageTypes,
options.types,
options.damage?.types,
options.workflow?.damageItem?.damageDetail?.map?.(part => part.type)
];
const values = candidates.flat().filter(Boolean);
return [...new Set(values)];
}