From 1d55075f9978da3d26cfcafe4041efa283dabf1e Mon Sep 17 00:00:00 2001 From: Dev Date: Fri, 13 Sep 2024 20:51:22 +0100 Subject: [PATCH] Fixed location loot generator not creating unique IDs for weapons/armor child items --- .../src/generators/LocationLootGenerator.ts | 28 +++---------------- project/src/helpers/ItemHelper.ts | 21 ++++++++++++++ .../src/services/LocationLifecycleService.ts | 1 + 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/project/src/generators/LocationLootGenerator.ts b/project/src/generators/LocationLootGenerator.ts index ed8783db..a47ffa0c 100644 --- a/project/src/generators/LocationLootGenerator.ts +++ b/project/src/generators/LocationLootGenerator.ts @@ -873,13 +873,13 @@ export class LocationLootGenerator { } else { // Also used by armors to get child mods // Get item + children and add into array we return - const itemWithChildren = this.itemHelper.findAndReturnChildrenAsItems( + let itemWithChildren = this.itemHelper.findAndReturnChildrenAsItems( spawnPoint.template.Items, chosenItem._id, ); - // We need to reparent to ensure ids are unique - this.reparentItemAndChildren(itemWithChildren); + // Ensure all IDs are unique + itemWithChildren = this.itemHelper.replaceIDs(itemWithChildren); itemWithMods.push(...itemWithChildren); } @@ -890,26 +890,6 @@ export class LocationLootGenerator { return { items: itemWithMods, width: size.width, height: size.height }; } - /** - * Replace the _id value for base item + all children items parentid value - * @param itemWithChildren Item with mods to update - * @param newId new id to add on chidren of base item - */ - protected reparentItemAndChildren(itemWithChildren: Item[], newId = this.objectId.generate()): void { - // original id on base item - const oldId = itemWithChildren[0]._id; - - // Update base item to use new id - itemWithChildren[0]._id = newId; - - // Update all parentIds of items attached to base item to use new id - for (const item of itemWithChildren) { - if (item.parentId === oldId) { - item.parentId = newId; - } - } - } - /** * Find an item in array by its _tpl, handle differently if chosenTpl is a weapon * @param items Items array to search @@ -977,7 +957,7 @@ export class LocationLootGenerator { } } else { // RSP30 (62178be9d0050232da3485d9/624c0b3340357b5f566e8766/6217726288ed9f0845317459) doesnt have any default presets and kills this code below as it has no chidren to reparent - this.logger.debug(`createItem() No preset found for weapon: ${chosenTpl}`); + this.logger.debug(`createStaticLootItem() No preset found for weapon: ${chosenTpl}`); } const rootItem = items[0]; diff --git a/project/src/helpers/ItemHelper.ts b/project/src/helpers/ItemHelper.ts index 3b9f15c2..4de9c210 100644 --- a/project/src/helpers/ItemHelper.ts +++ b/project/src/helpers/ItemHelper.ts @@ -787,6 +787,27 @@ export class ItemHelper { return matchingItems; } + /** + * Replace the _id value for base item + all children that are children of it + * REPARENTS ROOT ITEM ID, NOTHING ELSE + * @param itemWithChildren Item with mods to update + * @param newId new id to add on chidren of base item + */ + public replaceRootItemID(itemWithChildren: Item[], newId = this.objectId.generate()): void { + // original id on base item + const oldId = itemWithChildren[0]._id; + + // Update base item to use new id + itemWithChildren[0]._id = newId; + + // Update all parentIds of items attached to base item to use new id + for (const item of itemWithChildren) { + if (item.parentId === oldId) { + item.parentId = newId; + } + } + } + /** * Regenerate all GUIDs with new IDs, for the exception of special item types (e.g. quest, sorting table, etc.) This * function will not mutate the original items array, but will return a new array with new GUIDs. diff --git a/project/src/services/LocationLifecycleService.ts b/project/src/services/LocationLifecycleService.ts index f69ba823..b85ef3db 100644 --- a/project/src/services/LocationLifecycleService.ts +++ b/project/src/services/LocationLifecycleService.ts @@ -85,6 +85,7 @@ export class LocationLifecycleService { this.locationConfig = this.configServer.getConfig(ConfigTypes.LOCATION); } + /** Handle client/match/local/start */ public startLocalRaid(sessionId: string, request: IStartLocalRaidRequestData): IStartLocalRaidResponseData { const playerProfile = this.profileHelper.getPmcProfile(sessionId);