|
| 1 | +import { Identifier, ItemStack } from 'deepslate/core' |
| 2 | +import type { VersionId } from '../../services/Versions.js' |
| 3 | +import { checkVersion } from '../../services/Versions.js' |
| 4 | +import { jsonToNbt } from '../../Utils.js' |
| 5 | + |
| 6 | +export function placeItems(version: VersionId, recipe: any, animation: number, itemTags: Map<string, any>): Map<string, ItemStack> { |
| 7 | + const items = new Map<string, ItemStack>() |
| 8 | + const type: string = recipe.type?.replace(/^minecraft:/, '') |
| 9 | + if (!type || type.startsWith('crafting_special') || type === 'crafting_decorated_pot') { |
| 10 | + return items |
| 11 | + } |
| 12 | + |
| 13 | + if (type === 'crafting_shapeless') { |
| 14 | + const ingredients: any[] = Array.isArray(recipe.ingredients) ? recipe.ingredients : [] |
| 15 | + ingredients.forEach((ingredient, i) => { |
| 16 | + const choices = allIngredientChoices(version, ingredient, itemTags) |
| 17 | + if (i >= 0 && i < 9 && choices.length > 0) { |
| 18 | + const choice = choices[(3 * i + animation) % choices.length] |
| 19 | + items.set(`crafting.${i}`, choice) |
| 20 | + } |
| 21 | + }) |
| 22 | + } else if (type === 'crafting_shaped') { |
| 23 | + const keys = new Map<string, ItemStack>() |
| 24 | + for (const [key, ingredient] of Object.entries(recipe.key ?? {})) { |
| 25 | + const choices = allIngredientChoices(version, ingredient, itemTags) |
| 26 | + if (choices.length > 0) { |
| 27 | + const choice = choices[animation % choices.length] |
| 28 | + keys.set(key, choice) |
| 29 | + } |
| 30 | + } |
| 31 | + const pattern = Array.isArray(recipe.pattern) ? recipe.pattern : [] |
| 32 | + for (let row = 0; row < Math.min(3, pattern.length); row += 1) { |
| 33 | + for (let col = 0; col < Math.min(3, pattern[row].length); col += 1) { |
| 34 | + const key = pattern[row].split('')[col] |
| 35 | + const choice = key === ' ' ? undefined : keys.get(key) |
| 36 | + if (choice) { |
| 37 | + items.set(`crafting.${row * 3 + col}`, choice) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } else if (type === 'crafting_transmute') { |
| 42 | + const inputs = allIngredientChoices(version, recipe.input, itemTags) |
| 43 | + if (inputs.length > 0) { |
| 44 | + const choice = inputs[animation % inputs.length] |
| 45 | + items.set('crafting.0', choice) |
| 46 | + } |
| 47 | + const materials = allIngredientChoices(version, recipe.material, itemTags) |
| 48 | + if (materials.length > 0) { |
| 49 | + const choice = materials[animation % materials.length] |
| 50 | + items.set('crafting.1', choice) |
| 51 | + } |
| 52 | + } else if (type === 'smelting' || type === 'smoking' || type === 'blasting' || type === 'campfire_cooking') { |
| 53 | + const choices = allIngredientChoices(version, recipe.ingredient, itemTags) |
| 54 | + if (choices.length > 0) { |
| 55 | + const choice = choices[animation % choices.length] |
| 56 | + items.set('smelting.ingredient', choice) |
| 57 | + } |
| 58 | + } else if (type === 'stonecutting') { |
| 59 | + const choices = allIngredientChoices(version, recipe.ingredient, itemTags) |
| 60 | + if (choices.length > 0) { |
| 61 | + const choice = choices[animation % choices.length] |
| 62 | + items.set('stonecutting.ingredient', choice) |
| 63 | + } |
| 64 | + } else if (type === 'smithing_transform' || type === 'smithing_trim') { |
| 65 | + for (const ingredient of ['template', 'base', 'addition'] as const) { |
| 66 | + const choices = allIngredientChoices(version, recipe[ingredient], itemTags) |
| 67 | + if (choices.length > 0) { |
| 68 | + const choice = choices[animation % choices.length] |
| 69 | + items.set(`smithing.${ingredient}`, choice) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + let resultSlot = 'crafting.result' |
| 75 | + if (type === 'smelting' || type === 'smoking' || type === 'blasting' || type === 'campfire_cooking') { |
| 76 | + resultSlot = 'smelting.result' |
| 77 | + } else if (type === 'stonecutting') { |
| 78 | + resultSlot = 'stonecutting.result' |
| 79 | + } else if (type === 'smithing_transform' || type === 'smithing_trim') { |
| 80 | + resultSlot = 'smithing.result' |
| 81 | + } |
| 82 | + const result = recipe.result |
| 83 | + if (type === 'smithing_trim') { |
| 84 | + const base = items.get('smithing.base') |
| 85 | + if (base) { |
| 86 | + items.set(resultSlot, base) |
| 87 | + } |
| 88 | + } else if (typeof result === 'string') { |
| 89 | + items.set(resultSlot, new ItemStack(Identifier.parse(result), 1)) |
| 90 | + } else if (typeof result === 'object' && result !== null) { |
| 91 | + const id = typeof result.id === 'string' ? result.id |
| 92 | + : typeof result.item === 'string' ? result.item |
| 93 | + : 'minecraft:air' |
| 94 | + if (id !== 'minecraft:air') { |
| 95 | + const count = typeof result.count === 'number' ? result.count : 1 |
| 96 | + const components = new Map(Object.entries(result.components ?? {}) |
| 97 | + .map(([k, v]) => [k, jsonToNbt(v)])) |
| 98 | + items.set(resultSlot, new ItemStack(Identifier.parse(id), count, components)) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return items |
| 103 | +} |
| 104 | + |
| 105 | +function allIngredientChoices(version: VersionId, ingredient: any, itemTags: Map<string, any>): ItemStack[] { |
| 106 | + if (Array.isArray(ingredient)) { |
| 107 | + return ingredient.flatMap(i => allIngredientChoices(version, i, itemTags)) |
| 108 | + } |
| 109 | + |
| 110 | + if (checkVersion(version, '1.21.2')) { |
| 111 | + if (ingredient !== null) { |
| 112 | + if (typeof ingredient === 'string') { |
| 113 | + if (ingredient.startsWith('#')) { |
| 114 | + return parseTag(version, ingredient.slice(1), itemTags) |
| 115 | + } |
| 116 | + return [new ItemStack(Identifier.parse(ingredient), 1)] |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return [new ItemStack(Identifier.create('stone'), 1)] |
| 121 | + } else { |
| 122 | + if (typeof ingredient === 'object' && ingredient !== null) { |
| 123 | + if (typeof ingredient.item === 'string') { |
| 124 | + return [new ItemStack(Identifier.parse(ingredient.item), 1)] |
| 125 | + } else if (typeof ingredient.tag === 'string') { |
| 126 | + return parseTag(version, ingredient.tag, itemTags) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return [] |
| 132 | +} |
| 133 | + |
| 134 | +function parseTag(version: VersionId, tagId: any, itemTags: Map<string, any>): ItemStack[] { |
| 135 | + const tag: any = itemTags.get(tagId.replace(/^minecraft:/, '')) |
| 136 | + if (typeof tag === 'object' && tag !== null && Array.isArray(tag.values)) { |
| 137 | + return tag.values.flatMap((value: any) => { |
| 138 | + if (typeof value !== 'string') return [] |
| 139 | + if (value.startsWith('#')) return parseTag(version, value.slice(1), itemTags) |
| 140 | + return [new ItemStack(Identifier.parse(value), 1)] |
| 141 | + }) |
| 142 | + } |
| 143 | + return [] |
| 144 | +} |
0 commit comments