Fixed parsing of resource.max

This commit is contained in:
WBHarry 2025-07-13 16:34:55 +02:00
parent 9fc1005caa
commit de4267ff3c
6 changed files with 36 additions and 51 deletions

View file

@ -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();
}

View file

@ -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;

View file

@ -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 => `<i class="fa-solid ${symbol}"></i>`));
}
static tertiary(a, b) {
return a ?? b;
static rollParsed(value, actor) {
return Roll.replaceFormulaData(value, actor);
}
}