yeow-api 0.2.21 → 0.2.23

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yeow-api",
3
- "version": "0.2.21",
3
+ "version": "0.2.23",
4
4
  "description": "Yeow API �?TypeScript OOP wrappers over the task protocol",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/command.ts CHANGED
@@ -19,10 +19,13 @@ export interface CommandOptions {
19
19
  permission?: string;
20
20
  aliases?: string[];
21
21
  executor: (payload: CommandPayload) => void;
22
+ /** Optional tab completer. Receives (sender, args) and returns completion suggestions. */
23
+ completer?: (sender: CommandSender, args: string[]) => string[];
22
24
  }
23
25
 
24
26
  export function registerCommand(name: string, options: CommandOptions): boolean {
25
27
  const executor = options.executor;
28
+ const completer = options.completer;
26
29
  const pluginName = (globalThis as any).__plugin?.name || 'unknown';
27
30
 
28
31
  const cbId = (globalThis as any)._registerCallback((payload: CommandPayload) => {
@@ -35,10 +38,18 @@ export function registerCommand(name: string, options: CommandOptions): boolean
35
38
  executor(payload);
36
39
  }, { persistent: true });
37
40
 
41
+ let compCbId = '';
42
+ if (completer) {
43
+ compCbId = (globalThis as any)._registerCallback((data: any) => {
44
+ return completer(data.sender, data.args);
45
+ }, { persistent: true });
46
+ }
47
+
38
48
  return call('command.register', {
39
49
  pluginName,
40
50
  commandName: name,
41
51
  callbackId: String(cbId),
52
+ completerCbId: compCbId,
42
53
  description: options.description,
43
54
  usage: options.usage,
44
55
  permission: options.permission,
package/src/index.ts CHANGED
@@ -27,4 +27,4 @@ export { fs, readFile, readFileSync, readFileBase64, readFileBase64Sync, writeFi
27
27
  export { assets, read as assetsRead, readSync as assetsReadSync, readBase64 as assetsReadBase64, readBase64Sync as assetsReadBase64Sync, extract as assetsExtract, extractSync as assetsExtractSync } from './assets.js';
28
28
  export { path } from './path.js';
29
29
  export { listen, respond, close, request, requestSync } from './http.js';
30
- export { createServer } from './create-server.js';
30
+ export { logError } from './log-error.js';
@@ -0,0 +1,16 @@
1
+ export function logError(err: any, context?: string): void {
2
+ if (typeof (globalThis as any).$send !== 'function') return;
3
+ const info: any = {
4
+ message: err?.message || String(err),
5
+ stack: err?.stack || '',
6
+ fileName: err?.fileName || 'main.js',
7
+ lineNumber: err?.lineNumber || 0,
8
+ columnNumber: err?.columnNumber || 0,
9
+ };
10
+ if (context) info.context = context;
11
+ if (!info.fileName || info.fileName === 'main.js' || !info.lineNumber) {
12
+ const m = err?.stack?.match(/at\s+(?:\S+\s+)?\(?([^\s(]+):(\d+):(\d+)\)?/);
13
+ if (m) { info.fileName = m[1]; info.lineNumber = parseInt(m[2]); info.columnNumber = parseInt(m[3]); }
14
+ }
15
+ (globalThis as any).$send('js-error', JSON.stringify(info));
16
+ }
package/src/world.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { call, post } from './task.js';
2
2
  import { Location } from './location.js';
3
+ import { Block } from './block.js';
3
4
 
4
5
  export class World {
5
6
  static get(name: string): World | null { const d = call('world.get', { name }) as any; return d ? new World(d.name) : null; }
@@ -23,7 +24,7 @@ export class World {
23
24
  setGameRule(rule: string, value: string): Promise<boolean> { return post('world.setGameRule', { world: this.name, rule, value }) as Promise<boolean>; }
24
25
  setGameRuleSync(rule: string, value: string): boolean { return call('world.setGameRule', { world: this.name, rule, value }) as boolean; }
25
26
  getBiome(x: number, y: number, z: number): string { return call('world.getBiome', { world: this.name, x, y, z }) as string; }
26
- getBlock(x: number, y: number, z: number): any { return call('world.getBlock', { world: this.name, x, y, z }); }
27
+ getBlock(x: number, y: number, z: number): Block | null { const r = call('world.getBlock', { world: this.name, x, y, z }) as any; return r ? new Block(this.name, r.x, r.y, r.z, r.type) : null; }
27
28
  setBlock(x: number, y: number, z: number, blockType: string): Promise<void> { return post('world.setBlock', { world: this.name, x, y, z, blockType }) as Promise<void>; }
28
29
  setBlockSync(x: number, y: number, z: number, blockType: string) { call('world.setBlock', { world: this.name, x, y, z, blockType }); }
29
30
  getEntities(): string[] { return call('world.getEntities', { world: this.name }) as string[]; }
@@ -1,29 +0,0 @@
1
- import { listen, respond, close } from './http.js';
2
-
3
- export async function createServer(port?: number) {
4
- const routes: Record<string, Record<string, (req: any) => any>> = {};
5
- const srv = await listen(async (req: any) => {
6
- const method = (req.method || 'GET').toUpperCase();
7
- const handler = routes[method]?.[req.path];
8
- if (handler) {
9
- const result = await handler(req);
10
- if (result !== undefined) {
11
- const opts = typeof result === 'string' ? { body: result } : result;
12
- await respond(req.serverId, req.connId, opts);
13
- return;
14
- }
15
- } else {
16
- await respond(req.serverId, req.connId, { status: 404, body: 'Not Found' });
17
- }
18
- }, port);
19
-
20
- const api = {
21
- port: srv.port,
22
- get(path: string, handler: (req: any) => any) { (routes['GET'] ??= {})[path] = handler; return api; },
23
- post(path: string, handler: (req: any) => any) { (routes['POST'] ??= {})[path] = handler; return api; },
24
- put(path: string, handler: (req: any) => any) { (routes['PUT'] ??= {})[path] = handler; return api; },
25
- del(path: string, handler: (req: any) => any) { (routes['DELETE'] ??= {})[path] = handler; return api; },
26
- close() { close(srv.serverId); },
27
- };
28
- return api;
29
- }