Add all textures and models from create
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
// 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. one java draws in 3D (a wrench, goggles) is drawn flat here, with a
|
||||
// texture of its model, until bworld items can have models
|
||||
import { join } from "@std/path";
|
||||
import { FORMAT_VERSION } from "$/common/mod_data.ts";
|
||||
import { made_by, model_source } from "./models.ts";
|
||||
import { merged_textures, 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)
|
||||
.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;
|
||||
if (made_by(location, read)?.startsWith("an item sprite")) {
|
||||
texture = resolve(textures.layer0);
|
||||
if (textures.layer1 !== undefined) {
|
||||
report.warnings.push({ location, warning: "only the first layer of sprites with several was kept" });
|
||||
}
|
||||
} else {
|
||||
texture = resolve(textures.particle) ?? Object.values(textures).map(resolve).find((t) => t !== undefined);
|
||||
report.warnings.push({ location, warning: "drawn in 3D in java, it's a flat texture of its model here" });
|
||||
}
|
||||
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) || !is_valid_id(texture_id)) {
|
||||
report.errors.push({ location, problems: [`its id ${id} or texture ${texture_id} isn't a valid id`] });
|
||||
continue;
|
||||
}
|
||||
if (!claim(id, location)) continue;
|
||||
const json = { format_version: FORMAT_VERSION, item: { id, texture: texture_id } };
|
||||
report.written.push({
|
||||
location,
|
||||
id,
|
||||
file: join(options.output, `${name}.json`),
|
||||
text: format_json(json) + "\n",
|
||||
});
|
||||
}
|
||||
write_report(report);
|
||||
return report;
|
||||
}
|
||||
Reference in New Issue
Block a user