@minecraftbot-com/plugin 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/AGENTS.md +188 -0
  2. package/README.md +47 -3
  3. package/dist/api.d.ts +354 -0
  4. package/dist/api.js +2 -0
  5. package/dist/cli/api.d.ts +49 -0
  6. package/dist/cli/api.js +69 -0
  7. package/dist/cli/build.d.ts +24 -0
  8. package/dist/cli/build.js +72 -0
  9. package/dist/cli/check.d.ts +17 -0
  10. package/dist/cli/check.js +99 -0
  11. package/dist/cli/config.d.ts +11 -0
  12. package/dist/cli/config.js +38 -0
  13. package/dist/cli/create.d.ts +2 -0
  14. package/dist/cli/create.js +39 -0
  15. package/dist/cli/deploy.d.ts +13 -0
  16. package/dist/cli/deploy.js +51 -0
  17. package/dist/cli/dev-mcp.d.ts +36 -0
  18. package/dist/cli/dev-mcp.js +101 -0
  19. package/dist/cli/dev.d.ts +15 -0
  20. package/dist/cli/dev.js +261 -0
  21. package/dist/cli/fetch-guard.d.ts +7 -0
  22. package/dist/cli/fetch-guard.js +66 -0
  23. package/dist/cli/main.d.ts +2 -0
  24. package/dist/cli/main.js +119 -0
  25. package/dist/cli/mcp.d.ts +24 -0
  26. package/dist/cli/mcp.js +136 -0
  27. package/dist/cli/output.d.ts +11 -0
  28. package/dist/cli/output.js +25 -0
  29. package/dist/cli/test.d.ts +2 -0
  30. package/dist/cli/test.js +39 -0
  31. package/dist/errors.d.ts +22 -0
  32. package/dist/errors.js +35 -0
  33. package/dist/generated/block-states.d.ts +4087 -0
  34. package/dist/generated/block-states.js +2 -0
  35. package/dist/generated/blocks.d.ts +1289 -0
  36. package/dist/generated/blocks.js +1291 -0
  37. package/dist/generated/contracts.d.ts +304 -0
  38. package/dist/generated/contracts.js +327 -0
  39. package/dist/generated/effects.d.ts +43 -0
  40. package/dist/generated/effects.js +45 -0
  41. package/dist/generated/entities.d.ts +164 -0
  42. package/dist/generated/entities.js +166 -0
  43. package/dist/generated/items.d.ts +1661 -0
  44. package/dist/generated/items.js +1663 -0
  45. package/dist/generated/operations.d.ts +7 -0
  46. package/dist/generated/operations.js +65 -0
  47. package/dist/generated/permission-labels.d.ts +3 -0
  48. package/dist/generated/permission-labels.js +17 -0
  49. package/dist/generated/version.d.ts +2 -0
  50. package/dist/generated/version.js +3 -0
  51. package/dist/host/index.d.ts +7 -0
  52. package/dist/host/index.js +5 -0
  53. package/dist/hosted/fetch.d.ts +53 -0
  54. package/dist/hosted/fetch.js +216 -0
  55. package/dist/hosted/index.d.ts +3 -0
  56. package/dist/hosted/index.js +81 -0
  57. package/dist/index.d.ts +19 -0
  58. package/dist/index.js +13 -0
  59. package/dist/permissions.d.ts +11 -0
  60. package/dist/permissions.js +18 -0
  61. package/dist/plugin.d.ts +86 -0
  62. package/dist/plugin.js +26 -0
  63. package/dist/protocol.d.ts +167 -0
  64. package/dist/protocol.js +4 -0
  65. package/dist/runtime/connection.d.ts +52 -0
  66. package/dist/runtime/connection.js +132 -0
  67. package/dist/runtime/handles.d.ts +95 -0
  68. package/dist/runtime/handles.js +289 -0
  69. package/dist/runtime/memory-transport.d.ts +6 -0
  70. package/dist/runtime/memory-transport.js +39 -0
  71. package/dist/runtime/runtime.d.ts +57 -0
  72. package/dist/runtime/runtime.js +265 -0
  73. package/dist/runtime/scope.d.ts +25 -0
  74. package/dist/runtime/scope.js +94 -0
  75. package/dist/runtime/storage-runtime.d.ts +10 -0
  76. package/dist/runtime/storage-runtime.js +131 -0
  77. package/dist/settings.d.ts +111 -0
  78. package/dist/settings.js +78 -0
  79. package/dist/storage.d.ts +78 -0
  80. package/dist/storage.js +24 -0
  81. package/dist/testing/fake-container.d.ts +175 -0
  82. package/dist/testing/fake-container.js +483 -0
  83. package/dist/testing/index.d.ts +37 -0
  84. package/dist/testing/index.js +54 -0
  85. package/dist/types.d.ts +65 -0
  86. package/dist/types.js +54 -0
  87. package/llms.txt +11 -0
  88. package/package.json +60 -4
  89. package/src/api.ts +337 -0
  90. package/src/cli/api.ts +105 -0
  91. package/src/cli/build.ts +83 -0
  92. package/src/cli/check.ts +104 -0
  93. package/src/cli/config.ts +48 -0
  94. package/src/cli/create.ts +38 -0
  95. package/src/cli/deploy.ts +60 -0
  96. package/src/cli/dev-mcp.ts +131 -0
  97. package/src/cli/dev.ts +272 -0
  98. package/src/cli/fetch-guard.ts +61 -0
  99. package/src/cli/main.ts +120 -0
  100. package/src/cli/mcp.ts +168 -0
  101. package/src/cli/output.ts +30 -0
  102. package/src/cli/test.ts +38 -0
  103. package/src/errors.ts +34 -0
  104. package/src/generated/block-states.ts +892 -0
  105. package/src/generated/blocks.ts +1291 -0
  106. package/src/generated/contracts.ts +329 -0
  107. package/src/generated/effects.ts +45 -0
  108. package/src/generated/entities.ts +166 -0
  109. package/src/generated/items.ts +1663 -0
  110. package/src/generated/operations.ts +69 -0
  111. package/src/generated/permission-labels.ts +19 -0
  112. package/src/generated/version.ts +3 -0
  113. package/src/host/index.ts +9 -0
  114. package/src/hosted/fetch.ts +224 -0
  115. package/src/hosted/index.ts +121 -0
  116. package/src/index.ts +34 -0
  117. package/src/permissions.ts +23 -0
  118. package/src/plugin.ts +105 -0
  119. package/src/protocol.ts +107 -0
  120. package/src/runtime/connection.ts +167 -0
  121. package/src/runtime/handles.ts +337 -0
  122. package/src/runtime/memory-transport.ts +41 -0
  123. package/src/runtime/runtime.ts +281 -0
  124. package/src/runtime/scope.ts +101 -0
  125. package/src/runtime/storage-runtime.ts +138 -0
  126. package/src/settings.ts +118 -0
  127. package/src/storage.ts +82 -0
  128. package/src/testing/fake-container.ts +571 -0
  129. package/src/testing/index.ts +70 -0
  130. package/src/types.ts +88 -0
  131. package/templates/basic/AGENTS.md +32 -0
  132. package/templates/basic/README.md +14 -0
  133. package/templates/basic/gitignore +3 -0
  134. package/templates/basic/package.json +19 -0
  135. package/templates/basic/src/index.ts +20 -0
  136. package/templates/basic/test/plugin.test.ts +14 -0
  137. package/templates/basic/tsconfig.json +14 -0
@@ -0,0 +1,37 @@
1
+ import type { DefinedPlugin } from "../plugin.js";
2
+ import { PluginRuntime, type LogLevel } from "../runtime/runtime.js";
3
+ import { FakeContainer, type FakeContainerOptions } from "./fake-container.js";
4
+ export { FakeContainer } from "./fake-container.js";
5
+ export type { FakeBlock, FakeBotOptions, FakeChest, FakeContainerOptions, FakeEntity, FakeItem, FakeRecipe, RecordedCall } from "./fake-container.js";
6
+ export interface TestRun {
7
+ /** The fake bot container: its world, what the plugin said, stored data, and `emit`/`chat` to send events. */
8
+ readonly container: FakeContainer;
9
+ readonly runtime: PluginRuntime;
10
+ /** Lets pending calls, events and timers of 0 ms settle. Call it after triggering something. */
11
+ settle(ms?: number): Promise<void>;
12
+ /** Waits until `condition` holds (checked after each settle), or throws after `timeoutMs`. */
13
+ until(condition: () => boolean, timeoutMs?: number): Promise<void>;
14
+ /** Stops the plugin and closes the fake connection. */
15
+ stop(): Promise<void>;
16
+ }
17
+ /**
18
+ * Starts a plugin against a fake bot. Permissions are checked as on a real bot, so a missing permission
19
+ * fails here too.
20
+ *
21
+ * @example
22
+ * import { test } from "node:test";
23
+ * import assert from "node:assert/strict";
24
+ * import { testPlugin } from "@minecraftbot-com/plugin/testing";
25
+ * import plugin from "../src/index.js";
26
+ *
27
+ * test("answers !ping", async () => {
28
+ * const run = await testPlugin(plugin);
29
+ * run.container.chat("bot-1", "!ping");
30
+ * await run.until(() => run.container.said.length > 0);
31
+ * assert.equal(run.container.said[0].text, "pong");
32
+ * await run.stop();
33
+ * });
34
+ */
35
+ export declare function testPlugin(plugin: DefinedPlugin, options?: FakeContainerOptions & {
36
+ onLog?: (level: LogLevel, message: string) => void;
37
+ }): Promise<TestRun>;
@@ -0,0 +1,54 @@
1
+ // @minecraftbot-com/plugin/testing: run a plugin against a fake bot in unit tests, no server needed.
2
+ import { PluginRuntime } from "../runtime/runtime.js";
3
+ import { memoryTransportPair } from "../runtime/memory-transport.js";
4
+ import { FakeContainer } from "./fake-container.js";
5
+ export { FakeContainer } from "./fake-container.js";
6
+ /**
7
+ * Starts a plugin against a fake bot. Permissions are checked as on a real bot, so a missing permission
8
+ * fails here too.
9
+ *
10
+ * @example
11
+ * import { test } from "node:test";
12
+ * import assert from "node:assert/strict";
13
+ * import { testPlugin } from "@minecraftbot-com/plugin/testing";
14
+ * import plugin from "../src/index.js";
15
+ *
16
+ * test("answers !ping", async () => {
17
+ * const run = await testPlugin(plugin);
18
+ * run.container.chat("bot-1", "!ping");
19
+ * await run.until(() => run.container.said.length > 0);
20
+ * assert.equal(run.container.said[0].text, "pong");
21
+ * await run.stop();
22
+ * });
23
+ */
24
+ export async function testPlugin(plugin, options = {}) {
25
+ const [pluginSide, containerSide] = memoryTransportPair();
26
+ const container = new FakeContainer(containerSide, options);
27
+ const runtime = await PluginRuntime.connect(plugin, pluginSide, "test-token", "0.0.0-test", { onLog: options.onLog ? (l, m) => options.onLog(l, m) : undefined });
28
+ container.startAll();
29
+ const settle = async (ms = 0) => {
30
+ for (let i = 0; i < 20; i++)
31
+ await Promise.resolve();
32
+ await new Promise(r => setTimeout(r, ms));
33
+ for (let i = 0; i < 20; i++)
34
+ await Promise.resolve();
35
+ };
36
+ await settle();
37
+ return {
38
+ container,
39
+ runtime,
40
+ settle,
41
+ async until(condition, timeoutMs = 2000) {
42
+ const deadline = Date.now() + timeoutMs;
43
+ while (!condition()) {
44
+ if (Date.now() > deadline)
45
+ throw new Error(`Condition not met within ${timeoutMs} ms`);
46
+ await settle(5);
47
+ }
48
+ },
49
+ async stop() {
50
+ runtime.close();
51
+ await settle();
52
+ },
53
+ };
54
+ }
@@ -0,0 +1,65 @@
1
+ /** A position or direction in the world. Block positions are whole numbers. */
2
+ export interface Vec3 {
3
+ readonly x: number;
4
+ readonly y: number;
5
+ readonly z: number;
6
+ }
7
+ export type JsonValue = string | number | boolean | null | JsonValue[] | {
8
+ [key: string]: JsonValue;
9
+ };
10
+ /**
11
+ * A length of time. Units are in the name so a number never means two things.
12
+ *
13
+ * @example { seconds: 5 } { ticks: 20 } { minutes: 1, seconds: 30 }
14
+ */
15
+ export interface Duration {
16
+ readonly ticks?: number;
17
+ readonly milliseconds?: number;
18
+ readonly seconds?: number;
19
+ readonly minutes?: number;
20
+ }
21
+ /** A Duration in milliseconds (one tick is 50 ms). */
22
+ export declare function toMilliseconds(d: Duration): number;
23
+ /**
24
+ * The outcome of an action that can fail for normal reasons (no path, out of reach). Check `ok`:
25
+ * failures are values, not exceptions.
26
+ *
27
+ * @example
28
+ * const moved = await bot.goto(target, { within: 2 });
29
+ * if (!moved.ok) log.warn(`Can't get there: ${moved.reason}`);
30
+ */
31
+ export type Result<F extends string, T = undefined> = ({
32
+ readonly ok: true;
33
+ } & (T extends undefined ? {} : {
34
+ readonly value: T;
35
+ })) | {
36
+ readonly ok: false;
37
+ readonly reason: F;
38
+ readonly message: string;
39
+ };
40
+ /**
41
+ * A position the SDK hands you. It prints as `x y z` in template strings and has a few helpers.
42
+ *
43
+ * @example
44
+ * log.info(`Going to ${chest}`); // "Going to 10 64 -3"
45
+ * const above = chest.offset(0, 1, 0);
46
+ */
47
+ export declare class Position implements Vec3 {
48
+ readonly x: number;
49
+ readonly y: number;
50
+ readonly z: number;
51
+ constructor(x: number, y: number, z: number);
52
+ /** A new position moved by the given amounts. */
53
+ offset(dx: number, dy: number, dz: number): Position;
54
+ /** Straight-line distance in blocks. */
55
+ distanceTo(other: Vec3): number;
56
+ /** The block this position is in (rounded down). */
57
+ floored(): Position;
58
+ equals(other: Vec3): boolean;
59
+ toString(): string;
60
+ toJSON(): Vec3;
61
+ }
62
+ /** Makes a position: `vec3(10, 64, -3)`. */
63
+ export declare function vec3(x: number, y: number, z: number): Position;
64
+ /** @internal Turns a position from a frame into a Position. */
65
+ export declare function toPosition(v: unknown): Position;
package/dist/types.js ADDED
@@ -0,0 +1,54 @@
1
+ /** A Duration in milliseconds (one tick is 50 ms). */
2
+ export function toMilliseconds(d) {
3
+ const ms = (d.ticks ?? 0) * 50 + (d.milliseconds ?? 0) + (d.seconds ?? 0) * 1000 + (d.minutes ?? 0) * 60_000;
4
+ if (!Number.isFinite(ms) || ms < 0)
5
+ throw new TypeError(`Invalid duration ${JSON.stringify(d)}: use e.g. { seconds: 5 }`);
6
+ return ms;
7
+ }
8
+ /**
9
+ * A position the SDK hands you. It prints as `x y z` in template strings and has a few helpers.
10
+ *
11
+ * @example
12
+ * log.info(`Going to ${chest}`); // "Going to 10 64 -3"
13
+ * const above = chest.offset(0, 1, 0);
14
+ */
15
+ export class Position {
16
+ x;
17
+ y;
18
+ z;
19
+ constructor(x, y, z) {
20
+ this.x = x;
21
+ this.y = y;
22
+ this.z = z;
23
+ }
24
+ /** A new position moved by the given amounts. */
25
+ offset(dx, dy, dz) {
26
+ return new Position(this.x + dx, this.y + dy, this.z + dz);
27
+ }
28
+ /** Straight-line distance in blocks. */
29
+ distanceTo(other) {
30
+ return Math.hypot(this.x - other.x, this.y - other.y, this.z - other.z);
31
+ }
32
+ /** The block this position is in (rounded down). */
33
+ floored() {
34
+ return new Position(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
35
+ }
36
+ equals(other) {
37
+ return this.x === other.x && this.y === other.y && this.z === other.z;
38
+ }
39
+ toString() {
40
+ return `${this.x} ${this.y} ${this.z}`;
41
+ }
42
+ toJSON() {
43
+ return { x: this.x, y: this.y, z: this.z };
44
+ }
45
+ }
46
+ /** Makes a position: `vec3(10, 64, -3)`. */
47
+ export function vec3(x, y, z) {
48
+ return new Position(x, y, z);
49
+ }
50
+ /** @internal Turns a position from a frame into a Position. */
51
+ export function toPosition(v) {
52
+ const p = v;
53
+ return new Position(p.x, p.y, p.z);
54
+ }
package/llms.txt ADDED
@@ -0,0 +1,11 @@
1
+ # @minecraftbot-com/plugin
2
+
3
+ > TypeScript SDK and CLI for MineBot plugins: programs that control Minecraft bots through an intent-level API
4
+ > (walk, dig, place, chat, inventory) with declared permissions.
5
+
6
+ - [AGENTS.md](./AGENTS.md): the whole API, rules, testing and limits. Read this first.
7
+ - [README.md](./README.md): install and commands.
8
+ - Types: `dist/index.d.ts` (plugin API), `dist/testing/index.d.ts` (fake bot for tests).
9
+ - Enums: `src/generated/blocks.ts`, `items.ts`, `entities.ts`, `effects.ts`, `block-states.ts` (every block's properties), `contracts.ts` (permissions, events, failure reasons, built-in plugins).
10
+ - Template: `templates/basic/` (what `minebot create` writes).
11
+ - Live: `minebot dev` serves MCP at `http://127.0.0.1:7788/mcp` (tools `status`, `logs`, `reload`, `query`).
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
2
  "name": "@minecraftbot-com/plugin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Write MineBot plugins in TypeScript: typed bot API, enums for every block, item and event, and the minebot CLI.",
5
+ "license": "MIT",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "type": "module",
10
+ "engines": {
11
+ "node": ">=22"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "minebot-source": "./src/index.ts",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "./testing": {
20
+ "types": "./dist/testing/index.d.ts",
21
+ "minebot-source": "./src/testing/index.ts",
22
+ "default": "./dist/testing/index.js"
23
+ },
24
+ "./host": {
25
+ "types": "./dist/host/index.d.ts",
26
+ "minebot-source": "./src/host/index.ts",
27
+ "default": "./dist/host/index.js"
28
+ },
29
+ "./hosted": {
30
+ "types": "./dist/hosted/index.d.ts",
31
+ "minebot-source": "./src/hosted/index.ts",
32
+ "default": "./dist/hosted/index.js"
33
+ }
34
+ },
35
+ "bin": {
36
+ "minebot": "dist/cli/main.js"
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "src",
41
+ "!src/**/*.test.ts",
42
+ "!dist/**/*.test.*",
43
+ "templates",
44
+ "AGENTS.md",
45
+ "llms.txt",
46
+ "README.md"
47
+ ],
48
+ "scripts": {
49
+ "build": "tsc -p tsconfig.build.json",
50
+ "check": "tsc -p tsconfig.json --noEmit",
51
+ "test": "tsc -p tsconfig.build.json && node --test \"dist/**/*.test.js\"",
52
+ "pack:check": "npm run build && node scripts/pack-check.mjs",
53
+ "prepublishOnly": "npm run pack:check"
54
+ },
55
+ "dependencies": {
56
+ "esbuild": "^0.28.2"
57
+ },
58
+ "devDependencies": {
59
+ "@types/node": "^22.20.5",
60
+ "typescript": "^7.0.2"
61
+ }
62
+ }
package/src/api.ts ADDED
@@ -0,0 +1,337 @@
1
+ // The bot and profile API plugin code sees. Implemented in runtime/handles.ts on top of protocol calls.
2
+
3
+ import type { Block } from "./generated/blocks.js";
4
+ import type { Item } from "./generated/items.js";
5
+ import type { Entity } from "./generated/entities.js";
6
+ import type { BlockStates } from "./generated/block-states.js";
7
+ import type {
8
+ AttackFailure, BotControlFailure, BotEvent, BuiltInPlugin, ContainerFailure, CraftFailure, DigFailure, EquipmentSlot,
9
+ GameMode, InteractFailure, InventoryFailure, MoveFailure, PlaceFailure, PluginControlFailure, ProfileEvent,
10
+ } from "./generated/contracts.js";
11
+ import type { Duration, Position, Result, Vec3 } from "./types.js";
12
+
13
+ /** The properties a block can have, all optional: what `findBlocks` matches on. */
14
+ export type BlockStateFilter<B> = B extends keyof BlockStates ? Partial<BlockStates[B]> : Record<string, never>;
15
+
16
+ /** A block in the world. */
17
+ export interface BlockInfo {
18
+ readonly position: Position;
19
+ readonly block: Block;
20
+ /** The block's properties, e.g. `{ age: 7 }`. */
21
+ readonly state: Readonly<Record<string, string | number | boolean>>;
22
+ }
23
+
24
+ /** An entity near the bot: a mob, an animal, another player, an item on the ground. */
25
+ export interface EntityInfo {
26
+ /** The id the server gave it; valid while it stays loaded. Pass it to `bot.attack` or `bot.follow`. */
27
+ readonly id: number;
28
+ readonly type: Entity;
29
+ readonly uuid: string | null;
30
+ /** The player name for players, the custom name for named mobs. */
31
+ readonly name: string | null;
32
+ readonly position: Position;
33
+ /** Distance from the bot in blocks. */
34
+ readonly distance: number;
35
+ }
36
+
37
+ /** A player on the server's player list. */
38
+ export interface PlayerInfo {
39
+ readonly name: string;
40
+ readonly uuid: string;
41
+ /** Latency in milliseconds. */
42
+ readonly ping: number;
43
+ readonly gameMode: GameMode | null;
44
+ }
45
+
46
+ /** A stack of items in the bot's inventory or in a container. */
47
+ export interface ItemStack {
48
+ readonly item: Item;
49
+ readonly count: number;
50
+ /**
51
+ * Where the stack is, numbered as Minecraft's commands do (`/item replace entity <bot> container.N`): the hotbar
52
+ * 0-8, the inventory 9-35, armor 100 (feet) to 103 (head), the offhand 98. A container's items: its own slot from 0.
53
+ */
54
+ readonly slot: number;
55
+ /** The custom name, if renamed in an anvil. */
56
+ readonly name: string | null;
57
+ }
58
+
59
+ /** The bot's health and similar. */
60
+ export interface BotStatus {
61
+ /** 0 to 20; 0 means dead. */
62
+ readonly health: number;
63
+ /** 0 to 20. */
64
+ readonly food: number;
65
+ readonly saturation: number;
66
+ readonly alive: boolean;
67
+ readonly gameMode: GameMode | null;
68
+ /** e.g. `"minecraft:overworld"`. */
69
+ readonly dimension: string | null;
70
+ readonly experienceLevel: number;
71
+ }
72
+
73
+ export interface ChatMessage {
74
+ /** The message as plain text, without formatting codes. */
75
+ readonly text: string;
76
+ /** The sending player's name, or null for system messages (server announcements, plugin output). */
77
+ readonly sender: string | null;
78
+ readonly senderUuid: string | null;
79
+ readonly system: boolean;
80
+ }
81
+
82
+ /** The data each `BotEvent` hands its listeners. */
83
+ export interface BotEventData {
84
+ [BotEvent.Chat]: ChatMessage;
85
+ [BotEvent.PlayerJoined]: { readonly player: { readonly name: string; readonly uuid: string } };
86
+ [BotEvent.PlayerLeft]: { readonly player: { readonly name: string; readonly uuid: string } };
87
+ [BotEvent.Died]: Record<string, never>;
88
+ [BotEvent.Respawned]: Record<string, never>;
89
+ [BotEvent.HealthChanged]: { readonly health: number; readonly food: number };
90
+ }
91
+
92
+ /** The data each `ProfileEvent` hands its listeners. */
93
+ export interface ProfileEventData {
94
+ [ProfileEvent.BotConnected]: { readonly bot: ProfileBot };
95
+ [ProfileEvent.BotDisconnected]: { readonly bot: ProfileBot; readonly reason: string };
96
+ }
97
+
98
+ /** A listener; return a promise to have errors logged. */
99
+ export type Listener<T> = (data: T) => unknown;
100
+
101
+ /** Starts and stops other plugins on one bot. Needs `Permission.ProfilePlugins`. */
102
+ export interface BotPlugins {
103
+ /**
104
+ * Starts a plugin instance on this bot, as a macro would. The profile must already have an instance of it
105
+ * (added on the dashboard).
106
+ *
107
+ * @example await bot.plugins.start(BuiltInPlugin.AutoEater);
108
+ */
109
+ start(plugin: BuiltInPlugin): Promise<Result<PluginControlFailure>>;
110
+ stop(plugin: BuiltInPlugin): Promise<Result<PluginControlFailure>>;
111
+ }
112
+
113
+ /**
114
+ * Any bot of the profile, connected or not. Its name and uuid are always available; connecting and
115
+ * disconnecting need `Permission.ProfileBots`.
116
+ */
117
+ export interface ProfileBot {
118
+ /** The bot's id in the profile. */
119
+ readonly id: string;
120
+ /** The Minecraft name. */
121
+ readonly name: string;
122
+ /** The Minecraft account's uuid. */
123
+ readonly uuid: string;
124
+ /** True while it is connected to the server. */
125
+ readonly connected: boolean;
126
+ /** True if this plugin is enabled on it (then `run(bot)` runs while it is connected). */
127
+ readonly enabled: boolean;
128
+
129
+ connect(): Promise<Result<BotControlFailure>>;
130
+ disconnect(): Promise<Result<BotControlFailure>>;
131
+ /** Disconnects and connects again, optionally after a pause: `bot.reconnect({ after: { seconds: 30 } })`. */
132
+ reconnect(options?: { after?: Duration }): Promise<Result<BotControlFailure>>;
133
+
134
+ readonly plugins: BotPlugins;
135
+ }
136
+
137
+ /** The world as the bot sees it. Needs `Permission.WorldRead`. */
138
+ export interface World {
139
+ /** The block at a position, or null if that part of the world isn't loaded. */
140
+ block(position: Vec3): Promise<BlockInfo | null>;
141
+
142
+ /**
143
+ * Blocks near the bot (or near `from`), closest first.
144
+ *
145
+ * @example
146
+ * const ripe = await bot.world.findBlocks({ block: Block.Wheat, state: { age: 7 }, radius: 16, limit: 8 });
147
+ * const logs = await bot.world.findBlocks({ block: [Block.OakLog, Block.BirchLog], radius: 32 });
148
+ */
149
+ findBlocks<B extends Block>(query: {
150
+ block: B | readonly B[];
151
+ state?: BlockStateFilter<B>;
152
+ /** In blocks, at most 64. Default 16. */
153
+ radius?: number;
154
+ /** At most 256. Default 16. */
155
+ limit?: number;
156
+ from?: Vec3;
157
+ }): Promise<BlockInfo[]>;
158
+ }
159
+
160
+ /** Entities and players. Needs `Permission.EntitiesRead`. */
161
+ export interface Entities {
162
+ /**
163
+ * Entities near the bot, closest first.
164
+ *
165
+ * @example const zombies = await bot.entities.nearby({ type: Entity.Zombie, radius: 8 });
166
+ */
167
+ nearby(query?: { type?: Entity | readonly Entity[]; radius?: number; limit?: number }): Promise<EntityInfo[]>;
168
+ /** Everyone on the server's player list. */
169
+ players(): Promise<PlayerInfo[]>;
170
+ }
171
+
172
+ /** The bot's inventory. Reading needs `Permission.InventoryRead`, changing `Permission.InventoryManage`. */
173
+ export interface Inventory {
174
+ items(): Promise<ItemStack[]>;
175
+ /** The item in the main hand, or null. */
176
+ held(): Promise<ItemStack | null>;
177
+ /** How many of an item the bot has in total. */
178
+ count(item: Item): Promise<number>;
179
+ /** Holds (or wears) an item: `await bot.inventory.equip(Item.DiamondSword)`. */
180
+ equip(item: Item, slot?: EquipmentSlot): Promise<Result<InventoryFailure>>;
181
+ /** Drops items on the ground: all of that item, or `count` of them. */
182
+ drop(item: Item, options?: { count?: number }): Promise<Result<InventoryFailure>>;
183
+ }
184
+
185
+ /** A crafting recipe, as `bot.recipes` lists it. */
186
+ export interface Recipe {
187
+ readonly result: Item;
188
+ /** How many one craft makes (4 sticks from 2 planks). */
189
+ readonly count: number;
190
+ /** True if it needs a crafting table's 3x3 grid; false if the inventory's 2x2 grid will do. */
191
+ readonly table: boolean;
192
+ /** What one craft takes: per ingredient, the items that will do and how many. */
193
+ readonly ingredients: readonly { readonly items: readonly Item[]; readonly count: number }[];
194
+ }
195
+
196
+ /**
197
+ * A chest, barrel, shulker box, hopper, furnace, ... the bot has open (from `bot.containers.open`). One container
198
+ * is open per bot at a time; it stays open until `close()`, another one opens, or the plugin stops on the bot.
199
+ */
200
+ export interface Container {
201
+ readonly position: Position;
202
+ readonly block: Block;
203
+ /** How many slots it has. */
204
+ readonly size: number;
205
+ /** What it holds now (`slot` is the container's slot). */
206
+ items(): Promise<ItemStack[]>;
207
+ /** How many of an item it holds. */
208
+ count(item: Item): Promise<number>;
209
+ /**
210
+ * Moves items from the inventory into the container: all of that item, or `count`. `value.moved` says how many went
211
+ * in; fewer than asked means it ran out of room or items.
212
+ *
213
+ * @example
214
+ * const stored = await chest.deposit(Item.Wheat);
215
+ * if (!stored.ok) log.warn(`Couldn't store wheat: ${stored.reason}`);
216
+ */
217
+ deposit(item: Item, options?: { count?: number }): Promise<Result<ContainerFailure, { moved: number }>>;
218
+ /** Moves items from the container into the inventory: all of that item, or `count`. */
219
+ withdraw(item: Item, options?: { count?: number }): Promise<Result<ContainerFailure, { moved: number }>>;
220
+ close(): Promise<void>;
221
+ }
222
+
223
+ /** Containers the bot can open. Needs `Permission.InventoryManage`. */
224
+ export interface Containers {
225
+ /**
226
+ * Opens the container at a position within reach (walk there first).
227
+ *
228
+ * @example
229
+ * await bot.goto(chestPosition, { within: 3 });
230
+ * const opened = await bot.containers.open(chestPosition);
231
+ * if (opened.ok) {
232
+ * await opened.value.deposit(Item.Wheat);
233
+ * await opened.value.close();
234
+ * }
235
+ */
236
+ open(position: Vec3): Promise<Result<ContainerFailure, Container>>;
237
+ }
238
+
239
+ /** One bot the plugin runs on, handed to `run(bot)`. */
240
+ export interface Bot extends ProfileBot {
241
+ readonly world: World;
242
+ readonly entities: Entities;
243
+ readonly inventory: Inventory;
244
+ readonly containers: Containers;
245
+ /** The server's Minecraft version, e.g. `"1.21.4"`. */
246
+ readonly version: string;
247
+
248
+ /**
249
+ * Listens to an event of this bot until the bot stops. Returns a function that stops listening.
250
+ *
251
+ * @example
252
+ * bot.on(BotEvent.Chat, async message => {
253
+ * if (message.text === "!ping") await bot.say("pong");
254
+ * });
255
+ */
256
+ on<E extends BotEvent>(event: E, listener: Listener<BotEventData[E]>): () => void;
257
+
258
+ status(): Promise<BotStatus>;
259
+ /** Where the bot stands. Needs `Permission.WorldRead`. */
260
+ position(): Promise<Position>;
261
+ /** True if the server's version has this block or item (older servers lack newer ones). */
262
+ supports(id: Block | Item | Entity): Promise<boolean>;
263
+
264
+ /**
265
+ * Walks to a position with the bot's pathfinder. Needs `Permission.Move`.
266
+ *
267
+ * @example
268
+ * const moved = await bot.goto(chest, { within: 2 });
269
+ * if (!moved.ok) log.warn(`Can't reach the chest: ${moved.reason}`);
270
+ */
271
+ goto(target: Vec3, options?: { within?: number; timeout?: Duration }): Promise<Result<MoveFailure>>;
272
+ /** Follows an entity until it is lost, `timeout` passes or the plugin stops. Needs `Permission.Move`. */
273
+ follow(entity: EntityInfo | number, options?: { distance?: number; timeout?: Duration }): Promise<Result<MoveFailure>>;
274
+ stopMoving(): Promise<void>;
275
+ /** Turns to look at a position. Needs `Permission.Move`. */
276
+ lookAt(target: Vec3): Promise<void>;
277
+ jump(): Promise<void>;
278
+
279
+ /** Breaks the block at a position (it must be within reach). Needs `Permission.Interact`. */
280
+ dig(position: Vec3): Promise<Result<DigFailure>>;
281
+ /** Places a block from the inventory at a position. Needs `Permission.Interact`. */
282
+ place(position: Vec3, item: Item): Promise<Result<PlaceFailure>>;
283
+ /** Right-clicks a block: opens doors, presses buttons, flips levers. Needs `Permission.Interact`. */
284
+ activateBlock(position: Vec3): Promise<Result<InteractFailure>>;
285
+ /** Uses the held item (eats food, throws a pearl). Needs `Permission.Interact`. */
286
+ useItem(): Promise<Result<InteractFailure>>;
287
+ /** Hits an entity within reach. Needs `Permission.Attack`. */
288
+ attack(entity: EntityInfo | number): Promise<Result<AttackFailure>>;
289
+
290
+ /**
291
+ * Crafts at least `count` of an item (default 1, rounded up to whole crafts) from the inventory. Small recipes use
292
+ * the inventory's 2x2 grid; bigger ones a crafting table within reach (`table`, or the nearest one). `value.crafted`
293
+ * says how many it made. Needs `Permission.InventoryManage`.
294
+ *
295
+ * @example
296
+ * const made = await bot.craft(Item.Bread, { count: 3 });
297
+ * if (!made.ok) log.warn(`No bread: ${made.message}`);
298
+ */
299
+ craft(item: Item, options?: { count?: number; table?: Vec3 }): Promise<Result<CraftFailure, { crafted: number }>>;
300
+ /** The crafting recipes that make an item on this server, with what each takes. */
301
+ recipes(item: Item): Promise<Recipe[]>;
302
+
303
+ /** Sends a chat message. Needs `Permission.ChatSend`; commands go through `command`. */
304
+ say(message: string): Promise<void>;
305
+ /** Runs a server command, with or without the slash: `bot.command("home")`. Needs `Permission.Commands`. */
306
+ command(command: string): Promise<void>;
307
+
308
+ /** Waits. Ends early (with the rest of `run`) when the bot stops. */
309
+ wait(duration: Duration): Promise<void>;
310
+ /**
311
+ * Runs `body` again and again until the bot stops: at most every `every` (default one tick), backing off
312
+ * when it throws (the error is logged), so a mistake can't spin the CPU.
313
+ *
314
+ * @example
315
+ * await bot.loop(async () => {
316
+ * const food = await bot.inventory.count(Item.Bread);
317
+ * if (food < 5) log.warn("Low on bread");
318
+ * }, { every: { seconds: 10 } });
319
+ */
320
+ loop(body: () => unknown, options?: { every?: Duration }): Promise<never>;
321
+ }
322
+
323
+ /** The profile, handed to `runProfile(profile)`. */
324
+ export interface Profile {
325
+ readonly id: string;
326
+ readonly name: string;
327
+ /** Every bot of the profile, enabled or not. */
328
+ bots(): readonly ProfileBot[];
329
+ /** A bot by id, name or uuid. */
330
+ bot(idNameOrUuid: string): ProfileBot | undefined;
331
+ /** Listens to a profile event until the plugin stops. */
332
+ on<E extends ProfileEvent>(event: E, listener: Listener<ProfileEventData[E]>): () => void;
333
+ /** Waits. */
334
+ wait(duration: Duration): Promise<void>;
335
+ /** As `bot.loop`, for the profile. */
336
+ loop(body: () => unknown, options?: { every?: Duration }): Promise<never>;
337
+ }