Item rendering
This commit is contained in:
+44
-4
@@ -19,7 +19,14 @@ import {
|
||||
WATERLOGGED,
|
||||
type WornProperties,
|
||||
} from "./everything_registry.ts";
|
||||
import { type BlockPart, type BlockVariant, MODEL_FACES, type ModelFace } from "./block_models.ts";
|
||||
import {
|
||||
type BlockPart,
|
||||
type BlockVariant,
|
||||
DISPLAY_CONTEXTS,
|
||||
type DisplayContext,
|
||||
MODEL_FACES,
|
||||
type ModelFace,
|
||||
} from "./block_models.ts";
|
||||
import { expand_items, list_matches } from "./tags.ts";
|
||||
import { GAME_VERSION } from "./constants.ts";
|
||||
|
||||
@@ -121,6 +128,8 @@ export interface ItemJson {
|
||||
texture?: string;
|
||||
// data key -> texture, see ItemRegistry.data_textures
|
||||
data_textures?: Record<string, string>;
|
||||
// see ItemRegistry.model
|
||||
model?: string;
|
||||
// tier and speed default to 0 and 2
|
||||
tool?: Omit<ToolProperties, "tier" | "speed"> & Partial<ToolProperties>;
|
||||
places?: string;
|
||||
@@ -389,6 +398,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
|
||||
}
|
||||
const json: ItemJson = { id, texture: item.texture_id };
|
||||
if (item.data_textures !== undefined) json.data_textures = item.data_textures;
|
||||
if (item.model !== undefined) json.model = item.model;
|
||||
if (item.tool !== undefined) json.tool = item.tool;
|
||||
if (item.block_id !== undefined) json.places = item.block_id;
|
||||
if (item.max_stack !== undefined) json.max_stack = item.max_stack;
|
||||
@@ -416,6 +426,7 @@ export function add_to_block_item(json: ItemJson, item: ItemRegistry) {
|
||||
function item_fields(json: ItemJson): Partial<ItemRegistry> {
|
||||
const item: Partial<ItemRegistry> = {};
|
||||
if (json.data_textures !== undefined) item.data_textures = json.data_textures;
|
||||
if (json.model !== undefined) item.model = json.model;
|
||||
if (json.tool !== undefined) item.tool = { tier: 0, speed: 2, ...json.tool };
|
||||
if (json.places !== undefined) item.block_id = json.places;
|
||||
if (json.max_stack !== undefined) item.max_stack = json.max_stack;
|
||||
@@ -589,6 +600,9 @@ export function validate_block(json: unknown): Problems {
|
||||
problems.push(`${where}.${axis} must be 0, 90, 180 or 270`);
|
||||
}
|
||||
}
|
||||
if (variant.uvlock !== undefined && typeof variant.uvlock !== "boolean") {
|
||||
problems.push(`${where}.uvlock must be true or false`);
|
||||
}
|
||||
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));
|
||||
@@ -632,6 +646,9 @@ export function validate_block(json: unknown): Problems {
|
||||
problems.push(`${where}.${axis} must be 0, 90, 180 or 270`);
|
||||
}
|
||||
}
|
||||
if (part.uvlock !== undefined && typeof part.uvlock !== "boolean") {
|
||||
problems.push(`${where}.uvlock must be true or false`);
|
||||
}
|
||||
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));
|
||||
@@ -781,10 +798,11 @@ export function validate_model(json: unknown): Problems {
|
||||
const problems = validate_id(json.id, "id");
|
||||
if (json.parent !== undefined) problems.push(...validate_id(json.parent, "parent"));
|
||||
if (json.textures !== undefined) problems.push(...validate_textures(json.textures, "textures", true));
|
||||
// a model can leave its elements to its parent, or be one that only gives texture defaults to its children
|
||||
if (json.display !== undefined) problems.push(...validate_display(json.display));
|
||||
// a model can leave its elements to its parent, or be one that only gives defaults to its children
|
||||
if (json.elements === undefined) {
|
||||
if (json.parent === undefined && json.textures === undefined) {
|
||||
problems.push("a model needs elements, a parent, or textures for models that use it as theirs");
|
||||
if (json.parent === undefined && json.textures === undefined && json.display === undefined) {
|
||||
problems.push("a model needs elements, a parent, or textures or display for models that use it as theirs");
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
@@ -844,6 +862,27 @@ export function validate_model(json: unknown): Problems {
|
||||
return problems;
|
||||
}
|
||||
|
||||
function validate_display(display: unknown): Problems {
|
||||
if (!is_object(display)) return [`display must map ${DISPLAY_CONTEXTS.join(", ")} to transforms`];
|
||||
const problems: Problems = [];
|
||||
for (const [context, transform] of Object.entries(display)) {
|
||||
if (!DISPLAY_CONTEXTS.includes(context as DisplayContext)) {
|
||||
problems.push(`display: "${context}" isn't one of ${DISPLAY_CONTEXTS.join(", ")}`);
|
||||
} else if (!is_object(transform)) {
|
||||
problems.push(`display.${context} must be { rotation, translation, scale }`);
|
||||
} else {
|
||||
for (const [key, value] of Object.entries(transform)) {
|
||||
if (!["rotation", "translation", "scale"].includes(key)) {
|
||||
problems.push(`display.${context}: "${key}" isn't rotation, translation or scale`);
|
||||
} else if (!Array.isArray(value) || value.length !== 3 || !value.every((v) => typeof v === "number")) {
|
||||
problems.push(`display.${context}.${key} must be [x, y, z]`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
|
||||
// a texture id, or texture variables mapped to ids. a model's own defaults may also point at variables ("#side")
|
||||
function validate_textures(value: unknown, field: string, allow_variables = false): Problems {
|
||||
if (typeof value === "string") return validate_id(value, field);
|
||||
@@ -869,6 +908,7 @@ export function validate_item(json: unknown): Problems {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (json.model !== undefined) problems.push(...validate_id(json.model, "model"));
|
||||
if (json.places !== undefined) problems.push(...validate_id(json.places, "places"));
|
||||
problems.push(...validate_text(json.name, "name"), ...validate_text(json.lore, "lore"));
|
||||
if (json.max_stack !== undefined && (!Number.isInteger(json.max_stack) || (json.max_stack as number) < 1)) {
|
||||
|
||||
Reference in New Issue
Block a user