Fix #642 more validation in loot table preview
Some checks are pending
Deploy to GitHub Pages / build (push) Waiting to run
Deploy to GitHub Pages / deploy (push) Blocked by required conditions

This commit is contained in:
Misode
2024-11-27 23:40:07 +01:00
parent dc72614e85
commit 94a8210e4c
2 changed files with 11 additions and 6 deletions

View File

@@ -274,7 +274,7 @@ function composeFunctions(functions: any[]): LootFunction {
for (const fn of functions) {
if (Array.isArray(fn)) {
composeFunctions(fn)
} else if (composeConditions(fn.conditions ?? [])(ctx)) {
} else if (isObject(fn) && composeConditions(fn.conditions ?? [])(ctx)) {
const type = fn.function?.replace(/^minecraft:/, '');
(LootFunctions[type]?.(fn) ?? (i => i))(item, ctx)
}
@@ -349,7 +349,7 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
set_attributes: ({ modifiers, replace }) => (item, ctx) => {
if (!Array.isArray(modifiers)) return
const newModifiers = modifiers.map<AttributeModifier>(m => {
if (typeof m !== 'object' || m === null) m = {}
if (!isObject(m)) m = {}
return {
id: Identifier.parse(typeof m.id === 'string' ? m.id : ''),
type: Identifier.parse(typeof m.attribute === 'string' ? m.attribute : ''),
@@ -386,7 +386,7 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
item.set('written_book_content', newContent)
},
set_components: ({ components }) => (item) => {
if (typeof components !== 'object' || components === null) {
if (!isObject(components)) {
return
}
for (const [key, value] of Object.entries(components)) {
@@ -432,7 +432,7 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
}
},
set_enchantments: ({ enchantments, add }) => (item, ctx) => {
if (typeof enchantments !== 'object' || enchantments === null) {
if (!isObject(enchantments)) {
return
}
if (item.is('book')) {
@@ -490,7 +490,9 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
}
},
toggle_tooltips: ({ toggles }) => (item) => {
if (typeof toggles !== 'object' || toggles === null) return
if (!isObject(toggles)) {
return
}
Object.entries(toggles).forEach(([key, value]) => {
if (typeof value !== 'boolean') return
const tag = item.get(key, tag => tag)

View File

@@ -271,7 +271,7 @@ function composeFunctions(functions: any[]): LootFunction {
for (const fn of functions) {
if (Array.isArray(fn)) {
composeFunctions(fn)
} else if (composeConditions(fn.conditions ?? [])(ctx)) {
} else if (isObject(fn) && composeConditions(fn.conditions ?? [])(ctx)) {
const type = fn.function?.replace(/^minecraft:/, '');
(LootFunctions[type]?.(fn) ?? (i => i))(item, ctx)
}
@@ -403,6 +403,9 @@ function testCondition(condition: any, ctx: LootContext): boolean {
if (Array.isArray(condition)) {
return composeConditions(condition)(ctx)
}
if (!isObject(condition) || typeof condition.condition !== 'string') {
return false
}
const type = condition.condition?.replace(/^minecraft:/, '')
return (LootConditions[type]?.(condition) ?? (() => true))(ctx)
}