helpers and modules

This commit is contained in:
Mike Bloy 2023-02-26 23:27:50 -06:00
parent 9e62b26a93
commit 43ee24bd46
5 changed files with 46 additions and 1 deletions

View File

@ -16,7 +16,7 @@
"verified": "10" "verified": "10"
}, },
"esmodules": [ "esmodules": [
"./scripts/module.ejs" "./scripts/module.js"
], ],
"packs": [ "packs": [
{ {

15
scripts/api.js Normal file
View File

@ -0,0 +1,15 @@
import { helpers } from './helpers.js'
export class api {
static registerFunctions () {
console.log('SWADE MB Helpers initialized')
api.globals()
}
static globals () {
globalThis.swadeMBHelpers = {
DEBUG: true,
runOnTargetOrSelectedTokens: helpers.runOnTargetOrSelectedTokens
}
}
}

14
scripts/helpers.js Normal file
View File

@ -0,0 +1,14 @@
export class helpers {
static runOnTargetOrSelectedTokens (runFunc) {
let tokens = []
const targets = Array.from(game.user.targets)
if (targets.length > 0) tokens = targets
if (canvas.tokens.controlled.length > 0) tokens = canvas.tokens.controlled
if (tokens.length > 0) {
runFunc(tokens)
} else {
ui.notifications.error('Please select or target a token')
}
}
}

View File

16
scripts/module.js Normal file
View File

@ -0,0 +1,16 @@
import { api } from './api.js'
function _checkModule (name) {
if (!game.modules.get(name)?.active && game.user.isGM) {
let action = 'install and activate'
if (game.modules.get(name)) action = 'activate'
ui.notifications.error(
`SWADE MB Helpers requires the ${name} module. Please ${action} it.`)
}
}
Hooks.on('setup', api.registerFunctions)
Hooks.on('ready', () => {
_checkModule('warpgate')
})