Don't use names for item suggestions
This commit is contained in:
@@ -834,7 +834,7 @@ is told something went wrong.
|
|||||||
| `word()` | one word | |
|
| `word()` | one word | |
|
||||||
| `greedy_string()` | the rest of the line, spaces and all. Nothing can come after it | |
|
| `greedy_string()` | the rest of the line, spaces and all. Nothing can come after it | |
|
||||||
| `choice([...])` | one of the given words | them |
|
| `choice([...])` | one of the given words | them |
|
||||||
| `item()` and `block()` | an id. `carrot` works too while only one mod has a `carrot` | ids, with the name as tooltip |
|
| `item()` and `block()` | an id. `carrot` works too while only one mod has a `carrot` | ids |
|
||||||
| `player()` | an online player's name, as a `Player` | online players |
|
| `player()` | an online player's name, as a `Player` | online players |
|
||||||
|
|
||||||
The simple form is still there, it's a command taking the rest of the line split on spaces:
|
The simple form is still there, it's a command taking the rest of the line split on spaces:
|
||||||
|
|||||||
+6
-6
@@ -342,7 +342,7 @@ export function choice<T extends string>(options: readonly T[]): ArgumentType<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// an id from a registry. without a namespace it's whichever id has that name, as long as only one does
|
// an id from a registry. without a namespace it's whichever id has that name, as long as only one does
|
||||||
function registry_id(registry: string, what: string, names: (id: string) => string | undefined): ArgumentType<string> {
|
function registry_id(registry: string, what: string): ArgumentType<string> {
|
||||||
return {
|
return {
|
||||||
parse(text) {
|
parse(text) {
|
||||||
if (text.includes(":")) {
|
if (text.includes(":")) {
|
||||||
@@ -361,15 +361,15 @@ function registry_id(registry: string, what: string, names: (id: string) => stri
|
|||||||
return EverythingRegistry.entries(registry)
|
return EverythingRegistry.entries(registry)
|
||||||
.map(([id]) => id)
|
.map(([id]) => id)
|
||||||
.filter((id) => id !== "bworld:air" && (id.startsWith(partial) || id.split(":")[1].startsWith(partial)))
|
.filter((id) => id !== "bworld:air" && (id.startsWith(partial) || id.split(":")[1].startsWith(partial)))
|
||||||
.map((id) => ({ text: id, tooltip: names(id) }));
|
.map((id) => ({ text: id }));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function item(names: (id: string) => string | undefined = () => undefined): ArgumentType<string> {
|
export function item(): ArgumentType<string> {
|
||||||
return registry_id("items", "item", names);
|
return registry_id("items", "item");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function block(names: (id: string) => string | undefined = () => undefined): ArgumentType<string> {
|
export function block(): ArgumentType<string> {
|
||||||
return registry_id("blocks", "block", names);
|
return registry_id("blocks", "block");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import type { WorldgenSetup } from "$/common/generation.ts";
|
|||||||
import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts";
|
import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts";
|
||||||
import { can_place, can_stand_on, display_name, placement_target } from "$/common/utils.ts";
|
import { can_place, can_stand_on, display_name, placement_target } from "$/common/utils.ts";
|
||||||
import { EXHAUSTION, MAX_HEALTH } from "./vitals.ts";
|
import { EXHAUSTION, MAX_HEALTH } from "./vitals.ts";
|
||||||
import { item_argument, ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
||||||
import {
|
import {
|
||||||
argument,
|
argument,
|
||||||
choice,
|
choice,
|
||||||
@@ -36,6 +36,7 @@ import {
|
|||||||
CommandError,
|
CommandError,
|
||||||
type CommandNode,
|
type CommandNode,
|
||||||
integer,
|
integer,
|
||||||
|
item,
|
||||||
literal,
|
literal,
|
||||||
word,
|
word,
|
||||||
} from "$/common/commands.ts";
|
} from "$/common/commands.ts";
|
||||||
@@ -953,7 +954,7 @@ export class GameServer {
|
|||||||
};
|
};
|
||||||
register(
|
register(
|
||||||
literal<ServerCommandSource>("give").describe("Gives you an item").then(
|
literal<ServerCommandSource>("give").describe("Gives you an item").then(
|
||||||
argument<string, ServerCommandSource>("item", item_argument())
|
argument<string, ServerCommandSource>("item", item())
|
||||||
.executes((context) => give(context, 1))
|
.executes((context) => give(context, 1))
|
||||||
.then(
|
.then(
|
||||||
argument<number, ServerCommandSource>("count", integer(1, MAX_GIVE))
|
argument<number, ServerCommandSource>("count", integer(1, MAX_GIVE))
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// see "Server scripts" in MODS.md. parts not built yet throw when used, saying so
|
// see "Server scripts" in MODS.md. parts not built yet throw when used, saying so
|
||||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||||
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
|
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
|
||||||
import { display_name, get_state_value, set_state_value } from "$/common/utils.ts";
|
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||||
import type {
|
import type {
|
||||||
BlockComponent,
|
BlockComponent,
|
||||||
BlockRef,
|
BlockRef,
|
||||||
@@ -375,8 +375,8 @@ export class ModRuntime {
|
|||||||
word,
|
word,
|
||||||
greedy_string,
|
greedy_string,
|
||||||
choice,
|
choice,
|
||||||
item: () => item_argument(),
|
item: () => item(),
|
||||||
block: () => block_argument(display_name_of_block),
|
block: () => block_argument(),
|
||||||
player: () => ({
|
player: () => ({
|
||||||
parse: (text: string) => {
|
parse: (text: string) => {
|
||||||
const found = game().players().find((p) => p.name === text);
|
const found = game().players().find((p) => p.name === text);
|
||||||
@@ -667,14 +667,6 @@ function simple_command(name: string, command: Command): CommandNode<ServerComma
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function item_argument() {
|
|
||||||
return item((id) => display_name(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
function display_name_of_block(id: string) {
|
|
||||||
return display_name(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// the engine's item stacks as mods see them: { id, count, data }
|
// the engine's item stacks as mods see them: { id, count, data }
|
||||||
export function item_api(stack: EngineItemStack): ItemStack {
|
export function item_api(stack: EngineItemStack): ItemStack {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ Deno.test("suggestions are for the word being typed, from words, types and provi
|
|||||||
assertEquals(suggest("say a ").texts, []);
|
assertEquals(suggest("say a ").texts, []);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("/give suggests items as they're typed, with their names", async () => {
|
Deno.test("/give suggests item ids as they're typed", async () => {
|
||||||
const { join, take, send } = await test_game("mods");
|
const { join, take, send } = await test_game("mods");
|
||||||
join(1, "alice");
|
join(1, "alice");
|
||||||
take(1);
|
take(1);
|
||||||
@@ -126,12 +126,8 @@ Deno.test("/give suggests items as they're typed, with their names", async () =>
|
|||||||
const carrots = suggest("/give car");
|
const carrots = suggest("/give car");
|
||||||
assertEquals(carrots.id, 3);
|
assertEquals(carrots.id, 3);
|
||||||
assertEquals(carrots.start, 6);
|
assertEquals(carrots.start, 6);
|
||||||
assert(
|
// just the ids
|
||||||
carrots.suggestions.some((s: { text: string; tooltip: string }) =>
|
assertEquals(carrots.suggestions, [{ text: "bworld:carrot" }]);
|
||||||
s.text === "bworld:carrot" && s.tooltip === "Carrot"
|
|
||||||
),
|
|
||||||
);
|
|
||||||
assert(carrots.suggestions.every((s: { text: string }) => s.text.split(":")[1].startsWith("car")));
|
|
||||||
|
|
||||||
// every item, but not air
|
// every item, but not air
|
||||||
const all = suggest("/give ").suggestions.map((s: { text: string }) => s.text);
|
const all = suggest("/give ").suggestions.map((s: { text: string }) => s.text);
|
||||||
|
|||||||
Reference in New Issue
Block a user