@minecraftbot-com/plugin 0.0.0-stage → 0.1.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 (128) hide show
  1. package/AGENTS.md +177 -0
  2. package/README.md +45 -2
  3. package/dist/api.d.ts +351 -0
  4. package/dist/api.js +2 -0
  5. package/dist/cli/api.d.ts +26 -0
  6. package/dist/cli/api.js +61 -0
  7. package/dist/cli/build.d.ts +23 -0
  8. package/dist/cli/build.js +65 -0
  9. package/dist/cli/check.d.ts +10 -0
  10. package/dist/cli/check.js +98 -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/dev-mcp.d.ts +36 -0
  16. package/dist/cli/dev-mcp.js +101 -0
  17. package/dist/cli/dev.d.ts +15 -0
  18. package/dist/cli/dev.js +261 -0
  19. package/dist/cli/fetch-guard.d.ts +7 -0
  20. package/dist/cli/fetch-guard.js +66 -0
  21. package/dist/cli/main.d.ts +2 -0
  22. package/dist/cli/main.js +112 -0
  23. package/dist/cli/mcp.d.ts +24 -0
  24. package/dist/cli/mcp.js +136 -0
  25. package/dist/cli/output.d.ts +11 -0
  26. package/dist/cli/output.js +25 -0
  27. package/dist/cli/test.d.ts +2 -0
  28. package/dist/cli/test.js +39 -0
  29. package/dist/errors.d.ts +22 -0
  30. package/dist/errors.js +35 -0
  31. package/dist/generated/block-states.d.ts +4087 -0
  32. package/dist/generated/block-states.js +2 -0
  33. package/dist/generated/blocks.d.ts +1289 -0
  34. package/dist/generated/blocks.js +1291 -0
  35. package/dist/generated/contracts.d.ts +304 -0
  36. package/dist/generated/contracts.js +327 -0
  37. package/dist/generated/effects.d.ts +43 -0
  38. package/dist/generated/effects.js +45 -0
  39. package/dist/generated/entities.d.ts +164 -0
  40. package/dist/generated/entities.js +166 -0
  41. package/dist/generated/items.d.ts +1661 -0
  42. package/dist/generated/items.js +1663 -0
  43. package/dist/generated/operations.d.ts +7 -0
  44. package/dist/generated/operations.js +65 -0
  45. package/dist/generated/permission-labels.d.ts +3 -0
  46. package/dist/generated/permission-labels.js +17 -0
  47. package/dist/generated/version.d.ts +2 -0
  48. package/dist/generated/version.js +3 -0
  49. package/dist/host/index.d.ts +7 -0
  50. package/dist/host/index.js +5 -0
  51. package/dist/index.d.ts +19 -0
  52. package/dist/index.js +13 -0
  53. package/dist/permissions.d.ts +11 -0
  54. package/dist/permissions.js +18 -0
  55. package/dist/plugin.d.ts +86 -0
  56. package/dist/plugin.js +26 -0
  57. package/dist/protocol.d.ts +167 -0
  58. package/dist/protocol.js +4 -0
  59. package/dist/runtime/connection.d.ts +52 -0
  60. package/dist/runtime/connection.js +132 -0
  61. package/dist/runtime/handles.d.ts +95 -0
  62. package/dist/runtime/handles.js +289 -0
  63. package/dist/runtime/memory-transport.d.ts +6 -0
  64. package/dist/runtime/memory-transport.js +39 -0
  65. package/dist/runtime/runtime.d.ts +57 -0
  66. package/dist/runtime/runtime.js +265 -0
  67. package/dist/runtime/scope.d.ts +25 -0
  68. package/dist/runtime/scope.js +94 -0
  69. package/dist/runtime/storage-runtime.d.ts +10 -0
  70. package/dist/runtime/storage-runtime.js +131 -0
  71. package/dist/settings.d.ts +111 -0
  72. package/dist/settings.js +78 -0
  73. package/dist/storage.d.ts +78 -0
  74. package/dist/storage.js +24 -0
  75. package/dist/testing/fake-container.d.ts +175 -0
  76. package/dist/testing/fake-container.js +483 -0
  77. package/dist/testing/index.d.ts +37 -0
  78. package/dist/testing/index.js +54 -0
  79. package/dist/types.d.ts +65 -0
  80. package/dist/types.js +54 -0
  81. package/llms.txt +11 -0
  82. package/package.json +55 -4
  83. package/src/api.ts +334 -0
  84. package/src/cli/api.ts +79 -0
  85. package/src/cli/build.ts +76 -0
  86. package/src/cli/check.ts +103 -0
  87. package/src/cli/config.ts +48 -0
  88. package/src/cli/create.ts +38 -0
  89. package/src/cli/dev-mcp.ts +131 -0
  90. package/src/cli/dev.ts +272 -0
  91. package/src/cli/fetch-guard.ts +61 -0
  92. package/src/cli/main.ts +113 -0
  93. package/src/cli/mcp.ts +168 -0
  94. package/src/cli/output.ts +30 -0
  95. package/src/cli/test.ts +38 -0
  96. package/src/errors.ts +34 -0
  97. package/src/generated/block-states.ts +892 -0
  98. package/src/generated/blocks.ts +1291 -0
  99. package/src/generated/contracts.ts +329 -0
  100. package/src/generated/effects.ts +45 -0
  101. package/src/generated/entities.ts +166 -0
  102. package/src/generated/items.ts +1663 -0
  103. package/src/generated/operations.ts +69 -0
  104. package/src/generated/permission-labels.ts +19 -0
  105. package/src/generated/version.ts +3 -0
  106. package/src/host/index.ts +9 -0
  107. package/src/index.ts +34 -0
  108. package/src/permissions.ts +23 -0
  109. package/src/plugin.ts +105 -0
  110. package/src/protocol.ts +107 -0
  111. package/src/runtime/connection.ts +167 -0
  112. package/src/runtime/handles.ts +337 -0
  113. package/src/runtime/memory-transport.ts +41 -0
  114. package/src/runtime/runtime.ts +281 -0
  115. package/src/runtime/scope.ts +101 -0
  116. package/src/runtime/storage-runtime.ts +138 -0
  117. package/src/settings.ts +118 -0
  118. package/src/storage.ts +82 -0
  119. package/src/testing/fake-container.ts +571 -0
  120. package/src/testing/index.ts +70 -0
  121. package/src/types.ts +88 -0
  122. package/templates/basic/AGENTS.md +31 -0
  123. package/templates/basic/README.md +13 -0
  124. package/templates/basic/gitignore +3 -0
  125. package/templates/basic/package.json +18 -0
  126. package/templates/basic/src/index.ts +20 -0
  127. package/templates/basic/test/plugin.test.ts +14 -0
  128. package/templates/basic/tsconfig.json +14 -0
package/src/types.ts ADDED
@@ -0,0 +1,88 @@
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
+
8
+ export type JsonValue = string | number | boolean | null | JsonValue[] | { [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
+
22
+ /** A Duration in milliseconds (one tick is 50 ms). */
23
+ export function toMilliseconds(d: Duration): number {
24
+ const ms = (d.ticks ?? 0) * 50 + (d.milliseconds ?? 0) + (d.seconds ?? 0) * 1000 + (d.minutes ?? 0) * 60_000;
25
+ if (!Number.isFinite(ms) || ms < 0) throw new TypeError(`Invalid duration ${JSON.stringify(d)}: use e.g. { seconds: 5 }`);
26
+ return ms;
27
+ }
28
+
29
+ /**
30
+ * The outcome of an action that can fail for normal reasons (no path, out of reach). Check `ok`:
31
+ * failures are values, not exceptions.
32
+ *
33
+ * @example
34
+ * const moved = await bot.goto(target, { within: 2 });
35
+ * if (!moved.ok) log.warn(`Can't get there: ${moved.reason}`);
36
+ */
37
+ export type Result<F extends string, T = undefined> =
38
+ | ({ readonly ok: true } & (T extends undefined ? {} : { readonly value: T }))
39
+ | { readonly ok: false; readonly reason: F; readonly message: string };
40
+
41
+ /**
42
+ * A position the SDK hands you. It prints as `x y z` in template strings and has a few helpers.
43
+ *
44
+ * @example
45
+ * log.info(`Going to ${chest}`); // "Going to 10 64 -3"
46
+ * const above = chest.offset(0, 1, 0);
47
+ */
48
+ export class Position implements Vec3 {
49
+ constructor(readonly x: number, readonly y: number, readonly z: number) {}
50
+
51
+ /** A new position moved by the given amounts. */
52
+ offset(dx: number, dy: number, dz: number): Position {
53
+ return new Position(this.x + dx, this.y + dy, this.z + dz);
54
+ }
55
+
56
+ /** Straight-line distance in blocks. */
57
+ distanceTo(other: Vec3): number {
58
+ return Math.hypot(this.x - other.x, this.y - other.y, this.z - other.z);
59
+ }
60
+
61
+ /** The block this position is in (rounded down). */
62
+ floored(): Position {
63
+ return new Position(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
64
+ }
65
+
66
+ equals(other: Vec3): boolean {
67
+ return this.x === other.x && this.y === other.y && this.z === other.z;
68
+ }
69
+
70
+ toString(): string {
71
+ return `${this.x} ${this.y} ${this.z}`;
72
+ }
73
+
74
+ toJSON(): Vec3 {
75
+ return { x: this.x, y: this.y, z: this.z };
76
+ }
77
+ }
78
+
79
+ /** Makes a position: `vec3(10, 64, -3)`. */
80
+ export function vec3(x: number, y: number, z: number): Position {
81
+ return new Position(x, y, z);
82
+ }
83
+
84
+ /** @internal Turns a position from a frame into a Position. */
85
+ export function toPosition(v: unknown): Position {
86
+ const p = v as Vec3;
87
+ return new Position(p.x, p.y, p.z);
88
+ }
@@ -0,0 +1,31 @@
1
+ # AGENTS.md: writing this MineBot plugin
2
+
3
+ This is a MineBot plugin: TypeScript that controls Minecraft bots through `@minecraftbot-com/plugin`. The plugin runs on
4
+ the developer's machine under `minebot dev` and, once uploaded, on MineBot's servers. Read
5
+ `node_modules/@minecraftbot-com/plugin/AGENTS.md` for the whole API; this file is the short version.
6
+
7
+ ## Commands
8
+
9
+ | Command | What it does |
10
+ |---|---|
11
+ | `npm run dev` | Runs the plugin on the user's running profile, restarting on every save. Logs print here. |
12
+ | `npm test` | Runs `test/*.test.ts` against a fake bot (no server). |
13
+ | `npm run check` | Builds the plugin as it would be uploaded and lists the permissions users will be asked for. |
14
+ | `npm run typecheck` | TypeScript errors. Run it after every change. |
15
+
16
+ While `npm run dev` runs, its MCP endpoint (`http://127.0.0.1:7788/mcp`, printed at start) gives you the plugin's
17
+ status and logs, a `reload` that waits for the result, and read-only `query` calls on its bots.
18
+
19
+ ## Rules that save time
20
+
21
+ - Everything comes from one import: `import { definePlugin, Block, Item, BotEvent, Permission, ... } from "@minecraftbot-com/plugin"`.
22
+ - Use the enums, never strings: `Block.Wheat`, `Item.Bread`, `Entity.Zombie`, `BotEvent.Chat`. Typos are compile errors.
23
+ - Declare every permission the code needs in `permissions`. A missing one throws `PermissionDeniedError` whose
24
+ message names the permission to add. Network access is `Permission.network("api.example.com")`.
25
+ - Actions that can fail for normal reasons return `{ ok, reason }`; check `ok` instead of catching:
26
+ `const moved = await bot.goto(pos, { within: 2 }); if (!moved.ok) log.warn(moved.reason);`
27
+ - Durations name their unit: `{ seconds: 5 }`, `{ ticks: 20 }`. Distances are blocks.
28
+ - Use `bot.loop(async () => { ... })` for repeating work; it backs off on errors and stops with the bot.
29
+ - Never `while (true)` without `await`: hosted plugins are cut off after 50 ms of busy CPU.
30
+ - Keep data in `storage` (declared in `definePlugin`), not in global variables, if it must survive a restart.
31
+ - Hosted plugins have about 1.5 MB of memory after start. Avoid large libraries.
@@ -0,0 +1,13 @@
1
+ # __TITLE__
2
+
3
+ A MineBot plugin.
4
+
5
+ ```sh
6
+ npm install
7
+ npx minebot login # once: approve the sign-in on the dashboard
8
+ npm run dev # runs the plugin on your running profile; edit src/index.ts and it restarts
9
+ npm test # tests against a fake bot
10
+ npm run check # what users will be asked to allow, and the bundle size
11
+ ```
12
+
13
+ The API is documented in `node_modules/@minecraftbot-com/plugin/AGENTS.md`.
@@ -0,0 +1,3 @@
1
+ node_modules/
2
+ .minebot/
3
+ dist/
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "__NAME__",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "minebot dev",
8
+ "check": "minebot check",
9
+ "build": "minebot build",
10
+ "test": "minebot test",
11
+ "typecheck": "tsc --noEmit"
12
+ },
13
+ "devDependencies": {
14
+ "@minecraftbot-com/plugin": "^__SDK_VERSION__",
15
+ "@types/node": "^22.20.5",
16
+ "typescript": "^7.0.2"
17
+ }
18
+ }
@@ -0,0 +1,20 @@
1
+ import { BotEvent, definePlugin, Permission, Setting } from "@minecraftbot-com/plugin";
2
+
3
+ // Answers "!hello" in chat. Run it on your own bots with `npm run dev`.
4
+ export default definePlugin({
5
+ name: "__TITLE__",
6
+ permissions: [Permission.ChatRead, Permission.ChatSend],
7
+ settings: {
8
+ greeting: Setting.text({ label: "Greeting", default: "Hello" }),
9
+ },
10
+
11
+ async run(bot, { settings, log }) {
12
+ log.info(`Running on ${bot.name}`);
13
+
14
+ bot.on(BotEvent.Chat, async message => {
15
+ if (message.text.trim() === "!hello") {
16
+ await bot.say(`${settings.greeting}, ${message.sender ?? "everyone"}!`);
17
+ }
18
+ });
19
+ },
20
+ });
@@ -0,0 +1,14 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { testPlugin } from "@minecraftbot-com/plugin/testing";
4
+ import plugin from "../src/index.js";
5
+
6
+ test("answers !hello with the greeting", async () => {
7
+ const run = await testPlugin(plugin, { bots: [{ id: "bot-1", name: "Helper" }], settings: { "*": { greeting: "Hi" } } });
8
+
9
+ run.container.chat("bot-1", "!hello", "Steve");
10
+ await run.until(() => run.container.said.length > 0);
11
+
12
+ assert.equal(run.container.said[0].text, "Hi, Steve!");
13
+ await run.stop();
14
+ });
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "lib": ["ES2023"],
5
+ "module": "NodeNext",
6
+ "moduleResolution": "NodeNext",
7
+ "types": ["node"],
8
+ "strict": true,
9
+ "noEmit": true,
10
+ "skipLibCheck": true,
11
+ "isolatedModules": true
12
+ },
13
+ "include": ["src", "test"]
14
+ }