Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- #151: Fix senses configuration for dnd5e 4.1.
  • Loading branch information
Larkinabout committed Jan 10, 2025
1 parent 28b4f78 commit 5baa8a9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
3 changes: 3 additions & 0 deletions scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ export const CONSTANTS = {
SETTING: {
KEY: 'senses'
},
TEMPLATE: {
CONFIG_FORM_GROUP: 'modules/custom-dnd5e/templates/movement-senses-config-form-group.hbs'
},
UUID: 'Compendium.custom-dnd5e.custom-dnd5e-journals.JournalEntry.B48iqFBddUikMMer.JournalEntryPage.UC0cWoAGMtU6yISR'
},
SHEET: {
Expand Down
6 changes: 4 additions & 2 deletions scripts/forms/config-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ConfigForm extends CustomDnd5eForm {
async _prepareContext () {
this.config = foundry.utils.deepClone(CONFIG.DND5E[this.type])
this.setting = getSetting(this.settingKey)
const data = foundry.utils.mergeObject(this.config, this.setting)
const data = (this.includeConfig) ? foundry.utils.mergeObject(this.config, this.setting) : this.setting

const labelise = (data) => {
Object.entries(data).forEach(([key, value]) => {
Expand Down Expand Up @@ -477,10 +477,12 @@ export class SensesForm extends ConfigForm {
super()
this.requiresReload = false
this.settingKey = CONSTANTS.SENSES.SETTING.KEY
this.setConfig = setSenses
// this.setConfig = setSenses // Temporarily removed until custom senses is supported in the dnd5e system
this.setConfig = null
this.type = 'senses'
this.headerButton = JOURNAL_HELP_BUTTON
this.headerButton.uuid = CONSTANTS.SENSES.UUID
this.includeConfig = false
}

static DEFAULT_OPTIONS = {
Expand Down
5 changes: 4 additions & 1 deletion scripts/forms/custom-dnd5e-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class CustomDnd5eForm extends HandlebarsApplicationMixin(ApplicationV2) {
super(options)
this.nestable = false
this.nestType = 'children'
this.includeConfig = true
}

static DEFAULT_OPTIONS = {
Expand Down Expand Up @@ -419,7 +420,9 @@ export class CustomDnd5eForm extends HandlebarsApplicationMixin(ApplicationV2) {
async handleSubmit (processedFormData, settingKey, setConfig, requiresReload = false) {
try {
await setSetting(settingKey, processedFormData)
setConfig(processedFormData)
if (setConfig) {
setConfig(processedFormData)
}

if (requiresReload) {
SettingsConfig.reloadConfirm()
Expand Down
2 changes: 1 addition & 1 deletion scripts/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Hooks.on('init', async () => {

setAbilities(getSetting(CONSTANTS.ABILITIES.SETTING.KEY))
setCurrency(getSetting(CONSTANTS.CURRENCY.SETTING.KEY))
// setSenses(getSetting(CONSTANTS.SENSES.SETTING.KEY))
setSkills(getSetting(CONSTANTS.SKILLS.SETTING.KEY))

// Must be registered after abilities and skills are set
Expand Down Expand Up @@ -196,7 +197,6 @@ Hooks.on('ready', async () => {
setItemProperties(getSetting(CONSTANTS.ITEM_PROPERTIES.SETTING.KEY))
setItemRarity(getSetting(CONSTANTS.ITEM_RARITY.SETTING.KEY))
setLanguages(getSetting(CONSTANTS.LANGUAGES.SETTING.KEY))
setSenses(getSetting(CONSTANTS.SENSES.SETTING.KEY))
setSpellSchools(getSetting(CONSTANTS.SPELL_SCHOOLS.SETTING.KEY))
setToolIds(getSetting(CONSTANTS.TOOL_IDS.SETTING.KEY))
setWeaponIds(getSetting(CONSTANTS.WEAPON_IDS.SETTING.KEY))
Expand Down
64 changes: 45 additions & 19 deletions scripts/senses.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import { CONSTANTS, SHEET_TYPE } from './constants.js'
import { checkEmpty, getFlag, getSetting, registerMenu, registerSetting, resetDnd5eConfig } from './utils.js'
import { Logger, c5eLoadTemplates, checkEmpty, getFlag, getSetting, registerMenu, registerSetting, resetDnd5eConfig } from './utils.js'
import { SensesForm } from './forms/config-form.js'

const property = 'senses'
const constants = CONSTANTS.SENSES

/**
* Register
*/
export function register () {
registerSettings()
registerHooks()

const templates = [constants.TEMPLATE.CONFIG_FORM_GROUP]
c5eLoadTemplates(templates)
}

/**
* Register settings
*/
function registerSettings () {
registerMenu(
CONSTANTS.SENSES.MENU.KEY,
constants.MENU.KEY,
{
hint: game.i18n.localize(CONSTANTS.SENSES.MENU.HINT),
label: game.i18n.localize(CONSTANTS.SENSES.MENU.LABEL),
name: game.i18n.localize(CONSTANTS.SENSES.MENU.NAME),
icon: CONSTANTS.SENSES.MENU.ICON,
hint: game.i18n.localize(constants.MENU.HINT),
label: game.i18n.localize(constants.MENU.LABEL),
name: game.i18n.localize(constants.MENU.NAME),
icon: constants.MENU.ICON,
type: SensesForm,
restricted: true,
scope: 'world'
}
)

registerSetting(
CONSTANTS.SENSES.SETTING.KEY,
constants.SETTING.KEY,
{
scope: 'world',
config: false,
Expand All @@ -44,20 +48,42 @@ function registerSettings () {
* Register hooks
*/
function registerHooks () {
Hooks.on('renderActorSensesConfig', (app, html, data) => {
const actor = data.document
Hooks.on('renderMovementSensesConfig', async (app, html) => {
const actor = app.document
const systemSenses = ['blindsight', 'darkvision', 'tremorsense', 'truesight']
const inputs = html[0].querySelectorAll('input[type="number"]')
inputs.forEach(input => {
const key = input.name.split('.').pop()
if (!systemSenses.includes(key)) {
input.name = `flags.custom-dnd5e.${key}`
const flag = getFlag(actor, key)
if (flag) {
input.value = flag
const senses = getSetting(constants.SETTING.KEY)
const outerElement = html.querySelector('fieldset.card')
let lastElement = null

for (const [key, value] of Object.entries(senses)) {
const existingElement = html.querySelector(`input[name$='${key}']`)?.closest('.form-group')
if (existingElement) {
if (!value.visible) {
existingElement.remove()
} else {
if (lastElement) {
lastElement.insertAdjacentElement('afterend', existingElement)
}
lastElement = existingElement
}
} else {
if (value.visible && !systemSenses.includes(key)) {
const data = { label: value.label, inputName: `flags.custom-dnd5e.${key}`, inputValue: getFlag(actor, key) }
const template = await renderTemplate(constants.TEMPLATE.CONFIG_FORM_GROUP, data)

if (lastElement) {
lastElement.insertAdjacentHTML('afterend', template)
} else {
outerElement.insertAdjacentHTML('afterbegin', template)
}

const currentElement = html.querySelector(`input[name$='${key}']`)?.closest('.form-group')
if (currentElement) {
lastElement = currentElement
}
}
}
})
}
})

/**
Expand All @@ -69,7 +95,7 @@ function registerHooks () {

if (!sheetType) return

const senses = getSetting(CONSTANTS.SENSES.SETTING.KEY)
const senses = getSetting(constants.SETTING.KEY)
Object.entries(senses).forEach(([key, value]) => {
const flag = getFlag(app.document, key)
if (flag) {
Expand Down
6 changes: 6 additions & 0 deletions templates/movement-senses-config-form-group.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="form-group">
<label>{{label}}</label>
<div class="form-fields">
<input type="number" name="{{inputName}}" value="{{inputValue}}" min="0" step="1">
</div>
</div>

0 comments on commit 5baa8a9

Please sign in to comment.