diff --git a/docs/modding/misc/commands.md b/docs/modding/misc/commands.md index a54e99d..504d0e7 100644 --- a/docs/modding/misc/commands.md +++ b/docs/modding/misc/commands.md @@ -125,6 +125,8 @@ Commands can only be registered while the server phase runs. | `/skin [url] [slim\|normal]` | Changes your skin to a 64×64 PNG, or back to the default | | `/help [command]` | Every command's usage, or one's | +`/fill` and `/exportschematic` suggest the block you're looking at for each corner. + `/give`'s data is the stack's `data`, and with `fluid` it's the fluid's (see [Fluids](../resources/fluids.md#containers)): diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 5dd54b2..dfffa48 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -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 | CommandNode, + index = 0, ): CommandNode => { const [name, ...rest] = names; const node = argument(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; diff --git a/tests/commands_test.ts b/tests/commands_test.ts index 682eaae..4040567 100644 --- a/tests/commands_test.ts +++ b/tests/commands_test.ts @@ -241,3 +241,29 @@ Deno.test("/fill replaces every block between two corners, both included", async assertEquals(game.world.get_block_id(50, 120, 50), "cubi:air"); assertEquals(say("/fill 0 0 0 1 1 1 bworld:nothing"), "There's no block bworld:nothing"); }); + +Deno.test("/fill and /exportschematic suggest the block the player is looking at for each corner", async () => { + const { game, join, take, send } = await test_game("mods"); + join(1, "alice"); + for (let z = -6; z <= 1; z++) { + for (let y = 150; y <= 152; y++) game.set_block(0, y, z, "cubi:air"); + } + // looking north, level, at a block 3 away at eye height + send(1, { type: "move", x: 0.5, y: 150, z: 0.5, yaw: 0, pitch: 0 }); + const suggest = (text: string) => { + take(1); + send(1, { type: "suggest", id: 1, text }); + return take(1).find((m) => m.type === "suggestions").suggestions.map((s: { text: string }) => s.text); + }; + assertEquals(suggest("/fill "), []); + + game.set_block(0, 151, -3, "bworld:stone"); + assertEquals(suggest("/fill "), ["0 151 -3"]); + assertEquals(suggest("/fill 0 "), ["151 -3"]); + assertEquals(suggest("/fill 0 151 "), ["-3"]); + assertEquals(suggest("/fill 5"), []); + assertEquals(suggest("/fill 0 151 -3 "), ["0 151 -3"]); + assertEquals(suggest("/fill 0 151 -3 0 151 "), ["-3"]); + assertEquals(suggest("/exportschematic "), ["0 151 -3"]); + assertEquals(suggest("/exportschematic 0 151 -3 0 "), ["151 -3"]); +});