@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.
- package/AGENTS.md +188 -0
- package/README.md +47 -3
- package/dist/api.d.ts +354 -0
- package/dist/api.js +2 -0
- package/dist/cli/api.d.ts +49 -0
- package/dist/cli/api.js +69 -0
- package/dist/cli/build.d.ts +24 -0
- package/dist/cli/build.js +72 -0
- package/dist/cli/check.d.ts +17 -0
- package/dist/cli/check.js +99 -0
- package/dist/cli/config.d.ts +11 -0
- package/dist/cli/config.js +38 -0
- package/dist/cli/create.d.ts +2 -0
- package/dist/cli/create.js +39 -0
- package/dist/cli/deploy.d.ts +13 -0
- package/dist/cli/deploy.js +51 -0
- package/dist/cli/dev-mcp.d.ts +36 -0
- package/dist/cli/dev-mcp.js +101 -0
- package/dist/cli/dev.d.ts +15 -0
- package/dist/cli/dev.js +261 -0
- package/dist/cli/fetch-guard.d.ts +7 -0
- package/dist/cli/fetch-guard.js +66 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +119 -0
- package/dist/cli/mcp.d.ts +24 -0
- package/dist/cli/mcp.js +136 -0
- package/dist/cli/output.d.ts +11 -0
- package/dist/cli/output.js +25 -0
- package/dist/cli/test.d.ts +2 -0
- package/dist/cli/test.js +39 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +35 -0
- package/dist/generated/block-states.d.ts +4087 -0
- package/dist/generated/block-states.js +2 -0
- package/dist/generated/blocks.d.ts +1289 -0
- package/dist/generated/blocks.js +1291 -0
- package/dist/generated/contracts.d.ts +304 -0
- package/dist/generated/contracts.js +327 -0
- package/dist/generated/effects.d.ts +43 -0
- package/dist/generated/effects.js +45 -0
- package/dist/generated/entities.d.ts +164 -0
- package/dist/generated/entities.js +166 -0
- package/dist/generated/items.d.ts +1661 -0
- package/dist/generated/items.js +1663 -0
- package/dist/generated/operations.d.ts +7 -0
- package/dist/generated/operations.js +65 -0
- package/dist/generated/permission-labels.d.ts +3 -0
- package/dist/generated/permission-labels.js +17 -0
- package/dist/generated/version.d.ts +2 -0
- package/dist/generated/version.js +3 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.js +5 -0
- package/dist/hosted/fetch.d.ts +53 -0
- package/dist/hosted/fetch.js +216 -0
- package/dist/hosted/index.d.ts +3 -0
- package/dist/hosted/index.js +81 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +13 -0
- package/dist/permissions.d.ts +11 -0
- package/dist/permissions.js +18 -0
- package/dist/plugin.d.ts +86 -0
- package/dist/plugin.js +26 -0
- package/dist/protocol.d.ts +167 -0
- package/dist/protocol.js +4 -0
- package/dist/runtime/connection.d.ts +52 -0
- package/dist/runtime/connection.js +132 -0
- package/dist/runtime/handles.d.ts +95 -0
- package/dist/runtime/handles.js +289 -0
- package/dist/runtime/memory-transport.d.ts +6 -0
- package/dist/runtime/memory-transport.js +39 -0
- package/dist/runtime/runtime.d.ts +57 -0
- package/dist/runtime/runtime.js +265 -0
- package/dist/runtime/scope.d.ts +25 -0
- package/dist/runtime/scope.js +94 -0
- package/dist/runtime/storage-runtime.d.ts +10 -0
- package/dist/runtime/storage-runtime.js +131 -0
- package/dist/settings.d.ts +111 -0
- package/dist/settings.js +78 -0
- package/dist/storage.d.ts +78 -0
- package/dist/storage.js +24 -0
- package/dist/testing/fake-container.d.ts +175 -0
- package/dist/testing/fake-container.js +483 -0
- package/dist/testing/index.d.ts +37 -0
- package/dist/testing/index.js +54 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.js +54 -0
- package/llms.txt +11 -0
- package/package.json +60 -4
- package/src/api.ts +337 -0
- package/src/cli/api.ts +105 -0
- package/src/cli/build.ts +83 -0
- package/src/cli/check.ts +104 -0
- package/src/cli/config.ts +48 -0
- package/src/cli/create.ts +38 -0
- package/src/cli/deploy.ts +60 -0
- package/src/cli/dev-mcp.ts +131 -0
- package/src/cli/dev.ts +272 -0
- package/src/cli/fetch-guard.ts +61 -0
- package/src/cli/main.ts +120 -0
- package/src/cli/mcp.ts +168 -0
- package/src/cli/output.ts +30 -0
- package/src/cli/test.ts +38 -0
- package/src/errors.ts +34 -0
- package/src/generated/block-states.ts +892 -0
- package/src/generated/blocks.ts +1291 -0
- package/src/generated/contracts.ts +329 -0
- package/src/generated/effects.ts +45 -0
- package/src/generated/entities.ts +166 -0
- package/src/generated/items.ts +1663 -0
- package/src/generated/operations.ts +69 -0
- package/src/generated/permission-labels.ts +19 -0
- package/src/generated/version.ts +3 -0
- package/src/host/index.ts +9 -0
- package/src/hosted/fetch.ts +224 -0
- package/src/hosted/index.ts +121 -0
- package/src/index.ts +34 -0
- package/src/permissions.ts +23 -0
- package/src/plugin.ts +105 -0
- package/src/protocol.ts +107 -0
- package/src/runtime/connection.ts +167 -0
- package/src/runtime/handles.ts +337 -0
- package/src/runtime/memory-transport.ts +41 -0
- package/src/runtime/runtime.ts +281 -0
- package/src/runtime/scope.ts +101 -0
- package/src/runtime/storage-runtime.ts +138 -0
- package/src/settings.ts +118 -0
- package/src/storage.ts +82 -0
- package/src/testing/fake-container.ts +571 -0
- package/src/testing/index.ts +70 -0
- package/src/types.ts +88 -0
- package/templates/basic/AGENTS.md +32 -0
- package/templates/basic/README.md +14 -0
- package/templates/basic/gitignore +3 -0
- package/templates/basic/package.json +19 -0
- package/templates/basic/src/index.ts +20 -0
- package/templates/basic/test/plugin.test.ts +14 -0
- package/templates/basic/tsconfig.json +14 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { BotEvent, ProfileEvent } from "../generated/contracts.js";
|
|
2
|
+
import type { BotInfo, Manifest } from "../protocol.js";
|
|
3
|
+
import type { Transport } from "../runtime/connection.js";
|
|
4
|
+
import type { Vec3 } from "../types.js";
|
|
5
|
+
/** A block in the fake world. */
|
|
6
|
+
export interface FakeBlock {
|
|
7
|
+
position: Vec3;
|
|
8
|
+
block: string;
|
|
9
|
+
state?: Record<string, string | number | boolean>;
|
|
10
|
+
}
|
|
11
|
+
export interface FakeEntity {
|
|
12
|
+
id: number;
|
|
13
|
+
type: string;
|
|
14
|
+
position: Vec3;
|
|
15
|
+
uuid?: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface FakeItem {
|
|
19
|
+
item: string;
|
|
20
|
+
count: number;
|
|
21
|
+
slot?: number;
|
|
22
|
+
name?: string;
|
|
23
|
+
}
|
|
24
|
+
/** A recipe the fake bot can craft: what one craft takes, what it makes. */
|
|
25
|
+
export interface FakeRecipe {
|
|
26
|
+
result: string;
|
|
27
|
+
/** Made per craft (default 1). */
|
|
28
|
+
count?: number;
|
|
29
|
+
/** Item to how many one craft takes. */
|
|
30
|
+
ingredients: Record<string, number>;
|
|
31
|
+
/** Needs a crafting table (a `minecraft:crafting_table` block within reach). */
|
|
32
|
+
table?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** A chest (or other container) of the fake world; its block is added to `blocks`. */
|
|
35
|
+
export interface FakeChest {
|
|
36
|
+
position: Vec3;
|
|
37
|
+
/** Default `minecraft:chest`. */
|
|
38
|
+
block?: string;
|
|
39
|
+
/** Slots (default 27). */
|
|
40
|
+
size?: number;
|
|
41
|
+
items?: FakeItem[];
|
|
42
|
+
}
|
|
43
|
+
/** One bot of the fake profile. */
|
|
44
|
+
export interface FakeBotOptions {
|
|
45
|
+
id?: string;
|
|
46
|
+
name?: string;
|
|
47
|
+
uuid?: string;
|
|
48
|
+
position?: Vec3;
|
|
49
|
+
health?: number;
|
|
50
|
+
food?: number;
|
|
51
|
+
inventory?: FakeItem[];
|
|
52
|
+
connected?: boolean;
|
|
53
|
+
/** Whether the plugin runs on it (default true). */
|
|
54
|
+
enabled?: boolean;
|
|
55
|
+
version?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface FakeContainerOptions {
|
|
58
|
+
bots?: FakeBotOptions[];
|
|
59
|
+
blocks?: FakeBlock[];
|
|
60
|
+
entities?: FakeEntity[];
|
|
61
|
+
players?: {
|
|
62
|
+
name: string;
|
|
63
|
+
uuid: string;
|
|
64
|
+
ping?: number;
|
|
65
|
+
}[];
|
|
66
|
+
/** Positions `goto` can't reach (NoPath). */
|
|
67
|
+
unreachable?: Vec3[];
|
|
68
|
+
/** Settings per bot id (or "*" for all). Missing keys fall back to the manifest's defaults. */
|
|
69
|
+
settings?: Record<string, Record<string, unknown>>;
|
|
70
|
+
/** Grant only these; default: whatever the manifest declares, as `minebot dev` does. */
|
|
71
|
+
granted?: string[];
|
|
72
|
+
/** Recipes `bot.craft` and `bot.recipes` know (the real container uses the game's). */
|
|
73
|
+
recipes?: FakeRecipe[];
|
|
74
|
+
containers?: FakeChest[];
|
|
75
|
+
profile?: {
|
|
76
|
+
id: string;
|
|
77
|
+
name: string;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export interface RecordedCall {
|
|
81
|
+
bot: string | null;
|
|
82
|
+
op: string;
|
|
83
|
+
args: Record<string, unknown>;
|
|
84
|
+
}
|
|
85
|
+
interface BotState {
|
|
86
|
+
info: BotInfo;
|
|
87
|
+
position: Vec3;
|
|
88
|
+
health: number;
|
|
89
|
+
food: number;
|
|
90
|
+
inventory: FakeItem[];
|
|
91
|
+
version: string;
|
|
92
|
+
/** The container open on this bot (a key of `chests`). */
|
|
93
|
+
open: string | null;
|
|
94
|
+
}
|
|
95
|
+
interface ChestState {
|
|
96
|
+
position: Vec3;
|
|
97
|
+
block: string;
|
|
98
|
+
size: number;
|
|
99
|
+
items: FakeItem[];
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* An in-memory bot container for tests: a tiny world, inventories, chat, storage, and the same permission
|
|
103
|
+
* checks as the real one. It speaks the real protocol over a Transport, so the SDK is exercised end to end.
|
|
104
|
+
*/
|
|
105
|
+
export declare class FakeContainer {
|
|
106
|
+
private readonly transport;
|
|
107
|
+
private readonly options;
|
|
108
|
+
readonly calls: RecordedCall[];
|
|
109
|
+
readonly said: {
|
|
110
|
+
bot: string;
|
|
111
|
+
text: string;
|
|
112
|
+
}[];
|
|
113
|
+
readonly commands: {
|
|
114
|
+
bot: string;
|
|
115
|
+
command: string;
|
|
116
|
+
}[];
|
|
117
|
+
readonly logs: {
|
|
118
|
+
bot: string | null;
|
|
119
|
+
level: string;
|
|
120
|
+
message: string;
|
|
121
|
+
}[];
|
|
122
|
+
readonly warnings: Map<string, string>;
|
|
123
|
+
readonly stats: Map<string, number>;
|
|
124
|
+
readonly errors: {
|
|
125
|
+
op: string;
|
|
126
|
+
code: string;
|
|
127
|
+
message: string;
|
|
128
|
+
}[];
|
|
129
|
+
readonly blocks: Map<string, FakeBlock>;
|
|
130
|
+
readonly storage: Map<string, {
|
|
131
|
+
value: unknown;
|
|
132
|
+
version: number;
|
|
133
|
+
}>;
|
|
134
|
+
manifest: Manifest | null;
|
|
135
|
+
private readonly bots;
|
|
136
|
+
private readonly subscriptions;
|
|
137
|
+
private readonly running;
|
|
138
|
+
private granted;
|
|
139
|
+
private entities;
|
|
140
|
+
private readonly players;
|
|
141
|
+
private readonly unreachable;
|
|
142
|
+
private readonly pendingFollows;
|
|
143
|
+
/** The fake world's containers by position, readable from tests. */
|
|
144
|
+
readonly chests: Map<string, ChestState>;
|
|
145
|
+
constructor(transport: Transport, options?: FakeContainerOptions);
|
|
146
|
+
bot(id: string): BotState;
|
|
147
|
+
/** Starts run(bot) on every enabled, connected bot (and runProfile), as the container does after welcome. */
|
|
148
|
+
startAll(): void;
|
|
149
|
+
startBot(id: string): void;
|
|
150
|
+
stopBot(id: string, reason?: string): void;
|
|
151
|
+
/** Sends a bot event if the plugin subscribed to it. Returns whether it was sent. */
|
|
152
|
+
emit(bot: string, event: BotEvent, data: unknown): boolean;
|
|
153
|
+
/** Sends a profile event (`botId` in data becomes a ProfileBot in the SDK). */
|
|
154
|
+
emitProfile(event: ProfileEvent, data: unknown): boolean;
|
|
155
|
+
/** A chat message from a player (or the server, without sender) to a bot. */
|
|
156
|
+
chat(bot: string, text: string, sender?: string | null): boolean;
|
|
157
|
+
disconnectBot(id: string, reason?: string): void;
|
|
158
|
+
setEntities(entities: FakeEntity[]): void;
|
|
159
|
+
close(): void;
|
|
160
|
+
private settingsFor;
|
|
161
|
+
private send;
|
|
162
|
+
private receive;
|
|
163
|
+
private hello;
|
|
164
|
+
private fail;
|
|
165
|
+
private ok;
|
|
166
|
+
private error;
|
|
167
|
+
private call;
|
|
168
|
+
private describe;
|
|
169
|
+
/** Crafts as the real container does: whole crafts, ingredients from the inventory, a table within reach if needed. */
|
|
170
|
+
private craft;
|
|
171
|
+
private storageCall;
|
|
172
|
+
}
|
|
173
|
+
/** Hosts a declared network permission covers, for tests of fetch rules. */
|
|
174
|
+
export declare function declaredHosts(manifest: Manifest): string[];
|
|
175
|
+
export {};
|
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { OPERATIONS, PROFILE_WIDE_OPERATIONS, EVENT_PERMISSIONS } from "../generated/operations.js";
|
|
2
|
+
import { BotEvent, ProfileEvent, StorageScope } from "../generated/contracts.js";
|
|
3
|
+
import { networkHost } from "../permissions.js";
|
|
4
|
+
const STACK = 64;
|
|
5
|
+
const INVENTORY_SLOTS = 36;
|
|
6
|
+
function countOf(items, item) {
|
|
7
|
+
return items.filter(s => s.item === item).reduce((n, s) => n + s.count, 0);
|
|
8
|
+
}
|
|
9
|
+
/** Takes up to `count` of an item out of a slot list; returns how many it took. */
|
|
10
|
+
function takeFrom(items, item, count) {
|
|
11
|
+
let taken = 0;
|
|
12
|
+
for (const s of items) {
|
|
13
|
+
if (s.item !== item || taken >= count)
|
|
14
|
+
continue;
|
|
15
|
+
const n = Math.min(s.count, count - taken);
|
|
16
|
+
s.count -= n;
|
|
17
|
+
taken += n;
|
|
18
|
+
}
|
|
19
|
+
for (let i = items.length - 1; i >= 0; i--)
|
|
20
|
+
if (items[i].count <= 0)
|
|
21
|
+
items.splice(i, 1);
|
|
22
|
+
return taken;
|
|
23
|
+
}
|
|
24
|
+
/** Puts up to `count` of an item into a slot list of `slots` slots, stacking to 64; returns how many fit. */
|
|
25
|
+
function putInto(items, slots, item, count) {
|
|
26
|
+
let put = 0;
|
|
27
|
+
for (const s of items) {
|
|
28
|
+
if (s.item !== item || s.count >= STACK || put >= count)
|
|
29
|
+
continue;
|
|
30
|
+
const n = Math.min(STACK - s.count, count - put);
|
|
31
|
+
s.count += n;
|
|
32
|
+
put += n;
|
|
33
|
+
}
|
|
34
|
+
while (put < count && items.length < slots) {
|
|
35
|
+
const used = new Set(items.map(s => s.slot));
|
|
36
|
+
let slot = 0;
|
|
37
|
+
while (used.has(slot))
|
|
38
|
+
slot++;
|
|
39
|
+
const n = Math.min(STACK, count - put);
|
|
40
|
+
items.push({ item, count: n, slot });
|
|
41
|
+
put += n;
|
|
42
|
+
}
|
|
43
|
+
return put;
|
|
44
|
+
}
|
|
45
|
+
const REACH = 4.5;
|
|
46
|
+
const LIMITS = { callsPerSecond: 50, concurrentCalls: 64, frameBytes: 131072, eventQueue: 256, storageBytes: 1_048_576, storageValueBytes: 65_536 };
|
|
47
|
+
function key(p) {
|
|
48
|
+
return `${p.x},${p.y},${p.z}`;
|
|
49
|
+
}
|
|
50
|
+
function distance(a, b) {
|
|
51
|
+
return Math.hypot(a.x - b.x, a.y - b.y, a.z - b.z);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* An in-memory bot container for tests: a tiny world, inventories, chat, storage, and the same permission
|
|
55
|
+
* checks as the real one. It speaks the real protocol over a Transport, so the SDK is exercised end to end.
|
|
56
|
+
*/
|
|
57
|
+
export class FakeContainer {
|
|
58
|
+
transport;
|
|
59
|
+
options;
|
|
60
|
+
calls = [];
|
|
61
|
+
said = [];
|
|
62
|
+
commands = [];
|
|
63
|
+
logs = [];
|
|
64
|
+
warnings = new Map();
|
|
65
|
+
stats = new Map();
|
|
66
|
+
errors = [];
|
|
67
|
+
blocks = new Map();
|
|
68
|
+
storage = new Map();
|
|
69
|
+
manifest = null;
|
|
70
|
+
bots = new Map();
|
|
71
|
+
subscriptions = new Set();
|
|
72
|
+
running = new Set();
|
|
73
|
+
granted = new Set();
|
|
74
|
+
entities;
|
|
75
|
+
players;
|
|
76
|
+
unreachable;
|
|
77
|
+
pendingFollows = new Map();
|
|
78
|
+
/** The fake world's containers by position, readable from tests. */
|
|
79
|
+
chests = new Map();
|
|
80
|
+
constructor(transport, options = {}) {
|
|
81
|
+
this.transport = transport;
|
|
82
|
+
this.options = options;
|
|
83
|
+
const bots = options.bots?.length ? options.bots : [{}];
|
|
84
|
+
bots.forEach((b, i) => {
|
|
85
|
+
const id = b.id ?? `bot-${i + 1}`;
|
|
86
|
+
this.bots.set(id, {
|
|
87
|
+
info: { id, name: b.name ?? `Bot${i + 1}`, uuid: b.uuid ?? `00000000-0000-0000-0000-${String(i + 1).padStart(12, "0")}`, connected: b.connected ?? true, enabled: b.enabled ?? true },
|
|
88
|
+
position: b.position ?? { x: 0, y: 64, z: 0 },
|
|
89
|
+
health: b.health ?? 20,
|
|
90
|
+
food: b.food ?? 20,
|
|
91
|
+
inventory: (b.inventory ?? []).map((it, slot) => ({ slot, ...it })),
|
|
92
|
+
version: b.version ?? "1.21.4",
|
|
93
|
+
open: null,
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
for (const b of options.blocks ?? [])
|
|
97
|
+
this.blocks.set(key(b.position), { ...b, state: b.state ?? {} });
|
|
98
|
+
for (const c of options.containers ?? []) {
|
|
99
|
+
const block = c.block ?? "minecraft:chest";
|
|
100
|
+
this.chests.set(key(c.position), { position: c.position, block, size: c.size ?? 27, items: (c.items ?? []).map((it, slot) => ({ slot, ...it })) });
|
|
101
|
+
this.blocks.set(key(c.position), { position: c.position, block, state: {} });
|
|
102
|
+
}
|
|
103
|
+
this.entities = [...(options.entities ?? [])];
|
|
104
|
+
this.players = (options.players ?? []).map(p => ({ ping: 20, ...p }));
|
|
105
|
+
this.unreachable = new Set((options.unreachable ?? []).map(key));
|
|
106
|
+
transport.onmessage = text => this.receive(text);
|
|
107
|
+
transport.onclose = () => { };
|
|
108
|
+
}
|
|
109
|
+
bot(id) {
|
|
110
|
+
const b = this.bots.get(id);
|
|
111
|
+
if (!b)
|
|
112
|
+
throw new Error(`No fake bot ${id}`);
|
|
113
|
+
return b;
|
|
114
|
+
}
|
|
115
|
+
/** Starts run(bot) on every enabled, connected bot (and runProfile), as the container does after welcome. */
|
|
116
|
+
startAll() {
|
|
117
|
+
if (this.manifest?.profile)
|
|
118
|
+
this.send({ type: "start", bot: null, settings: this.settingsFor("*") });
|
|
119
|
+
for (const b of this.bots.values())
|
|
120
|
+
if (b.info.enabled && b.info.connected)
|
|
121
|
+
this.startBot(b.info.id);
|
|
122
|
+
}
|
|
123
|
+
startBot(id) {
|
|
124
|
+
this.running.add(id);
|
|
125
|
+
this.send({ type: "start", bot: id, settings: this.settingsFor(id), version: this.bot(id).version });
|
|
126
|
+
}
|
|
127
|
+
stopBot(id, reason = "UserStopped") {
|
|
128
|
+
this.running.delete(id);
|
|
129
|
+
this.send({ type: "stop", bot: id, reason });
|
|
130
|
+
}
|
|
131
|
+
/** Sends a bot event if the plugin subscribed to it. Returns whether it was sent. */
|
|
132
|
+
emit(bot, event, data) {
|
|
133
|
+
if (!this.subscriptions.has(`${bot}|${event}`))
|
|
134
|
+
return false;
|
|
135
|
+
this.send({ type: "event", bot, event, data });
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
/** Sends a profile event (`botId` in data becomes a ProfileBot in the SDK). */
|
|
139
|
+
emitProfile(event, data) {
|
|
140
|
+
if (!this.subscriptions.has(`|${event}`))
|
|
141
|
+
return false;
|
|
142
|
+
this.send({ type: "event", bot: null, event, data });
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
/** A chat message from a player (or the server, without sender) to a bot. */
|
|
146
|
+
chat(bot, text, sender = "Steve") {
|
|
147
|
+
return this.emit(bot, BotEvent.Chat, { text, sender, senderUuid: null, system: sender === null });
|
|
148
|
+
}
|
|
149
|
+
disconnectBot(id, reason = "Kicked") {
|
|
150
|
+
const b = this.bot(id);
|
|
151
|
+
b.info.connected = false;
|
|
152
|
+
if (this.running.has(id))
|
|
153
|
+
this.stopBot(id, "BotDisconnected");
|
|
154
|
+
this.send({ type: "bots", bots: [...this.bots.values()].map(x => x.info) });
|
|
155
|
+
this.emitProfile(ProfileEvent.BotDisconnected, { botId: id, reason });
|
|
156
|
+
}
|
|
157
|
+
setEntities(entities) {
|
|
158
|
+
this.entities = [...entities];
|
|
159
|
+
}
|
|
160
|
+
close() {
|
|
161
|
+
this.transport.close(1000, "Test finished");
|
|
162
|
+
}
|
|
163
|
+
settingsFor(id) {
|
|
164
|
+
return { ...(this.options.settings?.["*"] ?? {}), ...(this.options.settings?.[id] ?? {}) };
|
|
165
|
+
}
|
|
166
|
+
send(frame) {
|
|
167
|
+
this.transport.send(JSON.stringify(frame));
|
|
168
|
+
}
|
|
169
|
+
receive(text) {
|
|
170
|
+
const frame = JSON.parse(text);
|
|
171
|
+
switch (frame.type) {
|
|
172
|
+
case "hello":
|
|
173
|
+
this.hello(frame);
|
|
174
|
+
break;
|
|
175
|
+
case "call":
|
|
176
|
+
this.call(frame);
|
|
177
|
+
break;
|
|
178
|
+
case "cancel": {
|
|
179
|
+
const resolve = this.pendingFollows.get(frame.id);
|
|
180
|
+
this.pendingFollows.delete(frame.id);
|
|
181
|
+
resolve?.();
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
case "subscribe": {
|
|
185
|
+
const event = frame.event;
|
|
186
|
+
const needs = frame.bot === null ? null : EVENT_PERMISSIONS[event];
|
|
187
|
+
if (needs && !this.granted.has(needs)) {
|
|
188
|
+
this.errors.push({ op: `subscribe ${event}`, code: "PermissionDenied", message: `BotEvent.${event} needs Permission.${needs}` });
|
|
189
|
+
this.send({ type: "error", id: null, code: "PermissionDenied", message: `BotEvent.${event} needs Permission.${needs}` });
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
this.subscriptions.add(`${frame.bot ?? ""}|${frame.event}`);
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
case "unsubscribe":
|
|
196
|
+
this.subscriptions.delete(`${frame.bot ?? ""}|${frame.event}`);
|
|
197
|
+
break;
|
|
198
|
+
case "log":
|
|
199
|
+
this.logs.push({ bot: frame.bot, level: frame.level, message: frame.message });
|
|
200
|
+
break;
|
|
201
|
+
case "warning":
|
|
202
|
+
if (frame.message === null)
|
|
203
|
+
this.warnings.delete(`${frame.bot ?? ""}|${frame.key}`);
|
|
204
|
+
else
|
|
205
|
+
this.warnings.set(`${frame.bot ?? ""}|${frame.key}`, frame.message);
|
|
206
|
+
break;
|
|
207
|
+
case "stat":
|
|
208
|
+
this.stats.set(`${frame.bot ?? ""}|${frame.key}`, frame.value);
|
|
209
|
+
break;
|
|
210
|
+
case "done":
|
|
211
|
+
if (frame.bot)
|
|
212
|
+
this.running.delete(frame.bot);
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
hello(frame) {
|
|
217
|
+
this.manifest = frame.manifest;
|
|
218
|
+
this.granted = new Set(this.options.granted ?? frame.manifest.permissions);
|
|
219
|
+
this.send({
|
|
220
|
+
type: "welcome",
|
|
221
|
+
installation: "test-installation",
|
|
222
|
+
mode: "Dev",
|
|
223
|
+
profile: this.options.profile ?? { id: "test-profile", name: "Test profile" },
|
|
224
|
+
bots: [...this.bots.values()].map(b => b.info),
|
|
225
|
+
granted: [...this.granted],
|
|
226
|
+
settings: this.settingsFor("*"),
|
|
227
|
+
limits: LIMITS,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
fail(frame, failure, message) {
|
|
231
|
+
this.send({ type: "result", id: frame.id, ok: false, failure, message });
|
|
232
|
+
}
|
|
233
|
+
ok(frame, value = null) {
|
|
234
|
+
this.send({ type: "result", id: frame.id, ok: true, value });
|
|
235
|
+
}
|
|
236
|
+
error(frame, code, message) {
|
|
237
|
+
this.errors.push({ op: frame.op, code, message });
|
|
238
|
+
this.send({ type: "error", id: frame.id, code, message });
|
|
239
|
+
}
|
|
240
|
+
call(frame) {
|
|
241
|
+
this.calls.push({ bot: frame.bot, op: frame.op, args: frame.args });
|
|
242
|
+
if (!(frame.op in OPERATIONS))
|
|
243
|
+
return this.error(frame, "UnknownOperation", `Unknown operation ${frame.op}`);
|
|
244
|
+
const needs = OPERATIONS[frame.op];
|
|
245
|
+
if (needs && !this.granted.has(needs))
|
|
246
|
+
return this.error(frame, "PermissionDenied", `${frame.op} needs Permission.${needs}: add it to permissions in definePlugin`);
|
|
247
|
+
if (frame.bot !== null && !PROFILE_WIDE_OPERATIONS.has(frame.op) && !this.running.has(frame.bot)) {
|
|
248
|
+
return this.error(frame, "BotNotRunning", `The plugin isn't running on bot ${frame.bot}`);
|
|
249
|
+
}
|
|
250
|
+
const a = frame.args;
|
|
251
|
+
const b = frame.bot ? this.bots.get(frame.bot) : undefined;
|
|
252
|
+
if (frame.bot && !b)
|
|
253
|
+
return this.fail(frame, "UnknownBot", `No bot ${frame.bot}`);
|
|
254
|
+
switch (frame.op) {
|
|
255
|
+
case "bot.status":
|
|
256
|
+
return this.ok(frame, { health: b.health, food: b.food, saturation: 5, alive: b.health > 0, gameMode: "Survival", dimension: "minecraft:overworld", experienceLevel: 0 });
|
|
257
|
+
case "bot.position":
|
|
258
|
+
return this.ok(frame, b.position);
|
|
259
|
+
case "bot.supports":
|
|
260
|
+
return this.ok(frame, true);
|
|
261
|
+
case "bot.goto": {
|
|
262
|
+
const target = { x: a.x, y: a.y, z: a.z };
|
|
263
|
+
if (this.unreachable.has(key(target)))
|
|
264
|
+
return this.fail(frame, "NoPath", `No path to ${key(target)}`);
|
|
265
|
+
b.position = target;
|
|
266
|
+
return this.ok(frame);
|
|
267
|
+
}
|
|
268
|
+
case "bot.follow":
|
|
269
|
+
// Follows until cancelled: resolves when the plugin stops or cancels the call.
|
|
270
|
+
this.pendingFollows.set(frame.id, () => { });
|
|
271
|
+
return;
|
|
272
|
+
case "bot.stopMoving":
|
|
273
|
+
case "bot.lookAt":
|
|
274
|
+
case "bot.jump":
|
|
275
|
+
return this.ok(frame);
|
|
276
|
+
case "bot.dig": {
|
|
277
|
+
const at = { x: a.x, y: a.y, z: a.z };
|
|
278
|
+
if (distance(b.position, at) > REACH)
|
|
279
|
+
return this.fail(frame, "OutOfReach", "Too far away");
|
|
280
|
+
if (!this.blocks.delete(key(at)))
|
|
281
|
+
return this.fail(frame, "NothingThere", "No block there");
|
|
282
|
+
return this.ok(frame);
|
|
283
|
+
}
|
|
284
|
+
case "bot.place": {
|
|
285
|
+
const at = { x: a.x, y: a.y, z: a.z };
|
|
286
|
+
if (distance(b.position, at) > REACH)
|
|
287
|
+
return this.fail(frame, "OutOfReach", "Too far away");
|
|
288
|
+
if (this.blocks.has(key(at)))
|
|
289
|
+
return this.fail(frame, "Occupied", "Something is already there");
|
|
290
|
+
const stack = b.inventory.find(s => s.item === a.item && s.count > 0);
|
|
291
|
+
if (!stack)
|
|
292
|
+
return this.fail(frame, "NoItem", `No ${a.item} in the inventory`);
|
|
293
|
+
stack.count--;
|
|
294
|
+
this.blocks.set(key(at), { position: at, block: a.item, state: {} });
|
|
295
|
+
return this.ok(frame);
|
|
296
|
+
}
|
|
297
|
+
case "bot.activateBlock":
|
|
298
|
+
case "bot.useItem":
|
|
299
|
+
return this.ok(frame);
|
|
300
|
+
case "bot.attack": {
|
|
301
|
+
const target = this.entities.find(e => e.id === a.entity);
|
|
302
|
+
if (!target)
|
|
303
|
+
return this.fail(frame, "NotFound", "No such entity");
|
|
304
|
+
if (distance(b.position, target.position) > 3)
|
|
305
|
+
return this.fail(frame, "OutOfReach", "Too far away");
|
|
306
|
+
return this.ok(frame);
|
|
307
|
+
}
|
|
308
|
+
case "bot.say":
|
|
309
|
+
this.said.push({ bot: frame.bot, text: a.text });
|
|
310
|
+
return this.ok(frame);
|
|
311
|
+
case "bot.command":
|
|
312
|
+
this.commands.push({ bot: frame.bot, command: a.command });
|
|
313
|
+
return this.ok(frame);
|
|
314
|
+
case "bot.connect":
|
|
315
|
+
if (b.info.connected)
|
|
316
|
+
return this.fail(frame, "AlreadyConnected", "Already connected");
|
|
317
|
+
b.info.connected = true;
|
|
318
|
+
return this.ok(frame);
|
|
319
|
+
case "bot.disconnect":
|
|
320
|
+
if (!b.info.connected)
|
|
321
|
+
return this.fail(frame, "AlreadyDisconnected", "Already disconnected");
|
|
322
|
+
b.info.connected = false;
|
|
323
|
+
return this.ok(frame);
|
|
324
|
+
case "bot.reconnect":
|
|
325
|
+
return this.ok(frame);
|
|
326
|
+
case "world.block": {
|
|
327
|
+
const found = this.blocks.get(key(a));
|
|
328
|
+
return this.ok(frame, found ? { position: found.position, block: found.block, state: found.state ?? {} } : null);
|
|
329
|
+
}
|
|
330
|
+
case "world.findBlocks": {
|
|
331
|
+
const from = a.from ?? b.position;
|
|
332
|
+
const wanted = new Set(a.blocks);
|
|
333
|
+
const matches = [...this.blocks.values()]
|
|
334
|
+
.filter(x => wanted.has(x.block) && distance(from, x.position) <= a.radius)
|
|
335
|
+
.filter(x => Object.entries(a.state ?? {}).every(([k, v]) => String(x.state?.[k]) === String(v)))
|
|
336
|
+
.sort((p, q) => distance(from, p.position) - distance(from, q.position))
|
|
337
|
+
.slice(0, a.limit);
|
|
338
|
+
return this.ok(frame, matches.map(x => ({ position: x.position, block: x.block, state: x.state ?? {} })));
|
|
339
|
+
}
|
|
340
|
+
case "entities.nearby": {
|
|
341
|
+
const types = a.types ? new Set(a.types) : null;
|
|
342
|
+
const list = this.entities
|
|
343
|
+
.map(e => ({ ...e, distance: distance(b.position, e.position) }))
|
|
344
|
+
.filter(e => e.distance <= a.radius && (!types || types.has(e.type)))
|
|
345
|
+
.sort((p, q) => p.distance - q.distance)
|
|
346
|
+
.slice(0, a.limit)
|
|
347
|
+
.map(e => ({ id: e.id, type: e.type, uuid: e.uuid ?? null, name: e.name ?? null, position: e.position, distance: e.distance }));
|
|
348
|
+
return this.ok(frame, list);
|
|
349
|
+
}
|
|
350
|
+
case "entities.players":
|
|
351
|
+
return this.ok(frame, this.players.map(p => ({ ...p, gameMode: "Survival" })));
|
|
352
|
+
case "inventory.items":
|
|
353
|
+
return this.ok(frame, b.inventory.filter(s => s.count > 0).map(s => ({ item: s.item, count: s.count, slot: s.slot ?? 0, name: s.name ?? null })));
|
|
354
|
+
case "inventory.held": {
|
|
355
|
+
const s = b.inventory.find(x => x.count > 0);
|
|
356
|
+
return this.ok(frame, s ? { item: s.item, count: s.count, slot: s.slot ?? 0, name: s.name ?? null } : null);
|
|
357
|
+
}
|
|
358
|
+
case "inventory.equip":
|
|
359
|
+
return b.inventory.some(s => s.item === a.item && s.count > 0) ? this.ok(frame) : this.fail(frame, "NoItem", `No ${a.item}`);
|
|
360
|
+
case "inventory.drop": {
|
|
361
|
+
const s = b.inventory.find(x => x.item === a.item && x.count > 0);
|
|
362
|
+
if (!s)
|
|
363
|
+
return this.fail(frame, "NoItem", `No ${a.item}`);
|
|
364
|
+
s.count = a.count === null ? 0 : Math.max(0, s.count - a.count);
|
|
365
|
+
return this.ok(frame);
|
|
366
|
+
}
|
|
367
|
+
case "bot.recipes":
|
|
368
|
+
return this.ok(frame, (this.options.recipes ?? []).filter(r => r.result === a.item).map(r => ({
|
|
369
|
+
result: r.result, count: r.count ?? 1, table: r.table ?? false,
|
|
370
|
+
ingredients: Object.entries(r.ingredients).map(([item, count]) => ({ items: [item], count })),
|
|
371
|
+
})));
|
|
372
|
+
case "bot.craft":
|
|
373
|
+
return this.craft(frame, b, a);
|
|
374
|
+
case "container.open": {
|
|
375
|
+
const at = { x: a.x, y: a.y, z: a.z };
|
|
376
|
+
const chest = this.chests.get(key(at));
|
|
377
|
+
if (!chest)
|
|
378
|
+
return this.fail(frame, "NotAContainer", `${this.blocks.get(key(at))?.block ?? "Nothing"} at ${key(at)} isn't a container`);
|
|
379
|
+
if (distance(b.position, at) > REACH)
|
|
380
|
+
return this.fail(frame, "OutOfReach", "Too far away");
|
|
381
|
+
b.open = key(at);
|
|
382
|
+
return this.ok(frame, { position: chest.position, block: chest.block, size: chest.size, items: this.describe(chest.items) });
|
|
383
|
+
}
|
|
384
|
+
case "container.items": {
|
|
385
|
+
const chest = b.open ? this.chests.get(b.open) : undefined;
|
|
386
|
+
return chest ? this.ok(frame, this.describe(chest.items)) : this.fail(frame, "Closed", "No container is open");
|
|
387
|
+
}
|
|
388
|
+
case "container.deposit":
|
|
389
|
+
case "container.withdraw": {
|
|
390
|
+
const chest = b.open ? this.chests.get(b.open) : undefined;
|
|
391
|
+
if (!chest)
|
|
392
|
+
return this.fail(frame, "Closed", "No container is open");
|
|
393
|
+
const deposit = frame.op === "container.deposit";
|
|
394
|
+
const [from, to, toSlots] = deposit ? [b.inventory, chest.items, chest.size] : [chest.items, b.inventory, INVENTORY_SLOTS];
|
|
395
|
+
const wanted = Math.min(a.count ?? Infinity, countOf(from, a.item));
|
|
396
|
+
if (wanted === 0)
|
|
397
|
+
return this.fail(frame, "NoItem", `No ${a.item} to move`);
|
|
398
|
+
const moved = putInto(to, toSlots, a.item, wanted);
|
|
399
|
+
takeFrom(from, a.item, moved);
|
|
400
|
+
return moved > 0 ? this.ok(frame, { moved }) : this.fail(frame, "Full", "No room");
|
|
401
|
+
}
|
|
402
|
+
case "container.close":
|
|
403
|
+
b.open = null;
|
|
404
|
+
return this.ok(frame);
|
|
405
|
+
case "plugins.start":
|
|
406
|
+
case "plugins.stop":
|
|
407
|
+
return this.ok(frame);
|
|
408
|
+
case "storage.get":
|
|
409
|
+
case "storage.set":
|
|
410
|
+
case "storage.delete":
|
|
411
|
+
case "storage.entries":
|
|
412
|
+
return this.storageCall(frame, a);
|
|
413
|
+
}
|
|
414
|
+
return this.error(frame, "UnknownOperation", `The fake container doesn't implement ${frame.op}`);
|
|
415
|
+
}
|
|
416
|
+
describe(items) {
|
|
417
|
+
return items.filter(s => s.count > 0).map(s => ({ item: s.item, count: s.count, slot: s.slot ?? 0, name: s.name ?? null }));
|
|
418
|
+
}
|
|
419
|
+
/** Crafts as the real container does: whole crafts, ingredients from the inventory, a table within reach if needed. */
|
|
420
|
+
craft(frame, b, a) {
|
|
421
|
+
const recipe = (this.options.recipes ?? []).find(r => r.result === a.item);
|
|
422
|
+
if (!recipe)
|
|
423
|
+
return this.fail(frame, "NoRecipe", `No crafting recipe makes ${a.item}`);
|
|
424
|
+
const per = recipe.count ?? 1;
|
|
425
|
+
const crafts = Math.ceil((a.count ?? 1) / per);
|
|
426
|
+
const missing = Object.entries(recipe.ingredients).filter(([item, n]) => countOf(b.inventory, item) < n * crafts);
|
|
427
|
+
if (missing.length)
|
|
428
|
+
return this.fail(frame, "MissingIngredients", `Crafting ${a.item} needs ${missing.map(([item, n]) => `${n * crafts} ${item}`).join(", ")}`);
|
|
429
|
+
if (recipe.table) {
|
|
430
|
+
const tables = [...this.blocks.values()].filter(x => x.block === "minecraft:crafting_table" && distance(b.position, x.position) <= REACH);
|
|
431
|
+
const at = a.table ? tables.find(t => key(t.position) === key(a.table)) : tables[0];
|
|
432
|
+
if (!at)
|
|
433
|
+
return this.fail(frame, "NoCraftingTable", `${a.item} needs a crafting table and none is within reach`);
|
|
434
|
+
}
|
|
435
|
+
for (const [item, n] of Object.entries(recipe.ingredients))
|
|
436
|
+
takeFrom(b.inventory, item, n * crafts);
|
|
437
|
+
const made = putInto(b.inventory, INVENTORY_SLOTS, recipe.result, per * crafts);
|
|
438
|
+
if (made === 0)
|
|
439
|
+
return this.fail(frame, "InventoryFull", "No room in the inventory");
|
|
440
|
+
b.open = null;
|
|
441
|
+
return this.ok(frame, { crafted: made });
|
|
442
|
+
}
|
|
443
|
+
storageCall(frame, a) {
|
|
444
|
+
const declared = this.manifest?.storage[a.name];
|
|
445
|
+
if (!declared)
|
|
446
|
+
return this.error(frame, "BadArguments", `storage.${a.name} isn't declared in definePlugin's storage`);
|
|
447
|
+
if (declared.scope === StorageScope.Bot && !a.bot)
|
|
448
|
+
return this.error(frame, "BadArguments", `storage.${a.name} is bot-scoped: use storage.${a.name}.for(bot)`);
|
|
449
|
+
const base = `${a.name}|${declared.scope}|${a.bot ?? ""}`;
|
|
450
|
+
switch (frame.op) {
|
|
451
|
+
case "storage.get": {
|
|
452
|
+
const entry = this.storage.get(`${base}|${a.key ?? ""}`);
|
|
453
|
+
return this.ok(frame, { value: entry?.value ?? null, version: entry?.version ?? 0 });
|
|
454
|
+
}
|
|
455
|
+
case "storage.set": {
|
|
456
|
+
const k = `${base}|${a.key ?? ""}`;
|
|
457
|
+
const entry = this.storage.get(k);
|
|
458
|
+
const version = entry?.version ?? 0;
|
|
459
|
+
if (a.ifVersion !== null && a.ifVersion !== version)
|
|
460
|
+
return this.fail(frame, "Conflict", "Changed since it was read");
|
|
461
|
+
if (JSON.stringify(a.value).length > LIMITS.storageValueBytes)
|
|
462
|
+
return this.error(frame, "ValueTooLarge", "Values are limited to 64 KB");
|
|
463
|
+
this.storage.set(k, { value: JSON.parse(JSON.stringify(a.value)), version: version + 1 });
|
|
464
|
+
return this.ok(frame, { version: version + 1 });
|
|
465
|
+
}
|
|
466
|
+
case "storage.delete":
|
|
467
|
+
this.storage.delete(`${base}|${a.key ?? ""}`);
|
|
468
|
+
return this.ok(frame);
|
|
469
|
+
case "storage.entries": {
|
|
470
|
+
const prefix = `${base}|`;
|
|
471
|
+
const keys = [...this.storage.keys()].filter(k => k.startsWith(prefix) && k !== prefix).map(k => k.slice(prefix.length)).sort();
|
|
472
|
+
const start = a.after ? keys.findIndex(k => k > a.after) : 0;
|
|
473
|
+
const page = start < 0 ? [] : keys.slice(start, start + a.limit);
|
|
474
|
+
const next = start >= 0 && start + a.limit < keys.length ? page[page.length - 1] : null;
|
|
475
|
+
return this.ok(frame, { entries: page.map(k => [k, this.storage.get(prefix + k).value]), next });
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/** Hosts a declared network permission covers, for tests of fetch rules. */
|
|
481
|
+
export function declaredHosts(manifest) {
|
|
482
|
+
return manifest.permissions.map(networkHost).filter((h) => h !== null);
|
|
483
|
+
}
|