Improve tools system

This commit is contained in:
2026-09-29 17:54:17 -03:00
parent ad7f34a99e
commit 8fe581ea93
39 changed files with 225 additions and 49 deletions
+1
View File
@@ -2,3 +2,4 @@ build/
world/
server_mods/
dist/
ports/
+6 -7
View File
@@ -248,9 +248,10 @@ program**, so the server and each client can number blocks differently. Saves an
| `light_opacity` | see meaning | `light_opacity` | How much light passing through it loses, 0-15. Defaults to `15` (blocks all light) for solid blocks and `0` otherwise. Leaves and water use `1`. |
| `alpha` | `1` | `alpha` | Opacity for translucent blocks. |
| `collision` | `true` | `has_collision` | Whether entities collide with it. |
| `mining.toughness` | unbreakable | `toughness` | Seconds to break by hand. A matching tool is 2× faster. |
| `mining.toughness` | unbreakable | `toughness` | Seconds to break by hand. A matching tool is its `tool.speed` times faster. |
| `mining.tool` | none | `tool_to_break` | Tool type that speeds it up, like `pickaxe`, `axe` or `shovel`. |
| `mining.requires_tool` | `false` | `requires_tool` | Only drops when broken with `mining.tool`. |
| `mining.tier` | `0` | `tool_tier` | With `requires_tool`, the lowest `tool.tier` it drops for, like Minecraft's `incorrect_for_*_tool` tags. Iron ore is `1`, so a wood pickaxe gets nothing. |
| `drops` | nothing | `drop_table` | Item id dropped on the ground when broken, for players to pick up. |
| `item` | `true` | `register_block_item` | Also register an item that places this block, with the same id. |
| `interactive` | `false` | `interactive` | Right clicking it does something (opens a screen), so clients don't guess it places a block. |
@@ -409,10 +410,8 @@ clicked on somewhere it can go and eaten otherwise.
"item": {
"id": "copper_tools:copper_pickaxe",
"texture": "copper_tools:copper_pickaxe",
"tool": "pickaxe",
"max_stack": 1,
"lore": "Better than wood, worse than iron.",
"components": { "copper_tools:durability": { "max": 250 } }
"tool": { "type": "pickaxe", "tier": 1, "speed": 3.5, "durability": 190 },
"lore": "Better than wood, worse than iron."
}
}
```
@@ -423,9 +422,9 @@ clicked on somewhere it can go and eaten otherwise.
| `name` | from the id | `name` | What players see, like `"Copper Pickaxe"`. Made from the id when not set. |
| `texture` | required | `texture_id` | Texture id. |
| `data_textures` | none | `data_textures` | `{ "cast": "bworld:fishing_rod_cast" }`: a stack whose `data` has that key set is drawn with that texture instead, like a cast fishing rod. The first key that's set wins. |
| `tool` | none | `tool_type` | Tool type used by blocks' `mining.tool`. |
| `tool` | none | `tool` | Makes it a tool, like Fabric's `ToolMaterial`: `type` (matched against blocks' `mining.tool`, like `"pickaxe"`), `tier` (default `0`, see blocks' `mining.tier`), `speed` (default `2`, how many times faster than a hand) and `durability` (shown in its tooltip, nothing uses it up yet). The base game's tiers are wood and gold `0`, stone and copper `1`, iron `2`. |
| `places` | none | `block_id` | Block placed when used on a block face. |
| `max_stack` | `64` | new | Largest stack size. |
| `max_stack` | `64`, `1` for tools | new | Largest stack size. |
| `food` | none | `food` | Makes it edible: `{ "nutrition": 3, "saturation_modifier": 0.6 }`, with Minecraft's meanings (hunger points out of 20, and saturation of nutrition × modifier × 2). Optional `always_edible` (eaten even when full) and `returns` (an item left in the hand, like a bowl). Players eat it by right clicking while hungry. |
| `lore` | none | `lore` | Tooltip text under the name. `\n` starts a new line. |
| `components` | none | `on_create` / `get_lore` | Custom item components, handled by server scripts. |
+2 -2
View File
@@ -28,11 +28,11 @@ export class MultiPlayerGameMode {
// called every tick the attack button is held on a block
continue_destroy_block(hit: BlockHitResult) {
const block_info = EverythingRegistry.get_by_id<BlockRegistry>("blocks", hit.block)!;
const tool_type = this.#held_item()?.tool_type;
const tool = this.#held_item()?.tool;
this.destroy_pos = { x: hit.x, y: hit.y, z: hit.z };
this.destroy_time = block_info.toughness ?? 9999;
this.destroy_progress += TICK_DELTA * (tool_type === block_info.tool_to_break ? 2 : 1);
this.destroy_progress += TICK_DELTA * (tool && tool.type === block_info.tool_to_break ? tool.speed : 1);
if (this.destroy_progress >= this.destroy_time) {
// show it right away, the server decides drops and corrects us if it disagrees
+3
View File
@@ -29,6 +29,9 @@ export function item_tooltip(item: ItemStack): TooltipLine[] {
if (info?.food) {
lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR });
}
if (info?.tool) {
lines.push({ text: `Durability: ${info.tool.durability}`, color: LORE_COLOR });
}
const static_lore = info?.lore;
for (const lore of [static_lore, item.lore]) {
for (const text of lore?.split("\n") ?? []) {
+16 -1
View File
@@ -122,6 +122,8 @@ export interface BlockRegistry {
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[];
@@ -147,6 +149,18 @@ export interface BlockRegistry {
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 much use it can take. nothing uses it up yet
durability: number;
}
// what eating an item does, like minecraft's food properties
export interface FoodProperties {
// hunger points it restores, a bar is 20
@@ -166,7 +180,8 @@ export interface ItemRegistry<T = unknown | undefined> {
// 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_type?: string;
tool?: ToolProperties;
// 1 for tools and 64 for everything else when not set
max_stack?: number;
lore?: string;
food?: FoodProperties;
+1 -1
View File
@@ -21,7 +21,7 @@ export class ItemStack<T = unknown | undefined> {
const item_info = EverythingRegistry.get<ItemRegistry>("items", type_id);
this.type_id = type_id;
this.amount = amount;
this.max_amount = max_amount ?? item_info?.max_stack ?? 64;
this.max_amount = max_amount ?? item_info?.max_stack ?? (item_info?.tool ? 1 : 64);
this.data = undefined;
if (item_info?.on_create) {
item_info.on_create(this);
+30 -4
View File
@@ -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 (
+2 -1
View File
@@ -6,7 +6,8 @@
"mining": {
"toughness": 5,
"tool": "pickaxe",
"requires_tool": true
"requires_tool": true,
"tier": 1
},
"drops": "bworld:copper_ore"
}
+2 -1
View File
@@ -6,7 +6,8 @@
"mining": {
"toughness": 5,
"tool": "pickaxe",
"requires_tool": true
"requires_tool": true,
"tier": 1
},
"drops": "bworld:gold_ore"
}
+2 -1
View File
@@ -6,7 +6,8 @@
"mining": {
"toughness": 5,
"tool": "pickaxe",
"requires_tool": true
"requires_tool": true,
"tier": 1
},
"drops": "bworld:iron_ore"
}
+2 -1
View File
@@ -6,7 +6,8 @@
"mining": {
"toughness": 5,
"tool": "pickaxe",
"requires_tool": true
"requires_tool": true,
"tier": 1
},
"drops": "bworld:tin_ore"
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:copper_axe",
"texture": "bworld:copper_axe",
"tool": "axe"
"tool": {
"type": "axe",
"tier": 1,
"speed": 3.5,
"durability": 190
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:copper_hoe",
"texture": "bworld:copper_hoe",
"tool": "hoe"
"tool": {
"type": "hoe",
"tier": 1,
"speed": 3.5,
"durability": 190
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:copper_pickaxe",
"texture": "bworld:copper_pickaxe",
"tool": "pickaxe"
"tool": {
"type": "pickaxe",
"tier": 1,
"speed": 3.5,
"durability": 190
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:copper_shovel",
"texture": "bworld:copper_shovel",
"tool": "shovel"
"tool": {
"type": "shovel",
"tier": 1,
"speed": 3.5,
"durability": 190
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:golden_axe",
"texture": "bworld:golden_axe",
"tool": "axe"
"tool": {
"type": "axe",
"tier": 0,
"speed": 6,
"durability": 32
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:golden_hoe",
"texture": "bworld:golden_hoe",
"tool": "hoe"
"tool": {
"type": "hoe",
"tier": 0,
"speed": 6,
"durability": 32
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:golden_pickaxe",
"texture": "bworld:golden_pickaxe",
"tool": "pickaxe"
"tool": {
"type": "pickaxe",
"tier": 0,
"speed": 6,
"durability": 32
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:golden_shovel",
"texture": "bworld:golden_shovel",
"tool": "shovel"
"tool": {
"type": "shovel",
"tier": 0,
"speed": 6,
"durability": 32
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:iron_axe",
"texture": "bworld:iron_axe",
"tool": "axe"
"tool": {
"type": "axe",
"tier": 2,
"speed": 4,
"durability": 250
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:iron_hoe",
"texture": "bworld:iron_hoe",
"tool": "hoe"
"tool": {
"type": "hoe",
"tier": 2,
"speed": 4,
"durability": 250
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:iron_pickaxe",
"texture": "bworld:iron_pickaxe",
"tool": "pickaxe"
"tool": {
"type": "pickaxe",
"tier": 2,
"speed": 4,
"durability": 250
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:iron_shovel",
"texture": "bworld:iron_shovel",
"tool": "shovel"
"tool": {
"type": "shovel",
"tier": 2,
"speed": 4,
"durability": 250
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:stone_axe",
"texture": "bworld:stone_axe",
"tool": "axe"
"tool": {
"type": "axe",
"tier": 1,
"speed": 3,
"durability": 131
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:stone_hoe",
"texture": "bworld:stone_hoe",
"tool": "hoe"
"tool": {
"type": "hoe",
"tier": 1,
"speed": 3,
"durability": 131
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:stone_pickaxe",
"texture": "bworld:stone_pickaxe",
"tool": "pickaxe"
"tool": {
"type": "pickaxe",
"tier": 1,
"speed": 3,
"durability": 131
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:stone_shovel",
"texture": "bworld:stone_shovel",
"tool": "shovel"
"tool": {
"type": "shovel",
"tier": 1,
"speed": 3,
"durability": 131
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:wood_axe",
"texture": "bworld:wood_axe",
"tool": "axe"
"tool": {
"type": "axe",
"tier": 0,
"speed": 2,
"durability": 59
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:wood_hoe",
"texture": "bworld:wood_hoe",
"tool": "hoe"
"tool": {
"type": "hoe",
"tier": 0,
"speed": 2,
"durability": 59
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:wood_pickaxe",
"texture": "bworld:wood_pickaxe",
"tool": "pickaxe"
"tool": {
"type": "pickaxe",
"tier": 0,
"speed": 2,
"durability": 59
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "bworld:wood_shovel",
"texture": "bworld:wood_shovel",
"tool": "shovel"
"tool": {
"type": "shovel",
"tier": 0,
"speed": 2,
"durability": 59
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "farmersdelight:copper_knife",
"texture": "farmersdelight:copper_knife",
"tool": "knife"
"tool": {
"type": "knife",
"tier": 1,
"speed": 3.5,
"durability": 190
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "farmersdelight:gold_knife",
"texture": "farmersdelight:gold_knife",
"tool": "knife"
"tool": {
"type": "knife",
"tier": 0,
"speed": 6,
"durability": 32
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "farmersdelight:iron_knife",
"texture": "farmersdelight:iron_knife",
"tool": "knife"
"tool": {
"type": "knife",
"tier": 2,
"speed": 4,
"durability": 250
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "farmersdelight:stone_knife",
"texture": "farmersdelight:stone_knife",
"tool": "knife"
"tool": {
"type": "knife",
"tier": 1,
"speed": 3,
"durability": 131
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"item": {
"id": "farmersdelight:wood_knife",
"texture": "farmersdelight:wood_knife",
"tool": "knife"
"tool": {
"type": "knife",
"tier": 0,
"speed": 2,
"durability": 59
}
}
}
+4 -2
View File
@@ -540,14 +540,16 @@ export class GameServer {
}
// breaks a block the way breaking it by hand does: its components' on_break, then what it drops. without the
// right tool (or without a player) blocks that need one drop nothing
// right tool of a high enough tier (or without a player) blocks that need one drop nothing
destroy_block(x: number, y: number, z: number, player?: ServerPlayer) {
const info = this.world.get_block_info(x, y, z);
if (!info) {
return;
}
const held = EverythingRegistry.get<ItemRegistry>("items", player?.held_item?.type_id ?? "");
const drops = info.drop_table && (!info.requires_tool || held?.tool_type === info.tool_to_break);
const tool = held?.tool;
const right_tool = tool !== undefined && tool.type === info.tool_to_break && tool.tier >= (info.tool_tier ?? 0);
const drops = info.drop_table && (!info.requires_tool || right_tool);
this.set_block(x, y, z, AIR_ID, player);
if (drops) {
this.pop_item(x, y, z, new ItemStack(info.drop_table!));
+2 -2
View File
@@ -490,12 +490,12 @@ export class ModRuntime {
max_stack: (id) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`unknown item ${id}`);
return item.max_stack ?? 64;
return item.max_stack ?? (item.tool ? 1 : 64);
},
tool: (id) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`unknown item ${id}`);
return item.tool_type;
return item.tool?.type;
},
},
recipes: {
+2 -1
View File
@@ -234,7 +234,8 @@ Deno.test("every tier of pickaxe, axe, shovel and hoe exists and can be made", a
for (const tool of ["pickaxe", "axe", "shovel", "hoe"]) {
const id = `bworld:${tier}_${tool}`;
const item = EverythingRegistry.get<ItemRegistry>("items", id);
assertEquals(item?.tool_type, tool, id);
assertEquals(item?.tool?.type, tool, id);
assert(item!.tool!.durability > 0, id);
assertEquals(item?.texture_id, id);
assert(made.has(id), `nothing makes ${id}`);
}