diff --git a/module/documents/Actor/Handler.mjs b/module/documents/Actor/Handler.mjs index ecdb0fd..b93352d 100644 --- a/module/documents/Actor/Handler.mjs +++ b/module/documents/Actor/Handler.mjs @@ -39,4 +39,16 @@ export class ActorHandler extends Actor { if (!this.fn?.[`createCustom${data.embeddedCreate}`]) return; this.fn?.[`createCustom${data.embeddedCreate}`].bind(this)($event); }; + + /** + * @param {ItemHandler} item + * @returns {boolean} true to allow the document to be embedded + */ + async preItemEmbed(item) { + let type = item.type[0].toUpperCase() + item.type.slice(1); + if (this.fn?.[`pre${type}Embed`]) { + return await this.fn?.[`pre${type}Embed`].bind(this)(item); + }; + return true; + }; }; diff --git a/module/documents/Actor/Player.mjs b/module/documents/Actor/Player.mjs index 6df033b..3cce41d 100644 --- a/module/documents/Actor/Player.mjs +++ b/module/documents/Actor/Player.mjs @@ -1,3 +1,5 @@ +import { ItemHandler } from "../Item/Handler.mjs"; + /** @this {Actor} */ async function genericEmbeddedUpdate($event) { let data = $event.delegateTarget.dataset; @@ -68,10 +70,29 @@ async function createCustomSpell() { }]); }; +/** + * @param {ItemHandler} item + * @this {Actor} + */ +async function preAspectEmbed(item) { + let limit = 1 + if (this.itemTypes.aspect.length >= limit) { + ui.notifications.error( + game.i18n.format( + `dotdungeon.notification.error.aspect-limit-reached`, + { limit } + ), + { console: false } + ); + return false; + }; +}; + export default { genericEmbeddedDelete, genericEmbeddedUpdate, createCustomItem, createCustomAspect, - createCustomSpell + createCustomSpell, + preAspectEmbed, }; \ No newline at end of file diff --git a/module/documents/Item/Aspect.mjs b/module/documents/Item/Aspect.mjs index 2bd2e31..733e252 100644 --- a/module/documents/Item/Aspect.mjs +++ b/module/documents/Item/Aspect.mjs @@ -1,14 +1,7 @@ -/** @this {Item} */ -async function _preCreate(data, options, user) { - if (this.parent.itemTypes.aspect.length > 0) { - ui.notifications.error( - game.i18n.format( - `dotdungeon.notification.error.aspect-limit-reached`, - { limit: 1 } - ), - { console: false } - ); - return false; +/** @this {ItemHandler} */ +async function _preCreate(_data, _options, _user) { + if (this.isEmbedded) { + return await this.actor?.preItemEmbed(this); }; }; diff --git a/module/documents/Item/Handler.mjs b/module/documents/Item/Handler.mjs index 7b8cd51..a136641 100644 --- a/module/documents/Item/Handler.mjs +++ b/module/documents/Item/Handler.mjs @@ -1,6 +1,11 @@ import AspectItem from "./Aspect.mjs"; +/** + * @extends {Item} + */ export class ItemHandler extends Item { + /** @override */ + itemTypes = { aspect: AspectItem, };