Better block models !
This commit is contained in:
+18
-1
@@ -55,6 +55,7 @@ export interface BlockJson {
|
||||
transparent?: boolean;
|
||||
alpha?: number;
|
||||
collision?: boolean;
|
||||
shape?: number[][];
|
||||
mining?: { toughness: number; tool?: string; requires_tool?: boolean; tier?: number };
|
||||
drops?: string;
|
||||
item?: boolean;
|
||||
@@ -267,6 +268,7 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
|
||||
if (block.light_opacity !== undefined) json.light_opacity = block.light_opacity;
|
||||
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.toughness !== undefined) {
|
||||
json.mining = { toughness: block.toughness };
|
||||
if (block.tool_to_break !== undefined) json.mining.tool = block.tool_to_break;
|
||||
@@ -294,6 +296,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
|
||||
if (json.name !== undefined) block.name = json.name;
|
||||
if (json.model !== undefined) block.model = json.model;
|
||||
if (json.variants !== undefined) block.variants = json.variants;
|
||||
if (json.shape !== undefined) block.shape = json.shape;
|
||||
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;
|
||||
@@ -474,7 +477,7 @@ export function validate_block(json: unknown): Problems {
|
||||
}
|
||||
}
|
||||
if (!is_object(variant)) {
|
||||
problems.push(`${where} must be { model, textures, y }`);
|
||||
problems.push(`${where} must be { model, textures, y, shape }`);
|
||||
continue;
|
||||
}
|
||||
if (variant.model !== undefined) problems.push(...validate_id(variant.model, `${where}.model`));
|
||||
@@ -484,9 +487,11 @@ export function validate_block(json: unknown): Problems {
|
||||
if (variant.y !== undefined && ![0, 90, 180, 270].includes(variant.y as number)) {
|
||||
problems.push(`${where}.y must be 0, 90, 180 or 270`);
|
||||
}
|
||||
if (variant.shape !== undefined) problems.push(...validate_shape(variant.shape, `${where}.shape`));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (json.shape !== undefined) problems.push(...validate_shape(json.shape, "shape"));
|
||||
if (json.render_layer !== undefined && !RENDER_LAYERS.includes(json.render_layer as RenderLayer)) {
|
||||
problems.push(`render_layer must be one of ${RENDER_LAYERS.join(", ")}`);
|
||||
}
|
||||
@@ -549,6 +554,18 @@ 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 {
|
||||
const is_box = (box: unknown) =>
|
||||
Array.isArray(box) && box.length === 6 &&
|
||||
box.every((v) => typeof v === "number" && v >= 0 && v <= 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]]`];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export function validate_model(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["model must be an object"];
|
||||
const problems = validate_id(json.id, "id");
|
||||
|
||||
Reference in New Issue
Block a user