From 3e1f14f957392e1f8e67e30ea33641e6b63ebcd5 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 21 Apr 2026 16:57:05 -0600 Subject: [PATCH] Move the _preCreate logic into the Data Model instead of the Document --- module/data/Actor/player.mjs | 20 ++++++++++++++++++++ module/documents/Actor.mjs | 20 +------------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/module/data/Actor/player.mjs b/module/data/Actor/player.mjs index b3e1472..c525c9c 100644 --- a/module/data/Actor/player.mjs +++ b/module/data/Actor/player.mjs @@ -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() { return Object.keys(this.attr).length > 0; }; diff --git a/module/documents/Actor.mjs b/module/documents/Actor.mjs index 13fc3bb..98988ce 100644 --- a/module/documents/Actor.mjs +++ b/module/documents/Actor.mjs @@ -1,29 +1,11 @@ import { __ID__ } from "../consts.mjs"; const { Actor } = foundry.documents; -const { deepClone, hasProperty, setProperty } = foundry.utils; +const { deepClone, setProperty } = foundry.utils; export class TAFActor extends Actor { // #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 * gets changed (created, updated, deleted) so that we keep the cache as close