Data on /give
This commit is contained in:
@@ -333,6 +333,20 @@ export function greedy_string(): ArgumentType<string> {
|
||||
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<unknown> {
|
||||
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<T extends string>(options: readonly T[]): ArgumentType<T> {
|
||||
return {
|
||||
@@ -376,3 +390,7 @@ export function item(): ArgumentType<string> {
|
||||
export function block(): ArgumentType<string> {
|
||||
return registry_id("blocks", "block");
|
||||
}
|
||||
|
||||
export function fluid(): ArgumentType<string> {
|
||||
return registry_id("fluids", "fluid");
|
||||
}
|
||||
|
||||
@@ -176,10 +176,13 @@ export interface CommandRegistry {
|
||||
boolean(): ArgumentType<boolean, CommandSource>;
|
||||
word(): ArgumentType<string, CommandSource>;
|
||||
greedy_string(): ArgumentType<string, CommandSource>;
|
||||
// a json value, the rest of the line like greedy_string
|
||||
json(): ArgumentType<unknown, CommandSource>;
|
||||
choice<T extends string>(options: readonly T[]): ArgumentType<T, CommandSource>;
|
||||
// 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<Id, CommandSource>;
|
||||
block(): ArgumentType<Id, CommandSource>;
|
||||
fluid(): ArgumentType<Id, CommandSource>;
|
||||
// an online player's name
|
||||
player(): ArgumentType<Player, CommandSource>;
|
||||
};
|
||||
|
||||
@@ -62,13 +62,13 @@ server reloads the page, so no old mod code stays loaded.
|
||||
|
||||
### Useful Commands In Game
|
||||
|
||||
| Command | What it does |
|
||||
| ---------------------- | ------------------------------------------------------- |
|
||||
| `/give <item> [count]` | Gives you an item |
|
||||
| `/summon <entity>` | Spawns an entity where you stand |
|
||||
| `/time set <when>` | Jumps to `day`, `noon`, `sunset`, `night` or `midnight` |
|
||||
| `/time add <ticks>` | Moves time forward, running every morning it passes |
|
||||
| `/help` | Lists every command |
|
||||
| Command | What it does |
|
||||
| ---------------------- | ------------------------------------------------------------------------------------ |
|
||||
| `/give <item> [count]` | Gives you an item, see [Commands](../misc/commands.md#the-engines-commands) for data |
|
||||
| `/summon <entity>` | Spawns an entity where you stand |
|
||||
| `/time set <when>` | Jumps to `day`, `noon`, `sunset`, `night` or `midnight` |
|
||||
| `/time add <ticks>` | 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.
|
||||
|
||||
|
||||
@@ -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 <item> [count]` | Gives you an item |
|
||||
| `/give <item> [count] [data]` | Gives you an item, with `data` as JSON |
|
||||
| `/give <item> <count> fluid <fluid> [data]` | Gives you a fluid container full of the fluid, with the fluid's data |
|
||||
| `/effect <effect> [seconds]` | Gives you a status effect, for 30 seconds by default |
|
||||
| `/summon <entity>` | 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"}
|
||||
```
|
||||
|
||||
@@ -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<ServerCommandSource>) => this.mods.register_command("engine", node);
|
||||
|
||||
// TODO: only let operators do this
|
||||
const give = (context: CommandContext<ServerCommandSource>, 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<ServerCommandSource>, count: number, data?: unknown) => {
|
||||
const id = context.get<string>("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<ServerCommandSource>, data?: unknown) => {
|
||||
const id = context.get<string>("item");
|
||||
if (!EverythingRegistry.get<ItemRegistry>("items", id)?.fluid_container) {
|
||||
throw new CommandError(`${display_name(id)} doesn't hold fluids`);
|
||||
}
|
||||
const fluid = { id: context.get<string>("fluid"), ...(data === undefined ? {} : { data }) };
|
||||
give(context, context.get<number>("count"), { fluid });
|
||||
};
|
||||
register(
|
||||
literal<ServerCommandSource>("give").describe("Gives you an item").then(
|
||||
@@ -1607,7 +1620,23 @@ export class GameServer {
|
||||
.executes((context) => give(context, 1))
|
||||
.then(
|
||||
argument<number, ServerCommandSource>("count", integer(1, MAX_GIVE))
|
||||
.executes((context) => give(context, context.get<number>("count"))),
|
||||
.executes((context) => give(context, context.get<number>("count")))
|
||||
.then(
|
||||
literal<ServerCommandSource>("fluid").then(
|
||||
argument<string, ServerCommandSource>("fluid", fluid())
|
||||
.executes((context) => give_full(context))
|
||||
.then(
|
||||
argument<unknown, ServerCommandSource>("fluid_data", json())
|
||||
.executes((context) => give_full(context, context.get("fluid_data"))),
|
||||
),
|
||||
),
|
||||
)
|
||||
.then(
|
||||
argument<unknown, ServerCommandSource>("data", json())
|
||||
.executes((context) =>
|
||||
give(context, context.get<number>("count"), context.get("data"))
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
|
||||
+33
-1
@@ -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 <item> [<count>]");
|
||||
assertEquals(take(1).find((m) => m.type === "chat").text, "Not enough: /give <item> [<count> [fluid|<data>]]");
|
||||
});
|
||||
|
||||
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 () => {
|
||||
|
||||
Reference in New Issue
Block a user