From de4267ff3c8679a281258f98395f0abffa9e4de3 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Sun, 13 Jul 2025 16:34:55 +0200 Subject: [PATCH] Fixed parsing of resource.max --- .../applications/sheets/actors/character.mjs | 5 +- module/data/item/base.mjs | 53 +++++++------------ module/helpers/handlebarsHelper.mjs | 6 +-- styles/less/global/inventory-item.less | 1 + .../sheets/global/partials/item-resource.hbs | 20 +++---- templates/ui/tooltip/parts/tooltipTags.hbs | 2 +- 6 files changed, 36 insertions(+), 51 deletions(-) diff --git a/module/applications/sheets/actors/character.mjs b/module/applications/sheets/actors/character.mjs index a12fbd1f..c551084f 100644 --- a/module/applications/sheets/actors/character.mjs +++ b/module/applications/sheets/actors/character.mjs @@ -500,9 +500,8 @@ export default class CharacterSheet extends DHBaseActorSheet { const item = this.getItem(event.currentTarget); if (!item) return; - const value = item.system.resource.max - ? Math.min(Number(event.currentTarget.value), item.system.resource.max) - : event.currentTarget.value; + const max = item.system.resource.max ? Roll.replaceFormulaData(item.system.resource.max, this.document) : null; + const value = max ? Math.min(Number(event.currentTarget.value), max) : event.currentTarget.value; await item.update({ 'system.resource.value': value }); this.render(); } diff --git a/module/data/item/base.mjs b/module/data/item/base.mjs index 53e5075d..eee3e071 100644 --- a/module/data/item/base.mjs +++ b/module/data/item/base.mjs @@ -44,7 +44,7 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { initial: CONFIG.DH.ITEM.itemResourceTypes.simple }), value: new fields.NumberField({ integer: true, min: 0, initial: 0 }), - max: new fields.NumberField({ nullable: true, initial: null }), + max: new fields.StringField({ nullable: true, initial: null }), icon: new fields.StringField(), recovery: new fields.StringField({ choices: CONFIG.DH.GENERAL.refreshTypes, @@ -94,25 +94,25 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { async _preCreate(data, options, user) { // Skip if no initial action is required or actions already exist - if (!this.metadata.hasInitialAction || !foundry.utils.isEmpty(this.actions)) return; + if (this.metadata.hasInitialAction && foundry.utils.isEmpty(this.actions)) { + const metadataType = this.metadata.type; + const actionType = { weapon: 'attack' }[metadataType]; + const ActionClass = game.system.api.models.actions.actionsTypes[actionType]; - const metadataType = this.metadata.type; - const actionType = { weapon: 'attack' }[metadataType]; - const ActionClass = game.system.api.models.actions.actionsTypes[actionType]; + const action = new ActionClass( + { + _id: foundry.utils.randomID(), + type: actionType, + name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name), + ...ActionClass.getSourceConfig(this.parent) + }, + { + parent: this.parent + } + ); - const action = new ActionClass( - { - _id: foundry.utils.randomID(), - type: actionType, - name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name), - ...ActionClass.getSourceConfig(this.parent) - }, - { - parent: this.parent - } - ); - - this.updateSource({ actions: [action] }); + this.updateSource({ actions: [action] }); + } } _onCreate(data) { @@ -132,23 +132,6 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { ); } - async _preUpdate(data) { - if (data.system?.resource?.max) { - const diceStatesKeys = Object.keys(this.resource.diceStates); - const resourceDiff = Math.abs(data.system.resource.max - diceStatesKeys.length); - if (!resourceDiff) return; - - const diceStates = {}; - const deleting = data.system.resource.max < diceStatesKeys.length; - [...Array(resourceDiff).keys()].forEach(nr => { - const key = deleting ? diceStatesKeys.length - 1 - nr : diceStatesKeys.length + nr; - diceStates[`${deleting ? '-=' : ''}${key}`] = deleting ? null : { value: null }; - }); - - foundry.utils.setProperty(data, 'system.resource.diceStates', diceStates); - } - } - async _preDelete() { if (!this.actor || this.actor.type !== 'character') return; diff --git a/module/helpers/handlebarsHelper.mjs b/module/helpers/handlebarsHelper.mjs index 27e116f7..1f7b7c1a 100644 --- a/module/helpers/handlebarsHelper.mjs +++ b/module/helpers/handlebarsHelper.mjs @@ -6,7 +6,7 @@ export default class RegisterHandlebarsHelpers { times: this.times, damageFormula: this.damageFormula, damageSymbols: this.damageSymbols, - tertiary: this.tertiary + rollParsed: this.rollParsed }); } @@ -42,7 +42,7 @@ export default class RegisterHandlebarsHelpers { return new Handlebars.SafeString(Array.from(symbols).map(symbol => ``)); } - static tertiary(a, b) { - return a ?? b; + static rollParsed(value, actor) { + return Roll.replaceFormulaData(value, actor); } } diff --git a/styles/less/global/inventory-item.less b/styles/less/global/inventory-item.less index aeaffe2b..bb12457a 100644 --- a/styles/less/global/inventory-item.less +++ b/styles/less/global/inventory-item.less @@ -216,6 +216,7 @@ filter: drop-shadow(0 0 1px @golden); z-index: 2; font-size: 18px; + cursor: pointer; } img { diff --git a/templates/sheets/global/partials/item-resource.hbs b/templates/sheets/global/partials/item-resource.hbs index 5a349e11..0123dfd9 100644 --- a/templates/sheets/global/partials/item-resource.hbs +++ b/templates/sheets/global/partials/item-resource.hbs @@ -5,15 +5,17 @@ {{else}}
- {{#each item.system.resource.diceStates as | state key |}} - -
- - - {{#if state.used}}{{/if}} -
-
- {{/each}} + {{#times (rollParsed item.system.resource.max item.parent)}} + {{#with (ifThen (lookup ../item.system.resource.diceStates this) (lookup ../item.system.resource.diceStates this) this) as | state |}} + +
+ + + {{#if state.used}}{{/if}} +
+
+ {{/with}} + {{/times}}
{{/if}} \ No newline at end of file diff --git a/templates/ui/tooltip/parts/tooltipTags.hbs b/templates/ui/tooltip/parts/tooltipTags.hbs index a02a729a..ba4e875f 100644 --- a/templates/ui/tooltip/parts/tooltipTags.hbs +++ b/templates/ui/tooltip/parts/tooltipTags.hbs @@ -6,7 +6,7 @@
{{localize feature.name}}
{{#if feature.img}}{{/if}} -
{{{localize (tertiary feature.description feature.system.description)}}}
+
{{{localize (ifThen feature.description feature.description feature.system.description)}}}
{{/each}} \ No newline at end of file