Skip to content

Commit 94a8210

Browse files
committed
Fix #642 more validation in loot table preview
1 parent dc72614 commit 94a8210

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/app/components/previews/LootTable.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ function composeFunctions(functions: any[]): LootFunction {
274274
for (const fn of functions) {
275275
if (Array.isArray(fn)) {
276276
composeFunctions(fn)
277-
} else if (composeConditions(fn.conditions ?? [])(ctx)) {
277+
} else if (isObject(fn) && composeConditions(fn.conditions ?? [])(ctx)) {
278278
const type = fn.function?.replace(/^minecraft:/, '');
279279
(LootFunctions[type]?.(fn) ?? (i => i))(item, ctx)
280280
}
@@ -349,7 +349,7 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
349349
set_attributes: ({ modifiers, replace }) => (item, ctx) => {
350350
if (!Array.isArray(modifiers)) return
351351
const newModifiers = modifiers.map<AttributeModifier>(m => {
352-
if (typeof m !== 'object' || m === null) m = {}
352+
if (!isObject(m)) m = {}
353353
return {
354354
id: Identifier.parse(typeof m.id === 'string' ? m.id : ''),
355355
type: Identifier.parse(typeof m.attribute === 'string' ? m.attribute : ''),
@@ -386,7 +386,7 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
386386
item.set('written_book_content', newContent)
387387
},
388388
set_components: ({ components }) => (item) => {
389-
if (typeof components !== 'object' || components === null) {
389+
if (!isObject(components)) {
390390
return
391391
}
392392
for (const [key, value] of Object.entries(components)) {
@@ -432,7 +432,7 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
432432
}
433433
},
434434
set_enchantments: ({ enchantments, add }) => (item, ctx) => {
435-
if (typeof enchantments !== 'object' || enchantments === null) {
435+
if (!isObject(enchantments)) {
436436
return
437437
}
438438
if (item.is('book')) {
@@ -490,7 +490,9 @@ const LootFunctions: Record<string, (params: any) => LootFunction> = {
490490
}
491491
},
492492
toggle_tooltips: ({ toggles }) => (item) => {
493-
if (typeof toggles !== 'object' || toggles === null) return
493+
if (!isObject(toggles)) {
494+
return
495+
}
494496
Object.entries(toggles).forEach(([key, value]) => {
495497
if (typeof value !== 'boolean') return
496498
const tag = item.get(key, tag => tag)

src/app/components/previews/LootTable1204.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function composeFunctions(functions: any[]): LootFunction {
271271
for (const fn of functions) {
272272
if (Array.isArray(fn)) {
273273
composeFunctions(fn)
274-
} else if (composeConditions(fn.conditions ?? [])(ctx)) {
274+
} else if (isObject(fn) && composeConditions(fn.conditions ?? [])(ctx)) {
275275
const type = fn.function?.replace(/^minecraft:/, '');
276276
(LootFunctions[type]?.(fn) ?? (i => i))(item, ctx)
277277
}
@@ -403,6 +403,9 @@ function testCondition(condition: any, ctx: LootContext): boolean {
403403
if (Array.isArray(condition)) {
404404
return composeConditions(condition)(ctx)
405405
}
406+
if (!isObject(condition) || typeof condition.condition !== 'string') {
407+
return false
408+
}
406409
const type = condition.condition?.replace(/^minecraft:/, '')
407410
return (LootConditions[type]?.(condition) ?? (() => true))(ctx)
408411
}

0 commit comments

Comments
 (0)