Snapshot pre-AE data on actors (closes #178)

This commit is contained in:
Oliver-Akins 2024-04-23 19:47:14 -06:00
parent d479ef7559
commit ec6378092f
3 changed files with 28 additions and 13 deletions

View file

@ -1,4 +1,15 @@
export class DotDungeonActor extends Actor { export class DotDungeonActor extends Actor {
/*
Using this to take a "snapshot" of the system data prior to applying AE's so
that the inputs can still have the non-modified value in them, while we still
provide all that data to AE's without needing to disable any inputs.
*/
prepareEmbeddedDocuments() {
this.preAE = foundry.utils.deepClone(this.system);
super.prepareEmbeddedDocuments();
};
async createEmbeddedItem(defaults, opts = {}) { async createEmbeddedItem(defaults, opts = {}) {
let items = await this.createEmbeddedDocuments(`Item`, defaults); let items = await this.createEmbeddedDocuments(`Item`, defaults);
if (!Array.isArray(items)) items = items ? [items] : []; if (!Array.isArray(items)) items = items ? [items] : [];

View file

@ -5,11 +5,16 @@ export class Player extends DotDungeonActor {
applyActiveEffects() { applyActiveEffects() {
super.applyActiveEffects(); super.applyActiveEffects();
// These are the (groups of) fields that ActiveEffects may modify safely /*
// and remain editable in the sheet. This needs to be done because of default These are the (groups of) fields that ActiveEffects may modify safely and
// Foundry behaviour that otherwise prevents these fields from being edited remain editable in the sheet. This needs to be done because of default
delete this.overrides.system.stats; Foundry behaviour that otherwise prevents these fields from being edited.
delete this.overrides.system.skills; The deletes must use optional chaining otherwise they can cause issues
during the document preparation lifecycle as an actor with no AE's affecting
anything in one of these areas will result in these paths being undefined.
*/
delete this.overrides.system?.stats;
delete this.overrides.system?.skills;
}; };
async createCustomPet() { async createCustomPet() {

View file

@ -74,7 +74,7 @@ export class PlayerSheetv2 extends GenericActorSheet {
/** @type {ActorHandler} */ /** @type {ActorHandler} */
const actor = this.actor; const actor = this.actor;
ctx.original = actor.toObject().system; ctx.preAE = actor.preAE;
ctx.system = actor.system; ctx.system = actor.system;
ctx.flags = actor.flags; ctx.flags = actor.flags;
ctx.items = this.actor.itemTypes; ctx.items = this.actor.itemTypes;
@ -93,13 +93,12 @@ export class PlayerSheetv2 extends GenericActorSheet {
get #statData() { get #statData() {
const stats = []; const stats = [];
const original = this.actor.toObject().system; const usedDice = new Set(Object.values(this.actor.system.stats));
const usedDice = new Set(Object.values(original.stats)); for (const statName in this.actor.system.stats) {
for (const statName in original.stats) {
const stat = { const stat = {
key: statName, key: statName,
name: localizer(`dotdungeon.stat.${statName}`), name: localizer(`dotdungeon.stat.${statName}`),
original: original.stats[statName], original: this.actor.preAE.stats[statName],
value: this.actor.system.stats[statName], value: this.actor.system.stats[statName],
}; };
@ -114,7 +113,7 @@ export class PlayerSheetv2 extends GenericActorSheet {
return { return {
value: die, value: die,
label: localizer(`dotdungeon.die.${die}`, { stat: statName }), label: localizer(`dotdungeon.die.${die}`, { stat: statName }),
disabled: usedDice.has(die) && original.stats[statName] !== die, disabled: usedDice.has(die) && this.actor.preAE.stats[statName] !== die,
}; };
}) })
]; ];
@ -130,9 +129,9 @@ export class PlayerSheetv2 extends GenericActorSheet {
key: skill, key: skill,
name: game.i18n.format(`dotdungeon.skills.${skill}`), name: game.i18n.format(`dotdungeon.skills.${skill}`),
value, value,
original: original.skills[statName][skill], original: this.actor.preAE.skills[statName][skill],
formula: `1` + stat.value + modifierToString(value, { spaces: true }), formula: `1` + stat.value + modifierToString(value, { spaces: true }),
rollDisabled: value === -1, rollDisabled: this.actor.preAE.skills[statName][skill] === -1,
}); });
}; };