Skip to content

Commit

Permalink
Add recipe output convert format
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Nov 29, 2024
1 parent f694b56 commit 1203783
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/app/pages/Convert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { VersionId } from '../services/Versions.js'
import { checkVersion } from '../services/Versions.js'
import { jsonToNbt } from '../Utils.js'

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

interface Props {
Expand Down Expand Up @@ -154,6 +154,11 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
const itemModifier = createItemModifier(itemStack)
return JSON.stringify(itemModifier, null, 2)
},
'recipe-output': (input) => {
const itemStack = parseGiveCommand(new StringReader(input))
const recipe = createRecipe(itemStack)
return JSON.stringify(recipe, null, 2)
},
},
'loot-table': {
'give-command': (input) => {
Expand All @@ -168,6 +173,12 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
const itemModifier = createItemModifier(itemStack)
return JSON.stringify(itemModifier, null, 2)
},
'recipe-output': (input) => {
const lootTable = JSON.parse(input)
const itemStack = getItemFromLootTable(lootTable)
const recipe = createRecipe(itemStack)
return JSON.stringify(recipe, null, 2)
},
},
'item-modifier': {
'give-command': (input) => {
Expand All @@ -182,6 +193,31 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
const lootTable = createLootTable(itemStack)
return JSON.stringify(lootTable, null, 2)
},
'recipe-output': (input) => {
const itemModifier = JSON.parse(input)
const itemStack = getItemFromItemModifier(itemModifier)
const recipe = createRecipe(itemStack)
return JSON.stringify(recipe, null, 2)
},
},
'recipe-output': {
'give-command': (input) => {
const recipe = JSON.parse(input)
const itemStack = getRecipeOutput(recipe)
return `give @s ${stringifyItemStack(itemStack)}`
},
'loot-table': (input) => {
const recipe = JSON.parse(input)
const itemStack = getRecipeOutput(recipe)
const lootTable = createLootTable(itemStack)
return JSON.stringify(lootTable, null, 2)
},
'item-modifier': (input) => {
const recipe = JSON.parse(input)
const itemStack = getRecipeOutput(recipe)
const itemModifier = createItemModifier(itemStack)
return JSON.stringify(itemModifier, null, 2)
},
},
}

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

function createRecipe(item: ItemStack) {
return {
type: 'minecraft:crafting_shapeless',
ingredients: [],
result: item.toNbt().toSimplifiedJson(),
}
}

function getItemFromItemModifier(data: unknown): ItemStack {
const functions = Array.isArray(data)
? Json.readArray(data, e => Json.readObject(e) ?? {}) ?? []
Expand Down Expand Up @@ -369,6 +413,18 @@ function getItemFromLootFunctions(functions: Record<string, unknown>[], initialI
return new ItemStack(Identifier.parse(item ?? 'air'), count, components)
}

function getRecipeOutput(data: unknown) {
const root = Json.readObject(data) ?? {}
const result = Json.readObject(root.result) ?? {}
const id = Json.readString(result.id) ?? 'air'
const count = Json.readInt(result.count) ?? 1
const components = new Map()
for (const [key, value] of Object.entries(Json.readObject(result.components) ?? {})) {
components.set(key, jsonToNbt(value))
}
return new ItemStack(Identifier.parse(id), count, components)
}

function stringifyItemStack(itemStack: ItemStack) {
let result = itemStack.id.toString()
if (itemStack.components.size > 0) {
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"convert.format.give-command": "/give",
"convert.format.loot-table": "Loot Table",
"convert.format.item-modifier": "Item Modifier",
"convert.format.recipe-output": "Recipe Output",
"convert.select": "-- select --",
"convert.swap": "Swap",
"copied": "Copied!",
Expand Down

0 comments on commit 1203783

Please sign in to comment.