From 98b8e455dec0933436c171dcd0b3814347a2483d Mon Sep 17 00:00:00 2001 From: Haxxer Date: Sun, 30 Jan 2022 12:09:40 +0000 Subject: [PATCH] Fixed lights, fixed recursion --- scripts/constants.js | 7 +++- scripts/module.js | 79 ++++++++++++++++++++++---------------------- 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/scripts/constants.js b/scripts/constants.js index 65ae582..0bdc3b1 100644 --- a/scripts/constants.js +++ b/scripts/constants.js @@ -1,6 +1,11 @@ const CONSTANTS = { MODULE_NAME: "tagger", - TAGS: "tags" + TAGS: "tags", + DATA: "data" } +CONSTANTS["BASE_PROPERTY"] = `flags.${CONSTANTS.MODULE_NAME}`; +CONSTANTS["TAG_PROPERTY"] = `${CONSTANTS.BASE_PROPERTY}.${CONSTANTS.TAGS}` +CONSTANTS["DATA_PROPERTY"] = `${CONSTANTS.BASE_PROPERTY}.${CONSTANTS.DATA}` + export default CONSTANTS; \ No newline at end of file diff --git a/scripts/module.js b/scripts/module.js index 0260213..31daf16 100644 --- a/scripts/module.js +++ b/scripts/module.js @@ -403,14 +403,14 @@ class TaggerConfig { if (!elem) return; const tags = app?.object instanceof Actor - ? Tagger._validateTags(getProperty(app?.object, `data.token.flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.TAGS}`) ?? [], "_applyHtml") + ? Tagger._validateTags(getProperty(app?.object, `data.token.${CONSTANTS.TAG_PROPERTY}`) ?? [], "_applyHtml") : Tagger.getTags(app?.object?._object); const html = $(`
Tags (separated by commas)
- +
`); @@ -449,7 +449,7 @@ let temporaryIds = {}; class TaggerHandler { static applyUpdateTags(inDocument, updateData) { - let propertyName = `flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.TAGS}`; + let propertyName = CONSTANTS.TAG_PROPERTY; if (inDocument instanceof Actor) propertyName = "token." + propertyName; let tags = getProperty(updateData, propertyName); if (!tags || tags?.length === 0) return; @@ -457,24 +457,32 @@ class TaggerHandler { setProperty(updateData, propertyName, tags); } - static applyCreateTags(inDocument, tags) { - + static preCreateApplyTags(inDocument){ if(hotkeyState.altDown) return; - + let documentData = inDocument.data.toObject() + temporaryIds = {}; + this.applyCreateTags(documentData); temporaryIds = {}; + return inDocument.data.update(documentData); + } - let documentData = inDocument.data.toObject() + static applyCreateTags(documentData) { + + const preprocessed = getProperty(documentData, `${CONSTANTS.DATA_PROPERTY}.preprocessed`); + if(preprocessed){ + setProperty(documentData, `${CONSTANTS.DATA_PROPERTY}.preprocessed`, false); + return; + } - const taggerFlagProperty = `flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.TAGS}`; - tags = getProperty(inDocument.data, taggerFlagProperty); + let tags = getProperty(documentData, CONSTANTS.TAG_PROPERTY); - if(tags) { + if (tags) { tags = this.applyRules(tags); - setProperty(documentData, taggerFlagProperty, tags); + setProperty(documentData, CONSTANTS.TAG_PROPERTY, tags); } - if(game.modules.get("token-attacher")?.active){ - documentData = this.recurseTokenAttacher(documentData); + if (game.modules.get("token-attacher")?.active) { + this.recurseTokenAttacher(documentData); } if(game.modules.get("monks-active-tiles")?.active){ @@ -488,31 +496,18 @@ class TaggerHandler { }) } - inDocument.data.update(documentData); - - temporaryIds = {}; - } static recurseTokenAttacher(documentData){ - - const taggerFlagProperty = `flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.TAGS}`; const prototypeAttached = getProperty(documentData, "flags.token-attacher.prototypeAttached"); - - if(prototypeAttached){ - for(const [type, objects] of Object.entries(prototypeAttached)){ - for(let i = 0; i < objects.length; i++){ - let object = objects[i]; - let objectTags = getProperty(object, taggerFlagProperty); - if(objectTags) { - setProperty(documentData, `flags.token-attacher.prototypeAttached.${type}.${i}.${taggerFlagProperty}`, this.applyRules(objectTags)); - } - object = this.recurseTokenAttacher(object) + if(prototypeAttached) { + for(const objects of Object.values(prototypeAttached)){ + for(const object of objects){ + this.applyCreateTags(object) + setProperty(object, `${CONSTANTS.DATA_PROPERTY}.preprocessed`, true); } } } - - return documentData; } static applyRules(tags){ @@ -522,6 +517,8 @@ class TaggerHandler { return entry; }); + tags = Tagger._validateTags(tags, "TaggerHandler"); + return tags.map((tag, index) => { const applicableTagRules = tagRules.filter(([regx]) => { @@ -586,19 +583,21 @@ Hooks.on("preUpdateToken", (doc, update) => TaggerHandler.applyUpdateTags(doc, u Hooks.on("preUpdateTile", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); Hooks.on("preUpdateDrawing", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); Hooks.on("preUpdateWall", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); -Hooks.on("preUpdateLight", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); +Hooks.on("preUpdateLight", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); // 0.8.9 +Hooks.on("preUpdateAmbientLight", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); Hooks.on("preUpdateAmbientSound", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); Hooks.on("preUpdateMeasuredTemplate", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); Hooks.on("preUpdateNote", (doc, update) => TaggerHandler.applyUpdateTags(doc, update)); -Hooks.on("preCreateToken", (doc) => TaggerHandler.applyCreateTags(doc)); -Hooks.on("preCreateTile", (doc) => TaggerHandler.applyCreateTags(doc)); -Hooks.on("preCreateDrawing", (doc) => TaggerHandler.applyCreateTags(doc)); -Hooks.on("preCreateWall", (doc) => TaggerHandler.applyCreateTags(doc)); -Hooks.on("preCreateLight", (doc) => TaggerHandler.applyCreateTags(doc)); -Hooks.on("preCreateAmbientSound", (doc) => TaggerHandler.applyCreateTags(doc)); -Hooks.on("preCreateMeasuredTemplate", (doc) => TaggerHandler.applyCreateTags(doc)); -Hooks.on("preCreateNote", (doc) => TaggerHandler.applyCreateTags(doc)); +Hooks.on("preCreateToken", (doc) => TaggerHandler.preCreateApplyTags(doc)); +Hooks.on("preCreateTile", (doc) => TaggerHandler.preCreateApplyTags(doc)); +Hooks.on("preCreateDrawing", (doc) => TaggerHandler.preCreateApplyTags(doc)); +Hooks.on("preCreateWall", (doc) => TaggerHandler.preCreateApplyTags(doc)); +Hooks.on("preCreateLight", (doc) => TaggerHandler.preCreateApplyTags(doc)); // 0.8.9 +Hooks.on("preCreateAmbientLight", (doc) => TaggerHandler.preCreateApplyTags(doc)); +Hooks.on("preCreateAmbientSound", (doc) => TaggerHandler.preCreateApplyTags(doc)); +Hooks.on("preCreateMeasuredTemplate", (doc) => TaggerHandler.preCreateApplyTags(doc)); +Hooks.on("preCreateNote", (doc) => TaggerHandler.preCreateApplyTags(doc)); Hooks.once('init', async function () { registerHotkeysPre();