Skip to content

Commit

Permalink
Link up Actor#toggleStatusEffect with ActorPF2e#toggleCondition (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Jun 18, 2024
1 parent a4dbf43 commit e2ada44
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/module/actor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1659,18 +1659,34 @@ class ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | n
}

/** Toggle a condition as present or absent. If a valued condition is toggled on, it will be set to a value of 1. */
async toggleCondition(conditionSlug: ConditionSlug): Promise<void> {
async toggleCondition(conditionSlug: ConditionSlug, options?: { active?: boolean }): Promise<boolean | void> {
if (!setHasElement(CONDITION_SLUGS, conditionSlug)) {
throw ErrorPF2e(`Unrecognized condition: ${conditionSlug}`);
}

if (this.hasCondition(conditionSlug)) {
await this.decreaseCondition(conditionSlug, { forceRemove: true });
} else {
const hasCondition = this.hasCondition(conditionSlug);
const active = options?.active ?? !hasCondition;

if (active && !hasCondition) {
await this.increaseCondition(conditionSlug);
} else if (active) {
return true;
} else if (!active && hasCondition) {
await this.decreaseCondition(conditionSlug, { forceRemove: true });
return false;
}
}

/** Redirect to `toggleCondition` if possible. */
override async toggleStatusEffect(
statusId: string,
options?: { active?: boolean; overlay?: boolean },
): Promise<boolean | void | ActiveEffect<this>> {
return setHasElement(CONDITION_SLUGS, statusId)
? this.toggleCondition(statusId, options)
: super.toggleStatusEffect(statusId, options);
}

/** Assess and pre-process this JSON data, ensuring it's importable and fully migrated */
override async importFromJSON(json: string): Promise<this> {
const processed = await preImportJSON(json);
Expand Down

0 comments on commit e2ada44

Please sign in to comment.