This commit is contained in:
WBHarry 2026-03-27 23:05:32 +00:00 committed by GitHub
commit 783d1d977b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 293 additions and 139 deletions

View file

@ -1007,4 +1007,111 @@ export default class DhpActor extends Actor {
return allTokens;
}
async toggleDomainCardVault(card, options = { render: true }) {
const { render } = options;
const { available } = this.system.loadoutSlot;
if (card.system.inVault && !available && !card.system.loadoutIgnore) {
return ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.loadoutMaxReached'));
}
const toVault = options.toVault ?? !card.system.inVault;
await card?.update({ 'system.inVault': toVault }, { render });
return [{ item: card, add: !toVault }];
}
async unequipBeforeEquip(itemToEquip, options = { render: true }) {
const { render } = options;
const primary = this.system.primaryWeapon,
secondary = this.system.secondaryWeapon;
let unequippedItems = [];
if (itemToEquip.system.secondary) {
if (primary && primary.system.burden === CONFIG.DH.GENERAL.burden.twoHanded.value) {
unequippedItems.push(primary);
}
if (secondary) {
unequippedItems.push(secondary);
}
} else {
if (secondary && itemToEquip.system.burden === CONFIG.DH.GENERAL.burden.twoHanded.value) {
unequippedItems.push(secondary);
}
if (primary) {
unequippedItems.push(primary);
}
}
for (const item of unequippedItems) await item?.update({ 'system.equipped': false }, { render });
return unequippedItems;
}
async toggleEquipItem(item, options = { render: true }) {
const { render } = options;
const changedItems = [];
const updateAndAddChangedItem = async (item, equip) => {
changedItems.push({ item, add: equip });
await item.update({ 'system.equipped': equip }, { render });
};
if (item.system.equipped && [undefined, false].includes(options.equip)) {
await updateAndAddChangedItem(item, false);
return changedItems;
}
switch (item.type) {
case 'armor':
const currentArmor = this.system.armor;
if (currentArmor) {
await updateAndAddChangedItem(currentArmor, false);
}
await updateAndAddChangedItem(item, true);
break;
case 'weapon':
if (this.effects.find(x => !x.disabled && x.type === 'beastform')) {
return ui.notifications.warn(
game.i18n.localize('DAGGERHEART.UI.Notifications.beastformEquipWeapon')
);
}
const unequippedItems = await this.unequipBeforeEquip(item, { render: false });
changedItems.push(...unequippedItems.map(x => ({ item: x, add: false })));
await updateAndAddChangedItem(item, true);
break;
}
return changedItems;
}
/* This is very convoluted, and there is almost certainly a better way to do it. I couldn't get it working any better way atm though. */
async setFavoriteItem(item, setFavorited) {
const favoritesToRemove = [];
const favoritesToAdd = [];
if (['weapon', 'armor'].includes(item.type)) {
const changedData = await this.toggleEquipItem(item, { render: false, equip: setFavorited });
for (const data of changedData) {
if (data.add) favoritesToAdd.push(data.item);
else favoritesToRemove.push(data.item);
}
} else if (item.type === 'domainCard') {
const changedData = await this.toggleDomainCardVault(item, { render: false, toVault: !setFavorited });
for (const data of changedData) {
if (data.add) favoritesToAdd.push(data.item);
else favoritesToRemove.push(data.item);
}
} else if (setFavorited) favoritesToAdd.push(item);
else favoritesToRemove.push(item);
this.update({
'system.sidebarFavorites': [
...this.system.sidebarFavorites.filter(x => favoritesToRemove.every(r => r.id !== x.id)),
...favoritesToAdd
]
});
}
}

View file

@ -229,5 +229,11 @@ export default class DHItem extends foundry.documents.Item {
async _preDelete() {
this.deleteTriggers();
if (this.parent?.type === 'character') {
const filteredFavorites = this.parent.system.sidebarFavorites.filter(x => x.id !== this.id);
if (this.parent.system.sidebarFavorites.length !== filteredFavorites.length)
this.parent.update({ 'system.sidebarFavorites': filteredFavorites });
}
}
}