Feature: add methods for generate tags and labels for documents and actions (#499)

* FEAT: getTags and getLabels for weapons items

* FEAT: add _gettags and _getLabels to armor, domainCard, weapons and ActiveEffect

* define tags for actions

---------

Co-authored-by: Joaquin Pereyra <joaquinpereyra98@users.noreply.github.com>
This commit is contained in:
joaquinpereyra98 2025-07-31 23:33:26 -03:00 committed by GitHub
parent 9d025bf105
commit a27ee1578e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 237 additions and 165 deletions

View file

@ -1,6 +1,12 @@
import { itemAbleRollParse } from '../helpers/utils.mjs';
export default class DhActiveEffect extends ActiveEffect {
export default class DhActiveEffect extends foundry.documents.ActiveEffect {
/* -------------------------------------------- */
/* Properties */
/* -------------------------------------------- */
/**@override */
get isSuppressed() {
// If this is a copied effect from an attachment, never suppress it
// (These effects have attachmentSource metadata)
@ -41,14 +47,11 @@ export default class DhActiveEffect extends ActiveEffect {
});
}
get localizedStatuses() {
const statusMap = new Map(foundry.CONFIG.statusEffects.map(status => [status.id, status.name]));
return this.statuses.map(x => ({
key: x,
name: game.i18n.localize(statusMap.get(x))
}));
}
/* -------------------------------------------- */
/* Event Handlers */
/* -------------------------------------------- */
/**@inheritdoc*/
async _preCreate(data, options, user) {
const update = {};
if (!data.img) {
@ -62,13 +65,22 @@ export default class DhActiveEffect extends ActiveEffect {
await super._preCreate(data, options, user);
}
/* -------------------------------------------- */
/* Methods */
/* -------------------------------------------- */
/**@inheritdoc*/
static applyField(model, change, field) {
const evalValue = this.effectSafeEval(itemAbleRollParse(change.value, model, change.effect.parent));
change.value = evalValue ?? change.value;
super.applyField(model, change, field);
}
/* Altered Foundry safeEval to allow non-numeric returns */
/**
* Altered Foundry safeEval to allow non-numeric return
* @param {string} expression
* @returns
*/
static effectSafeEval(expression) {
let result;
try {
@ -82,6 +94,24 @@ export default class DhActiveEffect extends ActiveEffect {
return result;
}
/**
* Generates a list of localized tags based on this item's type-specific properties.
* @returns {string[]} An array of localized tag strings.
*/
_getTags() {
const tags = [
`${game.i18n.localize(this.parent.system.metadata.label)}: ${this.parent.name}`,
game.i18n.localize(this.isTemporary ? 'DAGGERHEART.EFFECTS.Duration.temporary' : 'DAGGERHEART.EFFECTS.Duration.passive')
];
for (const statusId of this.statuses) {
const status = CONFIG.statusEffects.find(s => s.id === statusId);
tags.push(game.i18n.localize(status.name));
}
return tags;
}
async toChat(origin) {
const cls = getDocumentClass('ChatMessage');
const systemData = {

View file

@ -74,8 +74,8 @@ export default class DHItem extends foundry.documents.Item {
isInventoryItem === true
? 'Inventory Items' //TODO localize
: isInventoryItem === false
? 'Character Items' //TODO localize
: 'Other'; //TODO localize
? 'Character Items' //TODO localize
: 'Other'; //TODO localize
return { value: type, label, group };
}
@ -96,6 +96,28 @@ export default class DHItem extends foundry.documents.Item {
});
}
/* -------------------------------------------- */
/**
* Generate an array of localized tag.
* @returns {string[]} An array of localized tag strings.
*/
getTags() {
const tags = [];
if (this.system.getTags) tags.push(...this.system.getTags());
return tags;
}
/**
* Generate a localized label array for this item.
* @returns {(string | { value: string, icons: string[] })[]} An array of localized strings and damage label objects.
*/
getLabels() {
const labels = [];
if (this.system.getLabels) labels.push(...this.system.getLabels());
return labels;
}
async use(event) {
const actions = new Set(this.system.actionsList);
if (actions?.size) {
@ -115,10 +137,10 @@ export default class DHItem extends foundry.documents.Item {
this.type === 'ancestry'
? game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.ancestryTitle')
: this.type === 'community'
? game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.communityTitle')
: this.type === 'feature'
? game.i18n.localize('TYPES.Item.feature')
: game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.subclassFeatureTitle'),
? game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.communityTitle')
: this.type === 'feature'
? game.i18n.localize('TYPES.Item.feature')
: game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.subclassFeatureTitle'),
origin: origin,
img: this.img,
name: this.name,