kandashi / midi-srd

MIT License
7 stars 29 forks source link

Shillelagh improvement #33

Open dstein766 opened 2 years ago

dstein766 commented 2 years ago

The Shillelagh macro v10.0.13 has two issues: 1) It assumes the caster is WIS-based rather than using the actual spellcasting attribute. 2) It does not set the "Magical" property on the empowered weapon.

This code fixes both of those issues. (It saves the pre-existing set of properties before forcibly setting Magical to true. Upon revert to normal, all the original props will be restored. So if the weapon was Magical pre-spell, it will remain Magical post-spell.)

const version = "10.0.13";
try {
    const lastArg = args[args.length - 1];
    let weapons = actor.items.filter(i => i.type === `weapon` && ["club","quarterstaff"].includes(i.system.baseItem));
    let weapon_content = ``;
    for (let weapon of weapons) {
        weapon_content += `<option value=${weapon.id}>${weapon.name}</option>`;
    }
    if (args[0] === "on") {
        let content = `
    <div class="form-group">
    <label>Weapons : </label>
    <select name="weapons">
        ${weapon_content}
    </select>
    </div>`;

        new Dialog({
            title: "Choose a club or quarterstaff",
            content,
            buttons:
            {
                Ok:
                {
                    label: `Ok`,
                    callback: (html) => {
                        let itemId = html.find('[name=weapons]')[0].value;
                        let weaponItem = actor.items.get(itemId);
                        let copy_item = duplicate(weaponItem.toObject(false));
                        DAE.setFlag(actor, `shillelagh`, {
                            id : itemId,
                            damage : copy_item.system.damage.parts[0][0],   
                props: copy_item.system.properties 
                        });
                        let damage = copy_item.system.damage.parts[0][0];
                        var newdamage = damage.replace(/1d(4|6)/g,"1d8");
                        copy_item.system.damage.parts[0][0] = newdamage;
                        copy_item.system.ability = actor.system.attributes.spellcasting;
            copy_item.system.properties["mgc"] = true;
                        actor.updateEmbeddedDocuments("Item", [copy_item]);
                        ChatMessage.create({content: copy_item.name + " is empowered"});
                    }
                },
                Cancel:
                {
                    label: `Cancel`
                }
            }
        }).render(true);
    }

    if (args[0] === "off") {
        let flag = DAE.getFlag(actor, `shillelagh`);
        let weaponItem = actor.items.get(flag.id);
        let copy_item = duplicate(weaponItem.toObject(false));
        copy_item.system.damage.parts[0][0] = flag.damage;
        copy_item.system.properties = flag.props;
        copy_item.system.ability = "";
        await actor.updateEmbeddedDocuments("Item", [copy_item]);
        DAE.unsetFlag(actor, `shillelagh`);
        ChatMessage.create({content: copy_item.name + " returns to normal"});
    }
} catch (err)  {
    console.error(`Shillelagh ${version}`, err);
}