diff options
author | mat <github@matdoes.dev> | 2022-04-23 22:08:29 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-04-23 22:08:29 -0500 |
commit | fa42cdc56e21d6d50f17db6143b74d8b57f179be (patch) | |
tree | 0ea27306653c23b683d89ffe8344e72f0626cd2a /src/lib/minecraft/inventory.ts | |
parent | 0f0a7eb82120a7cdaa392bc33983be656b85750f (diff) | |
download | skyblock-stats-fa42cdc56e21d6d50f17db6143b74d8b57f179be.tar.gz skyblock-stats-fa42cdc56e21d6d50f17db6143b74d8b57f179be.tar.bz2 skyblock-stats-fa42cdc56e21d6d50f17db6143b74d8b57f179be.zip |
add /items page
Diffstat (limited to 'src/lib/minecraft/inventory.ts')
-rw-r--r-- | src/lib/minecraft/inventory.ts | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/lib/minecraft/inventory.ts b/src/lib/minecraft/inventory.ts index 871af93..ec0e193 100644 --- a/src/lib/minecraft/inventory.ts +++ b/src/lib/minecraft/inventory.ts @@ -1,6 +1,6 @@ import * as skyblockAssets from 'skyblock-assets' import { vanilla } from '$lib/packs' - +import { browser } from '$app/env' export interface Item { id?: string @@ -97,6 +97,13 @@ export const inventoryIconMap: Record<string, string | Item> = { export type Inventories = { [name in keyof typeof INVENTORIES]: Item[] } +// we cache the item urls because it takes a bit of time to get them usually +// { "<pack> <item id>": "https://..." } +let itemUrlCache: Record<string, string> = {} +// clear the cache every 120 seconds, this number is arbitrary +setInterval(() => { + itemUrlCache = {} +}, 120 * 1000) export function itemToUrl(item: Item, pack?: skyblockAssets.MatcherFile, headSize?: number): string { const itemNbt: skyblockAssets.NBT = { display: { @@ -107,6 +114,11 @@ export function itemToUrl(item: Item, pack?: skyblockAssets.MatcherFile, headSiz }, } let textureUrl: string + + const itemCacheIdentifier = `${pack?.dir ?? 'v'} ${JSON.stringify(itemNbt)}` + if (itemCacheIdentifier in itemUrlCache) + return itemUrlCache[itemCacheIdentifier] + if (item.headTexture) { // if it's a head, try without vanilla and if it fails just use the mc-heads url textureUrl = skyblockAssets.getTextureUrl({ @@ -120,21 +132,24 @@ export function itemToUrl(item: Item, pack?: skyblockAssets.MatcherFile, headSiz if (headSize) textureUrl += `/${headSize}` } - } else + } else { textureUrl = skyblockAssets.getTextureUrl({ id: item.vanillaId, nbt: itemNbt, packs: pack ? [pack, vanilla as skyblockAssets.MatcherFile] : [vanilla as skyblockAssets.MatcherFile], }) + } + itemUrlCache[itemCacheIdentifier] = textureUrl return textureUrl } export function skyblockItemToUrl(skyblockItem: string | Item, pack?: skyblockAssets.MatcherFile, headSize?: number) { - const item: Item = typeof skyblockItem === 'string' ? skyblockItemNameToItem(skyblockItem) : skyblockItem + const item = typeof skyblockItem === 'string' ? skyblockItemNameToItem(skyblockItem) : skyblockItem const itemTextureUrl = itemToUrl(item, pack, headSize) return itemTextureUrl } + export function skyblockItemNameToItem(skyblockItemName: string): Item { let item: Item if (Object.keys(skyblockItems).includes(skyblockItemName)) { |