Skip to content

Commit

Permalink
[#342] Apply senses/movement/type to actor when race is added
Browse files Browse the repository at this point in the history
  • Loading branch information
arbron committed Nov 6, 2023
1 parent 59b5363 commit 50a5a37
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
15 changes: 6 additions & 9 deletions module/applications/actor/base-sheet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1403,15 +1403,12 @@ export default class ActorSheet5e extends ActorSheet {
if ( !game.settings.get("dnd5e", "disableAdvancements") ) {
const manager = AdvancementManager.forDeletedItem(this.actor, item.id);
if ( manager.steps.length ) {
if ( ["class", "subclass"].includes(item.type) ) {
try {
const shouldRemoveAdvancements = await AdvancementConfirmationDialog.forDelete(item);
if ( shouldRemoveAdvancements ) return manager.render(true);
} catch(err) {
return;
}
} else {
return manager.render(true);
try {
const shouldRemoveAdvancements = await AdvancementConfirmationDialog.forDelete(item);
if ( shouldRemoveAdvancements ) return manager.render(true);
else return item.delete({ shouldRemoveAdvancements });
} catch(err) {
return;
}
}
}
Expand Down
50 changes: 48 additions & 2 deletions module/data/item/race.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,31 @@ export default class RaceData extends SystemDataModel.mixin(ItemDescriptionTempl
*/
_onCreate(data, options, userId) {
if ( (game.user.id !== userId) || this.parent.actor?.type !== "character" ) return;
this.parent.actor.update({"system.details.race": this.parent.id});
const updates = { "system.details.race": this.parent.id };
const attributes = this.parent.actor.system.attributes;

for ( const key of Object.keys(CONFIG.DND5E.movementTypes) ) {
if ( this.movement[key] > attributes.movement[key] ) {
updates[`system.attributes.movement.${key}`] = this.movement[key];
}
}
if ( this.movement.hover ) updates["system.attributes.movement.hover"] = true;
updates["system.attributes.movement.units"] = this.movement.units;

for ( const key of Object.keys(CONFIG.DND5E.senses) ) {
if ( this.senses[key] > attributes.senses[key] ) {
updates[`system.attributes.senses.${key}`] = this.senses[key];
}
}
if ( this.senses.special ) {
updates[system.attributes.senses.special] = attributes.senses.special
? `${attributes.senses.special}:${this.senses.special}` : this.senses.special;
}
updates["system.attributes.senses.units"] = this.senses.units;

// TODO: Set creature type once defined on actor

this.parent.actor.update(updates);
}

/* -------------------------------------------- */
Expand All @@ -104,6 +128,28 @@ export default class RaceData extends SystemDataModel.mixin(ItemDescriptionTempl
*/
async _preDelete(options, user) {
if ( this.parent.actor?.type !== "character" ) return;
await this.parent.actor.update({"system.details.race": null});
const updates = { "system.details.race": null };
const attributes = this.parent.actor.system.attributes;
if ( options.shouldRemoveAdvancements === false ) return;

for ( const key of Object.keys(CONFIG.DND5E.movementTypes) ) {
if ( this.movement[key] === attributes.movement[key] ) {
updates[`system.attributes.movement.${key}`] = 0;
}
}
if ( this.movement.hover ) updates["system.attributes.movement.hover"] = false;

for ( const key of Object.keys(CONFIG.DND5E.senses) ) {
if ( this.senses[key] === attributes.senses[key] ) {
updates[`system.attributes.senses.${key}`] = 0;
}
}
if ( this.senses.special ) {
updates[system.attributes.senses.special] = attributes.senses.special.replace(this.senses.special, "");
}

// TODO: Unset creature type once defined on actor

await this.parent.actor.update(updates);
}
}

0 comments on commit 50a5a37

Please sign in to comment.