Skip to content

Commit

Permalink
Merge pull request #49 from kandashi/0.1.25
Browse files Browse the repository at this point in the history
duplicate fixes, and cleanup of removal from unlinked tokens
  • Loading branch information
kandashi authored Jan 23, 2021
2 parents c0aa6a3 + caa53f0 commit da760d2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "Active-Auras",
"description": "Active-Auras",
"author": "Kandashi",
"version": "0.1.3",
"version": "0.1.31",
"minimumCoreVersion": "0.7.5",
"compatibleCoreVersion": "0.7.9",
"packs": [
Expand Down
82 changes: 57 additions & 25 deletions src/aura.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Hooks.on("ready", () => {
* Hooks onto effect sheet to add aura configuration
*/
Hooks.on("renderActiveEffectConfig", async (sheet, html) => {
if(!sheet.object.data.flags?.ActiveAuras?.aura) {
if (!sheet.object.data.flags?.ActiveAuras?.aura) {
await sheet.object.setFlag(`${MODULE_NAME}`, 'aura')
}
const flags = sheet.object.data.flags;
Expand Down Expand Up @@ -99,7 +99,7 @@ Hooks.on("ready", () => {
/**
* Re-run aura detection on token creation
*/
Hooks.on("createToken", (scene, token) => {
Hooks.on("createToken", (_scene, token) => {
newToken = canvas.tokens.get(token._id)
setTimeout(() => {
CollateAuras(canvas, true, false)
Expand All @@ -110,20 +110,52 @@ Hooks.on("ready", () => {
* @todo
* Filter for aura effects on deleted token and remove from canvas tokens
*/
Hooks.on("preDeleteToken", async (scene, token) => {
Hooks.on("preDeleteToken", async (_scene, _token) => {
setTimeout(() => {
CollateAuras(canvas, false, true)
}, 20)

});

Hooks.on("preUpdateToken", (_scene, token, update, _flags, _id) => {
let removed = token.actorData?.effects?.filter(x => !update.actorData?.effects?.includes(x));
let added = update.actorData?.effects?.filter(x => !token.actorData?.effect?.includes(x))
if (added?.length > 0) {
for (let effect of added) {
if (effect.flags?.ActiveAuras?.isAura) {
setTimeout(() => {
CollateAuras(canvas, true, false)
}, 50)
return;
}
}
}
if (removed?.length > 0) {
for (let effect of removed) {
if (effect.flags?.ActiveAuras?.isAura) {
setTimeout(() => {
CollateAuras(canvas, true, true)
}, 50)
return;
}
}
}

})

/**
* On token movement run MainAura
*/
Hooks.on("updateToken", (scene, token, update, flags, id) => {
Hooks.on("updateToken", (_scene, token, update, _flags, _id) => {
if (("y" in update || "x" in update || "elevation" in update))
MainAura(token)
if ((update?.actorData?.effects) || ("hidden" in update)) {
let auraToken = false
if (token.actorData?.effects?.length > 0) {
for (let effect of token.actorData?.effects) {
if (effect.flags?.ActiveAuras?.isAura) auraToken = true; break;
}
}
if ("hidden" in update && auraToken) {
setTimeout(() => {
CollateAuras(canvas, true, true)
}, 20)
Expand All @@ -134,7 +166,7 @@ Hooks.on("ready", () => {
/**
* @todo
*/
Hooks.on("updateActiveEffect", (actor, effect, update) => {
Hooks.on("updateActiveEffect", (_actor, effect, _update) => {
if (effect.flags?.ActiveAuras?.isAura) {
setTimeout(() => {
CollateAuras(canvas, true, false)
Expand All @@ -145,7 +177,7 @@ Hooks.on("ready", () => {
/**
* On removal of active effect from linked actor, if aura remove from canvas.tokens
*/
Hooks.on("deleteActiveEffect", (actor, effect) => {
Hooks.on("deleteActiveEffect", (_actor, effect) => {
let applyStatus = effect.flags?.ActiveAuras?.applied;
if (!applyStatus) {
setTimeout(() => {
Expand All @@ -157,7 +189,7 @@ Hooks.on("ready", () => {
/**
* On creation of active effect on linked actor, run MainAura
*/
Hooks.on("createActiveEffect", (actor, effect) => {
Hooks.on("createActiveEffect", (_actor, effect) => {
if (!effect.flags?.ActiveAuras?.applied && effect.flags?.ActiveAuras?.isAura) {
setTimeout(() => {
CollateAuras(canvas, true, false)
Expand All @@ -179,11 +211,12 @@ Hooks.on("ready", () => {
}
}

function IsAuraToken(token,canvas){
function IsAuraToken(token, canvas) {
let MapKey = canvas.scene._id;
MapObject = AuraMap.get(MapKey);
for (let effect of MapObject.effects){
if(effect.tokenId === token._id) return true;
for (let effect of MapObject.effects) {
if (effect.tokenId === token._id) return true;

}
}

Expand All @@ -202,7 +235,7 @@ Hooks.on("ready", () => {
for (let testEffect of testToken?.actor?.effects.entries) {
if (testEffect.getFlag('ActiveAuras', 'isAura')) {
if (testEffect.getFlag('ActiveAuras', 'hidden') && testToken.data.hidden) continue;
let newEffect = { data: testEffect.data, parentActorLink: testEffect.parent.data.token.actorLink, parentActorId: testEffect.parent._id, tokenId: testToken.id }
let newEffect = { data: duplicate(testEffect.data), parentActorLink: testEffect.parent.data.token.actorLink, parentActorId: testEffect.parent._id, tokenId: testToken.id }
for (let change of newEffect.data.changes) {
if (typeof change.value === "string" && change.key !== "macro.execute") {
if (change.value.includes("@")) {
Expand All @@ -214,6 +247,13 @@ Hooks.on("ready", () => {
}
if (change.key === "macro.execute") newEffect.data.flags.ActiveAuras.isMacro = true
}
newEffect.data.disabled = false
let macro = newEffect.data.flags.ActiveAuras.isMacro !== undefined ? false : newEffect.data.flags.ActiveAuras.isMacro

newEffect.data.flags.ActiveAuras.isAura = false;
newEffect.data.flags.ActiveAuras.applied = true;
newEffect.data.flags.ActiveAuras.isMacro = macro;

effectArray.push(newEffect)
}
}
Expand Down Expand Up @@ -262,8 +302,9 @@ Hooks.on("ready", () => {

let map = new Map();
let updateTokens = canvas.tokens.placeables
if(movedToken !== undefined){
if(!IsAuraToken(movedToken, canvas)) {

if (movedToken !== undefined) {
if (!IsAuraToken(movedToken, canvas)) {
updateTokens = [];
updateTokens.push(canvas.tokens.get(movedToken._id))
}
Expand All @@ -272,17 +313,7 @@ Hooks.on("ready", () => {

for (let mapEffect of map) {
let MapKey = mapEffect[0]
let newEffectData = duplicate(mapEffect[1].effect.data)

newEffectData.disabled = false
let macro = newEffectData.flags.ActiveAuras.isMacro

newEffectData.flags.ActiveAuras = {
isAura: false,
applied: true,
isMacro: macro
}
map.set(MapKey, { add: mapEffect[1].add, token: mapEffect[1].token, effect: newEffectData })
map.set(MapKey, { add: mapEffect[1].add, token: mapEffect[1].token, effect: mapEffect[1].effect.data })
}

for (let update of map) {
Expand All @@ -293,6 +324,7 @@ Hooks.on("ready", () => {
RemoveActiveEffects(update[1].token, update[1].effect.label)
}
}
sequencialUpdate = false
}


Expand Down

0 comments on commit da760d2

Please sign in to comment.