From fa42cdc56e21d6d50f17db6143b74d8b57f179be Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 23 Apr 2022 22:08:29 -0500 Subject: add /items page --- src/lib/minecraft/Item.svelte | 35 +++++++++++++++++++++++++++---- src/lib/minecraft/MinecraftTooltip.svelte | 2 ++ src/lib/minecraft/inventory.ts | 21 ++++++++++++++++--- 3 files changed, 51 insertions(+), 7 deletions(-) (limited to 'src/lib/minecraft') diff --git a/src/lib/minecraft/Item.svelte b/src/lib/minecraft/Item.svelte index 4d0997c..cfcc219 100644 --- a/src/lib/minecraft/Item.svelte +++ b/src/lib/minecraft/Item.svelte @@ -1,5 +1,5 @@ @@ -33,7 +60,7 @@ class:item-custom-head={imageUrl.startsWith('https://mc-heads.net/head/')} /> {/if} - {#if item.count !== 1} + {#if item.count !== undefined && item.count !== 1} {item.count} {/if} diff --git a/src/lib/minecraft/MinecraftTooltip.svelte b/src/lib/minecraft/MinecraftTooltip.svelte index 0cdb237..7149626 100644 --- a/src/lib/minecraft/MinecraftTooltip.svelte +++ b/src/lib/minecraft/MinecraftTooltip.svelte @@ -26,6 +26,8 @@ .minecraft-tooltip { /* this makes it be less dumb about the height so it doesn't add extra or anything */ display: grid; + /* this is so it doesn't change width to fill the container in the item list page */ + max-width: fit-content; } /* these elements exist so we can copy them later from GlobalTooltip */ .tooltip-name, 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 = { 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 +// { " ": "https://..." } +let itemUrlCache: Record = {} +// 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)) { -- cgit