This commit is contained in:
2026-10-03 16:28:09 -03:00
parent 24fe7cdede
commit 91655c28c5
53 changed files with 1752 additions and 114 deletions
+59 -6
View File
@@ -11,7 +11,7 @@ import {
type ToolProperties,
type WornProperties,
} from "./everything_registry.ts";
import { type BlockVariant, MODEL_FACES, type ModelFace } from "./block_models.ts";
import { type BlockPart, type BlockVariant, MODEL_FACES, type ModelFace } from "./block_models.ts";
import { expand_items, list_matches } from "./tags.ts";
export const FORMAT_VERSION = 1;
@@ -56,6 +56,9 @@ export interface BlockJson {
alpha?: number;
collision?: boolean;
shape?: number[][];
collision_shape?: number[][];
parts?: BlockPart[];
item_model?: string;
mining?: { toughness: number; tool?: string; requires_tool?: boolean; tier?: number };
drops?: string;
item?: boolean;
@@ -269,6 +272,9 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
if (block.alpha !== undefined) json.alpha = block.alpha;
if (!block.has_collision) json.collision = false;
if (block.shape !== undefined) json.shape = block.shape;
if (block.collision_shape !== undefined) json.collision_shape = block.collision_shape;
if (block.parts !== undefined) json.parts = block.parts;
if (block.item_model !== undefined) json.item_model = block.item_model;
if (block.toughness !== undefined) {
json.mining = { toughness: block.toughness };
if (block.tool_to_break !== undefined) json.mining.tool = block.tool_to_break;
@@ -297,6 +303,9 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
if (json.model !== undefined) block.model = json.model;
if (json.variants !== undefined) block.variants = json.variants;
if (json.shape !== undefined) block.shape = json.shape;
if (json.collision_shape !== undefined) block.collision_shape = json.collision_shape;
if (json.parts !== undefined) block.parts = json.parts;
if (json.item_model !== undefined) block.item_model = json.item_model;
const render_layer = json.render_layer ?? (json.transparent ? "translucent" : undefined);
if (render_layer && render_layer !== "solid") block.render_layer = render_layer;
if (json.cull_same !== undefined) block.cull_same = json.cull_same;
@@ -452,7 +461,7 @@ export function validate_block(json: unknown): Problems {
if (!is_object(json)) return ["block must be an object"];
const problems = validate_id(json.id, "id");
if (json.model !== undefined) problems.push(...validate_id(json.model, "model"));
const is_cube = json.model === undefined || json.model === "engine:cube";
const is_cube = json.parts === undefined && (json.model === undefined || json.model === "engine:cube");
const textures = json.textures;
if (is_cube) {
const texture_ok = typeof textures === "string" ||
@@ -488,10 +497,53 @@ export function validate_block(json: unknown): Problems {
problems.push(`${where}.y must be 0, 90, 180 or 270`);
}
if (variant.shape !== undefined) problems.push(...validate_shape(variant.shape, `${where}.shape`));
if (variant.collision_shape !== undefined) {
problems.push(...validate_shape(variant.collision_shape, `${where}.collision_shape`, 24));
}
}
}
}
if (json.shape !== undefined) problems.push(...validate_shape(json.shape, "shape"));
if (json.collision_shape !== undefined) {
problems.push(...validate_shape(json.collision_shape, "collision_shape", 24));
}
if (json.item_model !== undefined) problems.push(...validate_id(json.item_model, "item_model"));
if (json.parts !== undefined) {
if (!Array.isArray(json.parts) || json.parts.length === 0) {
problems.push("parts must be a list of { when, model, y }");
} else {
const names = new Set(Array.isArray(json.states) ? json.states.map((s) => is_object(s) ? s.name : "") : []);
json.parts.forEach((part, i) => {
const where = `parts[${i}]`;
if (!is_object(part)) {
problems.push(`${where} must be { when, model, y }`);
return;
}
problems.push(...validate_id(part.model, `${where}.model`));
if (part.when !== undefined) {
const parts = typeof part.when === "string" ? part.when.split(",") : [undefined];
for (const condition of part.when === "" ? [] : parts) {
const [name, value] = (condition ?? "").split("=").map((p) => p.trim());
if (!names.has(name) || !/^\d+$/.test(value ?? "")) {
problems.push(
`${where}.when: "${condition}" must be a state of this block and a number, like north=1`,
);
}
}
}
if (part.textures !== undefined) {
problems.push(...validate_textures(part.textures, `${where}.textures`));
}
if (part.y !== undefined && ![0, 90, 180, 270].includes(part.y as number)) {
problems.push(`${where}.y must be 0, 90, 180 or 270`);
}
if (part.shape !== undefined) problems.push(...validate_shape(part.shape, `${where}.shape`));
if (part.collision_shape !== undefined) {
problems.push(...validate_shape(part.collision_shape, `${where}.collision_shape`, 24));
}
});
}
}
if (json.render_layer !== undefined && !RENDER_LAYERS.includes(json.render_layer as RenderLayer)) {
problems.push(`render_layer must be one of ${RENDER_LAYERS.join(", ")}`);
}
@@ -554,14 +606,15 @@ export function validate_block(json: unknown): Problems {
return problems;
}
// boxes in pixels: [[x1, y1, z1, x2, y2, z2], ...], each corner from 0 to 16 and the second past the first
function validate_shape(shape: unknown, where: string): Problems {
// boxes in pixels: [[x1, y1, z1, x2, y2, z2], ...], each corner from 0 to 16 (y to top) and the second past the first
function validate_shape(shape: unknown, where: string, top = 16): Problems {
const is_box = (box: unknown) =>
Array.isArray(box) && box.length === 6 &&
box.every((v) => typeof v === "number" && v >= 0 && v <= 16) &&
box.every((v, i) => typeof v === "number" && v >= 0 && v <= (i % 3 === 1 ? top : 16)) &&
box[0] <= box[3] && box[1] <= box[4] && box[2] <= box[5];
if (!Array.isArray(shape) || shape.length === 0 || !shape.every(is_box)) {
return [`${where} must be a list of boxes in pixels, like [[0, 0, 0, 16, 8, 16]]`];
const most = top === 16 ? "" : `, up to ${top} high`;
return [`${where} must be a list of boxes in pixels${most}, like [[0, 0, 0, 16, 8, 16]]`];
}
return [];
}