Skip to content

Commit 5ed96b5

Browse files
authored
Merge pull request #2 from bithir/main
Simplified inventory build, removing group when noted needed and more
2 parents 0fa3f93 + 556e71f commit 5ed96b5

4 files changed

Lines changed: 55 additions & 60 deletions

File tree

scripts/action-handler.js

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,30 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
120120
}
121121

122122
async #buildInventory() {
123-
if (this.items.size === 0) return;
123+
124+
const invItems = this.actor.items.filter((item) => (item.system.isGear || item.system.isEquipement) && !item.system.isArmor);
125+
126+
if (invItems.size === 0) {
127+
return;
128+
}
124129

125130
const actionTypeId = "item";
126131
const inventoryMap = new Map();
127132

128-
for (const [itemId, itemData] of this.items) {
133+
for (const itemData of invItems) {
129134
let type = itemData.type;
135+
const itemId = itemData.id;
130136

131137
if (
132138
((type === "weapon" || type === "equipment") && itemData.system?.isActive) ||
133139
((type === "weapon" || type === "equipment") && this.displayUnequipped)
134140
) {
141+
// For now - ignore this and add them separately once we have support for selecting the power to use
142+
/*
135143
if (itemData.system?.isArtifact || itemData.system?.isArtifact === "artifact") {
136144
type = "artifact";
137145
}
146+
*/
138147
const typeMap = inventoryMap.get(type) ?? new Map();
139148
typeMap.set(itemId, itemData);
140149
inventoryMap.set(type, typeMap);
@@ -185,20 +194,13 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
185194
const name = this.actor.system.combat.name;
186195
const img = this.actor.system.combat.img;
187196
const encodedValue = [actionTypeId, this.actor.system.combat.id].join(this.delimiter);
188-
const typeMap = new Map();
189-
const itemData = duplicate(this.actor.system.combat);
190-
itemData.type = type;
191-
typeMap.set(itemId, itemData);
192-
193-
const actions = [...typeMap].map(([itemId, itemData]) => {
194-
return {
197+
const actions = [{
195198
itemId,
196199
name,
197200
img,
198201
listName,
199-
encodedValue,
200-
};
201-
});
202+
encodedValue
203+
}];
202204
this.addActions(actions, groupData);
203205
}
204206

@@ -207,22 +209,23 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
207209
// Get traits
208210
const traits = this.actor.items.filter((item) => item.type === "trait" && item.system?.hasScript);
209211
// Exit if there are no traits with scripts
210-
if (traits.length === 0) return;
212+
213+
if (traits.length === 0)
214+
{
215+
// Remove group Trait if there are not traits
216+
return;
217+
}
211218

212219
// Get actions
213-
const actions = Object.entries(traits)
214-
.map((trait) => {
220+
const actions = traits.map((trait) => {
215221
try {
216-
const lookupId = trait[1].name.toLowerCase().replace(/\s/g, "");
217-
const id = trait[1].name;
218-
let name = `${coreModule.api.Utils.i18n(game.symbaroum.config.traitsList[lookupId])}`;
219-
if (name === "undefined") {
220-
name = trait[1].name;
221-
}
222+
const id = trait.id;
223+
let name = trait.name;
224+
222225
const actionTypeName = `${coreModule.api.Utils.i18n(ACTION_TYPE[actionType])}: ` ?? "";
223-
const listName = `${actionTypeName}${coreModule.api.Utils.i18n(game.symbaroum.config.traitsList[lookupId])}`;
226+
const listName = `${actionTypeName}${name}`;
224227
const encodedValue = [actionType, id].join(this.delimiter);
225-
const img = coreModule.api.Utils.getImage(trait[1].img);
228+
const img = coreModule.api.Utils.getImage(trait.img);
226229

227230
return {
228231
id,
@@ -236,8 +239,7 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
236239
coreModule.api.Logger.error(trait);
237240
return null;
238241
}
239-
})
240-
.filter((trait) => !!trait);
242+
});
241243

242244
// Create group data
243245
const groupData = { id: "traits", type: "system" };
@@ -249,25 +251,22 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
249251
async #buildAbilities() {
250252
const actionType = "ability";
251253
// Get traits
252-
const abilityList = this.actor.items.filter((item) => item.type === "ability" && item.system?.script);
254+
const abilities = this.actor.items.filter((item) => item.type === "ability" && item.system?.script);
253255

254-
// Exit if there are no abilityList with scripts
255-
if (abilityList.length === 0) return;
256+
// Exit if there are no abilities with scripts
257+
if (abilities.length === 0) {
258+
return;
259+
}
256260

257261
// Get actions
258-
const actions = Object.entries(abilityList)
259-
.map((ability) => {
262+
const actions = abilities.map((ability) => {
260263
try {
261-
const lookupId = ability[1].name.toLowerCase().replace(/\s/g, "");
262-
const id = ability[1].name;
263-
let name = `${coreModule.api.Utils.i18n(game.symbaroum.config.abilitiesList[lookupId])}`;
264-
if (name === "undefined") {
265-
name = ability[1].name;
266-
}
264+
const id = ability.id;
265+
let name = ability.name; // `${coreModule.api.Utils.i18n(game.symbaroum.config.abilitiesList[lookupId])}`;
267266
const actionTypeName = `${coreModule.api.Utils.i18n(ACTION_TYPE[actionType])}: ` ?? "";
268-
const listName = `${actionTypeName}${coreModule.api.Utils.i18n(game.symbaroum.config.abilitiesList[lookupId])}`;
267+
const listName = `${actionTypeName}${name}`;
269268
const encodedValue = [actionType, id].join(this.delimiter);
270-
const img = coreModule.api.Utils.getImage(ability[1].img);
269+
const img = coreModule.api.Utils.getImage(ability.img);
271270
return {
272271
id,
273272
name,
@@ -280,8 +279,7 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
280279
coreModule.api.Logger.error(ability);
281280
return null;
282281
}
283-
})
284-
.filter((ability) => !!ability);
282+
});
285283

286284
// Create group data
287285
const groupData = { id: "abilities", type: "system" };
@@ -295,22 +293,19 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
295293
// Get traits
296294
let powers = this.actor.items.filter((item) => item.type === "mysticalPower" && item.system?.hasScript);
297295
// Exit if there are no abilities with scripts
298-
if (powers.length === 0) return;
296+
if (powers.length === 0) {
297+
return;
298+
}
299299

300300
// Get actions
301-
const actions = Object.entries(powers)
302-
.map((power) => {
301+
const actions = powers.map((power) => {
303302
try {
304-
const lookupId = power[1].name.toLowerCase().replace(/\s/g, "");
305-
const id = power[1].name;
306-
let name = `${coreModule.api.Utils.i18n(game.symbaroum.config.powersList[lookupId])}`;
307-
if (name === "undefined") {
308-
name = power[1].name;
309-
}
303+
const id = power.id;
304+
const name = power.name;
310305
const actionTypeName = `${coreModule.api.Utils.i18n(ACTION_TYPE[actionType])}: ` ?? "";
311-
const listName = `${actionTypeName}${coreModule.api.Utils.i18n(game.symbaroum.config.powersList[lookupId])}`;
306+
const listName = `${actionTypeName}${power.name}`;
312307
const encodedValue = [actionType, id].join(this.delimiter);
313-
const img = coreModule.api.Utils.getImage(power[1].img);
308+
const img = coreModule.api.Utils.getImage(power.img);
314309
return {
315310
id,
316311
name,
@@ -323,8 +318,7 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
323318
coreModule.api.Logger.error(power);
324319
return null;
325320
}
326-
})
327-
.filter((power) => !!power);
321+
});
328322

329323
// Create group data
330324
const groupData = { id: "mysticalpower", type: "system" };

scripts/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
1111
group.name = coreModule.api.Utils.i18n(group.name);
1212
group.listName = `Group: ${coreModule.api.Utils.i18n(group.listName ?? group.name)}`;
1313
});
14+
1415
const groupsArray = Object.values(groups);
1516
DEFAULTS = {
1617
layout: [

scripts/roll-handler.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
123123
* @param {string} actionId The action id
124124
*/
125125
async #handleTraitAction(event, actor, actionId) {
126-
let usedPower = actor.items.filter((item) => item.name === actionId);
127-
actor.usePower(usedPower[0]);
126+
let usedPower = actor.items.get(actionId);
127+
actor.usePower(usedPower);
128128
}
129129

130130
/**
@@ -135,8 +135,8 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
135135
* @param {string} actionId The action id
136136
*/
137137
async #handleAbilityAction(event, actor, actionId) {
138-
let usedPower = actor.items.filter((item) => item.name === actionId);
139-
actor.usePower(usedPower[0]);
138+
let usedPower = actor.items.get(actionId);
139+
actor.usePower(usedPower);
140140
}
141141

142142
/**
@@ -147,8 +147,8 @@ Hooks.once("tokenActionHudCoreApiReady", async (coreModule) => {
147147
* @param {string} actionId The action id
148148
*/
149149
async #handleMysticalPowerAction(event, actor, actionId) {
150-
let usedPower = actor.items.filter((item) => item.name === actionId);
151-
actor.usePower(usedPower[0]);
150+
let usedPower = actor.items.get(actionId);
151+
actor.usePower(usedPower);
152152
}
153153

154154
/**

scripts/system-manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
6868
* @returns {object} The default layout and groups
6969
*/
7070
async doRegisterDefaultFlags () {
71-
return DEFAULTS
71+
return DEFAULTS;
7272
}
7373
}
7474
})

0 commit comments

Comments
 (0)