From e776667142478d4019cbb133611d0b2bf09cc32f Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Tue, 17 Dec 2024 14:21:35 +0100 Subject: [PATCH 1/3] Add all the other pack.type --- module/mosh.js | 85 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 10 deletions(-) diff --git a/module/mosh.js b/module/mosh.js index 07c5e5b..ae21b6e 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -955,9 +955,9 @@ export async function fromIdUuid(id_uuid, options={}){ //log where we are currentLocation = pack.metadata.id; //loop through each pack to find the right table - pack.index.forEach(function(table) { + pack.index.forEach(function(pack_item) { //is this our table? - if (table._id === id_uuid) { + if (pack_item._id === id_uuid) { //grab the table location objectLocation = currentLocation; } @@ -971,21 +971,86 @@ export async function fromIdUuid(id_uuid, options={}){ //it is a world item. switch (type) { case "RollTable": - return game.tables.filter(i=> i.id == id_uuid)[0]; + return getTableFromId(id_uuid); case "Item": - return game.items.filter(i=> i.id == id_uuid)[0]; + return getItemFromId(id_uuid); + case "Macro": + return getMacroFromId(id_uuid); + case "Actor": + return getActorFromId(id_uuid); + case "Adventure": + //adventures can only be defined in compendiums and not in the world (i think) + return null; + case "Cards": + return getCardFromId(id_uuid); + case "JournalEntry": + return getJournalFromId(id_uuid); + case "Playlist": + return getPlaylistFromId(id_uuid); + case "Scene": + return getSceneFromId(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[0]; + //now we search all world elements for the ID. + //this could lead to conflicts since ID could not be unique. + let tableData = getTableFromId(id_uuid); + if (tableData){ + return tableData; } - itemData = game.items.filter(i=> i.id == id_uuid); + let itemData = getItemFromId(id_uuid); if (itemData){ - return itemData[0]; + return itemData; + } + let macroData = getMacroFromId(id_uuid); + if (macroData){ + return macroData; + } + let actorData = getActorFromId(id_uuid); + if (actorData){ + return actorData; + } + let cardData = getCardFromId(id_uuid); + if (cardData){ + return cardData; + } + let journalData = getJournalFromId(id_uuid); + if (journalData){ + return journalData; + } + let scenneData = getSceneFromId(id_uuid); + if (scenneData){ + return scenneData; + } + let playlistData = getPlaylistFromId(id_uuid); + if (playlistData){ + return playlistData; } } } + function getSceneFromId(sceneId){ + return game.scenes.filter(i=> i.id == sceneId)[0]; + } + function getPlaylistFromId(playlistId){ + return game.playlists.filter(i=> i.id == playlistId)[0]; + } + function getJournalFromId(journalId){ + return game.journal.filter(i=> i.id == journalId)[0]; + } + function getCardFromId(cardId){ + return game.cards.filter(i=> i.id == cardId)[0]; + } + function getActorFromId(actorId){ + return game.actors.filter(i=> i.id == actorId)[0]; + } + function getTableFromId(tableId){ + return game.tables.filter(i=> i.id == tableId)[0]; + } + function getItemFromId(itemId){ + return game.items.filter(i=> i.id == itemId)[0]; + } + function getMacroFromId(macroId){ + return game.macros.filter(i=> i.id == macroId)[0]; + } } \ No newline at end of file From 5f75a209d94dd1f065c179cfc595ba883f79e8c0 Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Tue, 17 Dec 2024 14:28:08 +0100 Subject: [PATCH 2/3] add default return --- module/mosh.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/module/mosh.js b/module/mosh.js index ae21b6e..60a82bc 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -1027,6 +1027,8 @@ export async function fromIdUuid(id_uuid, options={}){ return playlistData; } } + //if we get here we have not found anything with that id. + return null; } function getSceneFromId(sceneId){ return game.scenes.filter(i=> i.id == sceneId)[0]; From bebf4c3a7bdde46fac4210c1eb246085c44071b4 Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Tue, 17 Dec 2024 14:36:46 +0100 Subject: [PATCH 3/3] Improved comments --- module/mosh.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/module/mosh.js b/module/mosh.js index 60a82bc..fcd75d9 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -936,19 +936,26 @@ async function noShipSelected() { } -//get item from world or compendiums by id or UUID. +/** + * get element from world or compendiums by id or UUID, filtering by specific type. + * @param {string} id_uuid The id or the full uuid of the element we want to retieve. + * @param {object} options General search options for this function and for 'fromUuid' + * @param {string} [options.type] A string to filter the compendium type to search or the world element type. Valid values =["RollTable","Item","Macro","Actor","Adventure","Cards","JournalEntry","Playlist","Scene"] + * @returns {Promise} Returns the Document if it could be found, otherwise null. + */ export async function fromIdUuid(id_uuid, options={}){ let type = options.type; + //first we try to find from UUID, asuming the parameter(id_uuid) is an UUID. let item = await fromUuid(id_uuid,options); if(item != null){ - //we found the item with the id, it probably was an uuid. + //we found the item with the id_uuid, it probably was an uuid. return item; } - //we need to manualy find the item: + //we need to manualy find the item let currentLocation = ''; let objectLocation = ''; - //loop through each compendium + //first loop through each compendium game.packs.forEach(function(pack){ //is this a pack of rolltables? if (pack.metadata.type === type) { @@ -965,10 +972,11 @@ export async function fromIdUuid(id_uuid, options={}){ } }); if (objectLocation){ - // Item found in a compendium -> get table data + // Item found in a compendium -> get document data return await game.packs.get(objectLocation).getDocument(id_uuid); }else{ - //it is a world item. + //if we dont find it in a compendium, its probable a world item: + //Lets filtery by type to search the relevant elements only. switch (type) { case "RollTable": return getTableFromId(id_uuid); @@ -1030,6 +1038,7 @@ export async function fromIdUuid(id_uuid, options={}){ //if we get here we have not found anything with that id. return null; } + /**functions to get world defined elements by type and ID */ function getSceneFromId(sceneId){ return game.scenes.filter(i=> i.id == sceneId)[0]; }