Borders: Move main call in init hook
The main function is called at script invocation, which is nice and early but everything we do is done in hooks. This makes it not really useful, and harder to properly disable the module with the settings. So, register our `init` hook at script invocation and call the main function from within. This makes handling the disabling via the setting trivial, and ensures that *everything* is properly disabled. However, the `socketlib.ready` hook fires earlier than the `init` one, so register the socket at invocation as well, but only register the remote methods in the main function.
This commit is contained in:
parent
95bb1c3445
commit
9bc73100f3
1 changed files with 52 additions and 54 deletions
|
@ -213,7 +213,51 @@ function combat_hook_update_token_borders(combat, data) {
|
|||
}
|
||||
|
||||
function combat_border_main() {
|
||||
Hooks.once("init", () => {
|
||||
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) => {
|
||||
// Only apply the borders to player tokens
|
||||
if (drawn_token.actor?.type !== "character")
|
||||
return;
|
||||
|
||||
let has_played_turn = token_has_played(drawn_token);
|
||||
const token_size = drawn_token.getSize();
|
||||
drawn_token.addChild(create_new_border(BorderTypes.Active, token_size.width, token_size.height, !has_played_turn));
|
||||
drawn_token.addChild(create_new_border(BorderTypes.Played, token_size.width, token_size.height, has_played_turn));
|
||||
})
|
||||
|
||||
|
||||
Hooks.on("ready", () => {
|
||||
// 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) {
|
||||
return;
|
||||
}
|
||||
|
||||
Hooks.on("combatTurn", combat_hook_update_token_borders);
|
||||
Hooks.on("combatRound", combat_hook_update_token_borders);
|
||||
|
||||
// No Foundry hook on end of combat, so use Fabula Ultima's.
|
||||
Hooks.on(FUHooks.COMBAT_EVENT, (combat_event) => {
|
||||
if (combat_event.type === FU.combatEvent.endOfCombat) {
|
||||
for (let combatant of combat_event.combatants) {
|
||||
// End of combat, clear all tokens.
|
||||
socket.executeForEveryone(SocketMessages.SetBorder,
|
||||
combatant.sceneId, combatant.tokenId, false).then();
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 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'),
|
||||
|
@ -248,59 +292,13 @@ function combat_border_main() {
|
|||
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));
|
||||
});
|
||||
|
||||
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);
|
||||
})
|
||||
|
||||
// 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;
|
||||
|
||||
let has_played_turn = token_has_played(drawn_token);
|
||||
const token_size = drawn_token.getSize();
|
||||
drawn_token.addChild(create_new_border(BorderTypes.Active, token_size.width, token_size.height, !has_played_turn));
|
||||
drawn_token.addChild(create_new_border(BorderTypes.Played, token_size.width, token_size.height, has_played_turn));
|
||||
})
|
||||
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
Hooks.on("combatTurn", combat_hook_update_token_borders);
|
||||
Hooks.on("combatRound", combat_hook_update_token_borders);
|
||||
|
||||
// No Foundry hook on end of combat, so use Fabula Ultima's.
|
||||
Hooks.on(FUHooks.COMBAT_EVENT, (combat_event) => {
|
||||
if (combat_event.type === FU.combatEvent.endOfCombat) {
|
||||
for (let combatant of combat_event.combatants) {
|
||||
// End of combat, clear all tokens.
|
||||
socket.executeForEveryone(SocketMessages.SetBorder,
|
||||
combatant.sceneId, combatant.tokenId, false).then();
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
combat_border_main()
|
||||
combat_border_main()
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue