Lavaeolous / PF1-StatBlock-Converter-Module

FoundryVTT Module to convert PF1 Statblocks into Foundry Actors (PC or NPC).
MIT License
14 stars 12 forks source link

Consumables (potions, wands, scrolls) break import #519

Closed websterguy closed 2 years ago

websterguy commented 2 years ago

The code tries to run .update before it is an actual Item, which fails.

https://github.com/Lavaeolous/PF1-StatBlock-Converter-Module/blob/master/statblock-converter/scripts/sbcParsers.js#L4012

if (consumableType == "wand")
    consumable.update({ "data.uses.value": parseInt(charges) });
else
    consumable.update({ "data.quantity": parseInt(charges) });
gear.rawName = consumable.name;
// Following is somewhat redundant re-creation
entity = await Item.create(consumable.toObject(), {temporary: true});

should be

if (consumableType == "wand")
    consumable.data.uses.value = parseInt(charges);
else
    consumable.data.quantity = parseInt(charges);
gear.rawName = consumable.name;
// Following is somewhat redundant re-creation
entity = await Item.create(consumable.toObject(), {temporary: true});

The creation isn't redundant, spell.toConsumable() returns a data object instead of an Item. So just have to edit the data object then create the Item.

mkahvi commented 2 years ago

As little background: The problem stems from .toConsumable() previously returning ItemData, and now it returns just the object used for creating items. Both have good and bad sides, but since you are never going to deal with the original document, getting ItemData was meaningless complication.

Sorry for submitting an MR to PF1 that changed this. (I guess?)

Lavaeolous commented 2 years ago

Fixed in 3.5.6 as follows:

if (entity) {
    const consumable = await CONFIG.Item.documentClasses.spell.toConsumable(entity.toObject(), consumableType);
    if (consumableType == "wand")
        consumable.data.uses.value = parseInt(charges);
    else
        consumable.data.quantity = parseInt(charges);
    gear.rawName = consumable.name;
    // Following is somewhat redundant re-creation
    entity = await Item.create(consumable, {temporary: true});
}