76 lines
2.0 KiB
JavaScript
76 lines
2.0 KiB
JavaScript
const MODULE_ID = "ringworld-content";
|
|
|
|
function isRingworldWorld() {
|
|
return String(game.system?.id || "") === "ringworld";
|
|
}
|
|
|
|
function canShowControls() {
|
|
return isRingworldWorld() && Boolean(game.user?.isGM);
|
|
}
|
|
|
|
function getRingworldApi() {
|
|
return game.ringworld || null;
|
|
}
|
|
|
|
async function importRingworldContent() {
|
|
if (!canShowControls()) return;
|
|
const api = getRingworldApi();
|
|
if (!api?.seedWorldCatalog) {
|
|
ui.notifications?.error("Ringworld API unavailable. Ensure Ringworld system is active.");
|
|
return;
|
|
}
|
|
|
|
const result = await api.seedWorldCatalog({ notify: true });
|
|
if (result && typeof result.added === "number") {
|
|
ui.notifications?.info(`Ringworld Content import complete: +${result.added}, updated ${result.updated || 0}.`);
|
|
}
|
|
}
|
|
|
|
function injectDirectoryButton(htmlLike) {
|
|
if (!canShowControls()) return;
|
|
if (!htmlLike) return;
|
|
const html = ("find" in htmlLike) ? htmlLike : $(htmlLike);
|
|
if (!html?.length || html.find(".ringworld-content-import").length) return;
|
|
|
|
const button = $(
|
|
`<button type="button" class="ringworld-directory-button ringworld-content-import">Import Ringworld Content</button>`
|
|
);
|
|
button.on("click", async () => {
|
|
await importRingworldContent();
|
|
});
|
|
|
|
const candidates = [
|
|
html.find(".directory-footer"),
|
|
html.find("footer.directory-footer"),
|
|
html.find("footer"),
|
|
html.find(".footer-actions"),
|
|
html.find(".header-actions"),
|
|
html.find(".action-buttons")
|
|
];
|
|
const container = candidates.find((candidate) => candidate.length);
|
|
if (container) {
|
|
container.first().append(button);
|
|
return;
|
|
}
|
|
|
|
html.append($('<div class="ringworld-directory-controls"></div>').append(button));
|
|
}
|
|
|
|
Hooks.once("ready", () => {
|
|
if (!canShowControls()) return;
|
|
game.ringworldContent = {
|
|
importCatalog: importRingworldContent
|
|
};
|
|
});
|
|
|
|
Hooks.on("renderItemDirectory", (_app, html) => {
|
|
injectDirectoryButton(html);
|
|
});
|
|
|
|
Hooks.on("renderSidebar", () => {
|
|
if (!canShowControls()) return;
|
|
const itemsElement = ui.items?.element;
|
|
injectDirectoryButton(itemsElement);
|
|
});
|
|
|