Skip to content

Commit e4401c8

Browse files
authored
refactor: Handle missing items causing systems not to load (#76)
1 parent b54715f commit e4401c8

File tree

7 files changed

+64
-9
lines changed

7 files changed

+64
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fabricate",
3-
"version": "0.7.3",
3+
"version": "0.7.4",
44
"description": "A system-agnostic, flexible crafting module for FoundryVT",
55
"main": "index.js",
66
"scripts": {

src/module.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "fabricate",
33
"title": "Fabricate",
4-
"version": "0.7.3",
4+
"version": "0.7.4",
55
"description": "A system-agnostic, flexible crafting module for FoundryVTT",
66
"authors": [
77
{

src/scripts/interface/apps/EditCraftingSystemDetailDialog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class CraftingSystemDetailSubmissionHandler implements SubmissionHandler<Craftin
119119
}
120120

121121
validate(formData: CraftingSystemDetailsJson): FormError[] {
122-
const GAME = new GameProvider().globalGameObject();
122+
const GAME = new GameProvider().globalGameObject();
123123
const errors: Array<FormError> = [];
124124
if (!formData.name || formData.name.length === 0) {
125125
errors.push({

src/scripts/interface/apps/core/Applications.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class DefaultDropHandler<V, M, S extends StateManager<V, M>> implements DropHand
144144
const actionData: ActionData = {
145145
action: targetData.get("dropTrigger"),
146146
event: dropEvent,
147-
data : null,
147+
data : new Map<string, string>(targetData),
148148
document: null,
149149
checked: false,
150150
keys: {
@@ -154,16 +154,14 @@ class DefaultDropHandler<V, M, S extends StateManager<V, M>> implements DropHand
154154
}
155155
}
156156
if (rawJsonDropData) {
157-
const data = new Map<string, string>(targetData);
158157
try {
159158
const dropData: any = JSON.parse(rawJsonDropData);
160159
Object.entries(dropData).forEach(entry => {
161-
if (data.has(entry[0])) {
160+
if (actionData.data.has(entry[0])) {
162161
console.warn(`The data key "${entry[0]}" exists in both the source and target. Overwriting source value with target value. `);
163162
}
164-
data.set(entry[0], <string>entry[1]);
163+
actionData.data.set(entry[0], <string>entry[1]);
165164
});
166-
actionData.data = data;
167165
} catch (e: any) {
168166
console.error(`Something was dropped onto a Fabricate Drop Zone, but the event data could not be read. Caused by ${e}`);
169167
}
@@ -174,6 +172,9 @@ class DefaultDropHandler<V, M, S extends StateManager<V, M>> implements DropHand
174172
if (Properties.module.documents.supportedTypes.indexOf(dropData.type) >= 0) {
175173
const document: any = await new DefaultDocumentManager().getDocumentByUuid(dropData.uuid);
176174
actionData.document = document;
175+
if (document) {
176+
actionData.data.set("partId", document.uuid);
177+
}
177178
}
178179
} catch (e: any) {
179180
console.error(`Something was dropped onto a Fabricate Drop Zone, but the event data could not be read. Caused by ${e}`);

src/scripts/module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import {CraftingSystemJson} from "./system/CraftingSystem";
99
import {ApplicationWindow, ItemSheetExtension} from "./interface/apps/core/Applications";
1010
import {FabricateItemSheetTab} from "./interface/FabricateItemSheetTab";
1111

12+
// `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
13+
// `id` is useless to Fabricate
14+
Hooks.on("deleteItem", async (item: any, _app: unknown, _id: string) => {
15+
console.log(`Item UUID ${item.uuid} deleted`);
16+
await FabricateApplication.systemRegistry.handleItemDeleted(item.uuid);
17+
});
18+
1219
Hooks.on("renderSidebarTab", async (app: any, html: any) => {
1320
const GAME = new GameProvider().globalGameObject();
1421
if (!(app instanceof ItemDirectory) || !GAME.user.isGM) {

src/scripts/registries/SystemRegistry.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface SystemRegistry {
2929

3030
createCraftingSystem(systemDefinition: CraftingSystemJson): Promise<CraftingSystem>;
3131

32+
handleItemDeleted(uuid: string): Promise<void>;
3233
}
3334

3435
enum ErrorDecisionType {
@@ -177,6 +178,52 @@ class DefaultSystemRegistry implements SystemRegistry {
177178
const craftingSystem = await this._craftingSystemFactory.make(systemDefinition);
178179
return this.saveCraftingSystem(craftingSystem);
179180
}
181+
182+
public async handleItemDeleted(uuid: string): Promise<void> {
183+
const systemsJson = await this._settingsManager.read();
184+
let referenceCount = 0;
185+
let recipeCount = 0;
186+
let componentCount = 0;
187+
Object.values(systemsJson).forEach(craftingSystemJson => {
188+
if (craftingSystemJson.parts.components[uuid]) {
189+
delete craftingSystemJson.parts.components[uuid];
190+
componentCount++;
191+
}
192+
Object.values(craftingSystemJson.parts.components)
193+
.forEach(componentJson => {
194+
if (componentJson.salvage[uuid]) {
195+
delete componentJson.salvage[uuid];
196+
referenceCount++;
197+
}
198+
});
199+
if (craftingSystemJson.parts.recipes[uuid]) {
200+
delete craftingSystemJson.parts.recipes[uuid];
201+
recipeCount++;
202+
}
203+
Object.values(craftingSystemJson.parts.recipes)
204+
.forEach(recipeJson => {
205+
if (recipeJson.catalysts[uuid]) {
206+
delete recipeJson.catalysts[uuid];
207+
referenceCount++;
208+
}
209+
recipeJson.ingredientGroups.forEach(ingredientGroup => {
210+
if (ingredientGroup[uuid]) {
211+
delete ingredientGroup[uuid];
212+
referenceCount++;
213+
}
214+
});
215+
recipeJson.resultGroups.forEach(ingredientGroup => {
216+
if (ingredientGroup[uuid]) {
217+
delete ingredientGroup[uuid];
218+
referenceCount++;
219+
}
220+
});
221+
});
222+
});
223+
await this._settingsManager.write(systemsJson);
224+
console.info(`Deleted ${recipeCount} Recipes, ${componentCount} Components and ${referenceCount} references to the Item with UUID ${uuid} across ${Object.keys(systemsJson).length} Crafting Systems. `);
225+
}
226+
180227
}
181228

182229
export { SystemRegistry, DefaultSystemRegistry, ErrorDecisionType }

test/PartDictionary.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {beforeEach, describe, expect, test} from "@jest/globals";
22
import * as Sinon from "sinon";
33

4-
import {ErrorDecisionType, PartDictionary, PartDictionaryFactory} from "../src/scripts/system/PartDictionary";
4+
import {PartDictionary, PartDictionaryFactory} from "../src/scripts/system/PartDictionary";
55
import {
66
testComponentFive,
77
testComponentFour,

0 commit comments

Comments
 (0)