Refactor/actions v2 (#402)

* Action Refactor Part #1

* Fixed Weapon/Armor features. Fixed Feature actions

* f

* Action Refactor Part #2

* Fixes

* Remove ActionsField from Companion

* Fixes

* Localization fix

* BaseDataItem hasActions false

---------

Co-authored-by: WBHarry <williambjrklund@gmail.com>
This commit is contained in:
Dapoulp 2025-07-25 01:10:49 +02:00 committed by GitHub
parent 80744381f5
commit 0632a8c6bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
52 changed files with 988 additions and 743 deletions

View file

@ -1,7 +1,6 @@
import AttachableItem from './attachableItem.mjs';
import ActionField from '../fields/actionField.mjs';
import { ActionsField } from '../fields/actionField.mjs';
import { armorFeatures } from '../../config/itemConfig.mjs';
import { actionsTypes } from '../action/_module.mjs';
export default class DHArmor extends AttachableItem {
/** @inheritDoc */
@ -10,7 +9,8 @@ export default class DHArmor extends AttachableItem {
label: 'TYPES.Item.armor',
type: 'armor',
hasDescription: true,
isInventoryItem: true
isInventoryItem: true,
hasActions: true
});
}
@ -39,8 +39,7 @@ export default class DHArmor extends AttachableItem {
baseThresholds: new fields.SchemaField({
major: new fields.NumberField({ integer: true, initial: 0 }),
severe: new fields.NumberField({ integer: true, initial: 0 })
}),
actions: new fields.ArrayField(new ActionField())
})
};
}
@ -65,7 +64,10 @@ export default class DHArmor extends AttachableItem {
actionIds.push(...feature.actionIds);
}
await this.parent.deleteEmbeddedDocuments('ActiveEffect', effectIds);
changes.system.actions = this.actions.filter(x => !actionIds.includes(x._id));
changes.system.actions = actionIds.reduce((acc, id) => {
acc[`-=${id}`] = null;
return acc;
}, {});
for (var feature of added) {
const featureData = armorFeatures[feature.value];
@ -79,17 +81,38 @@ export default class DHArmor extends AttachableItem {
]);
feature.effectIds = embeddedItems.map(x => x.id);
}
const newActions = {};
if (featureData.actions?.length > 0) {
const newActions = featureData.actions.map(action => {
const cls = actionsTypes[action.type];
return new cls(
{ ...action, _id: foundry.utils.randomID(), name: game.i18n.localize(action.name) },
for (let action of featureData.actions) {
const embeddedEffects = await this.parent.createEmbeddedDocuments(
'ActiveEffect',
(action.effects ?? []).map(effect => ({
...effect,
transfer: false,
name: game.i18n.localize(effect.name),
description: game.i18n.localize(effect.description)
}))
);
const cls = game.system.api.models.actions.actionsTypes[action.type];
const actionId = foundry.utils.randomID();
newActions[actionId] = new cls(
{
...cls.getSourceConfig(this),
...action,
_id: actionId,
name: game.i18n.localize(action.name),
description: game.i18n.localize(action.description),
effects: embeddedEffects.map(x => ({ _id: x.id }))
},
{ parent: this }
);
});
changes.system.actions = [...this.actions, ...newActions];
feature.actionIds = newActions.map(x => x._id);
}
}
changes.system.actions = newActions;
feature.actionIds = Object.keys(newActions);
}
}
}

View file

@ -8,6 +8,8 @@
* @property {boolean} isInventoryItem- Indicates whether items of this type is a Inventory Item
*/
import { ActionsField } from "../fields/actionField.mjs";
const fields = foundry.data.fields;
export default class BaseDataItem extends foundry.abstract.TypeDataModel {
@ -21,7 +23,8 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
hasDescription: false,
hasResource: false,
isQuantifiable: false,
isInventoryItem: false
isInventoryItem: false,
hasActions: false
};
}
@ -69,6 +72,9 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
if (this.metadata.isQuantifiable)
schema.quantity = new fields.NumberField({ integer: true, initial: 1, min: 0, required: true });
if (this.metadata.hasActions)
schema.actions = new ActionsField()
return schema;
}

View file

@ -1,5 +1,5 @@
import BaseDataItem from './base.mjs';
import ActionField from '../fields/actionField.mjs';
import { ActionField } from '../fields/actionField.mjs';
export default class DHConsumable extends BaseDataItem {
/** @inheritDoc */
@ -9,7 +9,8 @@ export default class DHConsumable extends BaseDataItem {
type: 'consumable',
hasDescription: true,
isQuantifiable: true,
isInventoryItem: true
isInventoryItem: true,
hasActions: true
});
}
@ -18,8 +19,7 @@ export default class DHConsumable extends BaseDataItem {
const fields = foundry.data.fields;
return {
...super.defineSchema(),
consumeOnUse: new fields.BooleanField({ initial: false }),
actions: new fields.ArrayField(new ActionField())
consumeOnUse: new fields.BooleanField({ initial: false })
};
}
}

View file

@ -1,5 +1,5 @@
import BaseDataItem from './base.mjs';
import ActionField from '../fields/actionField.mjs';
import { ActionField } from '../fields/actionField.mjs';
export default class DHDomainCard extends BaseDataItem {
/** @inheritDoc */
@ -8,7 +8,8 @@ export default class DHDomainCard extends BaseDataItem {
label: 'TYPES.Item.domainCard',
type: 'domainCard',
hasDescription: true,
hasResource: true
hasResource: true,
hasActions: true
});
}
@ -29,8 +30,7 @@ export default class DHDomainCard extends BaseDataItem {
required: true,
initial: CONFIG.DH.DOMAIN.cardTypes.ability.id
}),
inVault: new fields.BooleanField({ initial: false }),
actions: new fields.ArrayField(new ActionField())
inVault: new fields.BooleanField({ initial: false })
};
}

View file

@ -1,5 +1,5 @@
import BaseDataItem from './base.mjs';
import ActionField from '../fields/actionField.mjs';
import { ActionField, ActionsField } from '../fields/actionField.mjs';
export default class DHFeature extends BaseDataItem {
/** @inheritDoc */
@ -8,7 +8,8 @@ export default class DHFeature extends BaseDataItem {
label: 'TYPES.Item.feature',
type: 'feature',
hasDescription: true,
hasResource: true
hasResource: true,
hasActions: true
});
}
@ -24,8 +25,7 @@ export default class DHFeature extends BaseDataItem {
}),
subType: new fields.StringField({ choices: CONFIG.DH.ITEM.featureSubTypes, nullable: true, initial: null }),
originId: new fields.StringField({ nullable: true, initial: null }),
identifier: new fields.StringField(),
actions: new fields.ArrayField(new ActionField())
identifier: new fields.StringField()
};
}

View file

@ -1,5 +1,5 @@
import BaseDataItem from './base.mjs';
import ActionField from '../fields/actionField.mjs';
import { ActionField } from '../fields/actionField.mjs';
export default class DHMiscellaneous extends BaseDataItem {
/** @inheritDoc */
@ -9,16 +9,15 @@ export default class DHMiscellaneous extends BaseDataItem {
type: 'miscellaneous',
hasDescription: true,
isQuantifiable: true,
isInventoryItem: true
isInventoryItem: true,
hasActions: true
});
}
/** @inheritDoc */
static defineSchema() {
const fields = foundry.data.fields;
return {
...super.defineSchema(),
actions: new fields.ArrayField(new ActionField())
...super.defineSchema()
};
}
}

View file

@ -1,6 +1,5 @@
import AttachableItem from './attachableItem.mjs';
import { actionsTypes } from '../action/_module.mjs';
import ActionField from '../fields/actionField.mjs';
import { ActionsField, ActionField } from '../fields/actionField.mjs';
export default class DHWeapon extends AttachableItem {
/** @inheritDoc */
@ -9,8 +8,8 @@ export default class DHWeapon extends AttachableItem {
label: 'TYPES.Item.weapon',
type: 'weapon',
hasDescription: true,
isInventoryItem: true
// hasInitialAction: true
isInventoryItem: true,
hasActions: true
});
}
@ -64,8 +63,7 @@ export default class DHWeapon extends AttachableItem {
]
}
}
}),
actions: new fields.ArrayField(new ActionField())
})
};
}
@ -95,7 +93,10 @@ export default class DHWeapon extends AttachableItem {
}
await this.parent.deleteEmbeddedDocuments('ActiveEffect', removedEffectsUpdate);
changes.system.actions = this.actions.filter(x => !removedActionsUpdate.includes(x._id));
changes.system.actions = removedActionsUpdate.reduce((acc, id) => {
acc[`-=${id}`] = null;
return acc;
}, {});
for (let weaponFeature of added) {
const featureData = CONFIG.DH.ITEM.weaponFeatures[weaponFeature.value];
@ -110,7 +111,7 @@ export default class DHWeapon extends AttachableItem {
weaponFeature.effectIds = embeddedItems.map(x => x.id);
}
const newActions = [];
const newActions = {};
if (featureData.actions?.length > 0) {
for (let action of featureData.actions) {
const embeddedEffects = await this.parent.createEmbeddedDocuments(
@ -122,24 +123,25 @@ export default class DHWeapon extends AttachableItem {
description: game.i18n.localize(effect.description)
}))
);
const cls = actionsTypes[action.type];
newActions.push(
new cls(
{
...action,
_id: foundry.utils.randomID(),
name: game.i18n.localize(action.name),
description: game.i18n.localize(action.description),
effects: embeddedEffects.map(x => ({ _id: x.id }))
},
{ parent: this }
)
const cls = game.system.api.models.actions.actionsTypes[action.type];
const actionId = foundry.utils.randomID();
newActions[actionId] = new cls(
{
...cls.getSourceConfig(this),
...action,
_id: actionId,
name: game.i18n.localize(action.name),
description: game.i18n.localize(action.description),
effects: embeddedEffects.map(x => ({ _id: x.id }))
},
{ parent: this }
);
}
}
changes.system.actions = [...this.actions, ...newActions];
weaponFeature.actionIds = newActions.map(x => x._id);
changes.system.actions = newActions;
weaponFeature.actionIds = Object.keys(newActions);
}
}
}