edzillion / blood-n-guts

Other
4 stars 16 forks source link

PF1 - not all tokens have classes #248

Closed dmrickey closed 3 years ago

dmrickey commented 3 years ago
    pf1: {
        id: 'pf1',
        supportedTypes: ['character', 'npc'],
        currentHP: (token) => token.actor.data.data.attributes.hp.value,
        maxHP: (token) => token.actor.data.data.attributes.hp.max,
        currentHPChange: (changes) => { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = changes === null || changes === void 0 ? void 0 : changes.actorData) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.attributes) === null || _c === void 0 ? void 0 : _c.hp) === null || _d === void 0 ? void 0 : _d.value; },
        maxHPChange: (changes) => { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = changes === null || changes === void 0 ? void 0 : changes.actorData) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.attributes) === null || _c === void 0 ? void 0 : _c.hp) === null || _d === void 0 ? void 0 : _d.max; },
        creatureType: (token) => {
            const actorType = token.actor.data.type.toLowerCase();
            let creatureType;
            if (actorType === 'character') {
                // @ts-expect-error bad definition
                creatureType = token.actor.data.items.find((i) => i.type === 'race').name;
            }
            else if (actorType === 'npc') {
                // @ts-expect-error bad definition
                creatureType = token.actor.data.items.find((i) => i.type === 'class').name;
            }
            log(LogLevel.DEBUG, 'creatureType pf1: ', token.name, actorType, creatureType);
            return creatureType.toLowerCase();
        },
    },

Specifically this bit

                else if (actorType === 'npc') {
                // @ts-expect-error bad definition
                creatureType = token.actor.data.items.find((i) => i.type === 'class').name;
            }

I've got an animated token that's just a floating light source that one of my players controls. I'm pretty sure that's the one that's breaking this, but it literally has no Items attached to the character. Assuming the rest of your code can handle a creature without a class, you can just adding a ? in there for optional chaining (e === 'class')?.name;)

Edit: I guess I didn't specifically say it, I'm getting an error whenever the scene loads

    at Object.creatureType (systems.js:91)
    at helpers.js:281
    at Generator.next (<anonymous>)
    at helpers.js:7
    at new Promise (<anonymous>)
    at __awaiter (helpers.js:3)
    at lookupTokenBloodColor (helpers.js:272)
    at SplatToken.<anonymous> (SplatToken.js:58)
    at Generator.next (<anonymous>)
    at SplatToken.js:7

Ok I just tried it out locally... Plus nullish coalescing to a string makes it work (so (e === 'class')?.name ?? '';)

dmrickey commented 3 years ago

I went ahead and created a PR with the proposed fix https://github.com/edzillion/blood-n-guts/pull/249

dmrickey commented 3 years ago

Closing since #249 was merged