Refactor resource settings to not be a method (#1723)

This commit is contained in:
Carlos Fernandez 2026-03-10 20:37:11 -04:00 committed by GitHub
parent f72fb3cf31
commit f298b2160a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 82 additions and 66 deletions

View file

@ -9,7 +9,7 @@ export default class DhCreature extends BaseDataActor {
return {
...super.defineSchema(),
resources: new fields.SchemaField({
...Object.values(CONFIG.DH.RESOURCE[`all${this.metadata.type.capitalize()}Resources`]()).reduce(
...Object.values(CONFIG.DH.RESOURCE[this.metadata.type].all).reduce(
(acc, resource) => {
if (resource.max !== undefined) {
acc[resource.id] = resourceField(
@ -58,7 +58,7 @@ export default class DhCreature extends BaseDataActor {
prepareDerivedData() {
super.prepareDerivedData();
const resources = CONFIG.DH.RESOURCE[`all${this.parent.type.capitalize()}Resources`]();
const resources = CONFIG.DH.RESOURCE[this.metadata.type].all;
if (resources) {
for (const [key, value] of Object.entries(this.resources)) {
value.label = resources[key]?.label;

View file

@ -186,6 +186,12 @@ export default class DhHomebrew extends foundry.abstract.DataModel {
};
}
/**
* Backed up configured resources stored to prevent overwriting module settings.
* As of V13, setting objects are not preserved between reprepares.
*/
static originalResources = null;
/** @inheritDoc */
_initializeSource(source, options = {}) {
source = super._initializeSource(source, options);
@ -195,6 +201,53 @@ export default class DhHomebrew extends foundry.abstract.DataModel {
}
return source;
}
/** Invoked by the setting when data changes */
handleChange() {
if (this.maxFear) {
if (ui.resources) ui.resources.render({ force: true });
}
this.refreshConfig();
this.#resetActors();
}
/** Update config values based on homebrew data */
refreshConfig() {
DhHomebrew.originalResources ??= foundry.utils.duplicate(CONFIG.DH.RESOURCE);
for (const [actorType, actorData] of Object.entries(this.resources)) {
for (const [resourceKey, resourceData] of Object.entries(actorData.resources)) {
if (resourceKey in DhHomebrew.originalResources[actorType].all) {
continue;
}
CONFIG.DH.RESOURCE[actorType].all[resourceKey] = resourceData.toObject();
CONFIG.DH.RESOURCE[actorType].all[resourceKey].id = resourceKey;
}
}
}
/**
* Triggers a reset and non-forced re-render on all given actors (if given)
* or all world actors and actors in all scenes to show immediate results for a changed setting.
*/
#resetActors() {
const actors = new Set(
[
game.actors.contents,
game.scenes.contents.flatMap(s => s.tokens.contents).flatMap(t => t.actor ?? [])
].flat()
);
for (const actor of actors) {
for (const app of Object.values(actor.apps)) {
for (const element of app.element?.querySelectorAll('prose-mirror.active')) {
element.open = false; // This triggers a save
}
}
actor.reset();
actor.render();
}
}
}
export class Resource extends foundry.abstract.DataModel {