113 lines
2.8 KiB
JavaScript
113 lines
2.8 KiB
JavaScript
import { CommonItemData } from "./Common.mjs";
|
|
import { gameTerms } from "../../gameTerms.mjs";
|
|
import { localizer } from "../../utils/Localizer.mjs";
|
|
import { Logger } from "../../utils/Logger.mjs";
|
|
import { requiredInteger } from "../helpers.mjs";
|
|
|
|
const { fields } = foundry.data;
|
|
const { hasProperty, diffObject, mergeObject } = foundry.utils;
|
|
|
|
/** Used for Armour and Shields */
|
|
export class ArmourData extends CommonItemData {
|
|
// MARK: Schema
|
|
static defineSchema() {
|
|
return {
|
|
...super.defineSchema(),
|
|
protection: requiredInteger({ min: 0, initial: 1 }),
|
|
location: new fields.SetField(
|
|
new fields.StringField({
|
|
blank: false,
|
|
trim: true,
|
|
nullable: false,
|
|
options: Object.values(gameTerms.Anatomy),
|
|
}),
|
|
{
|
|
nullable: false,
|
|
required: true,
|
|
},
|
|
),
|
|
equipped: new fields.BooleanField({
|
|
initial: false,
|
|
required: true,
|
|
nullable: false,
|
|
}),
|
|
weight: new fields.StringField({
|
|
blank: false,
|
|
nullable: true,
|
|
initial: null,
|
|
options: Object.values(gameTerms.WeightRatings),
|
|
}),
|
|
};
|
|
};
|
|
|
|
// MARK: Base Data
|
|
prepareBaseData() {
|
|
super.prepareBaseData();
|
|
};
|
|
|
|
// MARK: Derived Data
|
|
prepareDerivedData() {
|
|
super.prepareDerivedData();
|
|
};
|
|
|
|
// #region Lifecycle
|
|
async _preUpdate(changes, options, user) {
|
|
// return false
|
|
if (options.force && game.settings.get(`ripcrypt`, `devMode`)) { return };
|
|
|
|
// Ensure changes is a diffed object
|
|
const diff = diffObject(this.parent._source, changes);
|
|
let valid = await super._preUpdate(changes, options, user);
|
|
|
|
if (hasProperty(diff, `system.equipped`) && !this._canEquip()) {
|
|
ui.notifications.error(
|
|
localizer(
|
|
`RipCrypt.notifs.error.cannot-equip`,
|
|
{ itemType: `@TYPES.Item.${this.parent.type}` },
|
|
),
|
|
{ console: false },
|
|
);
|
|
|
|
// Don't stop the update, but don't allow changing the equipped status
|
|
mergeObject(changes, {
|
|
"system.equipped": false,
|
|
});
|
|
|
|
// Set a flag so that we can tell the sheet that it needs to rerender
|
|
this.forceRerender = true;
|
|
};
|
|
|
|
return valid;
|
|
};
|
|
|
|
/** Used to tell the preUpdate logic whether or not to prevent the */
|
|
_canEquip() {
|
|
const parent = this.parent;
|
|
if (!parent.isEmbedded || !(parent.parent instanceof Actor)) {
|
|
Logger.error(`Unable to equip item when it's not embedded`);
|
|
return false;
|
|
};
|
|
|
|
if (this.location.size === 0) {
|
|
Logger.error(`Unable to equip an item without any locations`);
|
|
return false;
|
|
};
|
|
|
|
const slots = parent.parent.system.equippedArmour ?? {};
|
|
|
|
for (const locationTag of this.location) {
|
|
if (slots[locationTag.toLowerCase()] != null) {
|
|
Logger.error(`Unable to equip multiple items in the same slot`);
|
|
return false;
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
// #endregion
|
|
|
|
// #region Getters
|
|
get locationString() {
|
|
return [...this.location].join(`, `);
|
|
};
|
|
// #endregion
|
|
};
|