Waterlogging

This commit is contained in:
2026-10-07 20:56:24 -03:00
parent 78117f65c9
commit 757ee74bc7
26 changed files with 459 additions and 73 deletions
+78 -18
View File
@@ -8,7 +8,7 @@ import {
TICK_DELTA,
TICKS_PER_SECOND,
} from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, is_liquid_block, ItemRegistry } from "$/common/everything_registry.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { LiquidFlow } from "./liquids.ts";
import { Container, ItemData, ItemStack } from "$/common/inventory.ts";
import {
@@ -23,7 +23,17 @@ import type { ModListing, RecipeBook } from "$/common/mod_loader.ts";
import type { WorldgenSetup } from "$/common/generation.ts";
import { MAX_VIEW_DISTANCE, MIN_VIEW_DISTANCE } from "$/common/chunk_streaming.ts";
import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts";
import { box_overlaps_block, can_place, can_stand_on, display_name, placement_target } from "$/common/utils.ts";
import {
box_overlaps_block,
can_place,
can_stand_on,
display_name,
is_waterlogged,
liquid_block,
placement_target,
set_state_value,
waterlog_placement,
} from "$/common/utils.ts";
import { collision_shape_at, type Shape } from "$/common/block_shapes.ts";
import { EXHAUSTION } from "./vitals.ts";
import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
@@ -611,7 +621,10 @@ export class GameServer {
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);
// a waterlogged block leaves its liquid behind, a source
const value = this.world.get_block_value(x, y, z);
const liquid = is_waterlogged(value, info) ? liquid_block(value, info) : undefined;
this.set_block(x, y, z, liquid?.id ?? AIR_ID, player);
if (drops) {
this.pop_item(x, y, z, new ItemStack(info.drop_table!));
}
@@ -985,7 +998,12 @@ export class GameServer {
}
const player_api = this.mods.player(player);
const block = this.mods.block_ref(x, y, z, info.id);
const value = this.world.get_block_value(x, y, z);
const held_slot = player.inventory.get_slot(player.selected_slot);
const held = EverythingRegistry.get<ItemRegistry>("items", held_slot.type_id ?? "");
// an item aimed at liquids, like a bucket, uses the liquid in a waterlogged block instead of the block
const used = (held?.targets_liquids && liquid_block(value, info)) || info;
const block = this.mods.block_ref(x, y, z, used.id);
const interact = { player: player_api, block, item: player_api.held_item };
const before_interact = { ...interact, cancel: false };
this.mods.before.block_interact.emit(before_interact);
@@ -995,7 +1013,7 @@ export class GameServer {
}
let handled = false;
for (const { mod, id: component_id, component, params } of this.mods.components_of(info.id)) {
for (const { mod, id: component_id, component, params } of this.mods.components_of(used.id)) {
if (component.on_interact) {
handled = run_guarded(mod, `${component_id} on_interact`, () =>
component.on_interact!(block, params, player_api)) ===
@@ -1015,14 +1033,19 @@ export class GameServer {
return;
}
// placing a liquid, like a water bucket, into a block that can hold it fills it, like minecraft's
if (held?.block_id && info.waterloggable === held.block_id && !is_waterlogged(value, info)) {
this.#waterlog(player, x, y, z, face, hit);
return;
}
// place the held block against the face, or into the clicked block if it's replaceable. food that can be planted,
// like a potato on farmland, is planted, and eaten when it can't be
const held_slot = player.inventory.get_slot(player.selected_slot);
const held = EverythingRegistry.get<ItemRegistry>("items", held_slot.type_id ?? "");
const placeable = held?.block_id !== undefined && ty >= 0 && ty < CHUNK_HEIGHT &&
can_place(
EverythingRegistry.get<BlockRegistry>("blocks", held.block_id),
this.world.get_block_info(tx, ty, tz),
this.world.get_block_value(tx, ty, tz),
this.world.get_block_id(tx, ty - 1, tz),
);
if (!placeable || !held?.block_id) {
@@ -1032,15 +1055,21 @@ export class GameServer {
}
// solid blocks can't go where a mob or player is, the one placing it included, like minecraft
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id)!;
const value = this.mods.rules.placement_value(placing, player.yaw, {
x: tx,
y: ty,
z: tz,
face,
hit: [hit[0], hit[1], hit[2]],
});
const state = value >>> 16;
if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, value))) {
// put into a source of the liquid it can hold, it holds it
const placed_value = waterlog_placement(
this.mods.rules.placement_value(placing, player.yaw, {
x: tx,
y: ty,
z: tz,
face,
hit: [hit[0], hit[1], hit[2]],
}),
placing,
this.world.get_block_value(tx, ty, tz),
this.world.get_block_info(tx, ty, tz),
);
const state = placed_value >>> 16;
if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, placed_value))) {
this.#correct(player, tx, ty, tz);
return;
}
@@ -1064,6 +1093,35 @@ export class GameServer {
this.mods.after.block_place.emit(place);
}
// the held liquid poured into the block at x y z, which can hold it. it's placing the liquid as far as mods can tell,
// so a bucket empties like it does placing water anywhere else
#waterlog(player: ServerPlayer, x: number, y: number, z: number, face: Faces, hit: number[]) {
const held_slot = player.inventory.get_slot(player.selected_slot);
const player_api = this.mods.player(player);
const place = {
player: player_api,
block: { id: this.world.get_block_info(x, y, z)!.waterloggable!, x, y, z },
face,
hit: [hit[0], hit[1], hit[2]] as const,
item: player_api.held_item,
};
const before_place = { ...place, cancel: false };
this.mods.before.block_place.emit(before_place);
if (before_place.cancel) {
this.#correct(player, x, y, z);
return;
}
const info = this.world.get_block_info(x, y, z)!;
this.set_block_state(
x,
y,
z,
set_state_value(this.world.get_block_value(x, y, z), info, "waterlogged", 1)! >>> 16,
);
held_slot.amount = held_slot.amount! - 1;
this.mods.after.block_place.emit(place);
}
// whether a mob's or player's collision box overlaps a block with that shape at x, y, z
#occupied(x: number, y: number, z: number, shape: Shape) {
return this.players().some((player) =>
@@ -1614,9 +1672,11 @@ export class GameServer {
return false;
}
// in a block entities pass through and players can place into, like water
// in liquid, a waterlogged block's too
#in_liquid(x: number, y: number, z: number) {
return is_liquid_block(this.world.get_block_info(Math.floor(x), Math.floor(y), Math.floor(z)));
const [bx, by, bz] = [Math.floor(x), Math.floor(y), Math.floor(z)];
return liquid_block(this.world.get_block_value(bx, by, bz), this.world.get_block_info(bx, by, bz)) !==
undefined;
}
// right click with food: it's eaten if they're hungry, or it's always edible