@@ -11,7 +11,7 @@ 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+ const FORMATS = [ 'give-command' , 'loot-table' , 'item-modifier' , 'recipe-output' ] as const
1515type Format = typeof FORMATS [ number ]
1616
1717interface 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+
311355function 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+
372428function stringifyItemStack ( itemStack : ItemStack ) {
373429 let result = itemStack . id . toString ( )
374430 if ( itemStack . components . size > 0 ) {
0 commit comments