From 44372d0a17f25c608c8d66f6644a49edbc30ab26 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 23 Mar 2026 00:22:54 -0600 Subject: [PATCH] Add a customizable weight formatter function --- module/apps/PlayerSheet.mjs | 11 ++++------- module/config.mjs | 7 +++++++ module/utils/formatWeight.mjs | 12 ++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 module/config.mjs create mode 100644 module/utils/formatWeight.mjs diff --git a/module/apps/PlayerSheet.mjs b/module/apps/PlayerSheet.mjs index 81b54ae..295fd3c 100644 --- a/module/apps/PlayerSheet.mjs +++ b/module/apps/PlayerSheet.mjs @@ -1,9 +1,9 @@ import { __ID__, filePath } from "../consts.mjs"; import { AttributeManager } from "./AttributeManager.mjs"; import { attributeSorter } from "../utils/attributeSort.mjs"; +import { config } from "../config.mjs"; import { TAFDocumentSheetConfig } from "./TAFDocumentSheetConfig.mjs"; import { TAFDocumentSheetMixin } from "./mixins/TAFDocumentSheetMixin.mjs"; -import { toPrecision } from "../utils/roundToPrecision.mjs"; const { HandlebarsApplicationMixin } = foundry.applications.api; const { ActorSheetV2 } = foundry.applications.sheets; @@ -220,7 +220,6 @@ export class PlayerSheet extends async _prepareItems(ctx) { ctx.tabActive = this.tabGroups.primary === `items`; - const weightUnit = game.settings.get(__ID__, `weightUnit`); let totalWeight = 0; ctx.itemGroups = []; @@ -237,24 +236,22 @@ export class PlayerSheet extends ctx.itemGroups.push({ name: groupName.titleCase(), items: preparedItems, - weight: toPrecision(summedWeight, 2) + weightUnit, + weight: config.weightFormatter(totalWeight), }); }; - totalWeight = toPrecision(totalWeight, 2); - ctx.totalWeight = totalWeight + weightUnit; + ctx.totalWeight = config.weightFormatter(totalWeight); ctx.carryCapacityPercent = Math.round(totalWeight / this.actor.system.carryCapacity * 100); }; async _prepareItem(item) { - const weightUnit = game.settings.get(__ID__, `weightUnit`); const ctx = { uuid: item.uuid, img: item.img, name: item.name, equipped: item.system.equipped, quantity: item.system.quantity, - weight: item.system.quantifiedWeight + weightUnit, + weight: config.weightFormatter(item.system.quantifiedWeight), isExpanded: this.#expandedItems.has(item.uuid), canExpand: item.system.description.length > 0, }; diff --git a/module/config.mjs b/module/config.mjs new file mode 100644 index 0000000..f3b076d --- /dev/null +++ b/module/config.mjs @@ -0,0 +1,7 @@ +import { formatWeight } from "./utils/formatWeight.mjs"; + +const { deepSeal } = foundry.utils; + +export const config = CONFIG.TAF = deepSeal({ + weightFormatter: formatWeight, +}); diff --git a/module/utils/formatWeight.mjs b/module/utils/formatWeight.mjs new file mode 100644 index 0000000..57f945d --- /dev/null +++ b/module/utils/formatWeight.mjs @@ -0,0 +1,12 @@ +import { __ID__ } from "../consts.mjs"; +import { toPrecision } from "./roundToPrecision.mjs"; + +/** + * Formats a numerical value as a weight. + * + * @param {number} weight The numerical weight to format + */ +export function formatWeight(weight) { + const unit = game.settings.get(__ID__, `weightUnit`); + return toPrecision(weight, 2) + unit; +};