Skip to content

Commit

Permalink
Fix landing type configuration with trait management updates
Browse files Browse the repository at this point in the history
  • Loading branch information
surged20 committed Jun 10, 2024
1 parent accfe37 commit 0c6fe36
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"WJMAIS.Hardpoints": "Hardpoints",

"WJMAIS.LandingTypes": "Landing Types",
"WJMAIS.TraitLandingTypesPlural.other": "Landing Types",
"WJMAIS.LandingLand": "Land",
"WJMAIS.LandingWater": "Water",
"WJMAIS.LandingSpaceDock": "Space Dock",
Expand Down
17 changes: 17 additions & 0 deletions src/module/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,25 @@ function patchRollData() {
);
}

function patchPrepareData() {
libWrapper.register(
"wjmais",
"game.dnd5e.documents.Actor5e.prototype.prepareData",
function (wrapped, ...args) {
if (this.flags?.wjmais?.traits?.lt?.value)
this.flags.wjmais.traits.lt.value = new Set(
this.flags.wjmais.traits.lt.value
);

return wrapped(...args);
},
"MIXED"
);
}

export function applyPatches() {
patchCompendiumImport();
patchResourceBars();
patchRollData();
patchPrepareData();
}
64 changes: 36 additions & 28 deletions src/module/wildjammer-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,38 @@ export default class WildjammerSheet extends dnd5e.applications.actor
}

/**
* Prepare the data structure for traits data like languages, resistances & vulnerabilities, and proficiencies
* @param {object} traits The raw traits data object from the actor data
* @private
* Prepare the data structure for traits data like languages, resistances & vulnerabilities, and proficiencies.
* @param {object} systemData System data for the Actor being prepared.
* @returns {object} Prepared trait data.
* @protected
*/
_prepareTraits(traits) {
const map = {
lt: CONFIG.WJMAIS.landingTypes,
};
for (let [t, choices] of Object.entries(map)) {
if (!traits) break;
const trait = traits[t];
if (!trait) continue;
let values = [];
if (trait.value) {
values = trait.value instanceof Array ? trait.value : [trait.value];
}
trait.selected = values.reduce((obj, t) => {
obj[t] = choices[t];
return obj;
}, {});
// Add custom entry
if (trait.custom) {
trait.custom
.split(";")
.forEach((c, i) => (trait.selected[`custom${i + 1}`] = c.trim()));
}
trait.cssClass = !foundry.utils.isEmpty(trait.selected) ? "" : "inactive";
}
_prepareTraits(systemData) {
let traits = super._prepareTraits(systemData);
const flagData = this.actor.flags.wjmais;
const key = "traits.lt";
const data = foundry.utils.deepClone(
foundry.utils.getProperty(flagData, key)
);
foundry.utils.setProperty(traits, key, data);

let values = data.value;
if (!values) values = [];
else if (values instanceof Set) values = Array.from(values);
else if (!Array.isArray(values)) values = [values];

data.selected = values.reduce((obj, key) => {
obj[key] = CONFIG.WJMAIS.landingTypes[key];
return obj;
}, {});

// Add custom entries
if (data.custom)
data.custom
.split(";")
.forEach((c, i) => (data.selected[`custom${i + 1}`] = c.trim()));
data.cssClass = !foundry.utils.isEmpty(data.selected) ? "" : "inactive";

return traits;
}

async getData(options) {
Expand Down Expand Up @@ -989,6 +993,10 @@ export default class WildjammerSheet extends dnd5e.applications.actor
choices,
allowCustom: false,
};
new dnd5e.applications.TraitSelector(this.actor, options).render(true);
new dnd5e.applications.actor.TraitSelector(
this.actor,
"lt",
options
).render(true);
}
}
10 changes: 10 additions & 0 deletions src/module/wjmais.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function configProperties() {
formula: "10 + @ship.ac.mod",
};

CONFIG.DND5E.lt = CONFIG.WJMAIS.landingTypes;
CONFIG.DND5E.traits.lt = {
actorKeyPath: "flags.wjmais.traits.lt",
labels: {
title: "Landing Types",
localization: "WJMAIS.TraitLandingTypesPlural",
},
icon: "systems/dnd5e/icons/svg/trait-languages.svg",
};

foundry.utils.mergeObject(
globalThis.game.dnd5e.config.equipmentTypes,
translateObject({
Expand Down
7 changes: 4 additions & 3 deletions src/templates/actors/wildjammer.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,14 @@ <h4>{{localize 'WJMAIS.Hardpoints'}}</h4>
{{selectOptions config.actorSizes selected=system.traits.size localize=true}}
</select>
</div>
<div class="form-group">
<div class="form-group {{traits.traits.lt.cssClass}}">
<label>{{localize "WJMAIS.LandingTypes"}}</label>
<a class="trait-selector" data-options="landingTypes" data-target="flags.wjmais.traits.lt">
<a class="trait-selector" data-trait="lt"
data-tooltip="{{localize 'DND5E.TraitConfig' trait=(localize 'WJMAIS.LandingTypes')}}" tabindex="0">
<i class="fas fa-edit"></i>
</a>
<ul class="traits-list">
{{#each flags.wjmais.traits.lt.selected as |v k|}}
{{#each traits.traits.lt.selected as |v k|}}
<li class="tag {{k}}">{{v}}</li>
{{/each}}
</ul>
Expand Down

0 comments on commit 0c6fe36

Please sign in to comment.