From 05dcb9afbdc78e12d70cb00969cd57a8d099c887 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 25 Apr 2026 19:38:53 -0600 Subject: [PATCH] Prevent attributes with a min/max from going out of bounds during update --- module/data/Item/attribute.mjs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/module/data/Item/attribute.mjs b/module/data/Item/attribute.mjs index e7769e9..8e38272 100644 --- a/module/data/Item/attribute.mjs +++ b/module/data/Item/attribute.mjs @@ -1,6 +1,7 @@ import { isValidID, toID } from "../../utils/toID.mjs"; +import { clamp } from "../../utils/clamp.mjs"; -const { hasProperty } = foundry.utils; +const { getProperty, hasProperty, setProperty } = foundry.utils; export class AttributeItemData extends foundry.abstract.TypeDataModel { // #region Schema @@ -80,6 +81,17 @@ export class AttributeItemData extends foundry.abstract.TypeDataModel { delete data.system?.key; }; + // Prevent value going out of the bounds of min/max + if (hasProperty(data, `system.value`)) { + const value = getProperty(data, `system.value`); + const max = getProperty(data, `system.max`) ?? this.max; + + let min = getProperty(data, `system.min`) ?? this.min; + if (max != null) { min ??= 0; }; + + setProperty(data, `system.value`, clamp(min, value, max)); + }; + return super._preUpdate(data, options, user); }; // #endregion Lifecycle