195 lines
7.0 KiB
TypeScript
195 lines
7.0 KiB
TypeScript
import type { TintColor } from "./worldgen/biome_colors.ts";
|
|
import type { ItemStack } from "./inventory.ts";
|
|
import type { BlockVariant } from "./block_models.ts";
|
|
|
|
export class EverythingRegistry {
|
|
static #key_to_id = new Map<string, Map<string, number>>();
|
|
static #id_to_value = new Map<string, unknown[]>();
|
|
|
|
static register<T>(registry: string, key: string, value: T): T {
|
|
if (this.#key_to_id.get(registry)?.has(key)) {
|
|
throw new Error(`${key} is already registered in ${registry}`);
|
|
}
|
|
if (!this.#key_to_id.has(registry)) {
|
|
this.#key_to_id.set(registry, new Map());
|
|
this.#id_to_value.set(registry, []);
|
|
}
|
|
|
|
const key_map = this.#key_to_id.get(registry)!;
|
|
const values = this.#id_to_value.get(registry)!;
|
|
|
|
const id = values.length + 1;
|
|
|
|
key_map.set(key, id);
|
|
values[id] = value;
|
|
|
|
return value;
|
|
}
|
|
|
|
static get<T>(registry: string, key: string): T | undefined {
|
|
const id = this.#key_to_id.get(registry)?.get(key);
|
|
if (id === undefined) {
|
|
return undefined;
|
|
}
|
|
return this.#id_to_value.get(registry)?.[id] as T;
|
|
}
|
|
|
|
static get_id(registry: string, key: string): number | undefined {
|
|
return this.#key_to_id.get(registry)?.get(key);
|
|
}
|
|
|
|
static get_full<T>(registry: string, key: string): [number, T] | undefined {
|
|
const id = this.#key_to_id.get(registry)?.get(key);
|
|
if (id === undefined) {
|
|
return undefined;
|
|
}
|
|
return [id, this.#id_to_value.get(registry)?.[id] as T];
|
|
}
|
|
|
|
static get_by_id<T>(registry: string, id: number): T | undefined {
|
|
return this.#id_to_value.get(registry)?.[id] as T;
|
|
}
|
|
|
|
static get_registry<T>(registry: string): T[] {
|
|
return this.#id_to_value.get(registry) as T[];
|
|
}
|
|
|
|
// forget everything, for tests that load mods more than once
|
|
static clear() {
|
|
this.#key_to_id.clear();
|
|
this.#id_to_value.clear();
|
|
}
|
|
|
|
// [key, value] pairs in registration order
|
|
static entries<T>(registry: string): [string, T][] {
|
|
const values = this.#id_to_value.get(registry) ?? [];
|
|
return [...(this.#key_to_id.get(registry) ?? [])].map(([key, id]) => [key, values[id] as T]);
|
|
}
|
|
}
|
|
|
|
// one texture for everything, or the model's texture variables: { top, bottom, side } or { front, side } for
|
|
// cubes, { crop } for engine:crop and so on
|
|
export type BlockTextures = string | Record<string, string>;
|
|
|
|
export interface BlockStateDefinition {
|
|
name: string;
|
|
bits: number;
|
|
default: number;
|
|
}
|
|
|
|
interface CompiledStateDefinition {
|
|
name: string;
|
|
mask: number;
|
|
shift: number;
|
|
}
|
|
|
|
// solid: fully opaque. cutout: texels are either opaque or see-through (leaves).
|
|
// translucent: blended and sorted back to front (water, glass)
|
|
export const RENDER_LAYERS = ["solid", "cutout", "translucent"] as const;
|
|
export type RenderLayer = typeof RENDER_LAYERS[number];
|
|
|
|
// like minecraft: solid blocks stop light, everything else lets it through unless it says otherwise
|
|
export function block_light_opacity(block: BlockRegistry | undefined): number {
|
|
if (!block) return 0;
|
|
return block.light_opacity ?? ((block.render_layer ?? "solid") === "solid" ? 15 : 0);
|
|
}
|
|
|
|
export function block_light_emission(block: BlockRegistry | undefined): number {
|
|
return block?.light_emission ?? 0;
|
|
}
|
|
|
|
export interface BlockRegistry {
|
|
id: string;
|
|
// what players see, like "Iron Ore". made from the id when not set, see display_name in common/utils.ts
|
|
name?: string;
|
|
textures: BlockTextures;
|
|
// the block model, engine:cube when not set. see common/block_models.ts
|
|
model?: string;
|
|
// solid when not set. anything else doesn't hide its neighbors' faces
|
|
render_layer?: RenderLayer;
|
|
// hide faces between two of this block, like glass and water. defaults to true for translucent blocks
|
|
cull_same?: boolean;
|
|
// drawn in a color: the biome's grass or foliage color, like minecraft's grass block and leaves, or a fixed one
|
|
// like "#80a755". only the texture variables listed (like "top"), or all of them
|
|
tint?: { color: TintColor; textures?: string[] };
|
|
// light level 0-15 it gives off
|
|
light_emission?: number;
|
|
// how much light going through it loses, 0-15. see block_light_opacity for the default
|
|
light_opacity?: number;
|
|
alpha?: number;
|
|
|
|
has_collision: boolean;
|
|
toughness?: number;
|
|
requires_tool?: boolean;
|
|
tool_to_break?: string;
|
|
// the lowest tool tier it drops for when requires_tool, like minecraft's incorrect_for_*_tool tags. 0 when not set
|
|
tool_tier?: number;
|
|
drop_table?: string;
|
|
|
|
states?: BlockStateDefinition[];
|
|
// a different model or textures for some states, by conditions like "age=7". the first match wins
|
|
variants?: Record<string, BlockVariant>;
|
|
|
|
// right clicking it does something instead of placing a block, clients don't predict placing against it.
|
|
// behavior runs on the server, see server/game/blocks.ts
|
|
interactive?: boolean;
|
|
// placing a block into it replaces it, like water
|
|
replaceable?: boolean;
|
|
// the blocks it can stand on, like crops on farmland. it can't be placed elsewhere, and breaks when the block
|
|
// under it becomes something else. anything when not set
|
|
place_on?: string[];
|
|
// the blocks it can be placed into, replacing them, like rice into water. air and replaceable blocks when not set
|
|
place_in?: string[];
|
|
// custom components from mods and their params, the server runs them
|
|
components?: Record<string, unknown>;
|
|
// draws the item a server script gives it (ServerWorld.set_block_item) on top of it, like farmer's delight's
|
|
// cutting board. surface is the height in pixels it lies on. turned with the variant's y
|
|
item_display?: { surface: number };
|
|
|
|
compiled_states?: CompiledStateDefinition[];
|
|
}
|
|
|
|
// what a tool is made of, like fabric's ToolMaterial
|
|
export interface ToolProperties {
|
|
// like "pickaxe", matched against blocks' tool_to_break
|
|
type: string;
|
|
// blocks that need a tool only drop for tools of their tool_tier or higher. wood and gold are 0, stone 1, iron 2
|
|
tier: number;
|
|
// how many times faster than a hand it breaks blocks of its type
|
|
speed: number;
|
|
// how many deaths it lasts: each one takes 1 off every tool the player carried. what's used up is the stack's
|
|
// data.damage, see ItemStack.durability
|
|
durability: number;
|
|
}
|
|
|
|
// what eating an item does, like minecraft's food properties
|
|
export interface FoodProperties {
|
|
// hunger points it restores, a bar is 20
|
|
nutrition: number;
|
|
// saturation it gives is nutrition * saturation_modifier * 2, like minecraft. bread's is 0.6
|
|
saturation_modifier: number;
|
|
// can be eaten when not hungry
|
|
always_edible?: boolean;
|
|
// what's left in the hand after, like a bowl after soup
|
|
returns?: string;
|
|
}
|
|
|
|
export interface ItemRegistry<T = unknown | undefined> {
|
|
texture_id: string | ((item: ItemStack<T>) => string);
|
|
// shown in place of texture_id while the stack's data has that key set, like a cast fishing rod. the first wins
|
|
data_textures?: Record<string, string>;
|
|
// what players see, like "Iron Ingot". made from the id when not set, see display_name in common/utils.ts
|
|
name?: string;
|
|
block_id?: string;
|
|
tool?: ToolProperties;
|
|
// 1 for tools and 64 for everything else when not set
|
|
max_stack?: number;
|
|
lore?: string;
|
|
food?: FoodProperties;
|
|
components?: Record<string, unknown>;
|
|
|
|
on_create?(item: ItemStack<T>): void;
|
|
|
|
get_lore?(item: ItemStack<T>): string;
|
|
}
|