168 lines
6.7 KiB
TypeScript
168 lines
6.7 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// java recipes (data/<namespace>/recipe/**.json, recipes/ before 1.21) to bworld's recipes/, keeping their folders.
|
|
// each type bworld or a ported mod has an equivalent for is converted, the rest are reported. a recipe naming an item or tag that
|
|
// doesn't exist here is left out, since bworld's recipes have to be complete
|
|
import { join } from "@std/path";
|
|
import { FORMAT_VERSION } from "@/common/mod_data.ts";
|
|
import { known_tag, tag_id } from "./tags.ts";
|
|
import {
|
|
DEFAULT_REMAPS,
|
|
empty_report,
|
|
format_json,
|
|
type ImportOptions,
|
|
type ImportReport,
|
|
input_files,
|
|
type Known,
|
|
known_in,
|
|
location_of,
|
|
location_string,
|
|
type Remaps,
|
|
skip_pattern,
|
|
skips,
|
|
to_id,
|
|
write_report,
|
|
} from "./resources.ts";
|
|
|
|
// a converted recipe, or why it can't be
|
|
type Converted = { recipe: Record<string, unknown> } | { skipped: string } | { error: string };
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
type Json = any;
|
|
|
|
interface Context {
|
|
remaps: Remaps;
|
|
known: Known | undefined;
|
|
}
|
|
|
|
const CONVERTERS: Record<string, (json: Json, context: Context) => Converted> = {
|
|
"minecraft:stonecutting": (json, context) => {
|
|
const input = choice(json.ingredient, context);
|
|
if ("skipped" in input || "error" in input) return input;
|
|
const output = stack(json.result, json.count, context);
|
|
if ("skipped" in output || "error" in output) return output;
|
|
return { recipe: { type: "bworld:stonecutting", input: input.choice, output: output.stack } };
|
|
},
|
|
"create:pressing": processing,
|
|
};
|
|
|
|
// create's processing recipes keep their shape, with bworld's item choices as ingredients, results as { id, count } and
|
|
// their chance when it isn't certain, and processing_time and heat_requirement under 6.0's names. like
|
|
// ProcessingRecipeParams' codec
|
|
function processing(json: Json, context: Context): Converted {
|
|
if (!Array.isArray(json.ingredients) || !Array.isArray(json.results)) {
|
|
return { error: "it needs lists of ingredients and results" };
|
|
}
|
|
const ingredients: (string | string[])[] = [];
|
|
for (const ingredient of json.ingredients) {
|
|
const converted = choice(ingredient, context);
|
|
if (!("choice" in converted)) return converted;
|
|
ingredients.push(converted.choice);
|
|
}
|
|
const results: Record<string, unknown>[] = [];
|
|
for (const result of json.results) {
|
|
const converted = stack(result, undefined, context);
|
|
if (!("stack" in converted)) return converted;
|
|
const chance = result.chance ?? 1;
|
|
results.push(chance < 1 ? { ...converted.stack, chance } : converted.stack);
|
|
}
|
|
// 0.5 called them processingTime and heatRequirement
|
|
const time = json.processing_time ?? json.processingTime;
|
|
const heat = json.heat_requirement ?? json.heatRequirement;
|
|
return {
|
|
recipe: {
|
|
type: json.type,
|
|
ingredients,
|
|
results,
|
|
...(time !== undefined && { processing_time: time }),
|
|
...(heat !== undefined && { heat_requirement: heat }),
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function import_recipes(input: string, options: ImportOptions = {}): Promise<ImportReport> {
|
|
if (options.output === undefined) throw new Error("--output is needed for recipes");
|
|
const context = { remaps: options.remaps ?? DEFAULT_REMAPS, known: await known_in(options.output) };
|
|
const report = empty_report();
|
|
if (context.known === undefined) {
|
|
report.warnings.push({
|
|
location: options.output,
|
|
warning: "--output isn't in a mod, so the items and tags recipes name aren't checked",
|
|
});
|
|
}
|
|
const files = input_files(input, ["recipe", "recipes"], ".json", options.namespace, options.output);
|
|
for (const { root, file } of files) {
|
|
const where = location_of(root, file, ".json");
|
|
const location = location_string(where);
|
|
if (skips(location, context.remaps, report)) continue;
|
|
const json = JSON.parse(Deno.readTextFileSync(file));
|
|
const conditions = Object.keys(json).find((key) => key.endsWith("conditions"));
|
|
const convert = CONVERTERS[json.type];
|
|
const converted: Converted = conditions
|
|
? { skipped: `it only loads under ${conditions}, like another mod being installed` }
|
|
: convert
|
|
? convert(json, context)
|
|
: { skipped: `the importer can't convert ${json.type} recipes yet` };
|
|
if ("skipped" in converted) report.skipped.push({ location, reason: converted.skipped });
|
|
else if ("error" in converted) report.errors.push({ location, problems: [converted.error] });
|
|
else {
|
|
report.written.push({
|
|
location,
|
|
id: location,
|
|
file: join(options.output, `${where.path}.json`),
|
|
text: format_json({ format_version: FORMAT_VERSION, recipe: converted.recipe }) + "\n",
|
|
});
|
|
}
|
|
}
|
|
write_report(report);
|
|
return report;
|
|
}
|
|
|
|
// an ingredient as bworld's item choice: { item } an id, { tag } a #tag, a list any of which fits
|
|
function choice(json: Json, context: Context): { choice: string | string[] } | { skipped: string } | {
|
|
error: string;
|
|
} {
|
|
const one = (entry: Json): string | { skipped: string } | { error: string } => {
|
|
if (typeof entry?.item === "string") {
|
|
const id = to_id(entry.item, context.remaps.ids);
|
|
return item_problem(entry.item, id, context) ?? id;
|
|
}
|
|
if (typeof entry?.tag === "string") {
|
|
const id = tag_id(entry.tag, context.remaps);
|
|
if (skip_pattern(entry.tag, context.remaps) !== undefined) {
|
|
return { skipped: `it takes #${entry.tag}, which the remaps skip` };
|
|
}
|
|
return known_tag(context.known, "items", id)
|
|
? `#${id}`
|
|
: { skipped: `it takes #${id}, which nobody defines` };
|
|
}
|
|
return { error: `an ingredient of ${JSON.stringify(entry)} isn't an item or a tag` };
|
|
};
|
|
const entries = (Array.isArray(json) ? json : [json]).map(one);
|
|
const problem = entries.find((entry) => typeof entry !== "string");
|
|
if (problem !== undefined) return problem as { skipped: string } | { error: string };
|
|
return { choice: entries.length === 1 ? entries[0] as string : entries as string[] };
|
|
}
|
|
|
|
// a result as { id, count }: { id, count } since 1.20.5, { item, count } before, and an id with the recipe's count
|
|
// before that
|
|
function stack(json: Json, count: unknown, context: Context): { stack: { id: string; count: number } } | {
|
|
skipped: string;
|
|
} | { error: string } {
|
|
const raw = typeof json === "string" ? json : json?.id ?? json?.item;
|
|
if (typeof raw !== "string") return { error: `its result ${JSON.stringify(json)} isn't an item` };
|
|
const id = to_id(raw, context.remaps.ids);
|
|
const problem = item_problem(raw, id, context);
|
|
if (problem) return problem;
|
|
return { stack: { id, count: (typeof json === "object" ? json.count : undefined) ?? count ?? 1 } };
|
|
}
|
|
|
|
function item_problem(raw: string, id: string, context: Context): { skipped: string } | undefined {
|
|
if (skip_pattern(raw, context.remaps) !== undefined) return { skipped: `it uses ${raw}, which the remaps skip` };
|
|
if (context.known && !context.known.ids.items.has(id)) {
|
|
return { skipped: `it uses ${id}, which doesn't exist here` };
|
|
}
|
|
return undefined;
|
|
}
|