Health and hunger system

This commit is contained in:
2026-09-26 21:07:36 -03:00
parent 0fa6ec4ed3
commit 8d9c85d379
18 changed files with 662 additions and 8 deletions
+13
View File
@@ -138,6 +138,18 @@ export interface BlockRegistry {
compiled_states?: CompiledStateDefinition[];
}
// 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);
// what players see, like "Iron Ingot". made from the id when not set, see display_name in common/utils.ts
@@ -146,6 +158,7 @@ export interface ItemRegistry<T = unknown | undefined> {
tool_type?: string;
max_stack?: number;
lore?: string;
food?: FoodProperties;
components?: Record<string, unknown>;
on_create?(item: ItemStack<T>): void;
+28
View File
@@ -95,6 +95,19 @@ export interface BlockInteractEvent {
readonly item: ItemStack | undefined;
}
export interface PlayerDamageEvent {
readonly player: Player;
// before events can change it
amount: number;
// what hurt them: "fall", "void", "starve", or what a mod passed to damage
readonly cause: string;
}
export interface PlayerDeathEvent {
readonly player: Player;
readonly cause: string;
}
export interface ChatSendEvent {
readonly player: Player;
message: string;
@@ -105,6 +118,7 @@ export interface ServerBeforeEvents {
block_place: EventSignal<BlockPlaceEvent & Cancelable>;
block_interact: EventSignal<BlockInteractEvent & Cancelable>;
chat_send: EventSignal<ChatSendEvent & Cancelable>;
player_damage: EventSignal<PlayerDamageEvent & Cancelable>;
}
export interface ServerAfterEvents {
@@ -114,6 +128,8 @@ export interface ServerAfterEvents {
chat_send: EventSignal<Readonly<ChatSendEvent>>;
player_join: EventSignal<{ readonly player: Player }>;
player_leave: EventSignal<{ readonly player: Player }>;
// they drop what they carried and respawn right after
player_death: EventSignal<PlayerDeathEvent>;
server_start: EventSignal<Record<never, never>>;
tick: EventSignal<{ readonly dt: number }>;
// 06:00, after every block's on_new_day
@@ -150,6 +166,18 @@ export interface Player {
give_item(id: Id, count?: number, data?: unknown): void;
send_message(text: string): void;
teleport(x: number, y: number, z: number): void;
// health and hunger, like minecraft: 20 each, saturation is never above hunger
readonly health: number;
readonly max_health: number;
readonly food: number;
readonly saturation: number;
// cause is for events and the death message, like "fall"
damage(amount: number, cause?: string): void;
heal(amount: number): void;
// like eating: nutrition in hunger points, saturation_modifier like minecraft's (bread's is 0.6)
feed(nutrition: number, saturation_modifier: number): void;
// tires them out: 4 costs a point of saturation, or of hunger once it's gone
add_exhaustion(amount: number): void;
}
export interface PlayerList {
+1
View File
@@ -128,6 +128,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
}
uses(file, json.texture, "texture");
if (json.places) uses(file, json.places, "block");
if (json.food?.returns) uses(file, json.food.returns, "item");
}
for (const { file, json } of mod.recipes) {
switch (json.type) {
+18
View File
@@ -3,6 +3,7 @@
import {
type BlockRegistry,
type BlockStateDefinition,
type FoodProperties,
type ItemRegistry,
RENDER_LAYERS,
type RenderLayer,
@@ -68,6 +69,7 @@ export interface ItemJson {
tool?: string;
places?: string;
max_stack?: number;
food?: FoodProperties;
name?: string;
lore?: string;
components?: Record<string, unknown>;
@@ -166,6 +168,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
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;
if (item.food !== undefined) json.food = item.food;
if (item.name !== undefined) json.name = item.name;
if (item.lore !== undefined) json.lore = item.lore;
if (item.components) json.components = item.components;
@@ -177,6 +180,7 @@ export function item_from_json(json: ItemJson): ItemRegistry {
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;
if (json.food !== undefined) item.food = json.food;
if (json.name !== undefined) item.name = json.name;
if (json.lore !== undefined) item.lore = json.lore;
if (json.components) item.components = json.components;
@@ -434,6 +438,20 @@ 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.food !== undefined) {
const food = json.food;
if (
!is_object(food) || !Number.isInteger(food.nutrition) || (food.nutrition as number) < 0 ||
typeof food.saturation_modifier !== "number" || food.saturation_modifier < 0
) {
problems.push("food must be { nutrition, saturation_modifier }, nutrition a whole number");
} else {
if (food.always_edible !== undefined && typeof food.always_edible !== "boolean") {
problems.push("food.always_edible must be true or false");
}
if (food.returns !== undefined) problems.push(...validate_id(food.returns, "food.returns"));
}
}
return problems;
}
+4 -2
View File
@@ -7,7 +7,7 @@ export const AIR_ID = "bworld:air";
// bump when a client and server of different versions can't play together.
// the server rejects a different version before the client downloads anything
export const PROTOCOL_VERSION = 4;
export const PROTOCOL_VERSION = 5;
export interface PlayerInfo {
id: string;
@@ -123,7 +123,9 @@ export type ServerMessage =
| { type: "screen_properties"; properties: Record<string, number> }
| { type: "close_screen" }
// the world time, every second and when it's changed. clients count ticks in between
| { type: "time"; time: number };
| { type: "time"; time: number }
// the player's own health, hunger and saturation, when they change
| { type: "vitals"; health: number; food: number; saturation: number };
export const MAX_NAME_LENGTH = 16;
export const MAX_CHAT_LENGTH = 256;