Okay here's a version I think will work for what you need. It took a little bit of circuitous logic to get there.
Use an Ability named Loot. Enter the treasure in there like
100 gp
1 longsword
leather armor
Or maybe
[[3d6]] gp
1 longsword
leather armor
Launch the script with either
!Loot @{target|token_id}
or
!Loot @{selected|token_id}
Here's the script:
/*
!Loot @{target|token_id}
or
!Loot @{selected|token_id}
You must have an Ability on the character sheet named Loot.
This will be printed out, with any inline rolls rolled, and linebreaks maintained.
If you have items separated by a pipe (|) only one will be returned. This lets you include a simple randomizer.
*/
var loot = loot || (function () {
'use strict';
const version = '0.2',
COMMAND = 'Loot', // this is the name you must use for the Ability, make sure capitalisation matches.
HPBAR = 1, // this is the bar used for tracking HP
scriptName = 'Loot',
lastUpdate = 1545180290523,
divider = ' -|- ', // do not use same as newline
newline = '--',
random = '|',
checkInstall = function () {
log('--| ' + scriptName + ' v' + version + ' |-- [' + (new Date(lastUpdate)) + ']');
},
processInlinerolls = function (msg) {
if (_.has(msg, 'inlinerolls')) {
return _.chain(msg.inlinerolls)
.reduce(function(previous, current, index) {
previous['$[[' + index + ']]'] = current.results.total || 0;
return previous;
},{})
.reduce(function(previous, current, index) {
return previous.replace(index, current);
}, msg.content)
.value();
} else {
return msg.content;
}
},
getSpeaker = function(msg) {
let characters = findObjs({_type: 'character'});
let speaking;
characters.forEach(function(chr) { if(chr.get('name') == msg.who) speaking = chr; });
let speaker = speaking ? 'character|'+speaking.id : 'player|'+msg.playerid;
return speaker;
},
handleInput = function (msg) {
if ('api' === msg.type && msg.content.match(`!format${COMMAND} `)) {
let args = processInlinerolls(msg).split(divider); //);
let lootBase = args[2].split(newline);
let loot = [], i = 0;
lootBase.forEach(row => {
let item = row.split(random);
loot[i] = item.length === 1 ? item : item[randomInteger(item.length -1)];
i ++;
});
let output = `&{template:traits} {{name=${args[1].trim()}'s Treasure}} {{description=${loot.join('\n')}}}`;
let speaker = args[3];
sendChat(speaker,output);
} else if ('api' === msg.type && msg.content.match(`!${COMMAND} `)) {
let id = msg.content.replace(`!${COMMAND}`,'').trim();
let token = findObjs({_type: 'graphic', id: id})[0];
if(!token) {
sendChat(COMMAND,'/w GM Token Not Found');
return;
}
let character = getObj('character', token.get('represents'));
if(!character) {
sendChat(COMMAND,'/w GM Character Not Found');
return;
}
let greenVal = token.get(`bar${HPBAR}_value`);
if (isNaN(greenVal)) {
sendChat(COMMAND,"/w GM Target's Bar is not a number");
} else if (greenVal > 0) {
sendChat(COMMAND,'Target is not yet dead');
} else {
//let loot = getAttrByName(character.id, COMMAND, 'current');
let loot = findObjs({_type: 'ability', characterid: character.id, name: COMMAND})[0];
if(!loot) {
sendChat(COMMAND,'/w GM Loot Not Found.');
} else {
let speaker = getSpeaker(msg);
let name = token.get('name');
let action = loot.get('action').toString();
let output = action.split('\n').join(newline);
sendChat(COMMAND, `!format${COMMAND} ${divider} ${name} ${divider} ${output} ${divider} ${speaker}`); //
}
}
}
},
registerEventHandlers = function () {
on('chat:message', handleInput);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on('ready', function () {
'use strict';
loot.CheckInstall();
loot.RegisterEventHandlers();
});