RC-110 | Prevent Armour/Shield Equipping based on slots

This commit is contained in:
Oliver-Akins 2025-02-16 11:41:32 -07:00
parent b33d7b59eb
commit 4ccfc03e59
6 changed files with 94 additions and 18 deletions

View file

@ -135,7 +135,7 @@
}, },
"notifs": { "notifs": {
"error": { "error": {
"cannot-equip-not-embedded": "Cannot equip an {itemType} when it isn't within an Actor", "cannot-equip": "Cannot equip the {itemType}, see console for more details.",
"invalid-delta": "The delta for \"{name}\" is not a number, cannot finish processing the action." "invalid-delta": "The delta for \"{name}\" is not a number, cannot finish processing the action."
}, },
"warn": { "warn": {

View file

@ -43,6 +43,27 @@ export class AllItemSheetV1 extends GenericAppMixin(HandlebarsApplicationMixin(I
Logger.debug(`Context:`, ctx); Logger.debug(`Context:`, ctx);
return ctx; return ctx;
}; };
async _onRender() {
// remove the flag if it exists when we render the sheet
delete this.document?.system?.forceRerender;
};
/**
* Used to make it so that items that don't get updated because of the
* _preUpdate hook removing/changing the data submitted, can still get
* re-rendered when the diff is empty. If the document does get updated,
* this rerendering does not happen.
*
* @override
*/
async _processSubmitData(...args) {
await super._processSubmitData(...args);
if (this.document.system.forceRerender) {
await this.render(false);
};
};
// #endregion // #endregion
// #region Actions // #region Actions

View file

@ -1,13 +1,14 @@
import { CommonItemData } from "./Common.mjs"; import { CommonItemData } from "./Common.mjs";
import { gameTerms } from "../../gameTerms.mjs"; import { gameTerms } from "../../gameTerms.mjs";
import { localizer } from "../../utils/Localizer.mjs"; import { localizer } from "../../utils/Localizer.mjs";
import { Logger } from "../../utils/Logger.mjs";
import { requiredInteger } from "../helpers.mjs"; import { requiredInteger } from "../helpers.mjs";
const { fields } = foundry.data; const { fields } = foundry.data;
const { hasProperty, mergeObject } = foundry.utils; const { hasProperty, diffObject, mergeObject } = foundry.utils;
/** Used for Armour and Shields */ /** Used for Armour and Shields */
export class ProtectorData extends CommonItemData { export class ArmourData extends CommonItemData {
// MARK: Schema // MARK: Schema
static defineSchema() { static defineSchema() {
return { return {
@ -45,22 +46,56 @@ export class ProtectorData extends CommonItemData {
// #region Lifecycle // #region Lifecycle
async _preUpdate(changes, options, user) { async _preUpdate(changes, options, user) {
// return false
if (options.force && game.settings.get(`ripcrypt`, `devMode`)) { return }; if (options.force && game.settings.get(`ripcrypt`, `devMode`)) { return };
let valid = super._preUpdate(changes, options, user);
if (hasProperty(changes, `system.equipped`) && !this.parent.isEmbedded) { // Ensure changes is a diffed object
ui.notifications.error(localizer( const diff = diffObject(this.parent._source, changes);
`RipCrypt.notifs.error.cannot-equip-not-embedded`, 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}` }, { itemType: `@TYPES.Item.${this.parent.type}` },
)); ),
mergeObject( { console: false },
changes,
{ "-=system.equipped": null },
{ inplace: true, performDeletions: true },
); );
// 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; return false;
}; };
return valid;
if (this.location.size === 0) {
Logger.error(`Unable to equip an item without any locations`);
return false;
};
const slots = parent.parent.system.equippedArmour ?? {};
Logger.debug(`slots`, slots);
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 // #endregion

View file

@ -0,0 +1,19 @@
import { ArmourData } from "./Armour.mjs";
import { Logger } from "../../utils/Logger.mjs";
export class ShieldData extends ArmourData {
_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;
};
const shield = parent.parent.system.equippedShield;
if (shield) {
Logger.error(`Unable to equip multiple shields`);
return false;
};
return true;
};
};

View file

@ -2,7 +2,7 @@ export class RipCryptItem extends Item {
get quantifiedName() { get quantifiedName() {
if (this.system.quantity != null && this.system.quantity !== 1) { if (this.system.quantity != null && this.system.quantity !== 1) {
return `${this.name} (${this.system.quantity})`; return `${this.name} (${this.system.quantity})`;
} };
return this.name; return this.name;
}; };
}; };

View file

@ -8,10 +8,11 @@ import { RipCryptCombatTracker } from "../Apps/sidebar/CombatTracker.mjs";
// Data Models // Data Models
import { AmmoData } from "../data/Item/Ammo.mjs"; import { AmmoData } from "../data/Item/Ammo.mjs";
import { ArmourData } from "../data/Item/Armour.mjs";
import { CraftData } from "../data/Item/Craft.mjs"; import { CraftData } from "../data/Item/Craft.mjs";
import { GoodData } from "../data/Item/Good.mjs"; import { GoodData } from "../data/Item/Good.mjs";
import { HeroData } from "../data/Actor/Hero.mjs"; import { HeroData } from "../data/Actor/Hero.mjs";
import { ProtectorData } from "../data/Item/Protector.mjs"; import { ShieldData } from "../data/Item/Shield.mjs";
import { SkillData } from "../data/Item/Skill.mjs"; import { SkillData } from "../data/Item/Skill.mjs";
import { WeaponData } from "../data/Item/Weapon.mjs"; import { WeaponData } from "../data/Item/Weapon.mjs";
@ -49,10 +50,10 @@ Hooks.once(`init`, () => {
// #region Datamodels // #region Datamodels
CONFIG.Actor.dataModels.hero = HeroData; CONFIG.Actor.dataModels.hero = HeroData;
CONFIG.Item.dataModels.ammo = AmmoData, CONFIG.Item.dataModels.ammo = AmmoData,
CONFIG.Item.dataModels.armour = ProtectorData; CONFIG.Item.dataModels.armour = ArmourData;
CONFIG.Item.dataModels.craft = CraftData; CONFIG.Item.dataModels.craft = CraftData;
CONFIG.Item.dataModels.good = GoodData; CONFIG.Item.dataModels.good = GoodData;
CONFIG.Item.dataModels.shield = ProtectorData; CONFIG.Item.dataModels.shield = ShieldData;
CONFIG.Item.dataModels.skill = SkillData; CONFIG.Item.dataModels.skill = SkillData;
CONFIG.Item.dataModels.weapon = WeaponData; CONFIG.Item.dataModels.weapon = WeaponData;
// #endregion // #endregion