From fabdb1aed39a96a9953553415e5f4184798c5357 Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 29 Nov 2024 16:34:20 +0100 Subject: [PATCH] Add recipe output convert format --- src/app/pages/Convert.tsx | 59 ++++++++++++++++++++++++++++++++++++++- src/locales/en.json | 1 + vite.config.js | 2 +- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/app/pages/Convert.tsx b/src/app/pages/Convert.tsx index 50c3cf3c..09db7728 100644 --- a/src/app/pages/Convert.tsx +++ b/src/app/pages/Convert.tsx @@ -11,7 +11,8 @@ 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 +// 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] interface Props { @@ -154,6 +155,11 @@ const CONVERSIONS: Record 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) => { @@ -168,6 +174,12 @@ const CONVERSIONS: Record 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) => { @@ -182,6 +194,31 @@ const CONVERSIONS: Record 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) + }, }, } @@ -308,6 +345,14 @@ function createLootFunctions(item: ItemStack): Record[] { 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) ?? {}) ?? [] @@ -369,6 +414,18 @@ function getItemFromLootFunctions(functions: Record[], 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) { diff --git a/src/locales/en.json b/src/locales/en.json index 696a9226..fbe5b0e5 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -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!", diff --git a/vite.config.js b/vite.config.js index 69ebc043..48d0ecbe 100644 --- a/vite.config.js +++ b/vite.config.js @@ -7,7 +7,7 @@ import { viteStaticCopy } from 'vite-plugin-static-copy' const config = require('./src/config.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({ server: {