Move the _preCreate logic into the Data Model instead of the Document

This commit is contained in:
Oliver 2026-04-21 16:57:05 -06:00
parent f558b08c75
commit 3e1f14f957
2 changed files with 21 additions and 19 deletions

View file

@ -30,6 +30,26 @@ export class PlayerData extends foundry.abstract.TypeDataModel {
}; };
}; };
// #region Lifecycle
/**
* This makes sure that the actor gets created with the global attributes if
* they exist, while still allowing programmatic creation through the API with
* specific attributes.
*/
async _preCreate(data, options, user) {
// Assign the defaults from the world setting if they exist
const defaults = game.settings.get(__ID__, `actorDefaultAttributes`) ?? {};
if (!hasProperty(data, `system.attr`)) {
// Remove with issue: Foundry/taf#55
const value = game.release.generation > 13 ? _replace(defaults) : defaults;
this.updateSource({ "system.==attr": value });
};
return super._preCreate(data, options, user);
};
// #endregion Lifecycle
get hasAttributes() { get hasAttributes() {
return Object.keys(this.attr).length > 0; return Object.keys(this.attr).length > 0;
}; };

View file

@ -1,29 +1,11 @@
import { __ID__ } from "../consts.mjs"; import { __ID__ } from "../consts.mjs";
const { Actor } = foundry.documents; const { Actor } = foundry.documents;
const { deepClone, hasProperty, setProperty } = foundry.utils; const { deepClone, setProperty } = foundry.utils;
export class TAFActor extends Actor { export class TAFActor extends Actor {
// #region Lifecycle // #region Lifecycle
/**
* This makes sure that the actor gets created with the global attributes if
* they exist, while still allowing programmatic creation through the API with
* specific attributes.
*/
async _preCreate(data, options, user) {
// Assign the defaults from the world setting if they exist
const defaults = game.settings.get(__ID__, `actorDefaultAttributes`) ?? {};
if (!hasProperty(data, `system.attr`)) {
// Remove with issue: Foundry/taf#55
const value = game.release.generation > 13 ? _replace(defaults) : defaults;
this.updateSource({ "system.==attr": value });
};
return super._preCreate(data, options, user);
};
/** /**
* This resets the cache of the item groupings whenever a descedant document * This resets the cache of the item groupings whenever a descedant document
* gets changed (created, updated, deleted) so that we keep the cache as close * gets changed (created, updated, deleted) so that we keep the cache as close