From 06aa96173c71239379bdbdc0990041690ee7aab3 Mon Sep 17 00:00:00 2001 From: Matheus Clemente Date: Tue, 2 Jul 2024 16:24:39 -0300 Subject: [PATCH] Add Option Groups + Setup World Scripts --- src/languages/en.json | 7 ++-- src/languages/es.json | 2 +- src/languages/fi.json | 2 +- src/languages/fr.json | 1 - src/languages/pl.json | 2 +- src/languages/pt-BR.json | 4 +-- src/main.js | 71 +++++++++++++++++++++++----------------- 7 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/languages/en.json b/src/languages/en.json index 01376bc..02cb492 100644 --- a/src/languages/en.json +++ b/src/languages/en.json @@ -2,12 +2,11 @@ "advanced-macros": { "MACROS": { "runForSpecificUser": "Execute for Specific User", - "runForSpecificUserTooltip": "This will cause the macro to be executed by the GM for a specific player connected.\nFor security reasons, only applies to GM-created macros with no other owners.", "runForEveryone": "Everyone", "runForEveryoneElse": "Everyone else", - "none": "None", - "runAsWorldScript": "World Script", - "runAsWorldScriptTooltip": "This will cause the macro to be executed automatically be the world once it's loaded.\nFor security reasons, only applies to GM-created macros with no other owners." + "WorldScript": "World Script", + "runAsWorldScript": "Ready Hook", + "runAsWorldScriptSetup": "Setup Hook" }, "setting": { "legacySlashCommand": { diff --git a/src/languages/es.json b/src/languages/es.json index 0597eaf..ba39f10 100644 --- a/src/languages/es.json +++ b/src/languages/es.json @@ -2,7 +2,7 @@ "advanced-macros": { "MACROS": { "none": "Nadie", - "runAsWorldScript": "Script de mundo", + "WorldScript": "Script de mundo", "runForSpecificUser": "Ejecutar para el usuario específico", "runForSpecificUserTooltip": "Esto hará que la macro sea ejecutada por el GM para un jugador concreto que esté conectado.\nPor motivos de seguridad, solo aplica a macros creadas por los GM que no tengan ningún otro propietario.", "runForEveryone": "Todo el mundo", diff --git a/src/languages/fi.json b/src/languages/fi.json index 43834ac..d35503e 100644 --- a/src/languages/fi.json +++ b/src/languages/fi.json @@ -12,7 +12,7 @@ "runForEveryone": "Kaikille", "runForEveryoneElse": "Kaikille muille", "none": "Ei mitään", - "runAsWorldScript": "Maailmaskripti", + "WorldScript": "Maailmaskripti", "runAsWorldScriptTooltip": "Tämä makro suoritetaan automaattisesti, kun maailma on ladattu.\nTurvallisuussyistä tämä koskee vain PJ:n luomia makroja, joilla ei ole muita omistajia." } } diff --git a/src/languages/fr.json b/src/languages/fr.json index 83116d1..a8a6d60 100644 --- a/src/languages/fr.json +++ b/src/languages/fr.json @@ -25,7 +25,6 @@ "runForSpecificUserTooltip": "Ainsi, la macro sera exécutée par le MJ pour un joueur spécifique connecté.\nPour des raisons de sécurité, ne s'applique qu'aux macros créées par le MJ et n'ayant aucun autre propriétaire.", "none": "Aucun", "runForEveryoneElse": "Tous les autres", - "runAsWorldScript": "World Script", "runAsWorldScriptTooltip": "La macro sera alors exécutée automatiquement pour le monde une fois qu'elle aura été chargée.\nPour des raisons de sécurité, ne s'applique qu'aux macros créées par le MJ et n'ayant pas d'autres propriétaires." }, "setting": { diff --git a/src/languages/pl.json b/src/languages/pl.json index 524a31c..367c3ce 100644 --- a/src/languages/pl.json +++ b/src/languages/pl.json @@ -25,7 +25,7 @@ "runAsGMTooltip": "Makro zostanie uruchomione przez GMa kiedy gracz włączy makro. \nDla bezpieczeństwa, tylko dotyczy makra stworzone przez GMa i nie posiadające innych twórców.", "runForSpecificUserTooltip": "Makro zostanie uruchomione przez GMa tylko dla wybranego zalogowanego gracza. \nDla bezpieczeństwa, tylko dotyczy makra stworzone przez GMa i nie posiadające innych twórców.", "runForEveryoneElse": "Wszyscy inni", - "runAsWorldScript": "Skrypt globalny", + "WorldScript": "Skrypt globalny", "runAsWorldScriptTooltip": "Spowoduje to automatyczne wykonanie makra na świecie po jego załadowaniu.\nZe względów bezpieczeństwa dotyczy tylko makr stworzonych przez GM bez innych właścicieli." }, "setting": { diff --git a/src/languages/pt-BR.json b/src/languages/pt-BR.json index 6dd5ace..49be41f 100644 --- a/src/languages/pt-BR.json +++ b/src/languages/pt-BR.json @@ -3,11 +3,11 @@ "MACROS": { "runForSpecificUser": "Executar para usuário específico", "runForSpecificUserTooltip": "Isto fará com que a macro seja executada pelo MJ para um usuário específico conectado.\nPor questões de segurança, só se aplica a macros criadas pelo MJ sem outros donos.", - "runAsGM": "Mestre do Jogo", "runForEveryone": "Todos", "runForEveryoneElse": "Todos os outros", "none": "Ninguém", - "runAsWorldScript": "Script do Mundo", + "WorldScript": "Script do Mundo", + "runAsGM": "Como {gm}", "runAsWorldScriptTooltip": "Isto fará com que a macro seja executada automaticamente quando o Mundo for carregado.\nPor questões de segurança, só se aplica a macros criadas pelo MJ sem outros donos." }, "setting": { diff --git a/src/main.js b/src/main.js index d6ef20d..067025e 100644 --- a/src/main.js +++ b/src/main.js @@ -167,6 +167,22 @@ Hooks.on("chatMessage", (chatLog, message, chatData) => { return true; }); +function runWorldScripts(key) { + const worldScripts = game.macros.contents.filter( + (macro) => macro.getFlag("advanced-macros", "runForSpecificUser") === key + ); + for (const macro of worldScripts) { + try { + macro.execute(); + console.debug(`Advanced Macros | Executed "${macro.name}" world script (ID: ${macro.id})`); + } catch(err) { + console.error(`Advanced Macros | Error executing "${macro.name}" world script (ID: ${macro.id})`, err); + } + } +} + +Hooks.once("setup", () => runWorldScripts("runAsWorldScriptSetup")); + Hooks.once("ready", () => { Hooks.on("renderMacroConfig", (obj, html, data) => { if (!game.user.isGM) return; @@ -176,64 +192,59 @@ Hooks.once("ready", () => { const typeGroup = form.find("select[name=type]").parent(".form-group"); const runForSpecificUser = macro.getFlag("advanced-macros", "runForSpecificUser"); const options = [ - { value: "", label: game.i18n.localize("advanced-macros.MACROS.none") }, { value: "GM", label: game.i18n.localize("USER.RoleGamemaster"), selected: runForSpecificUser === "GM", }, - ...["runForEveryone", "runForEveryoneElse", "runAsWorldScript"].map((run) => ({ + ...["runForEveryone", "runForEveryoneElse"].map((run) => ({ + value: run, + label: game.i18n.localize(`advanced-macros.MACROS.${run}`), + selected: runForSpecificUser === run, + group: "DOCUMENT.Users" + })), + ...["runAsWorldScriptSetup", "runAsWorldScript"].map((run) => ({ value: run, label: game.i18n.localize(`advanced-macros.MACROS.${run}`), selected: runForSpecificUser === run, + group: "advanced-macros.MACROS.WorldScript" })), - ...game.users - .filter((user) => !user.isGM) + ...game.users.players .map((user) => ({ value: user.id, label: user.name, selected: runForSpecificUser === user.id, + group: "PLAYERS.Title", })), - ]; // filter out null values + ]; - const optionElements = options - .map((option) => { - const selectedAttr = option.selected ? 'selected="selected"' : ""; - return ``; - }) - .join(""); + const selected = options.find((option) => option.selected)?.value ?? ""; + const select = foundry.applications.fields.createSelectInput({ + name: "flags.advanced-macros.runForSpecificUser", + options, + value: selected, + blank: "", + labelAttr: "label", + localize: true, + disabled: !canRunAsGM(macro) + }); const specificOneDiv = $(` -
+
- + ${select.outerHTML}
`); specificOneDiv.insertAfter(typeGroup); - const typeSelect = form.find("select[name=type]"); + const typeSelect = form.find("select[name='type']"); typeSelect.on("change", (event) => { if (event.target.value === "chat") specificOneDiv.hide(); else specificOneDiv.show(); }); }); - const worldScripts = game.macros.contents.filter( - (macro) => macro.getFlag("advanced-macros", "runForSpecificUser") === "runAsWorldScript" - ); - for (const macro of worldScripts) { - try { - macro.execute(); - console.debug(`Advanced Macros | Executed "${macro.name}" world script (ID: ${macro.id})`); - } catch(err) { - console.error(`Advanced Macros | Error executing "${macro.name}" world script (ID: ${macro.id})`, err); - } - } + runWorldScripts("runAsWorldScript"); }); /**