Coords suggestion for fill and exportschematic

This commit is contained in:
2026-11-17 08:07:01 -03:00
parent eedaa955f6
commit 188b05f598
3 changed files with 56 additions and 4 deletions
+28 -4
View File
@@ -13,7 +13,7 @@ import {
TICK_DELTA,
TICKS_PER_SECOND,
} from "@/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "@/common/everything_registry.ts";
import { BlockRegistry, EverythingRegistry, is_fluid_block, ItemRegistry } from "@/common/everything_registry.ts";
import { FluidFlow } from "./fluids.ts";
import { contained_fluid, Container, ItemData, ItemStack } from "@/common/inventory.ts";
import {
@@ -43,7 +43,7 @@ import {
set_state_value,
waterlog_placement,
} from "@/common/utils.ts";
import { collision_shape_at, type Shape } from "@/common/block_shapes.ts";
import { collision_shape_at, raycast_blocks, type Shape, shape_at } from "@/common/block_shapes.ts";
import { physics } from "@/common/physics.ts";
import { EXHAUSTION } from "./vitals.ts";
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
@@ -1859,14 +1859,21 @@ export class GameServer {
);
// TODO: only let operators do this, it writes files on the server's disk
// whole number arguments one after another, the last one running it or going on to next
// whole number arguments one after another, the last one running it or going on to next. each corner's
// three suggest the block the player is looking at: all of it at its x, what's left at y and z
const coordinates = (
names: string[],
next: CommandFunction<ServerCommandSource> | CommandNode<ServerCommandSource>,
index = 0,
): CommandNode<ServerCommandSource> => {
const [name, ...rest] = names;
const node = argument<number, ServerCommandSource>(name, integer());
if (rest.length) return node.then(coordinates(rest, next));
node.suggests((partial, source) => {
const target = this.#targeted_block(source.server_player);
const text = target?.slice(index % 3).join(" ");
return text?.startsWith(partial) ? [{ text }] : [];
});
if (rest.length) return node.then(coordinates(rest, next, index + 1));
return next instanceof CommandNode ? node.then(next) : node.executes(next);
};
register(
@@ -2529,6 +2536,23 @@ export class GameServer {
this.#next_chunk_player = first + 1;
}
// the block the player's crosshair is on, like their client's pick: the boxes it's drawn with, past fluids, as far
// as they can reach
#targeted_block(player: ServerPlayer): [number, number, number] | undefined {
const { eye_position: eye, look_direction: look } = this.mods.player(player);
const hit = raycast_blocks(
[eye.x, eye.y, eye.z],
[look.x, look.y, look.z],
player.attribute(ATTRIBUTES.block_interaction_range),
(x, y, z) => {
const block = this.world.get_block_info(x, y, z);
if (!block || is_fluid_block(block)) return undefined;
return shape_at(block, this.world.get_block_value(x, y, z));
},
);
return hit && [hit.x, hit.y, hit.z];
}
// from the eyes to the block's middle
#in_reach(player: ServerPlayer, x: number, y: number, z: number) {
const dx = x + 0.5 - player.x;