From 2404e7a5ab5725bbadd69dcae10ba3385e1c6ceb Mon Sep 17 00:00:00 2001 From: DrakiaXYZ Date: Mon, 8 Apr 2024 17:39:08 +0000 Subject: [PATCH] Fix issues with incorrectly configured airdrops (!280) - Don't try to add items to the airdrop if the filtered item list is empty - If we somehow get into `findAndAddRandomPresetToLoot` with an empty list, don't try to output the id of undefined Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/280 Co-authored-by: DrakiaXYZ Co-committed-by: DrakiaXYZ --- project/src/generators/LootGenerator.ts | 55 ++++++++++++++++--------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/project/src/generators/LootGenerator.ts b/project/src/generators/LootGenerator.ts index a73a0094..5d5df833 100644 --- a/project/src/generators/LootGenerator.ts +++ b/project/src/generators/LootGenerator.ts @@ -99,12 +99,15 @@ export class LootGenerator && options.itemTypeWhitelist.includes(x[1]._parent) ); - const randomisedItemCount = this.randomUtil.getInt(options.itemCount.min, options.itemCount.max); - for (let index = 0; index < randomisedItemCount; index++) + if (items.length > 0) { - if (!this.findAndAddRandomItemToLoot(items, itemTypeCounts, options, result)) + const randomisedItemCount = this.randomUtil.getInt(options.itemCount.min, options.itemCount.max); + for (let index = 0; index < randomisedItemCount; index++) { - index--; + if (!this.findAndAddRandomItemToLoot(items, itemTypeCounts, options, result)) + { + index--; + } } } @@ -122,13 +125,21 @@ export class LootGenerator this.itemHelper.isOfBaseclass(preset._encyclopedia, BaseClasses.WEAPON) ); - for (let index = 0; index < randomisedWeaponPresetCount; index++) + if (weaponDefaultPresets.length > 0) { - if ( - !this.findAndAddRandomPresetToLoot(weaponDefaultPresets, itemTypeCounts, itemBlacklistArray, result) - ) + for (let index = 0; index < randomisedWeaponPresetCount; index++) { - index--; + if ( + !this.findAndAddRandomPresetToLoot( + weaponDefaultPresets, + itemTypeCounts, + itemBlacklistArray, + result, + ) + ) + { + index--; + } } } } @@ -146,18 +157,22 @@ export class LootGenerator const levelFilteredArmorPresets = armorDefaultPresets.filter((armor) => this.armorIsDesiredProtectionLevel(armor, options) ); - for (let index = 0; index < randomisedArmorPresetCount; index++) + + if (levelFilteredArmorPresets.length > 0) { - if ( - !this.findAndAddRandomPresetToLoot( - levelFilteredArmorPresets, - itemTypeCounts, - itemBlacklistArray, - result, - ) - ) + for (let index = 0; index < randomisedArmorPresetCount; index++) { - index--; + if ( + !this.findAndAddRandomPresetToLoot( + levelFilteredArmorPresets, + itemTypeCounts, + itemBlacklistArray, + result, + ) + ) + { + index--; + } } } } @@ -307,7 +322,7 @@ export class LootGenerator const randomPreset = this.randomUtil.getArrayValue(globalDefaultPresets); if (!randomPreset?._encyclopedia) { - this.logger.debug(`Airdrop - preset with id: ${randomPreset._id} lacks encyclopedia property, skipping`); + this.logger.debug(`Airdrop - preset with id: ${randomPreset?._id} lacks encyclopedia property, skipping`); return false; }