Compare commits

..

No commits in common. "c95520c9d8ebbe74a48fe61501a0270efb84561f" and "286944d2e63da423f66bdf87a2516ba41b6a1d05" have entirely different histories.

7 changed files with 93 additions and 146 deletions

View file

@ -754,6 +754,8 @@ export default class CharacterSheet extends DHBaseActorSheet {
await config.resourceUpdates.updateResources();
}
//TODO: redo toggleEquipItem method
/**
* Toggles the equipped state of an item (armor or weapon).
* @type {ApplicationClickAction}
@ -761,14 +763,32 @@ export default class CharacterSheet extends DHBaseActorSheet {
static async #toggleEquipItem(_event, button) {
const item = await getDocFromElement(button);
if (!item) return;
if (item.system.equipped) {
await item.update({ 'system.equipped': false });
return;
}
const changedData = await this.document.toggleEquipItem(item);
const removedData = changedData.filter(x => !x.add);
this.document.update({
'system.sidebarFavorites': [
...this.document.system.sidebarFavorites.filter(x => removedData.every(r => r.item.id !== x.id))
]
});
switch (item.type) {
case 'armor':
const currentArmor = this.document.system.armor;
if (currentArmor) {
await currentArmor.update({ 'system.equipped': false });
}
await item.update({ 'system.equipped': true });
break;
case 'weapon':
if (this.document.effects.find(x => !x.disabled && x.type === 'beastform')) {
return ui.notifications.warn(
game.i18n.localize('DAGGERHEART.UI.Notifications.beastformEquipWeapon')
);
}
await this.document.system.constructor.unequipBeforeEquip.bind(this.document.system)(item);
await item.update({ 'system.equipped': true });
break;
}
}
/**
@ -828,13 +848,12 @@ export default class CharacterSheet extends DHBaseActorSheet {
*/
static async #toggleVault(_event, button) {
const doc = await getDocFromElement(button);
const changedData = await this.document.toggleDomainCardVault(doc);
const removedData = changedData.filter(x => !x.add);
this.document.update({
'system.sidebarFavorites': [
...this.document.system.sidebarFavorites.filter(x => removedData.every(r => r.item.id !== x.id))
]
});
const { available } = this.document.system.loadoutSlot;
if (doc.system.inVault && !available && !doc.system.loadoutIgnore) {
return ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.loadoutMaxReached'));
}
await doc?.update({ 'system.inVault': !doc.system.inVault });
}
/**
@ -1023,6 +1042,6 @@ export default class CharacterSheet extends DHBaseActorSheet {
if (this.document.system.sidebarFavorites.some(x => x.id === item.id)) return;
this.document.setFavoriteItem(item, true);
this.document.update({ 'system.sidebarFavorites': [...this.document.system.sidebarFavorites, item] });
}
}

View file

@ -532,13 +532,22 @@ export default function DHApplicationMixin(Base) {
callback: async target => (await getDocFromElement(target)).toChat(this.document.uuid)
});
if (deletable)
options.push({
name: 'CONTROLS.CommonDelete',
icon: 'fa-solid fa-trash',
callback: async (target, event) => {
const doc = await getDocFromElement(target);
if (event.shiftKey) return doc.delete();
else return doc.deleteDialog();
}
});
options.push({
name: 'Unfavorite',
icon: 'fa-regular fa-star',
condition: target => {
const doc = getDocFromElementSync(target);
const isFavorited = this.document.system.sidebarFavorites.some(x => x.id === doc.id);
return this.document.type === 'character' && isFavorited;
return this.document.type === 'character' && target.closest('.items-sidebar-list');
},
callback: async (target, _event) => {
const doc = await getDocFromElement(target);
@ -553,30 +562,20 @@ export default function DHApplicationMixin(Base) {
icon: 'fa-solid fa-star',
condition: target => {
const doc = getDocFromElementSync(target);
const isFavorited = this.document.system.sidebarFavorites.some(x => x.id === doc.id);
return (
!(doc instanceof game.system.api.documents.DhActiveEffect) &&
this.document.type === 'character' &&
!isFavorited
!target.closest('.items-sidebar-list')
);
},
callback: async (target, _event) => {
const doc = await getDocFromElement(target);
this.document.setFavoriteItem(doc, true);
this.document.update({
'system.sidebarFavorites': [...this.document.system.sidebarFavorites, doc]
});
}
});
if (deletable)
options.push({
name: 'CONTROLS.CommonDelete',
icon: 'fa-solid fa-trash',
callback: async (target, event) => {
const doc = await getDocFromElement(target);
if (event.shiftKey) return doc.delete();
else return doc.deleteDialog();
}
});
return options.map(option => ({
...option,
icon: `<i class="${option.icon}"></i>`

View file

@ -578,6 +578,28 @@ export default class DhCharacter extends BaseDataActor {
return diceTypes[attackDiceIndex];
}
static async unequipBeforeEquip(itemToEquip) {
const primary = this.primaryWeapon,
secondary = this.secondaryWeapon;
if (itemToEquip.system.secondary) {
if (primary && primary.burden === CONFIG.DH.GENERAL.burden.twoHanded.value) {
await primary.update({ 'system.equipped': false });
}
if (secondary) {
await secondary.update({ 'system.equipped': false });
}
} else {
if (secondary && itemToEquip.system.burden === CONFIG.DH.GENERAL.burden.twoHanded.value) {
await secondary.update({ 'system.equipped': false });
}
if (primary) {
await primary.update({ 'system.equipped': false });
}
}
}
prepareBaseData() {
this.evasion += this.class.value?.system?.evasion ?? 0;

View file

@ -72,6 +72,26 @@ export default class DHDomainCard extends BaseDataItem {
/* -------------------------------------------- */
/**@inheritdoc */
async _preUpdate(data, options, user) {
const allowed = await super._preUpdate(data, options, user);
if (allowed === false) return;
if (this.parent.parent?.type === 'character') {
if (
data.system?.inVault &&
!this.inVault &&
this.parent.parent.system.sidebarFavorites.find(x => x?.id === this.parent.id)
) {
this.parent.parent.update({
'system.sidebarFavorites': this.parent.parent.system.sidebarFavorites.filter(
x => x.id !== this.parent.id
)
});
}
}
}
/**@inheritdoc */
async _preCreate(data, options, user) {
const allowed = await super._preCreate(data, options, user);

View file

@ -992,111 +992,4 @@ 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

@ -226,11 +226,5 @@ 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 });
}
}
}

View file

@ -2,7 +2,7 @@
"id": "daggerheart",
"title": "Daggerheart",
"description": "An unofficial implementation of the Daggerheart system",
"version": "1.6.1",
"version": "1.6.0",
"compatibility": {
"minimum": "13.346",
"verified": "13.351",