Skip to content

Commit

Permalink
Improve compatibility with DFreds Convenient Effects
Browse files Browse the repository at this point in the history
  • Loading branch information
dev7355608 committed Jul 26, 2023
1 parent 3200874 commit 2607349
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 46 deletions.
6 changes: 3 additions & 3 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"email": "[email protected]"
}
],
"version": "1.6.3",
"version": "1.7.0",
"compatibility": {
"minimum": "11.299",
"verified": "11.306"
Expand Down Expand Up @@ -47,8 +47,8 @@
},
"url": "https://github.com/dev7355608/vision-5e",
"manifest": "https://github.com/dev7355608/vision-5e/releases/latest/download/module.json",
"download": "https://github.com/dev7355608/vision-5e/releases/download/v1.6.3/module.zip",
"changelog": "https://github.com/dev7355608/vision-5e/releases/tag/v1.6.3",
"download": "https://github.com/dev7355608/vision-5e/releases/download/v1.7.0/module.zip",
"changelog": "https://github.com/dev7355608/vision-5e/releases/tag/v1.7.0",
"bugs": "https://github.com/dev7355608/vision-5e/issues",
"readme": "https://raw.githubusercontent.com/dev7355608/vision-5e/main/README.md",
"license": "https://raw.githubusercontent.com/dev7355608/vision-5e/main/LICENSE"
Expand Down
118 changes: 75 additions & 43 deletions scripts/compatibility.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { registerStatusEffect } from "./config.mjs";
import { DetectionModeDarkvision } from "./detection-modes/darkvision.mjs";
import { DetectionModeHearing } from "./detection-modes/hearing.mjs";
import { DetectionModeTremorsense } from "./detection-modes/tremorsense.mjs";
Expand Down Expand Up @@ -97,57 +96,90 @@ Hooks.once("init", () => {
});
});

Hooks.once("init", () => {
Hooks.once("setup", () => {
if (!game.modules.get("dfreds-convenient-effects")?.active) {
return;
}

function reregisterStatusEffect(id, name, icon, index) {
for (const statusEffect of CONFIG.statusEffects) {
if (statusEffect.id === id) {
return;
}
const customStatusEffects = [
{
id: "burrow",
name: "Burrowing",
icon: "modules/vision-5e/icons/burrow.svg"
},
{
id: "disease",
name: "Diseased",
icon: "icons/svg/biohazard.svg"
},
{
id: "ethereal",
name: "Ethereal",
icon: "modules/vision-5e/icons/ethereal.svg"
},
{
id: "fly",
name: "Flying",
icon: "icons/svg/wing.svg"
},
{
id: "inaudible",
name: "Inaudible",
icon: "modules/vision-5e/icons/inaudible.svg"
}
];

if (statusEffect.id.startsWith("Convenient Effect: ") && statusEffect.statuses?.includes(id)) {
const otherStatuses = statusEffect.statuses.filter(s => s !== id);
const EffectDefinitions = game.dfreds.effects.constructor;

if (otherStatuses.length === 0 || otherStatuses.length === 1 && otherStatuses[0] === statusEffect.id) {
return;
}
}
EffectDefinitions.prototype.initialize = ((initialize) => function () {
this._conditions = this.conditions;

for (const { id, name, icon } of customStatusEffects) {
this._conditions.push(this._effectHelpers.createActiveEffect({
name,
icon,
statuses: [id]
}));
}

registerStatusEffect(id, name, icon, index);
}
this._conditions.sort((e1, e2) => e1.name.localeCompare(e2.name, "en"));

Hooks.once("dfreds-convenient-effects.ready", () => {
reregisterStatusEffect(
"disease",
"EFFECT.StatusDisease",
"icons/svg/biohazard.svg",
CONFIG.statusEffects.findIndex(s => s.id === "Convenient Effect: Poisoned") + 1
);
reregisterStatusEffect(
"fly",
"EFFECT.StatusFlying",
"icons/svg/wing.svg"
);
reregisterStatusEffect(
"burrow",
"VISION5E.Burrowing",
"modules/vision-5e/icons/burrow.svg"
);
reregisterStatusEffect(
"ethereal",
"VISION5E.Ethereal",
"modules/vision-5e/icons/ethereal.svg"
);
reregisterStatusEffect(
"inaudible",
"VISION5E.Inaudible",
"modules/vision-5e/icons/inaudible.svg",
CONFIG.statusEffects.findIndex(s => s.id === "Convenient Effect: Invisible") + 1
);
return initialize.call(this);
})(EffectDefinitions.prototype.initialize);

game.settings.register(
"vision-5e",
"compatibility.dfreds-convenient-effects",
{
scope: "world",
config: false,
type: Object,
default: {}
}
);

Hooks.once("dfreds-convenient-effects.ready", async () => {
if (!game.users.activeGM?.isSelf) {
return;
}

const compatibility = foundry.utils.deepClone(await game.settings.get("vision-5e", "compatibility.dfreds-convenient-effects"));
const statusEffects = compatibility.addedStatusEffects ??= [];
let addedStatusEffect = false;

for (const { id, name } of customStatusEffects) {
if (!statusEffects.includes(id)) {
statusEffects.push(id);
await game.dfreds.effectInterface.addStatusEffect(name);
addedStatusEffect = true;
}
}

if (addedStatusEffect) {
statusEffects.sort((a, b) => a.localeCompare(b, "en"));
await game.settings.set("vision-5e", "compatibility.dfreds-convenient-effects", compatibility);
ui.notifications.warn("Foundry must be reloaded to update token status effects.", { permanent: true });
}
});
});

Expand Down

0 comments on commit 2607349

Please sign in to comment.