Skip to content

Commit

Permalink
Add hook to replace race on character
Browse files Browse the repository at this point in the history
Closes #384
  • Loading branch information
kmoschcau committed Aug 28, 2022
1 parent def442d commit dd6b2b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/typescript/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -17,6 +18,8 @@ export default function registerForHooks(): void {
registerForRenderChatLog();
registerForRenderChatMessage();

registerForPreCreateItem();

registerForPreUpdateToken();

registerForUpdateActor();
Expand Down
30 changes: 30 additions & 0 deletions src/main/typescript/hooks/preCreateItem.ts
Original file line number Diff line number Diff line change
@@ -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<Hooks.PreCreateDocument<typeof Item>>(
"preCreateItem",
replaceCharacterRace
);
}

type HookParams = Parameters<Hooks.PreCreateDocument<typeof Item>>;

/**
* 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<WvItem> => item.id !== null)
.map((item) => item.id)
);
}
5 changes: 4 additions & 1 deletion src/main/typescript/hooks/preUpdateToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hooks.PreUpdateDocument<typeof TokenDocument>>(
"preUpdateToken",
checkApCostForMovement
);
}

type HookParams = Parameters<Hooks.PreUpdateDocument<typeof TokenDocument>>;
Expand Down

0 comments on commit dd6b2b5

Please sign in to comment.