Add recipe output convert format

This commit is contained in:
Misode
2024-11-29 16:34:20 +01:00
parent f694b56244
commit fabdb1aed3
3 changed files with 60 additions and 2 deletions

View File

@@ -11,7 +11,8 @@ import type { VersionId } from '../services/Versions.js'
import { checkVersion } from '../services/Versions.js' import { checkVersion } from '../services/Versions.js'
import { jsonToNbt } from '../Utils.js' import { jsonToNbt } from '../Utils.js'
const FORMATS = ['give-command', 'loot-table', 'item-modifier'] as const // When adding new formats, also update the list in vite.config.js !!!
const FORMATS = ['give-command', 'loot-table', 'item-modifier', 'recipe-output'] as const
type Format = typeof FORMATS[number] type Format = typeof FORMATS[number]
interface Props { interface Props {
@@ -154,6 +155,11 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
const itemModifier = createItemModifier(itemStack) const itemModifier = createItemModifier(itemStack)
return JSON.stringify(itemModifier, null, 2) 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': { 'loot-table': {
'give-command': (input) => { 'give-command': (input) => {
@@ -168,6 +174,12 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
const itemModifier = createItemModifier(itemStack) const itemModifier = createItemModifier(itemStack)
return JSON.stringify(itemModifier, null, 2) 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': { 'item-modifier': {
'give-command': (input) => { 'give-command': (input) => {
@@ -182,6 +194,31 @@ const CONVERSIONS: Record<Format, Partial<Record<Format, (input: string) => stri
const lootTable = createLootTable(itemStack) const lootTable = createLootTable(itemStack)
return JSON.stringify(lootTable, null, 2) 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)
},
}, },
} }
@@ -308,6 +345,14 @@ function createLootFunctions(item: ItemStack): Record<string, unknown>[] {
return functions return functions
} }
function createRecipe(item: ItemStack) {
return {
type: 'minecraft:crafting_shapeless',
ingredients: [],
result: item.toNbt().toSimplifiedJson(),
}
}
function getItemFromItemModifier(data: unknown): ItemStack { function getItemFromItemModifier(data: unknown): ItemStack {
const functions = Array.isArray(data) const functions = Array.isArray(data)
? Json.readArray(data, e => Json.readObject(e) ?? {}) ?? [] ? Json.readArray(data, e => Json.readObject(e) ?? {}) ?? []
@@ -369,6 +414,18 @@ function getItemFromLootFunctions(functions: Record<string, unknown>[], initialI
return new ItemStack(Identifier.parse(item ?? 'air'), count, components) 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) { function stringifyItemStack(itemStack: ItemStack) {
let result = itemStack.id.toString() let result = itemStack.id.toString()
if (itemStack.components.size > 0) { if (itemStack.components.size > 0) {

View File

@@ -26,6 +26,7 @@
"convert.format.give-command": "/give", "convert.format.give-command": "/give",
"convert.format.loot-table": "Loot Table", "convert.format.loot-table": "Loot Table",
"convert.format.item-modifier": "Item Modifier", "convert.format.item-modifier": "Item Modifier",
"convert.format.recipe-output": "Recipe Output",
"convert.select": "-- select --", "convert.select": "-- select --",
"convert.swap": "Swap", "convert.swap": "Swap",
"copied": "Copied!", "copied": "Copied!",

View File

@@ -7,7 +7,7 @@ import { viteStaticCopy } from 'vite-plugin-static-copy'
const config = require('./src/config.json') const config = require('./src/config.json')
const English = require('./src/locales/en.json') const English = require('./src/locales/en.json')
const convertFormats = ['give-command', 'loot-table', 'item-modifier'] const convertFormats = ['give-command', 'loot-table', 'item-modifier', 'recipe-output']
export default defineConfig({ export default defineConfig({
server: { server: {