103 lines
4.3 KiB
TypeScript
103 lines
4.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// java item models (assets/<namespace>/models/item/*.json) to bworld's items/, for the items that aren't a block's: a
|
|
// block's item comes with the block (see blocks.ts). java says an item is a flat sprite with item/generated and its
|
|
// layer0 texture, which is what a bworld item is, and its layer1 and on are its layers. one java draws in 3D (a wrench)
|
|
// is drawn with its model, which the model kind writes, and gets its model's particle texture as its texture
|
|
import { join } from "@std/path";
|
|
import { FORMAT_VERSION } from "$/common/mod_data.ts";
|
|
import { made_by, model_source } from "./models.ts";
|
|
import { merged_textures, model_missing, models_near } from "./blocks.ts";
|
|
import {
|
|
DEFAULT_REMAPS,
|
|
empty_report,
|
|
find_roots,
|
|
format_json,
|
|
id_claims,
|
|
type ImportOptions,
|
|
type ImportReport,
|
|
input_files,
|
|
is_file,
|
|
is_valid_id,
|
|
location_of,
|
|
location_string,
|
|
skips,
|
|
to_id,
|
|
write_report,
|
|
} from "./resources.ts";
|
|
|
|
export function import_items(input: string, options: ImportOptions = {}): ImportReport {
|
|
if (options.output === undefined) throw new Error("--output is needed for items");
|
|
const remaps = options.remaps ?? DEFAULT_REMAPS;
|
|
// an item is a model directly in models/item, the folders under it are parts of models
|
|
const files = input_files(input, "models", ".json", options.namespace, options.output)
|
|
.filter(({ root, file }) => /^item\/[^/]+$/.test(location_of(root, file, ".json").path));
|
|
const roots = [...new Set(files.map(({ root }) => root))];
|
|
const read = model_source(models_near(input, roots, options));
|
|
// blockstates beside the models, so a block's item is left to the block
|
|
const blockstates = [
|
|
...(is_file(input) ? [] : find_roots(input, "blockstates", options.namespace)),
|
|
...roots.map((root) => ({ namespace: root.namespace, dir: root.dir.replace(/models$/, "blockstates") })),
|
|
];
|
|
const report = empty_report();
|
|
const claim = id_claims(report);
|
|
for (const { root, file } of files) {
|
|
const where = location_of(root, file, ".json");
|
|
const location = location_string(where);
|
|
if (skips(location, remaps, report)) continue;
|
|
const name = where.path.slice("item/".length);
|
|
if (blockstates.some((dir) => dir.namespace === where.namespace && is_file(`${dir.dir}/${name}.json`))) {
|
|
continue;
|
|
}
|
|
|
|
const id = to_id(`${where.namespace}:${name}`);
|
|
const textures = merged_textures(location, read);
|
|
const resolve = (reference: string | undefined) => {
|
|
for (let depth = 0; depth < 8 && reference?.startsWith("#"); depth++) {
|
|
reference = textures[reference.slice(1)];
|
|
}
|
|
return reference?.startsWith("#") ? undefined : reference;
|
|
};
|
|
let texture: string | undefined;
|
|
let model: string | undefined;
|
|
const layers: { texture: string }[] = [];
|
|
if (made_by(location, read)?.startsWith("an item sprite")) {
|
|
texture = resolve(textures.layer0);
|
|
for (let i = 1; textures[`layer${i}`] !== undefined; i++) {
|
|
const layer = resolve(textures[`layer${i}`]);
|
|
if (layer !== undefined) layers.push({ texture: to_id(layer, remaps.textures) });
|
|
}
|
|
} else {
|
|
texture = resolve(textures.particle) ?? Object.values(textures).map(resolve).find((t) => t !== undefined);
|
|
model = to_id(location, remaps.models);
|
|
const missing = model_missing(location, model, read, remaps);
|
|
if (missing) {
|
|
report.warnings.push({ location, warning: `its model ${missing}, so it's a flat texture of it here` });
|
|
model = undefined;
|
|
}
|
|
}
|
|
if (texture === undefined) {
|
|
// like item/pane, a template other item models use as their parent
|
|
report.skipped.push({ location, reason: "it has no texture, so it's only a parent of other item models" });
|
|
continue;
|
|
}
|
|
const texture_id = to_id(texture, remaps.textures);
|
|
if (!is_valid_id(id) || ![texture_id, ...layers.map((layer) => layer.texture)].every(is_valid_id)) {
|
|
report.errors.push({ location, problems: [`its id ${id} or one of its textures isn't a valid id`] });
|
|
continue;
|
|
}
|
|
if (!claim(id, location)) continue;
|
|
const item = { id, texture: texture_id, ...(model ? { model } : {}), ...(layers.length ? { layers } : {}) };
|
|
const json = { format_version: FORMAT_VERSION, item };
|
|
report.written.push({
|
|
location,
|
|
id,
|
|
file: join(options.output, `${name}.json`),
|
|
text: format_json(json) + "\n",
|
|
});
|
|
}
|
|
write_report(report);
|
|
return report;
|
|
}
|