diff --git a/package.json b/package.json index 4691278e..aa3736b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fabricate", - "version": "0.7.3", + "version": "0.7.4", "description": "A system-agnostic, flexible crafting module for FoundryVT", "main": "index.js", "scripts": { diff --git a/src/module.json b/src/module.json index 6c5063cf..a5d8bde6 100644 --- a/src/module.json +++ b/src/module.json @@ -1,7 +1,7 @@ { "id": "fabricate", "title": "Fabricate", - "version": "0.7.3", + "version": "0.7.4", "description": "A system-agnostic, flexible crafting module for FoundryVTT", "authors": [ { diff --git a/src/scripts/interface/apps/EditCraftingSystemDetailDialog.ts b/src/scripts/interface/apps/EditCraftingSystemDetailDialog.ts index ef2d455f..1f017309 100644 --- a/src/scripts/interface/apps/EditCraftingSystemDetailDialog.ts +++ b/src/scripts/interface/apps/EditCraftingSystemDetailDialog.ts @@ -119,7 +119,7 @@ class CraftingSystemDetailSubmissionHandler implements SubmissionHandler = []; if (!formData.name || formData.name.length === 0) { errors.push({ diff --git a/src/scripts/interface/apps/core/Applications.ts b/src/scripts/interface/apps/core/Applications.ts index bfd9a8ee..e20319ee 100644 --- a/src/scripts/interface/apps/core/Applications.ts +++ b/src/scripts/interface/apps/core/Applications.ts @@ -144,7 +144,7 @@ class DefaultDropHandler> implements DropHand const actionData: ActionData = { action: targetData.get("dropTrigger"), event: dropEvent, - data : null, + data : new Map(targetData), document: null, checked: false, keys: { @@ -154,16 +154,14 @@ class DefaultDropHandler> implements DropHand } } if (rawJsonDropData) { - const data = new Map(targetData); try { const dropData: any = JSON.parse(rawJsonDropData); Object.entries(dropData).forEach(entry => { - if (data.has(entry[0])) { + if (actionData.data.has(entry[0])) { console.warn(`The data key "${entry[0]}" exists in both the source and target. Overwriting source value with target value. `); } - data.set(entry[0], entry[1]); + actionData.data.set(entry[0], entry[1]); }); - actionData.data = data; } catch (e: any) { console.error(`Something was dropped onto a Fabricate Drop Zone, but the event data could not be read. Caused by ${e}`); } @@ -174,6 +172,9 @@ class DefaultDropHandler> implements DropHand if (Properties.module.documents.supportedTypes.indexOf(dropData.type) >= 0) { const document: any = await new DefaultDocumentManager().getDocumentByUuid(dropData.uuid); actionData.document = document; + if (document) { + actionData.data.set("partId", document.uuid); + } } } catch (e: any) { console.error(`Something was dropped onto a Fabricate Drop Zone, but the event data could not be read. Caused by ${e}`); diff --git a/src/scripts/module.ts b/src/scripts/module.ts index 76ecc1f1..c3ff7276 100644 --- a/src/scripts/module.ts +++ b/src/scripts/module.ts @@ -9,6 +9,13 @@ import {CraftingSystemJson} from "./system/CraftingSystem"; import {ApplicationWindow, ItemSheetExtension} from "./interface/apps/core/Applications"; import {FabricateItemSheetTab} from "./interface/FabricateItemSheetTab"; +// `app` is an unknown type. Will need to consult foundry docs or crawl `foundry.js` to figure out what it is, but it seems JQuery related +// `id` is useless to Fabricate +Hooks.on("deleteItem", async (item: any, _app: unknown, _id: string) => { + console.log(`Item UUID ${item.uuid} deleted`); + await FabricateApplication.systemRegistry.handleItemDeleted(item.uuid); +}); + Hooks.on("renderSidebarTab", async (app: any, html: any) => { const GAME = new GameProvider().globalGameObject(); if (!(app instanceof ItemDirectory) || !GAME.user.isGM) { diff --git a/src/scripts/registries/SystemRegistry.ts b/src/scripts/registries/SystemRegistry.ts index 955814e6..7d0ae2f5 100644 --- a/src/scripts/registries/SystemRegistry.ts +++ b/src/scripts/registries/SystemRegistry.ts @@ -29,6 +29,7 @@ interface SystemRegistry { createCraftingSystem(systemDefinition: CraftingSystemJson): Promise; + handleItemDeleted(uuid: string): Promise; } enum ErrorDecisionType { @@ -177,6 +178,52 @@ class DefaultSystemRegistry implements SystemRegistry { const craftingSystem = await this._craftingSystemFactory.make(systemDefinition); return this.saveCraftingSystem(craftingSystem); } + + public async handleItemDeleted(uuid: string): Promise { + const systemsJson = await this._settingsManager.read(); + let referenceCount = 0; + let recipeCount = 0; + let componentCount = 0; + Object.values(systemsJson).forEach(craftingSystemJson => { + if (craftingSystemJson.parts.components[uuid]) { + delete craftingSystemJson.parts.components[uuid]; + componentCount++; + } + Object.values(craftingSystemJson.parts.components) + .forEach(componentJson => { + if (componentJson.salvage[uuid]) { + delete componentJson.salvage[uuid]; + referenceCount++; + } + }); + if (craftingSystemJson.parts.recipes[uuid]) { + delete craftingSystemJson.parts.recipes[uuid]; + recipeCount++; + } + Object.values(craftingSystemJson.parts.recipes) + .forEach(recipeJson => { + if (recipeJson.catalysts[uuid]) { + delete recipeJson.catalysts[uuid]; + referenceCount++; + } + recipeJson.ingredientGroups.forEach(ingredientGroup => { + if (ingredientGroup[uuid]) { + delete ingredientGroup[uuid]; + referenceCount++; + } + }); + recipeJson.resultGroups.forEach(ingredientGroup => { + if (ingredientGroup[uuid]) { + delete ingredientGroup[uuid]; + referenceCount++; + } + }); + }); + }); + await this._settingsManager.write(systemsJson); + console.info(`Deleted ${recipeCount} Recipes, ${componentCount} Components and ${referenceCount} references to the Item with UUID ${uuid} across ${Object.keys(systemsJson).length} Crafting Systems. `); + } + } export { SystemRegistry, DefaultSystemRegistry, ErrorDecisionType } \ No newline at end of file diff --git a/test/PartDictionary.test.ts b/test/PartDictionary.test.ts index 0365b7b7..09133a9c 100644 --- a/test/PartDictionary.test.ts +++ b/test/PartDictionary.test.ts @@ -1,7 +1,7 @@ import {beforeEach, describe, expect, test} from "@jest/globals"; import * as Sinon from "sinon"; -import {ErrorDecisionType, PartDictionary, PartDictionaryFactory} from "../src/scripts/system/PartDictionary"; +import {PartDictionary, PartDictionaryFactory} from "../src/scripts/system/PartDictionary"; import { testComponentFive, testComponentFour,