Compare commits

..

2 commits

Author SHA1 Message Date
Carlos Fernandez
ac010482fe Also delete multiclass related stuff if reducing below the minimum multiclass level 2026-05-20 00:12:00 -04:00
Carlos Fernandez
f95e511a4d Fix removal when de-leveling 2026-05-19 23:36:28 -04:00

View file

@ -153,10 +153,13 @@ export default class DhpActor extends Actor {
async updateLevel(newLevel) { async updateLevel(newLevel) {
if (!['character', 'companion'].includes(this.type) || newLevel === this.system.levelData.level.changed) return; if (!['character', 'companion'].includes(this.type) || newLevel === this.system.levelData.level.changed) return;
const tiers = Object.values(game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.LevelTiers).tiers);
const maxLevel = tiers.reduce((acc, tier) => Math.max(acc, tier.levels.end), 0);
const multiclassMinLevel = Math.min(
maxLevel,
...tiers.filter(t => t.options.multiclass).map(t => t.levels.start)
);
if (newLevel > this.system.levelData.level.current) { if (newLevel > this.system.levelData.level.current) {
const maxLevel = Object.values(
game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.LevelTiers).tiers
).reduce((acc, tier) => Math.max(acc, tier.levels.end), 0);
if (newLevel > maxLevel) { if (newLevel > maxLevel) {
ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.tooHighLevel')); ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.tooHighLevel'));
} }
@ -231,18 +234,19 @@ export default class DhpActor extends Actor {
this.system.multiclass.subclass.update({ 'system.featureState': subclassFeatureState.multiclass }); this.system.multiclass.subclass.update({ 'system.featureState': subclassFeatureState.multiclass });
} }
if (multiclass) { // Remove multiclass if we're removing a multiclass feature or if we're below the multiclass minimum level
const multiclassItem = this.items.find(x => x.uuid === multiclass.itemUuid); // Multclasses cannot be manually removed on the sheet, so this allows recovering in the case of errors
const multiclassFeatures = this.items.filter( if (multiclass || newLevel < multiclassMinLevel) {
x => x.system.originItemType === 'class' && x.system.multiclassOrigin const multiclassItems = this.items.filter(
); x =>
const subclassFeatures = this.items.filter( x.uuid === multiclass?.itemUuid ||
x => x.system.originItemType === 'subclass' && x.system.multiclassOrigin x.system.isMulticlass ||
(['class', 'subclass'].includes(x.system.originItemType) && x.system.multiclassOrigin)
); );
this.deleteEmbeddedDocuments( this.deleteEmbeddedDocuments(
'Item', 'Item',
[multiclassItem, ...multiclassFeatures, ...subclassFeatures].map(x => x.id) multiclassItems.map(x => x.id)
); );
this.update({ this.update({
@ -394,6 +398,7 @@ export default class DhpActor extends Actor {
const embeddedItem = await this.createEmbeddedDocuments('Item', [ const embeddedItem = await this.createEmbeddedDocuments('Item', [
{ {
...multiclassData, ...multiclassData,
uuid: multiclassItem.uuid, // todo: replace with setting an id and using keepId
_stats: getStatsWithSource(multiclassItem), _stats: getStatsWithSource(multiclassItem),
system: { system: {
...multiclassData.system, ...multiclassData.system,
@ -407,6 +412,7 @@ export default class DhpActor extends Actor {
await this.createEmbeddedDocuments('Item', [ await this.createEmbeddedDocuments('Item', [
{ {
...subclassData, ...subclassData,
uuid: subclassItem.uuid, // todo: replace with setting an id and using keepId
_stats: getStatsWithSource(subclassItem), _stats: getStatsWithSource(subclassItem),
system: { system: {
...subclassData.system, ...subclassData.system,
@ -427,6 +433,7 @@ export default class DhpActor extends Actor {
const embeddedItem = await this.createEmbeddedDocuments('Item', [ const embeddedItem = await this.createEmbeddedDocuments('Item', [
{ {
...cardData, ...cardData,
uuid: cardItem.uuid, // todo: replace with setting an id and using keepId
_stats: getStatsWithSource(cardItem), _stats: getStatsWithSource(cardItem),
system: { system: {
...cardData.system, ...cardData.system,