From 9dd2d3cd04a7542305426336723906c1cc2ba709 Mon Sep 17 00:00:00 2001 From: Dev Date: Sat, 5 Aug 2023 14:50:05 +0100 Subject: [PATCH] Improve nullguard code inside findAndAddRandomPresetToLoot() --- project/src/generators/LootGenerator.ts | 21 +++++++++++++++---- project/src/models/eft/common/tables/IItem.ts | 3 ++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/project/src/generators/LootGenerator.ts b/project/src/generators/LootGenerator.ts index e5b7f3f7..bbd404b4 100644 --- a/project/src/generators/LootGenerator.ts +++ b/project/src/generators/LootGenerator.ts @@ -218,7 +218,20 @@ export class LootGenerator { // Choose random preset and get details from item.json using encyclopedia value (encyclopedia === tplId) const randomPreset = this.randomUtil.getArrayValue(globalDefaultPresets)[1]; - const itemDetails = this.databaseServer.getTables().templates.items[randomPreset._encyclopedia]; + if (!randomPreset?._encyclopedia) + { + this.logger.error(`Airdrop - preset with id: ${randomPreset._id} lacks encyclopedia property`); + + return false; + } + + const itemDetails = this.itemHelper.getItem(randomPreset._encyclopedia); + if (!itemDetails[0]) + { + this.logger.error(`Airdrop - Unable to find preset with tpl: ${randomPreset._encyclopedia}`); + + return false; + } // Skip blacklisted items if (itemBlacklist.includes(randomPreset._items[0]._tpl)) @@ -227,15 +240,15 @@ export class LootGenerator } // Some custom mod items are lacking a parent property - if (!itemDetails._parent) + if (!itemDetails[1]._parent) { - this.logger.error(this.localisationService.getText("loot-item_missing_parentid", itemDetails._name)); + this.logger.error(this.localisationService.getText("loot-item_missing_parentid", itemDetails[1]?._name)); return false; } // Check picked preset hasn't exceeded spawn limit - const itemLimitCount = itemTypeCounts[itemDetails._parent]; + const itemLimitCount = itemTypeCounts[itemDetails[1]._parent]; if (itemLimitCount && itemLimitCount.current > itemLimitCount.max) { return false; diff --git a/project/src/models/eft/common/tables/IItem.ts b/project/src/models/eft/common/tables/IItem.ts index cd583f5a..7dbc0f68 100644 --- a/project/src/models/eft/common/tables/IItem.ts +++ b/project/src/models/eft/common/tables/IItem.ts @@ -16,7 +16,8 @@ export interface Upd Togglable?: Togglable Map?: Map Tag?: Tag - sptPresetId?: string // SPT specific property, not made by BSG + /** SPT specific property, not made by BSG */ + sptPresetId?: string FaceShield?: FaceShield StackObjectsCount?: number UnlimitedCount?: boolean