Fix #832 enchant_with_levels loot preview

This commit is contained in:
Misode
2026-02-01 00:07:09 +01:00
parent 84028a06f1
commit f243be3d5e

View File

@@ -311,7 +311,9 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
})
},
enchant_with_levels: ({ options, levels }) => (item, ctx) => {
const allowed = getHomogeneousList(options, ctx.getEnchantmentTag)
const allowed = options
? getHomogeneousList(options, ctx.getEnchantmentTag)
: [...ctx.getEnchantments().keys()]
const selected = selectEnchantments(item, computeInt(levels, ctx), allowed, ctx)
if (item.is('book')) {
item.id = Identifier.create('enchanted_book')
@@ -817,10 +819,13 @@ interface Enchant {
}
function selectEnchantments(item: ResolvedItem, levels: number, options: string[], ctx: LootContext): Enchant[] {
const enchantable = item.get('enchantable', tag => tag.isCompound() ? tag.getNumber('value') : undefined)
let enchantable: number | undefined = 1 // Not fully correct before version 1.21.2
if (checkVersion(ctx.version, '1.21.2')) {
enchantable = item.get('enchantable', tag => tag.isCompound() ? tag.getNumber('value') : undefined)
if (enchantable === undefined) {
return []
}
}
let cost = levels + 1 + ctx.random.nextInt(Math.floor(enchantable / 4 + 1)) + ctx.random.nextInt(Math.floor(enchantable / 4 + 1))
const f = (ctx.random.nextFloat() + ctx.random.nextFloat() - 1) * 0.15
cost = clamp(Math.round(cost + cost * f), 1, Number.MAX_SAFE_INTEGER)