Update loot table preview, item display and tooltips to 1.21

This commit is contained in:
Misode
2024-09-11 02:31:17 +02:00
parent 337b7d9b0a
commit fd6de2ac85
15 changed files with 2073 additions and 408 deletions

View File

@@ -4,6 +4,7 @@ import { useEffect, useMemo, useRef, useState } from 'preact/hooks'
import { useLocale, useVersion } from '../../contexts/index.js'
import { useAsync } from '../../hooks/useAsync.js'
import { fetchAllPresets } from '../../services/index.js'
import { jsonToNbt } from '../../Utils.js'
import { Btn, BtnMenu } from '../index.js'
import { ItemDisplay } from '../ItemDisplay.jsx'
import type { PreviewProps } from './index.js'
@@ -172,18 +173,17 @@ function placeItems(recipe: any, animation: number, itemTags: Map<string, any>)
items.set(resultSlot, base)
}
} else if (typeof result === 'string') {
try {
items.set(resultSlot, new ItemStack(Identifier.parse(result), 1))
} catch (e) {}
items.set(resultSlot, new ItemStack(Identifier.parse(result), 1))
} else if (typeof result === 'object' && result !== null) {
const id = typeof result.id === 'string' ? result.id
: typeof result.item === 'string' ? result.item
: 'minecraft:air'
const count = typeof result.count === 'number' ? result.count : 1
// TODO: add components
try {
items.set(resultSlot, new ItemStack(Identifier.parse(id), count))
} catch (e) {}
if (id !== 'minecraft:air') {
const count = typeof result.count === 'number' ? result.count : 1
const components = new Map(Object.entries(result.components ?? {})
.map(([k, v]) => [k, jsonToNbt(v)]))
items.set(resultSlot, new ItemStack(Identifier.parse(id), count, components))
}
}
return items
@@ -195,19 +195,14 @@ function allIngredientChoices(ingredient: any, itemTags: Map<string, any>): Item
}
if (typeof ingredient === 'object' && ingredient !== null) {
if (typeof ingredient.item === 'string') {
try {
return [new ItemStack(Identifier.parse(ingredient.item), 1)]
} catch (e) {}
return [new ItemStack(Identifier.parse(ingredient.item), 1)]
} else if (typeof ingredient.tag === 'string') {
const tag: any = itemTags.get(ingredient.tag.replace(/^minecraft:/, ''))
if (typeof tag === 'object' && tag !== null && Array.isArray(tag.values)) {
return tag.values.flatMap((value: any) => {
if (typeof value !== 'string') return []
if (value.startsWith('#')) return allIngredientChoices({ tag: value.slice(1) }, itemTags)
try {
return [new ItemStack(Identifier.parse(value), 1)]
} catch (e) {}
return []
return [new ItemStack(Identifier.parse(value), 1)]
})
}
}