Trapdoors

This commit is contained in:
2026-10-03 17:52:58 -03:00
parent 91655c28c5
commit 63e6248f9f
35 changed files with 927 additions and 23 deletions
+45 -5
View File
@@ -850,7 +850,12 @@ export class GameServer {
break;
case "use_block":
if (is_block_position(message.x, message.y, message.z) && faces.includes(message.face)) {
this.#use_block(player, message.x, message.y, message.z, message.face);
// the middle of the block when a client doesn't say, or says something that isn't on it
const hit = Array.isArray(message.hit) && message.hit.length === 3 &&
message.hit.every((v) => typeof v === "number" && v >= 0 && v <= 1)
? message.hit
: [0.5, 0.5, 0.5];
this.#use_block(player, message.x, message.y, message.z, message.face, hit);
}
break;
case "select_slot":
@@ -975,7 +980,7 @@ export class GameServer {
return true;
}
#use_block(player: ServerPlayer, x: number, y: number, z: number, face: Faces) {
#use_block(player: ServerPlayer, x: number, y: number, z: number, face: Faces, hit: number[]) {
const info = this.world.get_block_info(x, y, z);
const target = placement_target(x, y, z, FACE_OFFSETS[face], info);
const [tx, ty, tz] = [target.x, target.y, target.z];
@@ -1033,9 +1038,9 @@ export class GameServer {
return;
}
// solid blocks can't go where a mob or player is, the one placing it included, like minecraft
const state = placed_state(held.block_id, player);
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id);
const value = state === undefined ? default_block_value(0, placing) : block_value(0, state);
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id)!;
const value = this.#placement_value(placing, player, { x: tx, y: ty, z: tz, face, hit });
const state = value >>> 16;
if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, value))) {
this.#correct(player, tx, ty, tz);
return;
@@ -1045,6 +1050,7 @@ export class GameServer {
player: player_api,
block: { id: held.block_id, x: tx, y: ty, z: tz },
face,
hit: [hit[0], hit[1], hit[2]] as const,
item: player_api.held_item,
};
const before_place = { ...place, cancel: false };
@@ -1059,6 +1065,40 @@ export class GameServer {
this.mods.after.block_place.emit(place);
}
// the block's value with the states a player placing it gets: its defaults, facing them when it has a facing,
// then what its components' state_for_placement say, like minecraft's getStateForPlacement
#placement_value(
block: BlockRegistry,
player: ServerPlayer,
where: { x: number; y: number; z: number; face: Faces; hit: number[] },
): number {
const facing = placed_state(block.id, player);
let value = facing === undefined ? default_block_value(0, block) : block_value(0, facing);
const components = this.mods.components_of(block.id).filter(({ component }) => component.state_for_placement);
if (components.length === 0) return value;
const placement = {
...where,
hit: [where.hit[0], where.hit[1], where.hit[2]] as const,
player: this.mods.player(player),
};
for (const { mod, id, component, params } of components) {
const states = run_guarded(
mod,
`${id} state_for_placement`,
() => component.state_for_placement!(placement, params),
);
for (const [name, state] of Object.entries(states ?? {})) {
const known = block.states?.find((s) => s.name === name);
if (known && Number.isInteger(state) && state >= 0 && state < 2 ** known.bits) {
value = set_state_value(value, block, name, state)!;
} else {
console.warn(`${id} state_for_placement: ${block.id} has no state ${name} that fits ${state}`);
}
}
}
return value;
}
// 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) =>