diff --git a/scripts/token_combat_border.mjs b/scripts/token_combat_border.mjs index 9884a41..9ee4a07 100644 --- a/scripts/token_combat_border.mjs +++ b/scripts/token_combat_border.mjs @@ -213,58 +213,11 @@ function combat_hook_update_token_borders(combat, data) { } function combat_border_main() { - Hooks.once("init", () => { - game.settings.register(MillsFabula.id, BorderSettings.BorderEnabled, { - name: game.i18n.localize('MF.Border.Settings.BorderEnabled.Name'), - hint: game.i18n.localize('MF.Border.Settings.BorderEnabled.Hint'), - type: Boolean, - config: true, - scope: 'world', - requiresReload: true, - default: true - }); - - // Only show the settings if the borders are enabled. - let borders_enabled = game.settings.get(MillsFabula.id, BorderSettings.BorderEnabled); - game.settings.register(MillsFabula.id, BorderSettings.BaseBorderPath, { - name: game.i18n.localize('MF.Border.Settings.BaseBorder.Name'), - hint: game.i18n.localize('MF.Border.Settings.BaseBorder.Hint'), - type: String, - config: borders_enabled, - scope: 'world', - requiresReload: true, - filePicker: 'image', - default: 'modules/the-mills-fabula/assets/default-borders/base.webp' - }); - - game.settings.register(MillsFabula.id, BorderSettings.PlayedBorderPath, { - name: game.i18n.localize('MF.Border.Settings.PlayedBorder.Name'), - hint: game.i18n.localize('MF.Border.Settings.PlayedBorder.Hint'), - type: String, - config: borders_enabled, - scope: 'world', - requiresReload: true, - filePicker: 'image', - default: 'modules/the-mills-fabula/assets/default-borders/played.webp' - }) - - // Create the border textures based on the image chose in the settings. - base_border = PIXI.BaseTexture.from(game.settings.get(MillsFabula.id, BorderSettings.BaseBorderPath)); - played_border = PIXI.BaseTexture.from(game.settings.get(MillsFabula.id, BorderSettings.PlayedBorderPath)); - }); - - Hooks.once("socketlib.ready", () => { - socket = socketlib.registerModule(MillsFabula.id); - socket.register(SocketMessages.CombatUpdateBorder, token_combat_visibility_remote_update); - socket.register(SocketMessages.SetBorder, token_remote_set_border_visibility); - }) + socket.register(SocketMessages.CombatUpdateBorder, token_combat_visibility_remote_update); + socket.register(SocketMessages.SetBorder, token_remote_set_border_visibility); // Create the borders from defined textures and add them to the tokens when they are first created on canvas. Hooks.on("drawToken", (drawn_token) => { - // FIXME: Handle deactivation properly - if (!game.settings.get(MillsFabula.id, BorderSettings.BorderEnabled)) { - return; - } // Only apply the borders to player tokens if (drawn_token.actor?.type !== "character") return; @@ -277,10 +230,6 @@ function combat_border_main() { Hooks.on("ready", () => { - // FIXME: Handle deactivation properly - if (!game.settings.get(MillsFabula.id, BorderSettings.BorderEnabled)) { - return; - } // Players cannot run the combat hooks used here, which trigger for the GM no matter what. // So register them for the GM only, who will execute the updates for players via SocketLib. if (!game.user?.isGM) { @@ -303,4 +252,53 @@ function combat_border_main() { }) } -combat_border_main() +// We need to setup socketlib early, doing it in the `init` hook is too late. +Hooks.once("socketlib.ready", () => { + socket = socketlib.registerModule(MillsFabula.id); +}) + +Hooks.once("init", () => { + game.settings.register(MillsFabula.id, BorderSettings.BorderEnabled, { + name: game.i18n.localize('MF.Border.Settings.BorderEnabled.Name'), + hint: game.i18n.localize('MF.Border.Settings.BorderEnabled.Hint'), + type: Boolean, + config: true, + scope: 'world', + requiresReload: true, + default: true + }); + + // Only show the settings if the borders are enabled. + let borders_enabled = game.settings.get(MillsFabula.id, BorderSettings.BorderEnabled); + game.settings.register(MillsFabula.id, BorderSettings.BaseBorderPath, { + name: game.i18n.localize('MF.Border.Settings.BaseBorder.Name'), + hint: game.i18n.localize('MF.Border.Settings.BaseBorder.Hint'), + type: String, + config: borders_enabled, + scope: 'world', + requiresReload: true, + filePicker: 'image', + default: 'modules/the-mills-fabula/assets/default-borders/base.webp' + }); + + game.settings.register(MillsFabula.id, BorderSettings.PlayedBorderPath, { + name: game.i18n.localize('MF.Border.Settings.PlayedBorder.Name'), + hint: game.i18n.localize('MF.Border.Settings.PlayedBorder.Hint'), + type: String, + config: borders_enabled, + scope: 'world', + requiresReload: true, + filePicker: 'image', + default: 'modules/the-mills-fabula/assets/default-borders/played.webp' + }) + + if (!game.settings.get(MillsFabula.id, BorderSettings.BorderEnabled)) { + return; + } + + // Create the border textures based on the image chose in the settings. + base_border = PIXI.BaseTexture.from(game.settings.get(MillsFabula.id, BorderSettings.BaseBorderPath)); + played_border = PIXI.BaseTexture.from(game.settings.get(MillsFabula.id, BorderSettings.PlayedBorderPath)); + + combat_border_main() +});