Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#342] Apply senses/movement to actor when race is added #2556

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
return item.delete({ shouldRemoveAdvancements });
} catch(err) {
return;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion module/data/actor/character.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class CharacterData extends CreatureTemplate {
required: true, fallback: true, label: "DND5E.Background"
}),
originalClass: new foundry.data.fields.StringField({required: true, label: "DND5E.ClassOriginal"}),
type: new CreatureTypeField({ swarm: false }),
type: new CreatureTypeField({ swarm: false }, { initial: { value: "humanoid" } }),
xp: new foundry.data.fields.SchemaField({
value: new foundry.data.fields.NumberField({
required: true, nullable: false, integer: true, min: 0, initial: 0, label: "DND5E.ExperiencePointsCurrent"
Expand Down
54 changes: 52 additions & 2 deletions module/data/item/race.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,32 @@ 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, this.senses.special].filterJoin(";");
}
updates["system.attributes.senses.units"] = this.senses.units;

if ( this.type.value ) updates["system.details.type.value"] = this.type.value;
if ( this.type.subtype ) updates["system.details.type.subtype"] = this.type.subtype;
if ( this.type.custom ) updates["system.details.type.custom"] = this.type.custom;

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

/* -------------------------------------------- */
Expand All @@ -104,6 +129,31 @@ 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, "");
}

const type = this.parent.actor.system.details.type;
if ( this.type.value === type.value ) updates["system.details.type.value"] = "humanoid";
if ( this.type.subtype === type.subtype ) updates["system.details.type.subtype"] = "";
if ( this.type.custom === type.custom ) updates["system.details.type.custom"] = "";

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