Import GK_Foundry_Macros_Fix_v5.zip do wg-greyknights (2025-08-21T19:30:33Z)

This commit is contained in:
2025-08-21 19:30:33 +00:00
parent edb3f0dffb
commit dc82906983
5 changed files with 95 additions and 20 deletions
@@ -1,20 +0,0 @@
// GK Auto Loadout on Archetype Assignment
// Rejestruje Hook createItem: jeśli do aktora dodano 'Grey Knight (Archetype)', uruchomi 'GK Loadout Manager'.
(() => {
if (window._gkAutoLoadoutHook) { ui.notifications.info("GK Auto-Loadout: już aktywne."); return; }
window._gkAutoLoadoutHook = Hooks.on("createItem", async (item, opts, userId) => {
try {
if (item?.type !== "archetype") return;
if (item?.name !== "Grey Knight (Archetype)") return;
const actor = item.parent;
if (!actor || game.userId !== userId) return;
const m = game.macros.getName("GK Loadout Manager") || game.macros.getName("GK-Loadout-Manager") || null;
if (m) {
await m.execute();
} else {
ui.notifications.warn("Brak makra 'GK Loadout Manager'. Utwórz je i uruchom ponownie.");
}
} catch (e) { console.error("GK Auto-Loadout error", e); }
});
ui.notifications.info("GK Auto-Loadout: hook zarejestrowany (createItem).");
})();
@@ -0,0 +1,15 @@
// GK Auto Loadout on Archetype Assignment (world esmodule friendly)
(() => {
if (window._gkAutoLoadoutHook) { return; }
window._gkAutoLoadoutHook = Hooks.on("createItem", async (item, opts, userId) => {
try {
if (item?.type !== "archetype") return;
if (item?.name !== "Grey Knight (Archetype)") return;
const actor = item.parent;
if (!actor || game.userId !== userId) return;
const macro = game.macros.getName("GK Loadout Manager") || game.macros.getName("GK-Loadout-Manager");
if (macro) await macro.execute();
else ui.notifications.warn("Brak makra 'GK Loadout Manager'. Dodaj je do świata (macros/).");
} catch (e) { console.error("GK Auto-Loadout error", e); }
});
})();
@@ -0,0 +1,80 @@
// GK Import World Items (from JSONs into Items Directory) — FIXED path handling
(async () => {
const FILES = [
"fvtt-Item-archetype-grey-knight_v5.json",
"fvtt-Item-ability-aegis-of-titan_v5.json",
"fvtt-Item-nemesis-force-halberd_v4.json",
"fvtt-Item-gk-armour-integrated-storm-bolter_v2.json",
"fvtt-Item-aegis-power-armour_v2.json",
"fvtt-Item-terminator-aegis-gk_v4.json"
];
const worldId = game.world?.id || game.world?.name || game.data?.world?.id || game.data?.world?.name;
const defaultDir = `worlds/${worldId}/foundry`;
const pick = await new Promise(resolve => {
const content = `
<p>Folder z JSON-ami (domyślnie: <code>${defaultDir}</code>)</p>
<div class="form-group">
<input type="text" name="path" style="width:100%" value="${defaultDir}">
<button type="button" class="browse"><i class="fas fa-folder-open"></i> Browse</button>
</div>
<p><label><input type="checkbox" name="overwrite" checked> Nadpisz istniejące itemy o tych nazwach</label></p>`;
const dlg = new Dialog({
title: "GK Import World Items (Fixed)",
content,
buttons: {
go: { label: "Importuj", icon: '<i class="fas fa-file-import"></i>', callback: html => resolve({ok:true, html}) },
cancel: { label: "Anuluj", callback: () => resolve({ok:false}) }
},
render: html => {
html.find("button.browse").on("click", () => {
const fp = new FilePicker({
type: "any",
callback: path => {
const folder = (path || "").replace(/[/\\][^/\\]*$/, "");
html.find('input[name="path"]').val(folder || defaultDir);
}
});
fp.browse("data", defaultDir);
});
},
default: "go"
});
dlg.render(true);
});
if (!pick?.ok) return;
const dir = pick.html.find('input[name="path"]').val()?.trim() || defaultDir;
const overwrite = pick.html.find('input[name="overwrite"]')[0]?.checked;
const ensureFolder = async (name, color="#4b6eaf") => {
let f = game.folders.find(x => x.type === "Item" && x.name === name);
if (!f) f = await Folder.create({name, type:"Item", color});
return f;
};
const worldFolder = await ensureFolder("Grey Knight Pack", "#4b6eaf");
const fetchJSON = async (path) => {
const res = await fetch(`/${path}`);
if (!res.ok) throw new Error(`Nie mogę pobrać: ${path} (HTTP ${res.status})`);
return await res.json();
};
let created = 0, updated = 0, skipped = 0;
for (const fn of FILES) {
try {
const full = `${dir}/${fn}`;
const data = await fetchJSON(full);
if (data._id) delete data._id;
data.folder = worldFolder.id;
const existing = game.items.getName(data.name);
if (existing) {
if (overwrite) { await existing.update(data); updated++; } else { skipped++; }
} else { await Item.create(data); created++; }
} catch (e) {
console.error(`Błąd importu ${fn}`, e);
ui.notifications.warn(`Błąd importu: ${fn} (konsola).`);
}
}
ui.notifications.info(`GK Import: utworzono ${created}, zaktualizowano ${updated}, pominięto ${skipped}.`);
})();