Prevent attributes with a min/max from going out of bounds during update
This commit is contained in:
parent
e8c73de6bd
commit
05dcb9afbd
1 changed files with 13 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue