From 1b715031a875481848494b86c82180e587451518 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Tue, 16 Feb 2021 02:07:34 +0100 Subject: [PATCH] Tie active effects to equipped flag for token actors, too --- src/module/actor/actor.ts | 145 ++++++++++++++++++------- src/module/actor/sheets/actor-sheet.ts | 1 - 2 files changed, 107 insertions(+), 39 deletions(-) diff --git a/src/module/actor/actor.ts b/src/module/actor/actor.ts index 648952bb..3800cedf 100644 --- a/src/module/actor/actor.ts +++ b/src/module/actor/actor.ts @@ -204,24 +204,12 @@ export class DS4Actor extends Actor { options?: Record, ): Promise | Promise { if (embeddedName === "OwnedItem") { - this._preCreateOwnedItem(data); + preCreateOwnedItem.bind(this)(data); return super.createEmbeddedEntity(embeddedName, data, options); } return super.createEmbeddedEntity(embeddedName, data, options); } - /** - * If the item that is going to be created is equipable, set it to be non equipped and disable all ActiveEffects - * contained in the item - * @param itemData - The data of the item to be created - */ - protected _preCreateOwnedItem(itemData: DeepPartial): void { - if (itemData.data && "equipped" in itemData.data) { - itemData.effects = itemData.effects?.map((effect) => ({ ...effect, disabled: true })); - itemData.data.equipped = false; - } - } - /** @override */ // TODO(types): Improve typing once it's fixed in upstream updateEmbeddedEntity(embeddedName: string, data: unknown[], options?: Entity.UpdateOptions): Promise; @@ -232,32 +220,113 @@ export class DS4Actor extends Actor { options?: Record, ): Promise { if (embeddedName === "OwnedItem") { - this._preUpdateOwnedItem(updateData as DeepPartial | Array>); + preUpdateOwnedItem.bind(this)(updateData as DeepPartial | Array>); } return super.updateEmbeddedEntity(embeddedName, updateData, options); } - - /** - * If the equipped flag of one or more items changed, update all ActiveEffects originating from those items - * accordingly. - * @param updateData - The change that is going to be applied to the owned item(s) - */ - private _preUpdateOwnedItem(updateData: DeepPartial | Array>): void { - const dataArray = updateData instanceof Array ? updateData : [updateData]; - dataArray.forEach((data) => { - if (data.data && "equipped" in data.data) { - const equipped = data.data.equipped; - const origin = `Actor.${this.id}.OwnedItem.${data._id}`; - const effects = this.effects - .filter((e) => e.data.origin === origin) - .map((e) => { - const effectData = duplicate(e.data); - effectData.disabled = !(equipped ?? true); - return effectData; - }); - if (effects.length > 0) - this.updateEmbeddedEntity("ActiveEffect", (effects as unknown) as Record); - } - }); - } } + +/** + * If the item that is going to be created is equipable, set it to be non equipped and disable all ActiveEffects + * contained in the item + * @param itemData - The data of the item to be created + */ +function preCreateOwnedItem(itemData: DeepPartial | DeepPartial[]): void { + const dataArray = itemData instanceof Array ? itemData : [itemData]; + dataArray.forEach((data) => { + if (data.data && "equipped" in data.data) { + data.effects = data.effects?.map((effect) => ({ ...effect, disabled: true })); + data.data.equipped = false; + } + }); + console.log(itemData); +} + +/** + * If the equipped flag of one or more items changed, update all ActiveEffects originating from those items + * accordingly. + * @param updateData - The change that is going to be applied to the owned item(s) + */ +function preUpdateOwnedItem( + this: T, + updateData: DeepPartial | Array>, +): void { + const dataArray = updateData instanceof Array ? updateData : [updateData]; + dataArray.forEach((data) => { + if (data.data && "equipped" in data.data) { + const equipped = data.data.equipped; + const origin = `Actor.${this.id}.OwnedItem.${data._id}`; + const effects = this.effects + .filter((e) => e.data.origin === origin) + .map((e) => { + const effectData = duplicate(e.data); + effectData.disabled = !(equipped ?? true); + return effectData; + }); + if (effects.length > 0) + this.updateEmbeddedEntity("ActiveEffect", (effects as unknown) as Record); + } + }); +} + +const oldTokenCreateEmbeddedEntity = ActorTokenHelpers.prototype.createEmbeddedEntity; +const oldTokenUpdateEmbeddedEntity = ActorTokenHelpers.prototype.updateEmbeddedEntity; + +function tokenCreateEmbeddedEntity( + this: T, + embeddedName: "OwnedItem" | "ActiveEffect", + data: Expanded extends DeepPartial | DeepPartial + ? U | U[] + : + | DeepPartial + | DeepPartial[] + | DeepPartial + | DeepPartial[], + options?: Entity.UpdateOptions, +) { + if (embeddedName === "OwnedItem") { + const itemDataArray = + data instanceof Array + ? data.map((it) => expandObject(it) as DS4ItemData) + : [expandObject(data as Record) as DS4ItemData]; + preCreateOwnedItem.bind(this)(itemDataArray); + // eslint-disable-next-line + // @ts-ignore + return oldTokenCreateEmbeddedEntity.bind(this)(embeddedName, itemDataArray, options); + } + // eslint-disable-next-line + // @ts-ignore + return oldTokenCreateEmbeddedEntity.bind(this)(embeddedName, data, options); +} + +function tokenUpdateEmbeddedEntity( + this: T, + embeddedName: "OwnedItem" | "ActiveEffect", + data: Expanded extends + | (DeepPartial & { _id: string }) + | (DeepPartial & { _id: string }) + ? U | U[] + : + | (DeepPartial & { _id: string }) + | (DeepPartial & { _id: string })[] + | (DeepPartial & { _id: string }) + | (DeepPartial & { _id: string })[], + options?: Entity.UpdateOptions, +) { + if (embeddedName === "OwnedItem") { + const itemDataArray = + data instanceof Array + ? data.map((it) => expandObject(it) as DS4ItemData) + : [expandObject(data as Record) as DS4ItemData]; + preUpdateOwnedItem.bind(this)(itemDataArray); + // eslint-disable-next-line + // @ts-ignore + return oldTokenUpdateEmbeddedEntity.bind(this)(embeddedName, itemDataArray, options); + } + // eslint-disable-next-line + // @ts-ignore + return oldTokenUpdateEmbeddedEntity.bind(this)(embeddedName, data, options); +} + +ActorTokenHelpers.prototype.createEmbeddedEntity = tokenCreateEmbeddedEntity; +ActorTokenHelpers.prototype.updateEmbeddedEntity = tokenUpdateEmbeddedEntity; diff --git a/src/module/actor/sheets/actor-sheet.ts b/src/module/actor/sheets/actor-sheet.ts index 9ad9974d..3edbbf0d 100644 --- a/src/module/actor/sheets/actor-sheet.ts +++ b/src/module/actor/sheets/actor-sheet.ts @@ -127,7 +127,6 @@ export class DS4ActorSheet extends ActorSheet> { */ protected _onItemChange(ev: JQuery.ChangeEvent): void { ev.preventDefault(); - console.log("Current target:", $(ev.currentTarget).get(0)["name"]); const el: HTMLFormElement = $(ev.currentTarget).get(0); const id = $(ev.currentTarget).parents(".item").data("itemId"); const item = duplicate(this.actor.getOwnedItem(id));