Fishing !
This commit is contained in:
@@ -158,6 +158,8 @@ export interface FoodProperties {
|
||||
|
||||
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;
|
||||
|
||||
@@ -129,6 +129,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
errors.push(`${file}: item ${json.id} is also defined by ${item_owners.get(json.id)!.join(", ")}`);
|
||||
}
|
||||
uses(file, json.texture, "texture");
|
||||
for (const texture of Object.values(json.data_textures ?? {})) uses(file, texture, "texture");
|
||||
if (json.places) uses(file, json.places, "block");
|
||||
if (json.food?.returns) uses(file, json.food.returns, "item");
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ export interface OreJson {
|
||||
export interface ItemJson {
|
||||
id: string;
|
||||
texture: string;
|
||||
// data key -> texture, see ItemRegistry.data_textures
|
||||
data_textures?: Record<string, string>;
|
||||
tool?: string;
|
||||
places?: string;
|
||||
max_stack?: number;
|
||||
@@ -192,6 +194,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
|
||||
throw new Error(`${id} picks its texture with a function, which json can't hold`);
|
||||
}
|
||||
const json: ItemJson = { id, texture: item.texture_id };
|
||||
if (item.data_textures !== undefined) json.data_textures = item.data_textures;
|
||||
if (item.tool_type !== undefined) json.tool = item.tool_type;
|
||||
if (item.block_id !== undefined) json.places = item.block_id;
|
||||
if (item.max_stack !== undefined) json.max_stack = item.max_stack;
|
||||
@@ -204,6 +207,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
|
||||
|
||||
export function item_from_json(json: ItemJson): ItemRegistry {
|
||||
const item: ItemRegistry = { texture_id: json.texture };
|
||||
if (json.data_textures !== undefined) item.data_textures = json.data_textures;
|
||||
if (json.tool !== undefined) item.tool_type = json.tool;
|
||||
if (json.places !== undefined) item.block_id = json.places;
|
||||
if (json.max_stack !== undefined) item.max_stack = json.max_stack;
|
||||
@@ -477,6 +481,14 @@ export function validate_item(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["item must be an object"];
|
||||
const problems = validate_id(json.id, "id");
|
||||
problems.push(...validate_id(json.texture, "texture"));
|
||||
if (json.data_textures !== undefined) {
|
||||
if (!is_object(json.data_textures)) problems.push("data_textures must be { data key: texture id }");
|
||||
else {
|
||||
for (const [key, id] of Object.entries(json.data_textures)) {
|
||||
problems.push(...validate_id(id, `data_textures.${key}`));
|
||||
}
|
||||
}
|
||||
}
|
||||
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)) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ID_MASK, STATE_SHIFT } from "./constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "./everything_registry.ts";
|
||||
import { block_item_texture, cube_face_texture } from "./block_models.ts";
|
||||
import type { ItemStack } from "./inventory.ts";
|
||||
|
||||
export function point_inside_rec(
|
||||
point_x: number,
|
||||
@@ -46,6 +47,19 @@ export function register_block_item(block: BlockRegistry) {
|
||||
EverythingRegistry.register<ItemRegistry>("items", block.id, item);
|
||||
}
|
||||
|
||||
// the texture a stack is drawn with: one of its data_textures when its data has that key set, or its texture
|
||||
export function item_texture(
|
||||
item: ItemStack,
|
||||
info = EverythingRegistry.get<ItemRegistry>("items", item.type_id),
|
||||
): string {
|
||||
if (!info) return "engine:missing";
|
||||
const data = item.data as Record<string, unknown> | undefined;
|
||||
for (const [key, texture] of Object.entries(info.data_textures ?? {})) {
|
||||
if (data?.[key]) return texture;
|
||||
}
|
||||
return typeof info.texture_id === "function" ? info.texture_id(item) : info.texture_id;
|
||||
}
|
||||
|
||||
// what players see an item called: its name, or one made from its id, so bworld:iron_ingot is "Iron Ingot"
|
||||
export function display_name(item_id: string): string {
|
||||
const name = EverythingRegistry.get<ItemRegistry>("items", item_id)?.name;
|
||||
|
||||
Reference in New Issue
Block a user