Skip to main content
The Deno 2 Release Candidate is here
Learn more
Latest
class ItemHelper
Re-export
import { ItemHelper } from "https://deno.land/x/sptaki@1.2.0/helpers/mod.ts";

Constructors

new
ItemHelper(
logger: ILogger,
hashUtil: HashUtil,
jsonUtil: JsonUtil,
randomUtil: RandomUtil,
objectId: ObjectId,
mathUtil: MathUtil,
databaseServer: DatabaseServer,
handbookHelper: HandbookHelper,
itemBaseClassService: ItemBaseClassService,
itemFilterService: ItemFilterService,
localisationService: LocalisationService,
localeService: LocaleService,
compareUtil: CompareUtil,
)

Properties

protected
compareUtil: CompareUtil
protected
databaseServer: DatabaseServer
protected
readonly
defaultInvalidBaseTypes: string[]
protected
handbookHelper: HandbookHelper
protected
hashUtil: HashUtil
protected
itemBaseClassService: ItemBaseClassService
protected
itemFilterService: ItemFilterService
protected
jsonUtil: JsonUtil
protected
localeService: LocaleService
protected
localisationService: LocalisationService
protected
logger: ILogger
protected
mathUtil: MathUtil
protected
objectId: ObjectId
protected
randomUtil: RandomUtil

Methods

protected
drawAmmoTpl(
caliber: string,
staticAmmoDist: Record<string, IStaticAmmoDetails[]>,
fallbackCartridgeTpl: string,
cartridgeWhitelist?: string[],
): string

Chose a randomly weighted cartridge that fits

protected
getRandomValidCaliber(magTemplate: ITemplateItem): string

Choose a random bullet type from the list of possible a magazine has

protected
getRepairableItemQualityValue(
itemDetails: ITemplateItem,
repairable: Repairable,
item: Item,
): number

Get a quality value based on a repairable items (weapon/armor) current state between current and max durability

addCartridgesToAmmoBox(ammoBox: Item[], ammoBoxDetails: ITemplateItem): void

Add cartridges to the ammo box with correct max stack sizes

addChildSlotItems(
itemToAdd: Item[],
itemToAddTemplate: ITemplateItem,
modSpawnChanceDict?: Record<string, number>,
requiredOnly?: boolean,
): Item[]

Add child slot items to an item, chooses random child item if multiple choices exist

addSingleStackCartridgesToAmmoBox(ammoBox: Item[], ammoBoxDetails: ITemplateItem): void

Add a single stack of cartridges to the ammo box

addUpdObjectToItem(item: Item, warningMessageWhenMissing?: string): boolean

Add a blank upd object to passed in item if it does not exist already

adoptOrphanedItems(rootId: string, items: Item[]): Item[]

Adopts orphaned items by resetting them as root "hideout" items. Helpful in situations where a parent has been deleted from a group of items and there are children still referencing the missing parent. This method will remove the reference from the children to the parent and set item properties to root values.

armorItemCanHoldMods(itemTpl: string): boolean

Does the provided item have the chance to require soft armor inserts Only applies to helmets/vest/armors. Not all head gear needs them

armorItemHasRemovableOrSoftInsertSlots(itemTpl: string): boolean

Does the provided item tpl need soft/removable inserts to function

armorItemHasRemovablePlateSlots(itemTpl: string): boolean

Does the pased in tpl have ability to hold removable plate items

createCartridges(
parentId: string,
ammoTpl: string,
stackCount: number,
location: number,
foundInRaid?: boolean,
): Item

Create a basic cartrige object

doesItemOrParentsIdMatch(tpl: string, tplsToCheck: string[]): boolean

WARNING, SLOW. Recursively loop down through an items hierarchy to see if any of the ids match the supplied list, return true if any do

fillMagazineWithCartridge(
magazineWithChildCartridges: Item[],
magTemplate: ITemplateItem,
cartridgeTpl: string,
minSizePercent?: number,
): void

Add child items to a magazine of a specific cartridge

fillMagazineWithRandomCartridge(
magazine: Item[],
magTemplate: ITemplateItem,
staticAmmoDist: Record<string, IStaticAmmoDetails[]>,
caliber?: string,
minSizePercent?: number,
weapon?: ITemplateItem,
): void

Add child items (cartridges) to a magazine

findAndReturnChildrenAsItems(
items: Item[],
baseItemId: string,
modsOnly?: boolean,
): Item[]

A variant of findAndReturnChildren where the output is list of item objects instead of their ids.

findAndReturnChildrenByAssort(itemIdToFind: string, assort: Item[]): Item[]

Find children of the item in a given assort (weapons parts for example, need recursive loop function)

findAndReturnChildrenByItems(items: Item[], baseItemId: string): string[]

Recursive function that looks at every item from parameter and gets their childrens Ids + includes parent item in results

findBarterItems(
by: "tpl" | "id",
itemsToSearch: Item[],
desiredBarterItemIds: string | string[],
): Item[]

Find Barter items from array of items

Update items upd.StackObjectsCount to be 1 if its upd is missing or StackObjectsCount is undefined

generateItemsMap(items: Item[]): Map<string, Item>

Populate a Map object of items for quick lookup using their ID.

Helper method to generate a Upd based on a template

getAttachmentMainParent(itemId: string, itemsMap: Map<string, Item>): Item | null

Retrieves the main parent item for a given attachment item.

This method traverses up the hierarchy of items starting from a given itemId, until it finds the main parent item that is not an attached attachment itself. In other words, if you pass it an item id of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, and return the gun that the suppressor is ultimately attached to, even if that gun is located within multiple containers.

It's important to note that traversal is expensive, so this method requires that you pass it a Map of the items to traverse, where the keys are the item IDs and the values are the corresponding Item objects. This alleviates some of the performance concerns, as it allows for quick lookups of items by ID.

getChildId(item: Item): string

Gets the identifier for a child using slotId, locationX and locationY.

getCompatibleTplFromArray(possibleTpls: string[], incompatibleModTpls: Set<string>): string

Get a compatible tpl from the array provided where it is not found in the provided incompatible mod tpls parameter

getDynamicItemPrice(tpl: string): number

Get the dynamic (flea) price in roubles for an item by tpl

getEquipmentParent(itemId: string, itemsMap: Map<string, Item>): Item | null

Retrieves the equipment parent item for a given item.

This method traverses up the hierarchy of items starting from a given itemId, until it finds the equipment parent item. In other words, if you pass it an item id of a suppressor, it will traverse up the muzzle brake, barrel, upper receiver, gun, nested backpack, and finally return the backpack Item that is equipped.

It's important to note that traversal is expensive, so this method requires that you pass it a Map of the items to traverse, where the keys are the item IDs and the values are the corresponding Item objects. This alleviates some of the performance concerns, as it allows for quick lookups of items by ID.

getItem(tpl: string): [boolean, ITemplateItem]

Gets item data from items.json

getItemAndChildrenPrice(tpls: string[]): number

Returns the items total price based on the handbook or as a fallback from the prices.json if the item is not found in the handbook. If the price can't be found at all return 0

getItemMaxPrice(tpl: string): number

Returns the item price based on the handbook or as a fallback from the prices.json if the item is not found in the handbook. If the price can't be found at all return 0

getItemName(itemTpl: string): string

Get the name of an item from the locale file using the item tpl

getItemPrice(tpl: string): number

Returns the item price based on the handbook or as a fallback from the prices.json if the item is not found in the handbook. If the price can't be found at all return 0

get normalized value (0-1) based on item condition

Calcualte the average quality of an item and its children

Get cloned copy of all item data from items.json

getItemSize(items: Item[], rootItemId: string): ItemHelper.ItemSize

Get the inventory size of an item

getItemStackSize(item: Item): number

Get the size of a stack, return 1 if no stack object count property found

getItemTplsOfBaseType(desiredBaseType: string): string[]

Get a random cartridge from an items Filter property

Get a list of slot names that hold removable plates

Get all soft insert slot ids

getStaticItemPrice(tpl: string): number

Get the static (handbook) price in roubles for an item by tpl

hasBuyRestrictions(itemToCheck: Item): boolean

Check if the passed in item has buy count restrictions

isAttachmentAttached(item: Item): boolean

Determines if an item is an attachment that is currently attached to it's parent item.

isDogtag(tpl: string): boolean

is the passed in template id a dog tag

isItemInDb(tpl: string): boolean
isItemTplStackable(tpl: string): boolean

Can the passed in item be stacked

isOfBaseclass(tpl: string, baseClassTpl: string): boolean

Check if the tpl / template Id provided is a descendent of the baseclass

isOfBaseclasses(tpl: string, baseClassTpls: string[]): boolean

Check if item has any of the supplied base classes

isQuestItem(tpl: string): boolean

Check if item is quest item

isRaidModdable(item: Item, parent: Item): boolean | null

Checks to see if the item is actually moddable in-raid. Checks include the items existence in the database, the parent items existence in the database, the existence (and value) of the items RaidModdable property, and that the parents slot-required property exists, matches that of the item, and it's value.

Note: this function does not preform any checks to see if the item and parent are actually related.

isRemovablePlateSlot(slotName: string): boolean

Is the provided item._props.Slots._name property a plate slot

isSameItem(
item1: Item,
item2: Item,
compareUpdProperties?: Set<string>,
): boolean

This method will compare two items and see if the are equivalent. This method will NOT compare IDs on the items

isSameItems(
item1: Item[],
item2: Item[],
compareUpdProperties?: Set<string>,
): boolean

This method will compare two items (with all its children) and see if the are equivalent. This method will NOT compare IDs on the items

isValidItem(tpl: string, invalidBaseTypes?: string[]): boolean

Checks if an id is a valid item. Valid meaning that it's an item that be stored in stash

itemHasSlots(itemTpl: string): boolean
itemIsInsideContainer(
item: Item,
desiredContainerSlotId: string,
items: Item[],
): boolean

Check if item is stored inside of a container

itemRequiresSoftInserts(itemTpl: string): boolean

Does the provided item tpl require soft inserts to become a valid armor item

remapRootItemId(itemWithChildren: Item[], newId?: string): string

Update a root items _id property value to be unique

reparentItemAndChildren(rootItem: Item, itemWithChildren: Item[]): Item[]

Generate new unique ids for child items while preserving hierarchy

replaceIDs(
originalItems: Item[],
pmcData?: IPmcData | null,
insuredItems?: InsuredItem[] | null,
fastPanel?: any,
): Item[]

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.

setFoundInRaid(items: Item[]): void

Mark the passed in array of items as found in raid. Modifies passed in items

splitStack(itemToSplit: Item): Item[]

Split item stack if it exceeds its items StackMaxSize property into child items of passed in parent

Turn items like money into separate stacks that adhere to max stack size