81 lines
2.5 KiB
TypeScript

import { encodeBase64 } from "@std/encoding/base64";
import { readDirFiles } from "../util/readDir.ts";
interface BlockItem {
name: string;
resourceLocation: string;
images: string[];
}
export const readBlocks = async (path: string) => {
const blocks: BlockItem[] =
(await readDirFiles(path + "/assets/minecraft/blockstates"))
.map((b) => ({
name: b.replace(".json", ""),
resourceLocation: "minecraft:" + b.replace(".json", ""),
images: [],
}));
const texPath = path + "/assets/minecraft/textures/block";
for await (const image of Deno.readDir(texPath)) {
for (const block of blocks) {
if (image.name.startsWith(block.name)) {
const data = await Deno.readFile(texPath + "/" + image.name);
block.images.push("image/png;base64," + encodeBase64(data));
}
}
}
return blocks;
};
export const readItems = async (path: string) => {
const items: BlockItem[] =
(await readDirFiles(path + "/assets/minecraft/models/item")).map((i) => ({
name: i.replace(".json", ""),
resourceLocation: "minecraft:" + i.replace(".json", ""),
images: [],
})).slice(0, 10);
for (const item of items) {
const data = await Deno.readFile(
path + "/assets/minecraft/models/item/" + item.name + ".json",
);
const json = JSON.parse(new TextDecoder().decode(data));
const texDir = path + "/assets/minecraft/textures/";
if (json.textures) {
const texLoc = json.textures.layer0;
if (texLoc) {
const data = await Deno.readFile(
texDir + texLoc.replace("minecraft:", "") + ".png",
);
item.images.push("image/png;base64," + encodeBase64(data));
}
} else if (json.parent) {
const parent = await Deno.readFile(
path + "/assets/minecraft/models/" +
json.parent.replace("minecraft:", "") + ".json",
);
const parentJson = JSON.parse(new TextDecoder().decode(parent));
if (parentJson.textures) {
let texLoc = parentJson.textures.all;
if (!texLoc) texLoc = parentJson.textures.side;
if (!texLoc) texLoc = parentJson.textures.top;
if (!texLoc) texLoc = parentJson.textures.bottom;
if (texLoc) {
const data = await Deno.readFile(
texDir + texLoc.replace("minecraft:", "") + ".png",
);
item.images.push("image/png;base64," + encodeBase64(data));
}
}
}
}
return items;
};
if (import.meta.main) {
const path = "./resources/1.21.1";
console.log(await readItems(path));
}