@@ -11,7 +11,8 @@ import type { VersionId } from '../services/Versions.js'
1111import { checkVersion } from '../services/Versions.js'
1212import { 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
1516type Format = typeof FORMATS [ number ]
1617
1718interface 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+
311356function 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+
372429function stringifyItemStack ( itemStack : ItemStack ) {
373430 let result = itemStack . id . toString ( )
374431 if ( itemStack . components . size > 0 ) {
0 commit comments