Skip to content

Commit 1203783

Browse files
committed
Add recipe output convert format
1 parent f694b56 commit 1203783

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/app/pages/Convert.tsx

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { VersionId } from '../services/Versions.js'
1111
import { checkVersion } from '../services/Versions.js'
1212
import { jsonToNbt } from '../Utils.js'
1313

14-
const FORMATS = ['give-command', 'loot-table', 'item-modifier'] as const
14+
const FORMATS = ['give-command', 'loot-table', 'item-modifier', 'recipe-output'] as const
1515
type Format = typeof FORMATS[number]
1616

1717
interface Props {
@@ -154,6 +154,11 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
154154
const itemModifier = createItemModifier(itemStack)
155155
return JSON.stringify(itemModifier, null, 2)
156156
},
157+
'recipe-output': (input) => {
158+
const itemStack = parseGiveCommand(new StringReader(input))
159+
const recipe = createRecipe(itemStack)
160+
return JSON.stringify(recipe, null, 2)
161+
},
157162
},
158163
'loot-table': {
159164
'give-command': (input) => {
@@ -168,6 +173,12 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
168173
const itemModifier = createItemModifier(itemStack)
169174
return JSON.stringify(itemModifier, null, 2)
170175
},
176+
'recipe-output': (input) => {
177+
const lootTable = JSON.parse(input)
178+
const itemStack = getItemFromLootTable(lootTable)
179+
const recipe = createRecipe(itemStack)
180+
return JSON.stringify(recipe, null, 2)
181+
},
171182
},
172183
'item-modifier': {
173184
'give-command': (input) => {
@@ -182,6 +193,31 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
182193
const lootTable = createLootTable(itemStack)
183194
return JSON.stringify(lootTable, null, 2)
184195
},
196+
'recipe-output': (input) => {
197+
const itemModifier = JSON.parse(input)
198+
const itemStack = getItemFromItemModifier(itemModifier)
199+
const recipe = createRecipe(itemStack)
200+
return JSON.stringify(recipe, null, 2)
201+
},
202+
},
203+
'recipe-output': {
204+
'give-command': (input) => {
205+
const recipe = JSON.parse(input)
206+
const itemStack = getRecipeOutput(recipe)
207+
return `give @s ${stringifyItemStack(itemStack)}`
208+
},
209+
'loot-table': (input) => {
210+
const recipe = JSON.parse(input)
211+
const itemStack = getRecipeOutput(recipe)
212+
const lootTable = createLootTable(itemStack)
213+
return JSON.stringify(lootTable, null, 2)
214+
},
215+
'item-modifier': (input) => {
216+
const recipe = JSON.parse(input)
217+
const itemStack = getRecipeOutput(recipe)
218+
const itemModifier = createItemModifier(itemStack)
219+
return JSON.stringify(itemModifier, null, 2)
220+
},
185221
},
186222
}
187223

@@ -308,6 +344,14 @@ function createLootFunctions(item: ItemStack): Record<string, unknown>[] {
308344
return functions
309345
}
310346

347+
function createRecipe(item: ItemStack) {
348+
return {
349+
type: 'minecraft:crafting_shapeless',
350+
ingredients: [],
351+
result: item.toNbt().toSimplifiedJson(),
352+
}
353+
}
354+
311355
function getItemFromItemModifier(data: unknown): ItemStack {
312356
const functions = Array.isArray(data)
313357
? Json.readArray(data, e => Json.readObject(e) ?? {}) ?? []
@@ -369,6 +413,18 @@ function getItemFromLootFunctions(functions: Record<string, unknown>[], initialI
369413
return new ItemStack(Identifier.parse(item ?? 'air'), count, components)
370414
}
371415

416+
function getRecipeOutput(data: unknown) {
417+
const root = Json.readObject(data) ?? {}
418+
const result = Json.readObject(root.result) ?? {}
419+
const id = Json.readString(result.id) ?? 'air'
420+
const count = Json.readInt(result.count) ?? 1
421+
const components = new Map()
422+
for (const [key, value] of Object.entries(Json.readObject(result.components) ?? {})) {
423+
components.set(key, jsonToNbt(value))
424+
}
425+
return new ItemStack(Identifier.parse(id), count, components)
426+
}
427+
372428
function stringifyItemStack(itemStack: ItemStack) {
373429
let result = itemStack.id.toString()
374430
if (itemStack.components.size > 0) {

src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"convert.format.give-command": "/give",
2727
"convert.format.loot-table": "Loot Table",
2828
"convert.format.item-modifier": "Item Modifier",
29+
"convert.format.recipe-output": "Recipe Output",
2930
"convert.select": "-- select --",
3031
"convert.swap": "Swap",
3132
"copied": "Copied!",

0 commit comments

Comments
 (0)