From dd6b2b522a55234c25a86f9a1cb526c9e8b8ec1c Mon Sep 17 00:00:00 2001 From: Kai Moschcau Date: Sun, 28 Aug 2022 12:27:17 +0200 Subject: [PATCH] Add hook to replace race on character Closes #384 --- src/main/typescript/hooks/index.ts | 3 +++ src/main/typescript/hooks/preCreateItem.ts | 30 +++++++++++++++++++++ src/main/typescript/hooks/preUpdateToken.ts | 5 +++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/main/typescript/hooks/preCreateItem.ts diff --git a/src/main/typescript/hooks/index.ts b/src/main/typescript/hooks/index.ts index b573bf49..1b22f3c1 100644 --- a/src/main/typescript/hooks/index.ts +++ b/src/main/typescript/hooks/index.ts @@ -1,6 +1,7 @@ import registerForDragRulerReady from "./dragRuler/ready.js"; import registerForHotbarDrop from "./hotbarDrop.js"; import registerForInit from "./init.js"; +import registerForPreCreateItem from "./preCreateItem.js"; import registerForPreUpdateToken from "./preUpdateToken.js"; import registerForQuenchReady from "./quench/ready.js"; import registerForReady from "./ready.js"; @@ -17,6 +18,8 @@ export default function registerForHooks(): void { registerForRenderChatLog(); registerForRenderChatMessage(); + registerForPreCreateItem(); + registerForPreUpdateToken(); registerForUpdateActor(); diff --git a/src/main/typescript/hooks/preCreateItem.ts b/src/main/typescript/hooks/preCreateItem.ts new file mode 100644 index 00000000..be54b675 --- /dev/null +++ b/src/main/typescript/hooks/preCreateItem.ts @@ -0,0 +1,30 @@ +import WvActor from "../actor/wvActor.js"; +import { TYPES } from "../constants.js"; +import type WvItem from "../item/wvItem.js"; + +export default function registerForPreCreateItem(): void { + Hooks.on>( + "preCreateItem", + replaceCharacterRace + ); +} + +type HookParams = Parameters>; + +/** + * Replace existing Race items on an actor, if a Race is created on an actor. + */ +function replaceCharacterRace(document: HookParams[0]): false | void { + if ( + document.data.type !== TYPES.ITEM.RACE || + !(document.parent instanceof WvActor) + ) + return; + + document.parent.deleteEmbeddedDocuments( + "Item", + document.parent.itemTypes.race + .filter((item): item is StoredDocument => item.id !== null) + .map((item) => item.id) + ); +} diff --git a/src/main/typescript/hooks/preUpdateToken.ts b/src/main/typescript/hooks/preUpdateToken.ts index 70cf5deb..35503ac3 100644 --- a/src/main/typescript/hooks/preUpdateToken.ts +++ b/src/main/typescript/hooks/preUpdateToken.ts @@ -7,7 +7,10 @@ import { LOG } from "../systemLogger.js"; /** Register system callbacks for the preUpdateToken hook. */ export default function registerForPreUpdateToken(): void { - Hooks.on("preUpdateToken", checkApCostForMovement); + Hooks.on>( + "preUpdateToken", + checkApCostForMovement + ); } type HookParams = Parameters>;