Fix not correctly enchanting books

This commit is contained in:
Misode
2022-10-13 02:31:44 +02:00
parent ac259bb83e
commit f309eef176
4 changed files with 20 additions and 7 deletions

View File

@@ -21,6 +21,7 @@ export function ItemTooltip({ id, tag, advanced, offset = [0, 0], swap }: Props)
const name = displayName ? JSON.parse(displayName) : (translatedName ?? fakeTranslation(id))
const maxDamage = MaxDamageItems.get(id)
const enchantments = (id === 'minecraft:enchanted_book' ? tag?.StoredEnchantments : tag?.Enchantments) ?? []
return <div class="item-tooltip" style={offset && {
left: (swap ? undefined : `${offset[0]}px`),
@@ -28,7 +29,7 @@ export function ItemTooltip({ id, tag, advanced, offset = [0, 0], swap }: Props)
top: `${offset[1]}px`,
}}>
<TextComponent component={name} base={{ color: 'white' }} />
{tag?.Enchantments?.map(({ id, lvl }: { id: string, lvl: number }) => {
{enchantments.map(({ id, lvl }: { id: string, lvl: number }) => {
const ench = getEnchantmentData(id)
const component: any[] = [{ translate: `enchantment.${id.replace(':', '.')}`, color: ench?.curse ? 'red' : 'gray' }]
if (lvl !== 1 || ench?.maxLevel !== 1) {

View File

@@ -25,6 +25,7 @@ export const LootTablePreview = ({ data }: PreviewProps) => {
const state = JSON.stringify(table)
useEffect(() => {
const items = generateLootTable(table, { version, seed, luck, daytime, weather, stackMixer: mixItems ? 'container' : 'default' })
console.log('Generated loot', items)
setItems(items)
}, [version, seed, luck, daytime, weather, mixItems, state])

View File

@@ -271,22 +271,33 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
return data.discoverable && (isBook || data.canEnchant(item.id))
})
}
const id = enchantments[ctx.random.nextInt(enchantments.length)]
const data = getEnchantmentData(id)
const lvl = ctx.random.nextInt(data.maxLevel - data.minLevel + 1) + data.minLevel
enchantItem(item, { id, lvl })
if (enchantments.length > 0) {
const id = enchantments[ctx.random.nextInt(enchantments.length)]
const data = getEnchantmentData(id)
const lvl = ctx.random.nextInt(data.maxLevel - data.minLevel + 1) + data.minLevel
if (isBook) {
item.tag = {}
item.count = 1
}
enchantItem(item, { id, lvl })
if (isBook) {
item.id = 'minecraft:enchanted_book'
}
}
},
enchant_with_levels: ({ levels, treasure }) => (item, ctx) => {
const enchants = selectEnchantments(ctx.random, item, computeInt(levels, ctx), treasure)
const isBook = item.id === 'minecraft:book'
if (isBook) {
item.id = 'minecraft:enchanted_book'
item.count = 1
item.tag = {}
}
for (const enchant of enchants) {
enchantItem(item, enchant)
}
if (isBook) {
item.id = 'minecraft:enchanted_book'
}
},
limit_count: ({ limit }) => (item, ctx) => {
const { min, max } = prepareIntRange(limit, ctx)