Skip to content

Commit d7cb371

Browse files
committed
Add status effect integration
1 parent d996530 commit d7cb371

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

src/ts/app/convenient-effects-app.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,16 @@ class ConvenientEffectsApp extends Application {
341341
this.#controller,
342342
),
343343
},
344+
{
345+
name: "ConvenientEffects.ToggleStatusEffect",
346+
icon: '<i class="far fa-person-rays fa-fw"></i>',
347+
condition: this.#controller.canToggleStatusEffect.bind(
348+
this.#controller,
349+
),
350+
callback: this.#controller.onToggleStatusEffect.bind(
351+
this.#controller,
352+
),
353+
},
344354
{
345355
name: "SIDEBAR.Duplicate",
346356
icon: '<i class="far fa-copy fa-fw"></i>',

src/ts/app/convenient-effects-controller.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ import {
1111
findFolders,
1212
findAllNestedEffectIds,
1313
findEffectByCeId,
14+
findModuleById,
1415
} from "../utils/finds.ts";
1516
import { getBaseType } from "../utils/gets.ts";
1617
import { log } from "../logger.ts";
1718
import { getInputFromDialog } from "../ui/create-edit-folder-dialog.ts";
1819
import { Flags } from "../utils/flags.ts";
1920
import { BackupConvenientEffectsApp } from "./backup/backup-convenient-effects-app.ts";
21+
import { MODULE_IDS } from "../constants.ts";
22+
import { StatusEffectsModule } from "../integrations/status-effect-types.ts";
2023

2124
interface ViewData {
2225
folderData: FolderData[];
@@ -254,6 +257,42 @@ class ConvenientEffectsController {
254257
});
255258
}
256259

260+
canToggleStatusEffect(_target: JQuery<HTMLElement>): boolean {
261+
return !!findModuleById(MODULE_IDS.STATUS_EFFECTS)?.active;
262+
}
263+
264+
async onToggleStatusEffect(target: JQuery<HTMLElement>): Promise<void> {
265+
const ceEffectId = this.#findClosestCeEffectIdByElement(target);
266+
if (!ceEffectId) return;
267+
268+
const ceEffect = findEffectByCeId(ceEffectId);
269+
if (!ceEffect) return;
270+
271+
const statusEffectsModule = findModuleById(
272+
MODULE_IDS.STATUS_EFFECTS,
273+
) as StatusEffectsModule | undefined;
274+
if (!statusEffectsModule?.active) return;
275+
276+
const statusEffectsApi = statusEffectsModule.api;
277+
const statusEffect = statusEffectsApi.findStatusEffect({
278+
effectId: ceEffectId,
279+
effectName: ceEffect.name,
280+
});
281+
282+
if (statusEffect) {
283+
await statusEffectsApi.deleteStatusEffect({
284+
effectId: ceEffectId,
285+
effectName: ceEffect.name,
286+
});
287+
} else {
288+
await statusEffectsApi.createNewStatusEffects({
289+
effectsData: [ceEffect.toObject()],
290+
});
291+
}
292+
293+
this.#viewMvc.render();
294+
}
295+
257296
onViewBackups(_event: Event): void {
258297
new BackupConvenientEffectsApp().render(true);
259298
}

src/ts/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const MODULE_IDS = {
1414
ATE: "ATL",
1515
MIDI: "midi-qol",
1616
TOKEN_MAGIC: "tokenmagic",
17+
STATUS_EFFECTS: "dfreds-status-effects",
1718
};
1819

1920
export const SECONDS = {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ActiveEffectSource } from "types/foundry/common/documents/active-effect.js";
2+
3+
interface StatusEffectsModule extends Module {
4+
api: StatusEffectsApi;
5+
}
6+
7+
interface IFindStatusEffect {
8+
/**
9+
* The foundry effect ID or the SE effect ID
10+
*/
11+
effectId?: string | null;
12+
13+
/**
14+
* The effect name
15+
*/
16+
effectName?: string | null;
17+
}
18+
19+
interface ICreateNewStatusEffects {
20+
/**
21+
* The data that defines the status effects to create
22+
*/
23+
effectsData: PreCreate<ActiveEffectSource>[];
24+
}
25+
26+
interface IDeleteStatusEffect {
27+
/**
28+
* The foundry effect ID or the SE effect ID to remove. If defined,
29+
* prioritized over `effectName`
30+
*/
31+
effectId?: string;
32+
33+
/**
34+
* The name of the effect to remove
35+
*/
36+
effectName?: string;
37+
}
38+
39+
interface StatusEffectsApi {
40+
findStatusEffects(): ActiveEffect<Item<any>>[];
41+
42+
findStatusEffect(
43+
input: IFindStatusEffect,
44+
): ActiveEffect<Item<null>> | undefined;
45+
46+
createNewStatusEffects(input: ICreateNewStatusEffects): Promise<Document[]>;
47+
48+
deleteStatusEffect(input: IDeleteStatusEffect): Promise<void>;
49+
}
50+
51+
export type { StatusEffectsModule, StatusEffectsApi };

src/ts/ui/handlebar-helpers.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Flags } from "../utils/flags.ts";
44
import { notEmpty } from "../utils/types.ts";
55
import { findAllNestedEffectIds, findModuleById } from "../utils/finds.ts";
66
import { MODULE_IDS } from "../constants.ts";
7+
import { StatusEffectsModule } from "../integrations/status-effect-types.ts";
78

89
class HandlebarHelpers {
910
#settings: Settings;
@@ -120,6 +121,10 @@ class HandlebarHelpers {
120121
findAllNestedEffectIds({ backup: false }),
121122
);
122123

124+
if (findModuleById(MODULE_IDS.STATUS_EFFECTS)?.active) {
125+
icons += this.#getStatusEffectsIcon(effect);
126+
}
127+
123128
if (findModuleById(MODULE_IDS.MIDI)?.active) {
124129
icons += this.#getMidiIcon(allChanges);
125130
}
@@ -179,6 +184,22 @@ class HandlebarHelpers {
179184
: "";
180185
}
181186

187+
#getStatusEffectsIcon(effect: ActiveEffect<Item<null>>): string {
188+
const statusEffectsModule = findModuleById(
189+
MODULE_IDS.STATUS_EFFECTS,
190+
) as StatusEffectsModule | undefined;
191+
192+
const statusEffectsApi = statusEffectsModule?.api;
193+
const statusEffect = statusEffectsApi?.findStatusEffect({
194+
effectId: Flags.getCeEffectId(effect),
195+
effectName: effect.name,
196+
});
197+
198+
return statusEffect
199+
? "<i class='fas fa-person-rays integration-icon' title='Status Effect'></i>"
200+
: "";
201+
}
202+
182203
#getMidiIcon(changes: DeepPartial<EffectChangeData>[]): string {
183204
return changes.some((change) =>
184205
change.key?.startsWith("flags.midi-qol"),

static/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"ShowEffect": "Show Effect",
3333
"HideEffect": "Hide Effect",
3434
"ToggleAsOverlay": "Toggle as Overlay",
35+
"ToggleStatusEffect": "Toggle Status Effect",
3536
"ResetSystemEffects": "Reset System Effects",
3637
"ResetSystemEffectsWarning": "This will completely reset to the built-in effects, undoing any custom creations, deletions, or edits that were made.",
3738

0 commit comments

Comments
 (0)