Improve tools system
This commit is contained in:
+30
-4
@@ -8,6 +8,7 @@ import {
|
||||
type ItemRegistry,
|
||||
RENDER_LAYERS,
|
||||
type RenderLayer,
|
||||
type ToolProperties,
|
||||
} from "./everything_registry.ts";
|
||||
import { type BlockVariant, MODEL_FACES, type ModelFace } from "./block_models.ts";
|
||||
|
||||
@@ -46,7 +47,7 @@ export interface BlockJson {
|
||||
transparent?: boolean;
|
||||
alpha?: number;
|
||||
collision?: boolean;
|
||||
mining?: { toughness: number; tool?: string; requires_tool?: boolean };
|
||||
mining?: { toughness: number; tool?: string; requires_tool?: boolean; tier?: number };
|
||||
drops?: string;
|
||||
item?: boolean;
|
||||
interactive?: boolean;
|
||||
@@ -72,7 +73,8 @@ export interface ItemJson {
|
||||
texture: string;
|
||||
// data key -> texture, see ItemRegistry.data_textures
|
||||
data_textures?: Record<string, string>;
|
||||
tool?: string;
|
||||
// tier and speed default to 0 and 2
|
||||
tool?: Omit<ToolProperties, "tier" | "speed"> & Partial<ToolProperties>;
|
||||
places?: string;
|
||||
max_stack?: number;
|
||||
food?: FoodProperties;
|
||||
@@ -194,6 +196,7 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
|
||||
json.mining = { toughness: block.toughness };
|
||||
if (block.tool_to_break !== undefined) json.mining.tool = block.tool_to_break;
|
||||
if (block.requires_tool) json.mining.requires_tool = true;
|
||||
if (block.tool_tier) json.mining.tier = block.tool_tier;
|
||||
}
|
||||
if (block.drop_table !== undefined) json.drops = block.drop_table;
|
||||
if (!has_item) json.item = false;
|
||||
@@ -227,6 +230,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
|
||||
block.toughness = json.mining.toughness;
|
||||
block.requires_tool = json.mining.requires_tool ?? false;
|
||||
if (json.mining.tool !== undefined) block.tool_to_break = json.mining.tool;
|
||||
if (json.mining.tier) block.tool_tier = json.mining.tier;
|
||||
}
|
||||
if (json.drops !== undefined) block.drop_table = json.drops;
|
||||
if (json.interactive) block.interactive = true;
|
||||
@@ -247,7 +251,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.tool_type !== undefined) json.tool = item.tool_type;
|
||||
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;
|
||||
if (item.food !== undefined) json.food = item.food;
|
||||
@@ -260,7 +264,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.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;
|
||||
if (json.food !== undefined) item.food = json.food;
|
||||
@@ -436,6 +440,8 @@ export function validate_block(json: unknown): Problems {
|
||||
if (json.mining !== undefined) {
|
||||
if (!is_object(json.mining) || typeof json.mining.toughness !== "number" || json.mining.toughness < 0) {
|
||||
problems.push("mining.toughness must be a number of seconds");
|
||||
} else if (json.mining.tier !== undefined && !is_whole(json.mining.tier)) {
|
||||
problems.push("mining.tier must be a whole number of 0 or more");
|
||||
}
|
||||
}
|
||||
if (json.drops !== undefined) problems.push(...validate_id(json.drops, "drops"));
|
||||
@@ -535,6 +541,10 @@ function validate_textures(value: unknown, field: string, allow_variables = fals
|
||||
);
|
||||
}
|
||||
|
||||
function is_whole(value: unknown): value is number {
|
||||
return Number.isInteger(value) && (value as number) >= 0;
|
||||
}
|
||||
|
||||
export function validate_item(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["item must be an object"];
|
||||
const problems = validate_id(json.id, "id");
|
||||
@@ -552,6 +562,22 @@ export function validate_item(json: unknown): Problems {
|
||||
if (json.max_stack !== undefined && (!Number.isInteger(json.max_stack) || (json.max_stack as number) < 1)) {
|
||||
problems.push("max_stack must be a positive whole number");
|
||||
}
|
||||
if (json.tool !== undefined) {
|
||||
const tool = json.tool;
|
||||
if (!is_object(tool) || typeof tool.type !== "string") {
|
||||
problems.push('tool must be { type, durability }, like "pickaxe"');
|
||||
} else {
|
||||
if (!is_whole(tool.durability) || tool.durability < 1) {
|
||||
problems.push("tool.durability must be a positive whole number");
|
||||
}
|
||||
if (tool.tier !== undefined && !is_whole(tool.tier)) {
|
||||
problems.push("tool.tier must be a whole number of 0 or more");
|
||||
}
|
||||
if (tool.speed !== undefined && (typeof tool.speed !== "number" || tool.speed <= 0)) {
|
||||
problems.push("tool.speed must be a positive number");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (json.food !== undefined) {
|
||||
const food = json.food;
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user