buttonpushertv

FVTT - DeathReasonsPlus

Jun 20th, 2021
950
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This macro is used to generate a death reason when an NPC token drops to 0 HP
  2. console.log("+++++++++++++ DEATH REASONS CALLED =====================");
  3.  
  4. if (args[0] === "on") {
  5. // let showingName = token.actor.token.data.name;
  6. let showingName = canvas.tokens.get(args[1]).data.name;
  7. await deathReason(showingName);
  8. }
  9. console.log("+++++++++++++ DEATH REASONS ENDED =====================");
  10.  
  11. async function deathReason(showingName) {
  12. //This is the RollTable method. You should create a RollTable called 'DeathReasons' to make this work.
  13. let tableName = "DeathReasons";
  14. let table = game.tables.contents.find(t => t.name == tableName);
  15.  
  16. // Roll the result, and mark it drawn
  17.   if (table) {
  18.     if (checkTable(table)) {
  19.       let roll = await table.roll();
  20.       let resultItem = roll.results[0];
  21.       var reason = resultItem.data.text;
  22. console.log(reason);
  23.     }
  24.   }
  25.  
  26.   function checkTable(table) {
  27.     let results = 0;
  28.     for (let data of table.data.results) {
  29.       if (!data.drawn) {
  30.         results++;
  31.       }
  32.     }
  33.     if (results < 1) {
  34.       table.reset();
  35.       ui.notifications.notify("Table Reset");
  36.       return false;
  37.     }
  38.     return true;
  39.   }
  40.  
  41. game.togglePause();
  42.  
  43. let result = await Dialog.prompt({
  44.   title: showingName + ' has dropped to 0 HP',
  45.   content: `<p><i>` + showingName + ` ` + reason + `</i></p><br /><p><strong>How do you want this to happen?</strong></p><p>Anything to add?</p><p><input type="text"></p>`,
  46.   callback: (html) => html.find('input').val()
  47. })
  48.  
  49. await ChatMessage.create({content: showingName + " " + reason + "<br /><br />" + result});
  50.  
  51. game.togglePause();
  52. }
RAW Paste Data