Improved registration and unregistration of triggers

This commit is contained in:
WBHarry 2026-01-15 21:52:22 +01:00
parent 9393bab6cf
commit 9fb9ae1b91
5 changed files with 129 additions and 27 deletions

View file

@ -164,26 +164,7 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
prepareBaseData() {
super.prepareBaseData();
for (const action of this.actions ?? []) {
if (!action.actor) continue;
const actionsToRegister = [];
for (let i = 0; i < action.triggers.length; i++) {
const trigger = action.triggers[i];
const { args } = CONFIG.DH.TRIGGER.triggers[trigger.trigger];
const fn = new foundry.utils.AsyncFunction(...args, `{${trigger.command}\n}`);
actionsToRegister.push(fn.bind(action));
if (i === action.triggers.length - 1)
game.system.registeredTriggers.registerTriggers(
trigger.trigger,
action.actor?.uuid,
trigger.triggeringActorType,
this.parent.uuid,
actionsToRegister
);
}
}
game.system.registeredTriggers.registerItemTriggers(this.parent);
}
async _preCreate(data, options, user) {

View file

@ -104,6 +104,10 @@ export default class DhpActor extends Actor {
}
}
async _preDelete() {
game.system.registeredTriggers.unregisterItemTriggers(this.items);
}
_onDelete(options, userId) {
super._onDelete(options, userId);
for (const party of this.parties) {

View file

@ -208,4 +208,18 @@ export default class DHItem extends foundry.documents.Item {
cls.create(msg);
}
deleteTriggers() {
const actions = Array.from(this.system.actions ?? []);
if (!actions.length) return;
game.system.registeredTriggers.unregisterTriggers(
actions.flatMap(action => action.triggers.map(x => x.trigger)),
this.uuid
);
}
async _preDelete() {
this.deleteTriggers();
}
}

View file

@ -536,4 +536,10 @@ export default class DHToken extends CONFIG.Token.documentClass {
};
}
//#endregion
async _preDelete() {
if (this.actor && !this.actor.prototypeToken?.actorLink) {
game.system.registeredTriggers.unregisterItemTriggers(this.actor.items);
}
}
}