From 0e417ca6cb48520a32490be6ac901c536b9bafdc Mon Sep 17 00:00:00 2001 From: Michal Tuszowski Date: Sat, 23 Aug 2025 19:48:54 +0000 Subject: [PATCH] Import gk-greyknight-0.6.4.3.zip do wg-greyknights (2025-08-23T19:48:54Z) --- wg-greyknights/module.json | 2 +- wg-greyknights/scripts/helpers.js | 72 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/wg-greyknights/module.json b/wg-greyknights/module.json index 3d164d6..583f4d0 100644 --- a/wg-greyknights/module.json +++ b/wg-greyknights/module.json @@ -1,7 +1,7 @@ { "id": "gk-greyknight", "title": "Grey Knights — Compendium (Wrath & Glory)", - "version": "0.6.4.2", + "version": "0.6.4.3", "compatibility": { "minimum": "11", "verified": "13" diff --git a/wg-greyknights/scripts/helpers.js b/wg-greyknights/scripts/helpers.js index b6559ce..397cf3f 100644 --- a/wg-greyknights/scripts/helpers.js +++ b/wg-greyknights/scripts/helpers.js @@ -70,3 +70,75 @@ Hooks.on("preCreateChatMessage", (doc, data, options, userId) => { else if (!prev) combat.setFlag("gk-greyknight", key, actor.id); } catch (e) { console.error("gk-greyknight helpers (house-rule reminder):", e); } }); + +// === gk-greyknight 0.6.4.3 — Brotherhood discipline prompt + starter grants === +Hooks.once("ready", () => { + const FLAG_SCOPE = "gk-greyknight"; + // Prevent duplicate hook registration on hot-reload + if (window._gkBroHookId) { Hooks.off("createItem", window._gkBroHookId); window._gkBroHookId = null; } + + async function findItemInAllPacksByName(name, type=null) { + for (const pack of game.packs) { + if (pack.documentName !== "Item") continue; + const index = await pack.getIndex(); + const hit = index.find(e => e.name === name && (!type || e.type === type)); + if (hit) return await pack.getDocument(hit._id); + } + return null; + } + async function ensureHasItemByName(actor, name, type=null) { + const existing = actor.items?.find(i => i.name === name && (!type || i.type === type)); + if (existing) return existing; + const doc = await findItemInAllPacksByName(name, type); + if (!doc) return null; + const data = doc.toObject(); delete data._id; + const [created] = await actor.createEmbeddedDocuments("Item", [data]); + return created; + } + async function promptDiscipline(actor) { + const existing = await actor.getFlag(FLAG_SCOPE, "discipline"); + if (existing) return existing; + return await new Promise(resolve => { + new Dialog({ + title: "Brotherhood of Psykers — Choose Discipline", + content: `

Select a discipline for this character:

+ + `, + buttons: { + ok: { label: "Confirm", icon: "", callback: html => resolve(html.find('input[name="disc"]:checked').val() || "sanctic") }, + cancel: { label: "Cancel", callback: () => resolve(null) } + }, + default: "ok" + }).render(true); + }); + } + + window._gkBroHookId = Hooks.on("createItem", async (item, opts, userId) => { + try { + if (game.userId !== userId) return; + if (item?.type !== "ability" || item?.name !== "Brotherhood of Psykers") return; + const actor = item.parent; if (!actor) return; + + const chosen = await promptDiscipline(actor); + if (!chosen) return; + await actor.setFlag(FLAG_SCOPE, "discipline", chosen); + + // Ensure PSYKER keyword so powers are available + const psyker = await ensureHasItemByName(actor, "PSYKER", "keyword"); + if (!psyker) ui.notifications?.warn("PSYKER keyword not found in loaded packs. Add it manually from wng-core."); + + // Starter power by discipline + if (chosen === "sanctic") { + const ok = await ensureHasItemByName(actor, "Hammerhand (Sanctic)", "psychicPower"); + if (!ok) ui.notifications?.warn("Couldn't auto-add Hammerhand (Sanctic). Drag a Sanctic power from the GK pack."); + } else { + const ok = await ensureHasItemByName(actor, "Smite", "psychicPower"); + if (!ok) { + ui.notifications?.warn("Smite not found in available packs. Please add a Librarius power manually."); + game.ui?.sidebar?.tabs?.compendium?.activate(); + } + } + } catch (e) { console.error("gk-greyknight — Brotherhood prompt:", e); } + }); +}); +