diff --git a/common/commands.ts b/common/commands.ts index d7d44af..1e128d0 100644 --- a/common/commands.ts +++ b/common/commands.ts @@ -333,6 +333,20 @@ export function greedy_string(): ArgumentType { return { greedy: true, parse: (text) => text }; } +// a json value, the rest of the line so it can have spaces, like an item's data +export function json(): ArgumentType { + return { + greedy: true, + parse(text) { + try { + return JSON.parse(text); + } catch { + throw new CommandError(`${text} isn't JSON`); + } + }, + }; +} + // one of a few words, suggested export function choice(options: readonly T[]): ArgumentType { return { @@ -376,3 +390,7 @@ export function item(): ArgumentType { export function block(): ArgumentType { return registry_id("blocks", "block"); } + +export function fluid(): ArgumentType { + return registry_id("fluids", "fluid"); +} diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 3947d0a..8d57a67 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -176,10 +176,13 @@ export interface CommandRegistry { boolean(): ArgumentType; word(): ArgumentType; greedy_string(): ArgumentType; + // a json value, the rest of the line like greedy_string + json(): ArgumentType; choice(options: readonly T[]): ArgumentType; - // an item or block id. without a namespace, whichever has that name + // an item, block or fluid id. without a namespace, whichever has that name item(): ArgumentType; block(): ArgumentType; + fluid(): ArgumentType; // an online player's name player(): ArgumentType; }; diff --git a/docs/modding/gettingstarted/index.md b/docs/modding/gettingstarted/index.md index b973fbe..cfe5b3e 100644 --- a/docs/modding/gettingstarted/index.md +++ b/docs/modding/gettingstarted/index.md @@ -62,13 +62,13 @@ server reloads the page, so no old mod code stays loaded. ### Useful Commands In Game -| Command | What it does | -| ---------------------- | ------------------------------------------------------- | -| `/give [count]` | Gives you an item | -| `/summon ` | Spawns an entity where you stand | -| `/time set ` | Jumps to `day`, `noon`, `sunset`, `night` or `midnight` | -| `/time add ` | Moves time forward, running every morning it passes | -| `/help` | Lists every command | +| Command | What it does | +| ---------------------- | ------------------------------------------------------------------------------------ | +| `/give [count]` | Gives you an item, see [Commands](../misc/commands.md#the-engines-commands) for data | +| `/summon ` | Spawns an entity where you stand | +| `/time set ` | Jumps to `day`, `noon`, `sunset`, `night` or `midnight` | +| `/time add ` | Moves time forward, running every morning it passes | +| `/help` | Lists every command | See [Commands](../misc/commands.md) for the rest, and for adding your own. diff --git a/docs/modding/misc/commands.md b/docs/modding/misc/commands.md index 87b4205..55f3b45 100644 --- a/docs/modding/misc/commands.md +++ b/docs/modding/misc/commands.md @@ -50,16 +50,17 @@ That's `/heal`, `/heal 5` and `/heal 5 alice`. ## Argument Types -| `types.` | Reads | Suggests | -| --------------------- | --------------------------------------------------------------- | -------------- | -| `integer(min?, max?)` | a whole number | | -| `number(min?, max?)` | any number | | -| `boolean()` | `true` or `false` | both | -| `word()` | one word | | -| `greedy_string()` | the rest of the line, spaces and all. Nothing can come after it | | -| `choice([...])` | one of the given words | them | -| `item()`, `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 | +| `types.` | Reads | Suggests | +| ------------------------------ | --------------------------------------------------------------- | -------------- | +| `integer(min?, max?)` | a whole number | | +| `number(min?, max?)` | any number | | +| `boolean()` | `true` or `false` | both | +| `word()` | one word | | +| `greedy_string()` | the rest of the line, spaces and all. Nothing can come after it | | +| `json()` | a JSON value, the rest of the line like `greedy_string()` | | +| `choice([...])` | one of the given words | them | +| `item()`, `block()`, `fluid()` | an id. `carrot` works too, while only one mod has a `carrot` | ids | +| `player()` | an online player's name, as a `Player` | online players | ## Errors @@ -107,7 +108,8 @@ Commands can only be registered during `setup`. | Command | What it does | | ------------------------------------------------------- | -------------------------------------------------------------------- | -| `/give [count]` | Gives you an item | +| `/give [count] [data]` | Gives you an item, with `data` as JSON | +| `/give fluid [data]` | Gives you a fluid container full of the fluid, with the fluid's data | | `/effect [seconds]` | Gives you a status effect, for 30 seconds by default | | `/summon ` | Spawns an entity where you are | | `/time` | Shows the time | @@ -120,3 +122,11 @@ Commands can only be registered during `setup`. | `/viewdistance [chunks]` | Shows or changes how far the server sends chunks, until it restarts | | `/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 | + +`/give`'s data is the stack's `data`, and with `fluid` it's the fluid's (see +[Fluids](../resources/fluids.md#containers)): + +``` +/give bworld:wood_pickaxe 1 {"damage": 5} +/give bworld:glass_bottle 3 fluid bworld:juice {"item": "bworld:strawberry"} +``` diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 1b2d433..4c700cf 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -16,7 +16,7 @@ import { } from "$/common/constants.ts"; import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; import { FluidFlow } from "./fluids.ts"; -import { Container, ItemData, ItemStack } from "$/common/inventory.ts"; +import { contained_fluid, Container, ItemData, ItemStack } from "$/common/inventory.ts"; import { AIR_ID, ClientMessage, @@ -56,8 +56,10 @@ import { type CommandContext, CommandError, type CommandNode, + fluid, integer, item, + json, literal, number, word, @@ -1596,10 +1598,21 @@ export class GameServer { const register = (node: CommandNode) => this.mods.register_command("engine", node); // TODO: only let operators do this - const give = (context: CommandContext, count: number) => { + // with data, any json, like { "damage": 10 } on a tool, or a fluid container full of a fluid with its data + const give = (context: CommandContext, count: number, data?: unknown) => { const id = context.get("item"); - this.give(context.source.server_player, id, count); - context.source.send_message(`Gave ${count} ${display_name(id)}`); + this.give(context.source.server_player, id, count, data); + const fluid = contained_fluid(id, data); + const of = fluid ? ` of ${this.mods.fluid_rules.name(fluid)}` : ""; + context.source.send_message(`Gave ${count} ${display_name(id)}${of}`); + }; + const give_full = (context: CommandContext, data?: unknown) => { + const id = context.get("item"); + if (!EverythingRegistry.get("items", id)?.fluid_container) { + throw new CommandError(`${display_name(id)} doesn't hold fluids`); + } + const fluid = { id: context.get("fluid"), ...(data === undefined ? {} : { data }) }; + give(context, context.get("count"), { fluid }); }; register( literal("give").describe("Gives you an item").then( @@ -1607,7 +1620,23 @@ export class GameServer { .executes((context) => give(context, 1)) .then( argument("count", integer(1, MAX_GIVE)) - .executes((context) => give(context, context.get("count"))), + .executes((context) => give(context, context.get("count"))) + .then( + literal("fluid").then( + argument("fluid", fluid()) + .executes((context) => give_full(context)) + .then( + argument("fluid_data", json()) + .executes((context) => give_full(context, context.get("fluid_data"))), + ), + ), + ) + .then( + argument("data", json()) + .executes((context) => + give(context, context.get("count"), context.get("data")) + ), + ), ), ), ); diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index fe22036..6c3edd1 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -57,9 +57,11 @@ import { CommandDispatcher, CommandError, CommandNode, + fluid as fluid_argument, greedy_string, integer, item, + json, literal, number, word, @@ -698,9 +700,11 @@ export class ModRuntime { boolean, word, greedy_string, + json, choice, item: () => item(), block: () => block_argument(), + fluid: () => fluid_argument(), player: () => ({ parse: (text: string) => { const found = game().players().find((p) => p.name === text); diff --git a/tests/commands_test.ts b/tests/commands_test.ts index 52b2dce..cab38c8 100644 --- a/tests/commands_test.ts +++ b/tests/commands_test.ts @@ -160,7 +160,39 @@ Deno.test("/give suggests item ids as they're typed", async () => { send(1, { type: "chat", text: "/give air" }); assertEquals(take(1).find((m) => m.type === "chat").text, "There's no item called air"); send(1, { type: "chat", text: "/give" }); - assertEquals(take(1).find((m) => m.type === "chat").text, "Not enough: /give []"); + assertEquals(take(1).find((m) => m.type === "chat").text, "Not enough: /give [ [fluid|]]"); +}); + +Deno.test("/give gives items with data, and fluid containers full of a fluid", async () => { + const { game, join, take, send } = await test_game("mods"); + join(1, "alice"); + take(1); + const player = game.players()[0]; + const give = (text: string) => { + player.inventory.clear(); + send(1, { type: "chat", text }); + return take(1).find((m) => m.type === "chat").text; + }; + const given = () => player.inventory.to_data().filter((item) => item); + + assertEquals(give('/give bworld:wood_pickaxe 1 {"damage": 5}'), "Gave 1 Wood Pickaxe"); + assertEquals(given(), [{ id: "bworld:wood_pickaxe", count: 1, data: { damage: 5 } }]); + assertEquals(give("/give bworld:stone 1 {oops"), "{oops isn't JSON"); + + const juice = { fluid: { id: "bworld:juice", data: { item: "bworld:carrot" } } }; + assertEquals( + give('/give bworld:glass_bottle 3 fluid bworld:juice {"item": "bworld:carrot"}'), + "Gave 3 Glass Bottle of Carrot Juice", + ); + assertEquals(given(), [{ id: "bworld:glass_bottle", count: 3, data: juice }]); + // a full bucket stacks to 1, and the fluid can be without data + assertEquals(give("/give bworld:bucket 2 fluid bworld:juice"), "Gave 2 Bucket of Juice"); + assertEquals(given().map((item) => item!.count), [1, 1]); + assertEquals(give("/give bworld:stone 1 fluid bworld:juice"), "Stone doesn't hold fluids"); + + send(1, { type: "suggest", id: 4, text: "/give bworld:bucket 1 fluid bworld:ju" }); + const suggested = take(1).find((m) => m.type === "suggestions").suggestions; + assertEquals(suggested.map((s: { text: string }) => s.text), ["bworld:juice"]); }); Deno.test("/locatebiome says where the nearest biome of a kind is", async () => {