From 582409e7cb1598b65bee6d1023b77620bb3791af Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 20 Feb 2022 02:16:09 -0600 Subject: add inventories --- src/lib/minecraft/Inventory.svelte | 22 +++------------ src/lib/minecraft/Item.svelte | 21 ++++++++++---- src/lib/minecraft/inventory.ts | 54 ++++++------------------------------ src/lib/sections/Armor.svelte | 4 ++- src/lib/sections/Inventories.svelte | 55 +++++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 70 deletions(-) create mode 100644 src/lib/sections/Inventories.svelte (limited to 'src/lib') diff --git a/src/lib/minecraft/Inventory.svelte b/src/lib/minecraft/Inventory.svelte index 3d3b9c0..d29b1e0 100644 --- a/src/lib/minecraft/Inventory.svelte +++ b/src/lib/minecraft/Inventory.svelte @@ -4,6 +4,7 @@ export let items export let name = '' export let pack = '' + export let groupLimit = 9 if (name === 'inventory') // in the inventory, the first 9 items are the hotbar and should be at the end @@ -13,14 +14,14 @@ let itemGroups = [] $: { itemGroups = [] - for (let i = 0; i < items.length; i += 9) { - itemGroups.push(items.slice(i, i + 9)) + for (let i = 0; i < items.length; i += groupLimit) { + itemGroups.push(items.slice(i, i + groupLimit)) } }
- {#each itemGroups as itemGroup, groupIndex} + {#each itemGroups as itemGroup}
{#each itemGroup as item} @@ -28,18 +29,3 @@
{/each}
- - diff --git a/src/lib/minecraft/Item.svelte b/src/lib/minecraft/Item.svelte index 8ddc4a8..c944f1b 100644 --- a/src/lib/minecraft/Item.svelte +++ b/src/lib/minecraft/Item.svelte @@ -1,22 +1,26 @@ {@html itemNameHtml} {@html itemLoreHtml} - + {#if item} {#if imageUrl} @@ -60,6 +64,13 @@ image-rendering: pixelated; } + img.item-custom-head { + width: 0.75em; + height: 0.75em; + margin-top: 0.1875em; + margin-left: 0.1875em; + } + .item-slot { margin: 0.05em; } diff --git a/src/lib/minecraft/inventory.ts b/src/lib/minecraft/inventory.ts index fa75887..cb926b4 100644 --- a/src/lib/minecraft/inventory.ts +++ b/src/lib/minecraft/inventory.ts @@ -1,7 +1,7 @@ -import vanillaDamages from 'skyblock-assets/data/vanilla_damages.json' import * as skyblockAssets from 'skyblock-assets' +import vanilla from 'skyblock-assets/matchers/vanilla.json' +import packshq from 'skyblock-assets/matchers/vanilla.json' -const itemToUrlCache: Record = {} interface Item { id?: string @@ -36,10 +36,7 @@ const INVENTORIES = { export type Inventories = { [name in keyof typeof INVENTORIES]: Item[] } -export async function itemToUrl(item: Item, packName?: string): Promise { - const stringifiedItem = (packName ?? 'packshq') + JSON.stringify(item) - if (stringifiedItem in itemToUrlCache) - return itemToUrlCache[stringifiedItem] +export function itemToUrl(item: Item, packName?: string): string { const itemNbt: skyblockAssets.NBT = { display: { Name: item.display?.name @@ -52,21 +49,22 @@ export async function itemToUrl(item: Item, packName?: string): Promise if (item.head_texture) textureUrl = `https://mc-heads.net/head/${item.head_texture}` else - textureUrl = await skyblockAssets.getTextureUrl({ + textureUrl = skyblockAssets.getTextureUrl({ id: item.vanillaId, nbt: itemNbt, - pack: packName ?? 'packshq' + packs: [packshq, vanilla] }) if (!textureUrl) console.log('no texture', item) - itemToUrlCache[stringifiedItem] = textureUrl return textureUrl } + export async function skyblockItemToUrl(skyblockItemName: string) { const item = skyblockItemNameToItem(skyblockItemName) const itemTextureUrl = await itemToUrl(item, 'packshq') return itemTextureUrl } + export function skyblockItemNameToItem(skyblockItemName: string): Item { let item: Item if (Object.keys(skyblockItems).includes(skyblockItemName)) { @@ -78,6 +76,7 @@ export function skyblockItemNameToItem(skyblockItemName: string): Item { } return item } + const skyblockItems: { [itemName: string]: Item } = { ink_sac: { vanillaId: 'minecraft:dye' }, cocoa_beans: { vanillaId: 'minecraft:dye:3' }, @@ -102,41 +101,4 @@ const skyblockItems: { [itemName: string]: Item } = { head_texture: '39b6e047d3b2bca85e8cc49e5480f9774d8a0eafe6dfa9559530590283715142' }, hard_stone: { vanillaId: 'minecraft:stone' }, -} - -export function itemToUrlCached(item: Item, packName?: string): string { - if (!item) return null - if (typeof item === 'string') { - let itemId: string = vanillaDamages[item] ?? item - let damage: number = null - // remove the minecraft: namespace since we already know it's all vanilla - if (itemId.startsWith('minecraft:')) itemId = itemId.slice('minecraft:'.length) - // split the damage into its own variable - if (itemId.includes(':')) { - damage = parseInt(itemId.split(':')[1]) - itemId = itemId.split(':')[0] - } - item = { - count: 1, - display: { - glint: false, - lore: null, - name: null - }, - id: null, - // vanillaId: damage === null ? `minecraft:${itemId}` : `minecraft:${itemId}:${damage}` - vanillaId: `minecraft:${itemId}` - } - } - const stringifiedItem = (packName ?? 'packshq') + JSON.stringify(item) - return itemToUrlCache[stringifiedItem] -} -/** Get all the items in an inventories object to cache them */ -export async function cacheInventories(inventories: Inventories, packName?: string) { - const promises: Promise[] = [] - for (const inventoryItems of Object.values(inventories ?? {})) - for (const inventoryItem of inventoryItems) - if (inventoryItem) - promises.push(itemToUrl(inventoryItem, packName)) - await Promise.all(promises) } \ No newline at end of file diff --git a/src/lib/sections/Armor.svelte b/src/lib/sections/Armor.svelte index c4e7950..ab0e73b 100644 --- a/src/lib/sections/Armor.svelte +++ b/src/lib/sections/Armor.svelte @@ -4,4 +4,6 @@ export let pack - + + + diff --git a/src/lib/sections/Inventories.svelte b/src/lib/sections/Inventories.svelte new file mode 100644 index 0000000..49a00c2 --- /dev/null +++ b/src/lib/sections/Inventories.svelte @@ -0,0 +1,55 @@ + + +
+ {#each displayingInventories as inventoryName} + + {/each} +
+{#each displayingInventories as inventoryName} + {#if inventoryName === selectedInventoryName} +
+ +
+ {/if} +{/each} + + -- cgit