Skip to content

Instantly share code, notes, and snippets.

@ccjmk
Created December 15, 2020 20:56
Show Gist options
  • Save ccjmk/3bc72932c6569b847b537e28dd244733 to your computer and use it in GitHub Desktop.
Save ccjmk/3bc72932c6569b847b537e28dd244733 to your computer and use it in GitHub Desktop.
FoundryVTT script macro for turning dead NPCs into lootables, using fvtt-loot-sheet-npc-5e - works best with a CUB Triggler trigger for NPCs dropping to hp <= 0
(async () => {
let defeated = canvas.tokens.placeables.filter(t => {
return t.data.disposition === -1 && t.data.actorData.data?.attributes?.hp?.value <= 0
});
for (let token of defeated) {
if (token.actor.data.type != 'npc')
continue;
let invalid = await token.getFlag(`world`,`lootyfied`) ? await token.getFlag(`world`,`lootyfied`) : false;
if (invalid)
continue;
let newItems = token.actor.data.items.filter(item => {
// Weapons are fine, unless they're natural.
if (item.type == 'weapon') {
return item.data.weaponType != 'natural';
}
// Equipment's fine, unless it's natural armor.
if (item.type == 'equipment') {
if (!item.data.armor)
return true;
return item.data.armor.type != 'natural';
}
// Item type blocklist.
// Less intuitive than an allowlist,
// but permits unknown module items.
return !(['class', 'spell', 'feat'].includes(item.type));
});
// Change sheet to lootable, and give players permissions.
let newActorData = {
'flags': {
'core': {
'sheetClass': 'dnd5e.LootSheet5eNPC'
},
'lootsheetnpc5e': {
'lootsheettype': 'Loot'
}
},
'items': newItems,
[`permission.default`]: 2
};
// Add proper currency info to the new data, if the actor needs it.
if (undefined == token.actor.data.data.currency || token.actor.data.data.currency.cp == 0 ) // should be an object, not a number
{
newActorData['data.currency'] = {
'cp': { 'value': null },
'ep': { 'value': null },
'gp': { 'value': null },
'pp': { 'value': null },
'sp': { 'value': null }
};
}
await token.actor.update(newActorData);
await token.setFlag(`world`,`lootyfied`,true);
}
})();
@thejoester
Copy link

get the following error now

Error: You are accessing PlaceableObject#data which is no longer used and instead the Document class should be referenced directly as PlaceableObject#document. Deprecated since Version 10 Backwards-compatible support will be removed in Version 12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment