diff --git a/module/actor/actor.js b/module/actor/actor.js index c2ea4c9..c0db14c 100644 --- a/module/actor/actor.js +++ b/module/actor/actor.js @@ -1375,34 +1375,8 @@ export class MothershipActor extends Actor { //A script to return the data from a table. async getRollTableData(tableId){ - let currentLocation = ''; - let tableLocation = ''; - //find where this table is located - //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); - if (tableLocation){ - // Item found in a compendium -> get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); - }else{ - //try to find the item in the world. (user defined) - let tableData = game.tables.filter(i=> i.id == tableId) - } + + let tableData = await fromIdUuid(tableId,type="RollTable"); //get table name let tableName = tableData.name; //get table name @@ -1550,32 +1524,9 @@ export class MothershipActor extends Actor { //set variables rollString = chosenRollType[0]; } - //find where this table is located + + let tableData = await fromIdUuid(tableId,type="RollTable"); //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); - if (tableLocation){ - // Table found in a compendium -> get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); - }else{ - //try to find the item in the world. (user defined) - let tableData = game.tables.filter(i=> i.id == tableId) - } //get table name let tableName = tableData.name; //get table name @@ -2635,34 +2586,9 @@ export class MothershipActor extends Actor { //prepare attribute label attributeLabel = 'Bankruptcy'; //get the bankruptcy table - //get current compendium - let compendium = game.packs; - let currentLocation = ``; - let tableLocation = ``; - let tableId = game.settings.get('mosh','table1eBankruptcy'); - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); - if (tableLocation){ - // Table found in a compendium -> get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); - }else{ - //try to find the item in the world. (user defined) - let tableData = game.tables.filter(i=> i.id == tableId) - } + let tableId = game.settings.get('mosh','table1eBankruptcy'); + //get Table Data + let tableData = await fromIdUuid(tableId,type="RollTable"); //prep text for success if (parsedRollResult.success && parsedRollResult.critical) { //flavor text @@ -3136,32 +3062,8 @@ export class MothershipActor extends Actor { let newValue = 0; let flavorText = ``; let chatId = (game.release.generation >= 12 ? foundry.utils.randomID(): randomID()) - //find where this item is located - //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of items? - if (pack.metadata.type === 'Item') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(item) { - //is this our table? - if (item._id === itemId) { - //grab the table location - itemLocation = currentLocation; - } - }); - } - }); - if (itemLocation){ - // Item found in a compendium -> get table data - let itemData = await game.packs.get(itemLocation).getDocument(itemId); - }else{ - //try to find the item in the world. (user defined) - let itemData = game.items.filter(i=> i.id == itemId) - } + //get item data + let itemData = await fromIdUuid(itemId,type="Item"); //add or increase the count of the item, depending on type, if the actor has it if (this.items.getName(itemData.name)) { //if this is an item, increase the count diff --git a/module/actor/ship-sheet-sbt.js b/module/actor/ship-sheet-sbt.js index 8f925dd..e3a0a74 100644 --- a/module/actor/ship-sheet-sbt.js +++ b/module/actor/ship-sheet-sbt.js @@ -179,29 +179,8 @@ export class MothershipShipSheetSBT extends ActorSheet { const actorData = sheetData; //A script to return the data from a table. let tableId = game.settings.get('mosh', 'table1eMegadamageEffects'); - let currentLocation = ''; - let tableLocation = ''; - //find where this table is located - //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function (pack) { - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function (table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); //get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); + let tableData = await fromIdUuid(tableId,type="RollTable"); let megadamageHTML = ""; diff --git a/module/mosh.js b/module/mosh.js index fac2334..2ba8b2f 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -933,4 +933,58 @@ async function noShipSelected() { //log what was done console.log(`Told the user that no character was selected.`); }); +} + + +//get item from world or compendiums by id or UUID. +export async function fromIdUuid(id_uuid,type="", options={}){ + let item = await fromUuid(id_uuid,options); + if(item != null){ + //we found the item with the id, it probably was an uuid. + return item; + } + + //we need to manualy find the item: + let currentLocation = ''; + let objectLocation = ''; + //loop through each compendium + game.packs.forEach(function(pack){ + //is this a pack of rolltables? + if (pack.metadata.type === type) { + //log where we are + currentLocation = pack.metadata.id; + //loop through each pack to find the right table + pack.index.forEach(function(table) { + //is this our table? + if (table._id === id_uuid) { + //grab the table location + objectLocation = currentLocation; + } + }); + } + }); + if (objectLocation){ + // Item found in a compendium -> get table data + return await game.packs.get(objectLocation).getDocument(id_uuid); + }else{ + //it is a world item. + switch (type) { + case "RollTable": + return game.tables.filter(i=> i.id == id_uuid); + case "Item": + return game.items.filter(i=> i.id == id_uuid); + default: + //type is not defined, and we could not find it in a compendium, + //now we search all world elements. + let itemData = game.tables.filter(i=> i.id == id_uuid); + if (itemData){ + return itemData + } + itemData = game.items.filter(i=> i.id == id_uuid); + if (itemData){ + return itemData + } + } + } + } \ No newline at end of file diff --git a/module/windows/ship-megadamage.js b/module/windows/ship-megadamage.js index 16b2887..3ff2610 100644 --- a/module/windows/ship-megadamage.js +++ b/module/windows/ship-megadamage.js @@ -41,29 +41,8 @@ export class DLShipMegaDamage extends FormApplication { //A script to return the data from a table. let tableId = game.settings.get('mosh','table1eMegadamageEffects'); - let currentLocation = ''; - let tableLocation = ''; - //find where this table is located - //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); //get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); + let tableData = await fromIdUuid(tableId,type="RollTable"); let entries = Array.from(tableData.results.entries());