Skip to content

Commit c3e4bb8

Browse files
committed
Allow API to work on item UUIDs
1 parent 035975f commit c3e4bb8

File tree

4 files changed

+82
-57
lines changed

4 files changed

+82
-57
lines changed

src/ts/effect-interface.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { ActiveEffectSource } from "types/foundry/common/documents/active-effect.js";
22
import { Settings } from "./settings.ts";
33
import {
4-
findActorByUuid,
5-
findActorByUuidSync,
4+
findDocumentByUuidSync,
65
findAllEffects,
76
findFolder,
87
findFolders,
8+
findDocumentByUuid,
99
} from "./utils/finds.ts";
1010
import { getActorUuids } from "./utils/gets.ts";
1111
import { error, log } from "./logger.ts";
@@ -254,10 +254,10 @@ class EffectInterface {
254254
effectName,
255255
uuid,
256256
}: IHasEffectApplied): boolean {
257-
const actor = findActorByUuidSync(uuid);
257+
const document = findDocumentByUuidSync(uuid);
258258

259259
return (
260-
actor?.effects?.some((effect) => {
260+
document?.effects?.some((effect) => {
261261
const isConvenient = Flags.isConvenient(effect);
262262
const isEnabled = !effect.disabled;
263263
const isMatchingId = effect.id === effectId;
@@ -372,9 +372,9 @@ class EffectInterface {
372372
return [];
373373
}
374374

375-
const actor = await findActorByUuid(uuid);
376-
if (!actor) {
377-
error(`Actor ${uuid} could not be found`);
375+
const document = await findDocumentByUuid(uuid);
376+
if (!document) {
377+
error(`Document ${uuid} could not be found`);
378378
return [];
379379
}
380380

@@ -418,9 +418,9 @@ class EffectInterface {
418418
return;
419419
}
420420

421-
const actor = await findActorByUuid(uuid);
422-
if (!actor) {
423-
error(`Actor ${uuid} could not be found`);
421+
const document = await findDocumentByUuid(uuid);
422+
if (!document) {
423+
error(`Document ${uuid} could not be found`);
424424
return;
425425
}
426426

src/ts/sockets/sockets.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ActiveEffectSource } from "types/foundry/common/documents/active-effect.js";
2-
import { findActorByUuid } from "../utils/finds.ts";
2+
import { findDocumentByUuid } from "../utils/finds.ts";
33
import { log } from "../logger.ts";
44
import { Mapping } from "../effects/mapping.ts";
55
import { Flags } from "../utils/flags.ts";
@@ -60,23 +60,25 @@ class Sockets {
6060
effectData,
6161
uuid,
6262
}: AddEffectMessageData): Promise<Document[]> {
63-
const actor = await findActorByUuid(uuid);
63+
const document = await findDocumentByUuid(uuid);
6464
const activeEffectsToApply = [effectData];
6565

66-
if (!actor) return []; // This should already be checked for before the socket
66+
if (!document) return []; // This should already be checked for before the socket
6767

6868
const isDynamic = Flags.isDynamic(effectData);
6969
if (isDynamic) {
7070
const mapping = new Mapping();
7171
const systemDefinition = mapping.findSystemDefinitionForSystemId();
7272

73-
await systemDefinition?.dynamicEffectsHandler?.handleDynamicEffects(
74-
effectData,
75-
actor,
76-
);
73+
if (document instanceof Actor) {
74+
await systemDefinition?.dynamicEffectsHandler?.handleDynamicEffects(
75+
effectData,
76+
document,
77+
);
78+
}
7779
}
7880

79-
const createdEffects = await actor.createEmbeddedDocuments(
81+
const createdEffects = await document.createEmbeddedDocuments(
8082
"ActiveEffect",
8183
activeEffectsToApply,
8284
);
@@ -104,7 +106,9 @@ class Sockets {
104106
}
105107
}
106108

107-
log(`Added effect ${effectData.name} to ${actor.name} - ${actor.id}`);
109+
log(
110+
`Added effect ${effectData.name} to ${document.name} - ${document.id}`,
111+
);
108112

109113
return createdEffects;
110114
}
@@ -122,30 +126,34 @@ class Sockets {
122126
uuid,
123127
origin,
124128
}: RemoveEffectMessageData): Promise<void> {
125-
const actor = await findActorByUuid(uuid);
126-
127-
if (!actor) return; // This should already be checked for before the socket
128-
129-
const effectToRemove = actor.effects.find((activeEffect) => {
130-
const isConvenient = Flags.isConvenient(activeEffect);
131-
const isMatchingId = activeEffect.id === effectId;
132-
const isMatchingName = activeEffect.name === effectName;
133-
const isMatchingCeId =
134-
Flags.getCeEffectId(activeEffect) === effectId;
135-
136-
const matches =
137-
isConvenient &&
138-
(isMatchingId || isMatchingName || isMatchingCeId);
139-
140-
return origin ? matches && activeEffect.origin === origin : matches;
141-
});
129+
const document = await findDocumentByUuid(uuid);
130+
131+
if (!document) return; // This should already be checked for before the socket
132+
133+
const effectToRemove = (document.effects as any).find(
134+
(activeEffect: ActiveEffect<any>) => {
135+
const isConvenient = Flags.isConvenient(activeEffect);
136+
const isMatchingId = activeEffect.id === effectId;
137+
const isMatchingName = activeEffect.name === effectName;
138+
const isMatchingCeId =
139+
Flags.getCeEffectId(activeEffect) === effectId;
140+
141+
const matches =
142+
isConvenient &&
143+
(isMatchingId || isMatchingName || isMatchingCeId);
144+
145+
return origin
146+
? matches && activeEffect.origin === origin
147+
: matches;
148+
},
149+
);
142150

143151
if (!effectToRemove) return;
144152

145153
await effectToRemove.delete();
146154

147155
log(
148-
`Removed effect ${effectToRemove.name} from ${actor.name} - ${actor.id}`,
156+
`Removed effect ${effectToRemove.name} from ${document.name} - ${document.id}`,
149157
);
150158
}
151159
}

src/ts/utils/finds.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,45 @@ function findModuleById(moduleId: string): Module | undefined {
1111
* @param uuid The actor UUID
1212
* @returns the actor that was found via the UUID or undefined if not found
1313
*/
14-
async function findActorByUuid(
14+
async function findDocumentByUuid(
1515
uuid: string,
16-
): Promise<Actor<TokenDocument<any> | null> | undefined> {
17-
const actorToken = await fromUuid(uuid);
16+
): Promise<Actor<any> | Item<any> | undefined> {
17+
const document = await fromUuid(uuid);
18+
19+
if (!document) return undefined;
1820

19-
if (!actorToken) return undefined;
21+
if (document instanceof TokenDocument) {
22+
return document.actor ?? undefined;
23+
}
24+
25+
if (document instanceof Actor) {
26+
return document;
27+
}
2028

21-
if (actorToken instanceof TokenDocument) {
22-
return actorToken.actor ?? undefined;
23-
} else if (actorToken instanceof Actor) {
24-
return actorToken;
29+
if (document instanceof Item) {
30+
return document;
2531
}
2632

2733
return undefined;
2834
}
2935

30-
function findActorByUuidSync(
36+
function findDocumentByUuidSync(
3137
uuid: string,
32-
): Actor<TokenDocument<any> | null> | undefined {
33-
const actorToken = fromUuidSync(uuid);
38+
): Actor<any> | Item<any> | undefined {
39+
const document = fromUuidSync(uuid);
3440

35-
if (!actorToken) return undefined;
41+
if (!document) return undefined;
42+
43+
if (document instanceof TokenDocument) {
44+
return document.actor ?? undefined;
45+
}
46+
47+
if (document instanceof Actor) {
48+
return document;
49+
}
3650

37-
if (actorToken instanceof TokenDocument) {
38-
return actorToken.actor ?? undefined;
39-
} else if (actorToken instanceof Actor) {
40-
return actorToken;
51+
if (document instanceof Item) {
52+
return document;
4153
}
4254

4355
return undefined;
@@ -178,8 +190,8 @@ function findEffectByCeId(ceId: string): ActiveEffect<any> | undefined {
178190

179191
export {
180192
findModuleById,
181-
findActorByUuid,
182-
findActorByUuidSync,
193+
findDocumentByUuid,
194+
findDocumentByUuidSync,
183195
findFolder,
184196
findFolders,
185197
findAllEffects,

src/ts/utils/flags.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { ActiveEffectSource } from "types/foundry/common/documents/active-effect.js";
1+
import BaseActiveEffect, {
2+
ActiveEffectSource,
3+
} from "types/foundry/common/documents/active-effect.js";
24
import { ItemSource } from "types/foundry/common/documents/item.js";
35
import { MODULE_ID } from "../constants.ts";
46

@@ -17,7 +19,10 @@ class Flags {
1719
};
1820

1921
static getCeEffectId(
20-
effect: ActiveEffect<any> | PreCreate<ActiveEffectSource>,
22+
effect:
23+
| ActiveEffect<any>
24+
| BaseActiveEffect<any>
25+
| PreCreate<ActiveEffectSource>,
2126
): string | undefined {
2227
if (effect instanceof ActiveEffect) {
2328
return effect.getFlag(MODULE_ID, this.#KEYS.CE_EFFECT_ID) as

0 commit comments

Comments
 (0)