Implement tags !
This commit is contained in:
+80
-9
@@ -11,11 +11,18 @@ import {
|
||||
type ToolProperties,
|
||||
} from "./everything_registry.ts";
|
||||
import { type BlockVariant, MODEL_FACES, type ModelFace } from "./block_models.ts";
|
||||
import { expand_items, list_matches } from "./tags.ts";
|
||||
|
||||
export const FORMAT_VERSION = 1;
|
||||
export const ID_PATTERN = /^[a-z0-9_]+:[a-z0-9_]+$/;
|
||||
export const NAMESPACE_PATTERN = /^[a-z0-9_]{1,32}$/;
|
||||
export const RESERVED_NAMESPACES = ["bworld", "engine"];
|
||||
// a tag's id may have folders, like c:ingots/iron. see docs/modding/resources/tags.md
|
||||
export const TAG_ID_PATTERN = /^[a-z0-9_]+:[a-z0-9_]+(\/[a-z0-9_]+)*$/;
|
||||
// the namespace for tags every mod shares, like neoforge's and fabric's. no mod owns it
|
||||
export const SHARED_TAG_NAMESPACE = "c";
|
||||
export const TAG_REGISTRIES = ["blocks", "items", "entities"] as const;
|
||||
export type TagRegistry = typeof TAG_REGISTRIES[number];
|
||||
|
||||
type BlockTextures = BlockRegistry["textures"];
|
||||
|
||||
@@ -135,13 +142,38 @@ export interface EntityCubeJson {
|
||||
inflate?: number;
|
||||
}
|
||||
|
||||
// an item a recipe takes: its id, or a list of ids any of which fits, like every kind of planks
|
||||
// something in a tag file's values or remove: an id, a "#tag", or either with whether it must exist. a plain one must
|
||||
export type TagEntryJson = string | { id: string; required?: boolean };
|
||||
|
||||
// a tag file, tags/<registry>/<namespace>/<path>.json, with the registry and id its path gives it. like neoforge's,
|
||||
// every mod's file for the same tag is merged, in load order
|
||||
export interface TagJson {
|
||||
registry: TagRegistry;
|
||||
id: string;
|
||||
values: TagEntryJson[];
|
||||
// drops what mods before this one put in the tag
|
||||
replace?: boolean;
|
||||
// takes these out again, if they're in it
|
||||
remove?: TagEntryJson[];
|
||||
}
|
||||
|
||||
// an item a recipe takes: its id, a "#tag" of items, or a list of those any of which fits, like every kind of planks
|
||||
export type ItemChoice = string | string[];
|
||||
|
||||
export function choice_ids(choice: ItemChoice): string[] {
|
||||
// the ids and #tags a choice lists, without looking inside the tags
|
||||
export function choice_list(choice: ItemChoice): string[] {
|
||||
return typeof choice === "string" ? [choice] : choice;
|
||||
}
|
||||
|
||||
// every item that fits a choice, with its tags' items, once each. the tags must be loaded
|
||||
export function choice_ids(choice: ItemChoice): string[] {
|
||||
return expand_items(choice_list(choice));
|
||||
}
|
||||
|
||||
export function choice_matches(choice: ItemChoice, id: string): boolean {
|
||||
return list_matches("items", choice_list(choice), id);
|
||||
}
|
||||
|
||||
export type RecipeJson =
|
||||
| {
|
||||
type: "shaped";
|
||||
@@ -285,7 +317,7 @@ export function grid_recipe_to_json(recipe: GridRecipe): RecipeJson {
|
||||
const letters = new Map<string, string>();
|
||||
for (const choice of recipe.pattern) {
|
||||
if (choice === undefined || letters.has(JSON.stringify(choice))) continue;
|
||||
const letter = pick_letter(choice_ids(choice)[0], new Set(letters.values()));
|
||||
const letter = pick_letter(choice_list(choice)[0].replace("#", ""), new Set(letters.values()));
|
||||
letters.set(JSON.stringify(choice), letter);
|
||||
key[letter] = choice;
|
||||
}
|
||||
@@ -318,7 +350,7 @@ export function grid_recipe_from_json(json: Extract<RecipeJson, { type: "shaped"
|
||||
|
||||
// a readable letter for an item in a pattern: its first letter if free, like P for planks
|
||||
function pick_letter(id: string, taken: Set<string>) {
|
||||
const name = id.split(":")[1].toUpperCase();
|
||||
const name = id.split(":")[1].split("/").at(-1)!.toUpperCase();
|
||||
for (const letter of [...name, ..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"]) {
|
||||
if (/[A-Z]/.test(letter) && !taken.has(letter)) {
|
||||
return letter;
|
||||
@@ -458,9 +490,9 @@ export function validate_block(json: unknown): Problems {
|
||||
const list = json[key];
|
||||
if (list === undefined) continue;
|
||||
if (!Array.isArray(list) || list.length === 0) {
|
||||
problems.push(`${key} must be a list of block ids`);
|
||||
problems.push(`${key} must be a list of block ids or #tags`);
|
||||
} else {
|
||||
for (const id of list) problems.push(...validate_id(id, key));
|
||||
for (const id of list) problems.push(...validate_id_or_tag(id, key));
|
||||
}
|
||||
}
|
||||
if (json.states !== undefined) {
|
||||
@@ -775,11 +807,50 @@ function validate_vec3(value: unknown, field: string): Problems {
|
||||
: [`${field} must be [x, y, z]`];
|
||||
}
|
||||
|
||||
// an item id, or a list of them any of which fits
|
||||
// an item id or #tag, 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);
|
||||
if (!Array.isArray(value)) return validate_id_or_tag(value, field);
|
||||
if (value.length === 0) return [`${field} must list at least one item`];
|
||||
return value.flatMap((id) => validate_id(id, field));
|
||||
return value.flatMap((id) => validate_id_or_tag(id, field));
|
||||
}
|
||||
|
||||
function validate_id_or_tag(value: unknown, field: string): Problems {
|
||||
if (typeof value === "string" && value.startsWith("#")) {
|
||||
return TAG_ID_PATTERN.test(value.slice(1)) ? [] : [`${field} must be a tag like "#my_mod:things"`];
|
||||
}
|
||||
return validate_id(value, field);
|
||||
}
|
||||
|
||||
// a tag file's contents, with the registry and id its path gives it
|
||||
export function validate_tag(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["tag must be an object"];
|
||||
const problems: Problems = [];
|
||||
if (!TAG_REGISTRIES.includes(json.registry as TagRegistry)) {
|
||||
problems.push(`registry must be one of ${TAG_REGISTRIES.join(", ")}`);
|
||||
}
|
||||
if (typeof json.id !== "string" || !TAG_ID_PATTERN.test(json.id)) {
|
||||
problems.push('id must be a tag id like "my_mod:things" or "c:ingots/iron"');
|
||||
}
|
||||
if (json.replace !== undefined && typeof json.replace !== "boolean") problems.push("replace must be true or false");
|
||||
for (const key of ["values", "remove"]) {
|
||||
const list = json[key];
|
||||
if (list === undefined && key === "remove") continue;
|
||||
if (!Array.isArray(list)) {
|
||||
problems.push(`${key} must be a list of ids and #tags`);
|
||||
continue;
|
||||
}
|
||||
for (const entry of list) {
|
||||
if (is_object(entry)) {
|
||||
problems.push(...validate_id_or_tag(entry.id, `${key}[].id`));
|
||||
if (entry.required !== undefined && typeof entry.required !== "boolean") {
|
||||
problems.push(`${key}[].required must be true or false`);
|
||||
}
|
||||
} else {
|
||||
problems.push(...validate_id_or_tag(entry, key));
|
||||
}
|
||||
}
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
|
||||
// optional text players see
|
||||
|
||||
Reference in New Issue
Block a user