Messy but working chicken

This commit is contained in:
2026-09-28 17:42:56 -03:00
parent fe149bb9ad
commit d606bc4b55
22 changed files with 622 additions and 21 deletions
+13 -2
View File
@@ -5,12 +5,16 @@ import type { ModelJson } from "./block_models.ts";
import { MAX_TEXTURE_SIZE } from "./constants.ts";
import {
type BlockJson,
type EntityJson,
type EntityModelJson,
ID_PATTERN,
type ItemJson,
type ManifestJson,
type OreJson,
type RecipeJson,
validate_block,
validate_entity,
validate_entity_model,
validate_item,
validate_manifest,
validate_model,
@@ -49,7 +53,7 @@ export class BmodError extends Error {
// the zip's layout:
// manifest.json the mod's manifest, with "scripts" and "credits" pointing into the zip
// data.json every block, model, item, recipe and ore
// data.json every block, model, item, recipe, ore, entity and entity model
// scripts/<side>.js
// textures/<name>.png the texture <mod id>:<name>
// credits.md
@@ -133,9 +137,16 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
items: checked(raw_data.items, "items", validate_item, problems) as ItemJson[],
recipes: checked(raw_data.recipes, "recipes", validate_recipe, problems) as RecipeJson[],
ores: checked(raw_data.ores, "ores", validate_ore, problems) as OreJson[],
entities: checked(raw_data.entities, "entities", validate_entity, problems) as EntityJson[],
entity_models: checked(
raw_data.entity_models,
"entity_models",
validate_entity_model,
problems,
) as EntityModelJson[],
};
// a mod only registers ids in its own namespace
for (const { id } of [...data.blocks, ...data.models, ...data.items]) {
for (const { id } of [...data.blocks, ...data.models, ...data.items, ...data.entities, ...data.entity_models]) {
if (typeof id === "string" && id.split(":")[0] !== mod) problems.push(`${id} isn't in this mod's namespace`);
}
+30 -2
View File
@@ -1,6 +1,14 @@
// checks between mods: dependencies, load order, and ids one mod uses from another. pure, so the build, check-mods
// and the server (on .bmod files) all use it
import { type BlockJson, choice_ids, type ItemJson, type OreJson, type RecipeJson } from "./mod_data.ts";
import {
type BlockJson,
choice_ids,
type EntityJson,
type EntityModelJson,
type ItemJson,
type OreJson,
type RecipeJson,
} from "./mod_data.ts";
import { BUILTIN_MODELS, type ModelJson } from "./block_models.ts";
export interface ModReport {
@@ -19,6 +27,8 @@ export interface LoadedMod {
items: { file: string; json: ItemJson }[];
recipes: { file: string; json: RecipeJson }[];
ores: { file: string; json: OreJson }[];
entities: { file: string; json: EntityJson }[];
entity_models: { file: string; json: EntityModelJson }[];
textures: string[];
// absolute paths by texture id
texture_files: Map<string, string>;
@@ -56,6 +66,8 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
const block_owners = new Map<string, string[]>();
const item_owners = new Map<string, string[]>();
const model_owners = new Map<string, string[]>();
const entity_owners = new Map<string, string[]>();
const entity_model_owners = new Map<string, string[]>();
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
const textures = new Set<string>(engine_textures);
@@ -67,6 +79,8 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
}
for (const { json } of mod.items) add(item_owners, json.id, mod.id);
for (const { json } of mod.models) add(model_owners, json.id, mod.id);
for (const { json } of mod.entities) add(entity_owners, json.id, mod.id);
for (const { json } of mod.entity_models) add(entity_model_owners, json.id, mod.id);
for (const texture of mod.textures) textures.add(texture);
}
@@ -81,13 +95,15 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
if (!mod_ids.has(dep)) errors.push(`manifest.json: depends on "${dep}", which isn't installed`);
}
const uses = (file: string, id: string, what: "block" | "item" | "texture" | "model") => {
const uses = (file: string, id: string, what: "block" | "item" | "texture" | "model" | "entity model") => {
const namespace = id.split(":")[0];
if (namespace !== mod.id && namespace !== "engine" && !dependencies.has(namespace)) {
warnings.push(`${file}: uses ${id} but doesn't list "${namespace}" in dependencies`);
}
if (what === "model") {
if (!BUILTIN_MODELS[id] && !model_owners.has(id)) errors.push(`${file}: model ${id} doesn't exist`);
} else if (what === "entity model") {
if (!entity_model_owners.has(id)) errors.push(`${file}: entity model ${id} doesn't exist`);
} else if (what === "texture") {
if (!textures.has(id)) warnings.push(`${file}: texture ${id} doesn't exist, it will show as missing`);
} else if (!(what === "block" ? block_owners : item_owners).has(id)) {
@@ -158,5 +174,17 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
uses(file, json.id, "block");
uses(file, json.replaces, "block");
}
for (const { file, json } of mod.entities) {
if ((entity_owners.get(json.id)?.length ?? 0) > 1) {
errors.push(`${file}: entity ${json.id} is also defined by ${entity_owners.get(json.id)!.join(", ")}`);
}
uses(file, json.model, "entity model");
uses(file, json.texture, "texture");
}
for (const { file, json } of mod.entity_models) {
if ((entity_model_owners.get(json.id)?.length ?? 0) > 1) {
errors.push(`${file}: entity model ${json.id} is defined more than once`);
}
}
}
}
+152
View File
@@ -80,6 +80,52 @@ export interface ItemJson {
components?: Record<string, unknown>;
}
// a kind of mob, drawn with an entity model and one texture. it wanders around, panics when hurt and looks at players
export interface EntityJson {
id: string;
// an entity model, see EntityModelJson
model: string;
texture: string;
// its collision box in blocks, width is for both x and z
width: number;
height: number;
max_health?: number;
// minecraft's per tick speed, 0.25 for most animals
movement_speed?: number;
}
type Vec3 = [number, number, number];
// a bedrock entity model (models/<name>.geo.json) with only what the engine uses. positions and sizes are in
// texture pixels, 16 to a block, and the model faces -z with its feet at 0
export interface EntityModelJson {
id: string;
// the size its uvs are laid out for, the texture is stretched to it
texture_width: number;
texture_height: number;
// parents come before their children
bones: EntityBoneJson[];
}
export interface EntityBoneJson {
name: string;
parent?: string;
// what it turns around
pivot: Vec3;
// degrees around x, y then z
rotation?: Vec3;
cubes: EntityCubeJson[];
}
// a box from origin to origin + size, textured like minecraft's box uv: see the renderer's FACES
export interface EntityCubeJson {
origin: Vec3;
size: Vec3;
uv: [number, number];
// grows the box this many pixels each way without moving its texture
inflate?: number;
}
// an item a recipe takes: its id, or a list of ids any of which fits, like every kind of planks
export type ItemChoice = string | string[];
@@ -579,6 +625,112 @@ export function validate_ore(json: unknown): Problems {
return problems;
}
export function validate_entity(json: unknown): Problems {
if (!is_object(json)) return ["entity must be an object"];
const problems = [
...validate_id(json.id, "id"),
...validate_id(json.model, "model"),
...validate_id(json.texture, "texture"),
];
for (const key of ["width", "height"]) {
if (typeof json[key] !== "number" || json[key] <= 0 || json[key] > 16) {
problems.push(`${key} must be a number of blocks above 0, at most 16`);
}
}
for (const key of ["max_health", "movement_speed"]) {
if (json[key] !== undefined && (typeof json[key] !== "number" || json[key] < 0)) {
problems.push(`${key} must be a number, 0 or more`);
}
}
return problems;
}
export function validate_entity_model(json: unknown): Problems {
if (!is_object(json)) return ["entity model must be an object"];
const problems = validate_id(json.id, "id");
for (const key of ["texture_width", "texture_height"]) {
if (!Number.isInteger(json[key]) || (json[key] as number) < 1) problems.push(`${key} must be a whole number`);
}
if (!Array.isArray(json.bones)) return [...problems, "bones must be a list"];
const names = new Set<string>();
json.bones.forEach((bone, i) => {
const field = `bones[${i}]`;
if (!is_object(bone) || typeof bone.name !== "string") {
problems.push(`${field} must be { name, pivot, cubes }`);
return;
}
if (names.has(bone.name)) problems.push(`${field}: there are two bones named ${bone.name}`);
if (bone.parent !== undefined && !names.has(bone.parent as string)) {
problems.push(`${field}: parent ${bone.parent} must be a bone before it`);
}
names.add(bone.name);
problems.push(...validate_vec3(bone.pivot, `${field}.pivot`));
if (bone.rotation !== undefined) problems.push(...validate_vec3(bone.rotation, `${field}.rotation`));
if (!Array.isArray(bone.cubes)) {
problems.push(`${field}.cubes must be a list`);
return;
}
bone.cubes.forEach((cube, j) => {
const cube_field = `${field}.cubes[${j}]`;
if (!is_object(cube)) {
problems.push(`${cube_field} must be { origin, size, uv }`);
return;
}
problems.push(...validate_vec3(cube.origin, `${cube_field}.origin`));
problems.push(...validate_vec3(cube.size, `${cube_field}.size`));
if (!Array.isArray(cube.uv) || cube.uv.length !== 2 || !cube.uv.every((n) => typeof n === "number")) {
problems.push(`${cube_field}.uv must be [u, v], per face uvs aren't supported`);
}
if (cube.inflate !== undefined && typeof cube.inflate !== "number") {
problems.push(`${cube_field}.inflate must be a number`);
}
});
});
return problems;
}
// a models/<name>.geo.json, as blockbench exports it for bedrock, to an EntityModelJson. its first geometry is used
export function entity_model_from_geo(json: unknown, id: string): { model?: EntityModelJson; problems: Problems } {
const geometry = is_object(json) && Array.isArray(json["minecraft:geometry"])
? json["minecraft:geometry"][0]
: undefined;
if (!is_object(geometry) || !is_object(geometry.description)) {
return {
problems: ['must be a bedrock model: { "minecraft:geometry": [{ "description": ..., "bones": [...] }] }'],
};
}
const bones = Array.isArray(geometry.bones) ? geometry.bones : [];
const model = {
id,
texture_width: geometry.description.texture_width,
texture_height: geometry.description.texture_height,
bones: bones.map((bone) => {
if (!is_object(bone)) return bone;
const { name, parent, pivot, rotation } = bone;
const cubes = Array.isArray(bone.cubes) ? bone.cubes : [];
return {
name,
...(parent === undefined ? {} : { parent }),
pivot: pivot ?? [0, 0, 0],
...(rotation === undefined ? {} : { rotation }),
cubes: cubes.map((cube) => {
if (!is_object(cube)) return cube;
const { origin, size, uv, inflate } = cube;
return { origin, size, uv, ...(inflate === undefined ? {} : { inflate }) };
}),
};
}),
};
const problems = validate_entity_model(model);
return problems.length === 0 ? { model: model as EntityModelJson, problems } : { problems };
}
function validate_vec3(value: unknown, field: string): Problems {
return Array.isArray(value) && value.length === 3 && value.every((n) => typeof n === "number")
? []
: [`${field} must be [x, y, z]`];
}
// an item id, or a list of them any of which fits
function validate_choice(value: unknown, field: string): Problems {
if (!Array.isArray(value)) return validate_id(value, field);
+11 -1
View File
@@ -4,6 +4,8 @@ import {
block_from_json,
BlockJson,
choice_ids,
EntityJson,
EntityModelJson,
grid_recipe_from_json,
GridRecipe,
is_mod_recipe,
@@ -24,6 +26,8 @@ export interface ModData {
items: ItemJson[];
recipes: RecipeJson[];
ores: OreJson[];
entities: EntityJson[];
entity_models: EntityModelJson[];
}
// a mod as the server lists it to clients, in load order: the .bmod players download, without its server script
@@ -57,7 +61,7 @@ export class ModLoadError extends Error {
}
}
// registers every mod's blocks, items and recipes, in the order given (dependencies first)
// registers every mod's blocks, items, entities and recipes, in the order given (dependencies first)
export function register_mod_data(mods: { id: string; data: ModData }[]): RecipeBook {
const recipes = new RecipeBook();
@@ -79,6 +83,12 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
for (const json of data.items) {
EverythingRegistry.register<ItemRegistry>("items", json.id, item_from_json(json));
}
for (const model of data.entity_models) {
EverythingRegistry.register<EntityModelJson>("entity_models", model.id, model);
}
for (const json of data.entities) {
EverythingRegistry.register<EntityJson>("entities", json.id, json);
}
} catch (e) {
fail((e as Error).message);
}