Make getItemSize() more resilient to bad data - return 1x1 size if item not found + show error to player

Fix incorrect return type for getItemSize()
This commit is contained in:
Dev 2023-04-12 15:51:52 +01:00
parent 43d9f6490c
commit 70da4a632e
2 changed files with 16 additions and 4 deletions

View File

@ -83,6 +83,7 @@
"inventory-unable_to_fill_container": "[OOB] for item: {{id}}; Error message: {{error}}", "inventory-unable_to_fill_container": "[OOB] for item: {{id}}; Error message: {{error}}",
"inventory-unable_to_find_item": "getExaminedItemTpl() Unable to find item with tpl: %s in database or flea", "inventory-unable_to_find_item": "getExaminedItemTpl() Unable to find item with tpl: %s in database or flea",
"inventory-unable_to_find_stash": "No stash found", "inventory-unable_to_find_stash": "No stash found",
"inventory-return_default_size": "Defaulting item %s to size 1x1",
"item-durability_value_invalid_use_default": "getRepairableItemQualityValue() weapon tpl: %s durability value is invalid, defaulting to 1", "item-durability_value_invalid_use_default": "getRepairableItemQualityValue() weapon tpl: %s durability value is invalid, defaulting to 1",
"linux_use_priviledged_port_non_root": "Non-root processes cannot bind to ports below 1024", "linux_use_priviledged_port_non_root": "Non-root processes cannot bind to ports below 1024",
"location-containers_generated_success": "A total of %s static containers generated", "location-containers_generated_success": "A total of %s static containers generated",

View File

@ -528,7 +528,7 @@ export class InventoryHelper
* inputs Item template ID, Item Id, InventoryItem (item from inventory having _id and _tpl) * inputs Item template ID, Item Id, InventoryItem (item from inventory having _id and _tpl)
* outputs [width, height] * outputs [width, height]
*/ */
public getItemSize(itemTpl: string, itemID: string, inventoryItem: Item[]): Record<number, number> public getItemSize(itemTpl: string, itemID: string, inventoryItem: Item[]): number[]
{ {
// -> Prepares item Width and height returns [sizeX, sizeY] // -> Prepares item Width and height returns [sizeX, sizeY]
return this.getSizeByInventoryItemHash(itemTpl, itemID, this.getInventoryItemHash(inventoryItem)); return this.getSizeByInventoryItemHash(itemTpl, itemID, this.getInventoryItemHash(inventoryItem));
@ -536,20 +536,31 @@ export class InventoryHelper
// note from 2027: there IS a thing i didn't explore and that is Merges With Children // note from 2027: there IS a thing i didn't explore and that is Merges With Children
// -> Prepares item Width and height returns [sizeX, sizeY] // -> Prepares item Width and height returns [sizeX, sizeY]
protected getSizeByInventoryItemHash(itemTpl: string, itemID: string, inventoryItemHash: InventoryHelper.InventoryItemHash): Record<number, number> protected getSizeByInventoryItemHash(itemTpl: string, itemID: string, inventoryItemHash: InventoryHelper.InventoryItemHash): number[]
{ {
const toDo = [itemID]; const toDo = [itemID];
const result = this.itemHelper.getItem(itemTpl); const result = this.itemHelper.getItem(itemTpl);
const tmpItem = result[1]; const tmpItem = result[1];
// Invalid item or no object
if (!(result[0] && result[1])) if (!(result[0] && result[1]))
{ {
this.logger.error(this.localisationService.getText("inventory-invalid_item_missing_from_db", itemTpl)); this.logger.error(this.localisationService.getText("inventory-invalid_item_missing_from_db", itemTpl));
} }
if (!tmpItem._props) // item found but no _props property
if (tmpItem && !tmpItem._props)
{ {
this.logger.error(`Item ${tmpItem._id} ${tmpItem._name} is missing a props field, a size for it cannot be acquired`); this.logger.error(`Item ${itemTpl} ${tmpItem?._name} is missing a props field, a size for it cannot be acquired`);
}
// No item object or getItem() returned false
if (!(tmpItem && result[0]))
{
// return default size of 1x1
this.logger.error(this.localisationService.getText("inventory-return_default_size", itemTpl));
return [1, 1];
} }
const rootItem = inventoryItemHash.byItemId[itemID]; const rootItem = inventoryItemHash.byItemId[itemID];