Improve crops system

This commit is contained in:
2026-09-26 18:32:38 -03:00
parent e22f12c0b6
commit 0fa6ec4ed3
15 changed files with 394 additions and 31 deletions
+3
View File
@@ -129,6 +129,9 @@ export interface BlockRegistry {
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[];
// custom components from mods and their params, the server runs them
components?: Record<string, unknown>;
+6
View File
@@ -42,6 +42,8 @@ export interface BlockComponent<P = any> {
on_interact?(block: BlockRef, params: P, player: Player): boolean;
on_tick?(block: BlockRef, params: P, dt: number): void;
on_second?(block: BlockRef, params: P, dt: number): void;
// every morning at 06:00, for every block with data in the world, near a player or not. day is ServerWorld.day
on_new_day?(block: BlockRef, params: P, day: number): void;
}
// deno-lint-ignore no-explicit-any
@@ -114,6 +116,8 @@ export interface ServerAfterEvents {
player_leave: EventSignal<{ readonly player: Player }>;
server_start: EventSignal<Record<never, never>>;
tick: EventSignal<{ readonly dt: number }>;
// 06:00, after every block's on_new_day
new_day: EventSignal<{ readonly day: number }>;
}
// world and players
@@ -130,6 +134,8 @@ export interface ServerWorld {
readonly seed: string;
// ticks since 06:00 of day 1. a day is 36000 ticks (06:00 to 18:00), a night 12000
readonly time: number;
// days since the world started, going up at 06:00 each morning: 0 on the first day
readonly day: number;
set_time(time: number): void;
}
+1
View File
@@ -107,6 +107,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
}
}
if (json.drops) uses(file, json.drops, "item");
for (const id of json.place_on ?? []) uses(file, id, "block");
}
for (const { file, json } of mod.models) {
if ((model_owners.get(json.id)?.length ?? 0) > 1 || BUILTIN_MODELS[json.id]) {
+10
View File
@@ -48,6 +48,7 @@ export interface BlockJson {
item?: boolean;
interactive?: boolean;
replaceable?: boolean;
place_on?: string[];
states?: BlockStateDefinition[];
components?: Record<string, unknown>;
}
@@ -120,6 +121,7 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
if (!has_item) json.item = false;
if (block.interactive) json.interactive = true;
if (block.replaceable) json.replaceable = true;
if (block.place_on) json.place_on = block.place_on;
if (block.states) json.states = block.states;
if (block.components) json.components = block.components;
return json;
@@ -148,6 +150,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
if (json.drops !== undefined) block.drop_table = json.drops;
if (json.interactive) block.interactive = true;
if (json.replaceable) block.replaceable = true;
if (json.place_on) block.place_on = json.place_on;
if (json.states) block.states = json.states;
if (json.components) block.components = json.components;
return { block, has_item: json.item ?? true };
@@ -337,6 +340,13 @@ export function validate_block(json: unknown): Problems {
}
}
if (json.drops !== undefined) problems.push(...validate_id(json.drops, "drops"));
if (json.place_on !== undefined) {
if (!Array.isArray(json.place_on) || json.place_on.length === 0) {
problems.push("place_on must be a list of block ids");
} else {
for (const id of json.place_on) problems.push(...validate_id(id, "place_on"));
}
}
if (json.states !== undefined) {
const states = json.states;
if (!Array.isArray(states)) {
+5
View File
@@ -118,6 +118,11 @@ function compile_block_states(block_info: BlockRegistry) {
});
}
// whether a block can be at a spot with below under it, see place_on
export function can_stand_on(block: BlockRegistry | undefined, below: string): boolean {
return !block?.place_on || block.place_on.includes(below);
}
// numeric so looking chunks up doesnt allocate a string every time, fine for |x|, |z| < 32768
export function chunk_key(x: number, z: number) {
return (x + 32768) * 65536 + (z + 32768);