Skip to content

Commit fabdb1a

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

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/app/pages/Convert.tsx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ 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+
// When adding new formats, also update the list in vite.config.js !!!
15+
const FORMATS = ['give-command', 'loot-table', 'item-modifier', 'recipe-output'] as const
1516
type Format = typeof FORMATS[number]
1617

1718
interface Props {
@@ -154,6 +155,11 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
154155
const itemModifier = createItemModifier(itemStack)
155156
return JSON.stringify(itemModifier, null, 2)
156157
},
158+
'recipe-output': (input) => {
159+
const itemStack = parseGiveCommand(new StringReader(input))
160+
const recipe = createRecipe(itemStack)
161+
return JSON.stringify(recipe, null, 2)
162+
},
157163
},
158164
'loot-table': {
159165
'give-command': (input) => {
@@ -168,6 +174,12 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
168174
const itemModifier = createItemModifier(itemStack)
169175
return JSON.stringify(itemModifier, null, 2)
170176
},
177+
'recipe-output': (input) => {
178+
const lootTable = JSON.parse(input)
179+
const itemStack = getItemFromLootTable(lootTable)
180+
const recipe = createRecipe(itemStack)
181+
return JSON.stringify(recipe, null, 2)
182+
},
171183
},
172184
'item-modifier': {
173185
'give-command': (input) => {
@@ -182,6 +194,31 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
182194
const lootTable = createLootTable(itemStack)
183195
return JSON.stringify(lootTable, null, 2)
184196
},
197+
'recipe-output': (input) => {
198+
const itemModifier = JSON.parse(input)
199+
const itemStack = getItemFromItemModifier(itemModifier)
200+
const recipe = createRecipe(itemStack)
201+
return JSON.stringify(recipe, null, 2)
202+
},
203+
},
204+
'recipe-output': {
205+
'give-command': (input) => {
206+
const recipe = JSON.parse(input)
207+
const itemStack = getRecipeOutput(recipe)
208+
return `give @s ${stringifyItemStack(itemStack)}`
209+
},
210+
'loot-table': (input) => {
211+
const recipe = JSON.parse(input)
212+
const itemStack = getRecipeOutput(recipe)
213+
const lootTable = createLootTable(itemStack)
214+
return JSON.stringify(lootTable, null, 2)
215+
},
216+
'item-modifier': (input) => {
217+
const recipe = JSON.parse(input)
218+
const itemStack = getRecipeOutput(recipe)
219+
const itemModifier = createItemModifier(itemStack)
220+
return JSON.stringify(itemModifier, null, 2)
221+
},
185222
},
186223
}
187224

@@ -308,6 +345,14 @@ function createLootFunctions(item: ItemStack): Record<string, unknown>[] {
308345
return functions
309346
}
310347

348+
function createRecipe(item: ItemStack) {
349+
return {
350+
type: 'minecraft:crafting_shapeless',
351+
ingredients: [],
352+
result: item.toNbt().toSimplifiedJson(),
353+
}
354+
}
355+
311356
function getItemFromItemModifier(data: unknown): ItemStack {
312357
const functions = Array.isArray(data)
313358
? Json.readArray(data, e => Json.readObject(e) ?? {}) ?? []
@@ -369,6 +414,18 @@ function getItemFromLootFunctions(functions: Record<string, unknown>[], initialI
369414
return new ItemStack(Identifier.parse(item ?? 'air'), count, components)
370415
}
371416

417+
function getRecipeOutput(data: unknown) {
418+
const root = Json.readObject(data) ?? {}
419+
const result = Json.readObject(root.result) ?? {}
420+
const id = Json.readString(result.id) ?? 'air'
421+
const count = Json.readInt(result.count) ?? 1
422+
const components = new Map()
423+
for (const [key, value] of Object.entries(Json.readObject(result.components) ?? {})) {
424+
components.set(key, jsonToNbt(value))
425+
}
426+
return new ItemStack(Identifier.parse(id), count, components)
427+
}
428+
372429
function stringifyItemStack(itemStack: ItemStack) {
373430
let result = itemStack.id.toString()
374431
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!",

vite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { viteStaticCopy } from 'vite-plugin-static-copy'
77
const config = require('./src/config.json')
88
const English = require('./src/locales/en.json')
99

10-
const convertFormats = ['give-command', 'loot-table', 'item-modifier']
10+
const convertFormats = ['give-command', 'loot-table', 'item-modifier', 'recipe-output']
1111

1212
export default defineConfig({
1313
server: {

0 commit comments

Comments
 (0)