yeow-api 0.1.6 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "yeow-api",
3
- "version": "0.1.6",
4
- "description": "Yeow API �?TypeScript OOP wrappers over the task protocol",
3
+ "version": "0.2.0",
4
+ "description": "Yeow API �?TypeScript OOP wrappers over the task protocol",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "types": "./src/index.ts",
@@ -9,4 +9,4 @@
9
9
  "src/"
10
10
  ],
11
11
  "license": "MIT"
12
- }
12
+ }
package/src/command.ts CHANGED
@@ -1,25 +1,15 @@
1
1
  import { call } from './task.js';
2
2
 
3
3
  export interface CommandSender {
4
- /** Sender display name. */
5
4
  readonly name: string;
6
- /** Player UUID, or "CONSOLE" for console senders. */
7
5
  readonly uuid: string;
8
- /** Whether this sender is a player. */
9
6
  readonly isPlayer: boolean;
10
- /**
11
- * Send a message back to the sender. Injected by the API at callback time.
12
- * MiniMessage format is supported.
13
- */
14
7
  sendMessage(msg: string): void;
15
8
  }
16
9
 
17
10
  export interface CommandPayload {
18
- /** The command sender. Use `Player.get(sender.uuid)` to access full Player properties. */
19
11
  readonly sender: CommandSender;
20
- /** Space-delimited command arguments. */
21
12
  readonly args: string[];
22
- /** The alias used to invoke the command. */
23
13
  readonly label: string;
24
14
  }
25
15
 
@@ -35,7 +25,7 @@ export function registerCommand(name: string, options: CommandOptions): boolean
35
25
  const executor = options.executor;
36
26
  const pluginName = (globalThis as any).__plugin?.name || 'unknown';
37
27
 
38
- const cbId = (globalThis as any)._registerCallback((payload: CommandPayload) => {
28
+ const cbId = (globalThis as any)._regCb((payload: CommandPayload) => {
39
29
  const sender = payload.sender;
40
30
  if (sender?.uuid && sender.isPlayer) {
41
31
  (sender as any).sendMessage = (msg: string) => {
package/src/event.ts CHANGED
@@ -1,78 +1,62 @@
1
1
  import { call } from './task.js';
2
2
 
3
- /**
4
- * Event subscription with cancellation support.
5
- * Sync handlers: e.cancelled = true takes effect immediately (sync task intercepted by EventBridge).
6
- * Async handlers: e.cancelled is @deprecated (no effect after await).
7
- */
8
- const _handlers: Record<string, Array<{ h: Function; async: boolean }>> = {};
9
-
10
- function _process(internalType: string, snapshot: Record<string, unknown>): Record<string, unknown> {
11
- const entries = _handlers[internalType];
12
- if (!entries?.length) return {};
13
-
14
- const cancellable = snapshot._cancellable;
15
- const mods: Record<string, unknown> = {};
16
-
17
- for (const entry of entries) {
18
- try {
19
- const proxy: any = {};
20
- for (const key of Object.keys(snapshot)) {
21
- if (!key.startsWith('_')) proxy[key] = snapshot[key];
22
- }
23
- if (cancellable) {
24
- Object.defineProperty(proxy, 'cancelled', {
25
- get: () => call('event.isCancelled', {}),
26
- set: (v: boolean) => { call('event.setCancelled', { cancelled: v }); },
27
- enumerable: true,
28
- });
29
- }
30
- entry.h(proxy);
31
- } catch (e: any) { console.error(`event handler error: ${e?.message || e}`); }
32
- }
33
- return mods;
34
- }
35
-
36
- // Install the global event handler that init.js calls
37
- (globalThis as any)._onEvent = (msg: any) => {
38
- const eventType = msg.ev;
39
- _process(eventType, msg.sn);
40
- // Notify Java that event processing is done
41
- if (typeof (globalThis as any)._eventComplete === 'function') {
42
- (globalThis as any)._eventComplete(eventType);
43
- }
44
- };
45
-
46
3
  export function eventOn(eventType: string, handler: (e: any) => void): () => void {
47
- const async = handler.constructor.name === 'AsyncFunction';
48
- if (!_handlers[eventType]) {
49
- _handlers[eventType] = [];
4
+ // eventOn registers a handler on the JS side.
5
+ // The _onEvent function in init.js dispatches SYNC_CALLBACK messages to handlers.
6
+ // For now, event handlers are set up via a global registry.
7
+ // This is a simplified version — full sync callback event system requires
8
+ // the PluginThread to call _onEvent which processes handlers.
9
+ if (!(globalThis as any).__yeowEventHandlers) (globalThis as any).__yeowEventHandlers = {};
10
+ if (!(globalThis as any).__yeowEventHandlers[eventType]) {
11
+ (globalThis as any).__yeowEventHandlers[eventType] = [];
50
12
  call('event.subscribe', {
51
13
  pluginName: (globalThis as any).__plugin?.name || 'unknown',
52
14
  eventType,
53
15
  });
54
16
  }
55
- const entry = { h: handler, async };
56
- _handlers[eventType].push(entry);
17
+ (globalThis as any).__yeowEventHandlers[eventType].push(handler);
57
18
  return () => eventOff(eventType, handler);
58
19
  }
59
20
 
60
- export function eventOnce(eventType: string, handler: (e: any) => void): () => void {
61
- const wrapper = (e: any) => { eventOff(eventType, wrapper); handler(e); };
62
- return eventOn(eventType, wrapper);
63
- }
64
-
65
- export function eventOff(eventType: string, handler: (e: any) => void) {
66
- const entries = _handlers[eventType];
67
- if (!entries) return;
68
- for (let i = 0; i < entries.length; i++) {
69
- if (entries[i].h === handler) { entries.splice(i, 1); break; }
70
- }
71
- if (entries.length === 0) {
72
- delete _handlers[eventType];
21
+ export function eventOff(eventType: string, handler: (e: any) => void): void {
22
+ const handlers = (globalThis as any).__yeowEventHandlers?.[eventType];
23
+ if (!handlers) return;
24
+ const idx = handlers.indexOf(handler);
25
+ if (idx !== -1) handlers.splice(idx, 1);
26
+ if (handlers.length === 0) {
27
+ delete (globalThis as any).__yeowEventHandlers[eventType];
73
28
  call('event.unsubscribe', {
74
29
  pluginName: (globalThis as any).__plugin?.name || 'unknown',
75
30
  eventType,
76
31
  });
77
32
  }
78
33
  }
34
+
35
+ // Install _onEvent handler called by init.js for SYNC_CALLBACK events
36
+ (globalThis as any)._onEvent = (eventType: string, snapshot: any): any => {
37
+ const handlers = (globalThis as any).__yeowEventHandlers?.[eventType];
38
+ if (!handlers?.length) return {};
39
+
40
+ const cancellable = snapshot?._cancellable;
41
+ const mods: Record<string, any> = {};
42
+
43
+ for (const handler of handlers) {
44
+ try {
45
+ const proxy: any = {};
46
+ for (const key of Object.keys(snapshot || {})) {
47
+ if (!key.startsWith('_')) proxy[key] = snapshot[key];
48
+ }
49
+ const localMods: Record<string, any> = {};
50
+ if (cancellable) {
51
+ Object.defineProperty(proxy, 'cancelled', {
52
+ get: () => localMods.cancelled || false,
53
+ set: (v: boolean) => { localMods.cancelled = v; },
54
+ enumerable: true,
55
+ });
56
+ }
57
+ handler(proxy);
58
+ if (cancellable && localMods.cancelled) mods.cancelled = true;
59
+ } catch (e: any) { console.error('event:', e?.message || e); }
60
+ }
61
+ return mods;
62
+ };
package/src/index.ts CHANGED
@@ -1,15 +1,7 @@
1
- export { call, post } from './task.js';
1
+ export { call } from './task.js';
2
2
  export { Location } from './location.js';
3
- export { Entity, LivingEntity } from './entity.js';
4
3
  export { Player } from './player.js';
5
- export { World } from './world.js';
6
- export { Block } from './block.js';
7
- export { Inventory } from './inventory.js';
8
- export { fs, readFileSync, writeFileSync, existsSync } from './fs.js';
9
- export { path, join, basename, dirname, extname } from './path.js';
10
4
  export { registerCommand } from './command.js';
11
- export type { CommandOptions, CommandPayload } from './command.js';
12
- export { eventOn, eventOnce, eventOff } from './event.js';
13
- export { listen, respond, close } from './http.js';
14
- export type { HttpRequest } from './http.js';
15
- export { broadcast, broadcastSync, dispatchCommand, dispatchCommandSync, getMotd, getMaxPlayers, getVersion } from './server.js';
5
+ export type { CommandOptions, CommandPayload, CommandSender } from './command.js';
6
+ export { eventOn, eventOff } from './event.js';
7
+ export { broadcast, getMotd, getVersion } from './server.js';
package/src/location.ts CHANGED
@@ -2,23 +2,13 @@ import { call } from './task.js';
2
2
 
3
3
  export class Location {
4
4
  constructor(
5
- public readonly x: number,
6
- public readonly y: number,
7
- public readonly z: number,
8
- public readonly yaw: number = 0,
9
- public readonly pitch: number = 0,
5
+ public readonly x: number, public readonly y: number, public readonly z: number,
6
+ public readonly yaw: number = 0, public readonly pitch: number = 0,
10
7
  public readonly world?: string
11
8
  ) {}
12
-
13
9
  static from(raw: Record<string, unknown>): Location {
14
- return new Location(
15
- raw.x as number, raw.y as number, raw.z as number,
16
- (raw.yaw as number) ?? 0, (raw.pitch as number) ?? 0,
17
- raw.world as string | undefined
18
- );
19
- }
20
-
21
- toObject(): Record<string, unknown> {
22
- return { x: this.x, y: this.y, z: this.z, yaw: this.yaw, pitch: this.pitch, world: this.world };
10
+ return new Location(raw.x as number, raw.y as number, raw.z as number,
11
+ (raw.yaw as number) ?? 0, (raw.pitch as number) ?? 0, raw.world as string | undefined);
23
12
  }
13
+ toObject(): Record<string, unknown> { return { x: this.x, y: this.y, z: this.z, yaw: this.yaw, pitch: this.pitch, world: this.world }; }
24
14
  }
package/src/player.ts CHANGED
@@ -1,65 +1,22 @@
1
1
  import { call } from './task.js';
2
2
  import { Location } from './location.js';
3
- import { LivingEntity } from './entity.js';
4
- import { Inventory } from './inventory.js';
5
3
 
6
- export class Player extends LivingEntity {
7
- static get(identifier: string): Player | null { const d = call('player.get',{identifier}) as any; return d?new Player(d.uuid,d.name):null; }
8
- static getAll(): Player[] { return (call('player.getAll',{}) as any[]).map((r:any)=>new Player(r.uuid,r.name)); }
9
-
10
- constructor(uuid: string, private _name?: string) { super(uuid); }
11
- get name(): string { return this._name ?? ''; }
12
- get ping(): number { return call('player.getPing',{uuid:this.uuid}) as number; }
13
- get gamemode(): string { return call('player.getGamemode',{uuid:this.uuid}) as string; }
14
- set gamemode(v: string) { call('player.setGamemode',{uuid:this.uuid,value:v}); }
15
- get displayName(): string { return call('player.getDisplayName',{uuid:this.uuid}) as string; }
16
- set displayName(v: string) { call('player.setDisplayName',{uuid:this.uuid,value:v}); }
17
- get playerListName(): string { return call('player.getPlayerListName',{uuid:this.uuid}) as string; }
18
- set playerListName(v: string) { call('player.setPlayerListName',{uuid:this.uuid,value:v}); }
19
- get foodLevel(): number { return call('player.getFoodLevel',{uuid:this.uuid}) as number; }
20
- set foodLevel(v: number) { call('player.setFoodLevel',{uuid:this.uuid,value:v}); }
21
- get saturation(): number { return call('player.getSaturation',{uuid:this.uuid}) as number; }
22
- set saturation(v: number) { call('player.setSaturation',{uuid:this.uuid,value:v}); }
23
- get exp(): number { return call('player.getExp',{uuid:this.uuid}) as number; }
24
- set exp(v: number) { call('player.setExp',{uuid:this.uuid,value:v}); }
25
- get level(): number { return call('player.getLevel',{uuid:this.uuid}) as number; }
26
- set level(v: number) { call('player.setLevel',{uuid:this.uuid,value:v}); }
27
- get totalExperience(): number { return call('player.getTotalExperience',{uuid:this.uuid}) as number; }
28
- giveExp(v: number): Promise<void> { return call('player.giveExp',{uuid:this.uuid,value:v}) as Promise<void>; }
29
- giveExpSync(v: number): void { call('player.giveExp',{uuid:this.uuid,value:v}); }
30
- get isOp(): boolean { return call('player.isOp',{uuid:this.uuid}) as boolean; }
31
- get allowFlight(): boolean { return call('player.getAllowFlight',{uuid:this.uuid}) as boolean; }
32
- set allowFlight(v: boolean) { call('player.setAllowFlight',{uuid:this.uuid,value:v}); }
33
- get isFlying(): boolean { return call('player.isFlying',{uuid:this.uuid}) as boolean; }
34
- set isFlying(v: boolean) { call('player.setFlying',{uuid:this.uuid,value:v}); }
35
- get walkSpeed(): number { return call('player.getWalkSpeed',{uuid:this.uuid}) as number; }
36
- set walkSpeed(v: number) { call('player.setWalkSpeed',{uuid:this.uuid,value:v}); }
37
- get flySpeed(): number { return call('player.getFlySpeed',{uuid:this.uuid}) as number; }
38
- set flySpeed(v: number) { call('player.setFlySpeed',{uuid:this.uuid,value:v}); }
39
- hasPermission(node: string): boolean { return call('player.hasPermission',{uuid:this.uuid,node}) as boolean; }
40
- get compassTarget(): Location | null { const r=call('player.getCompassTarget',{uuid:this.uuid}) as any; return r?Location.from(r):null; }
41
- set compassTarget(loc: Location) { call('player.setCompassTarget',{uuid:this.uuid,...loc.toObject()}); }
42
- get bedSpawnLocation(): Location | null { const r=call('player.getBedSpawnLocation',{uuid:this.uuid}) as any; return r?Location.from(r):null; }
43
- setBedSpawnLocation(loc: Location): Promise<void> { return call('player.setBedSpawnLocation',{uuid:this.uuid,...loc.toObject()}) as Promise<void>; }
44
- setBedSpawnLocationSync(loc: Location): void { call('player.setBedSpawnLocation',{uuid:this.uuid,...loc.toObject()}); }
45
-
46
- sendMessage(msg: string): Promise<void> { return call('player.sendMessage',{uuid:this.uuid,message:msg}) as Promise<void>; }
47
- sendMessageSync(msg: string): void { call('player.sendMessage',{uuid:this.uuid,message:msg}); }
48
- sendTitle(title: string, subtitle?: string, fadeIn?: number, stay?: number, fadeOut?: number): Promise<void> {
49
- return call('player.sendTitle',{uuid:this.uuid,title,subtitle,fadeIn,stay,fadeOut}) as Promise<void>;
50
- }
51
- sendTitleSync(title: string, subtitle?: string, fadeIn?: number, stay?: number, fadeOut?: number): void {
52
- call('player.sendTitle',{uuid:this.uuid,title,subtitle,fadeIn,stay,fadeOut});
4
+ export class Player {
5
+ static get(identifier: string): Player | null {
6
+ const d = call('player.get', { identifier }) as any;
7
+ return d ? new Player(d.uuid, d.name) : null;
53
8
  }
54
- playSound(sound: string, volume?: number, pitch?: number): Promise<void> {
55
- return call('player.playSound',{uuid:this.uuid,sound,volume,pitch}) as Promise<void>;
9
+ static getAll(): Player[] {
10
+ return (call('player.getAll', {}) as any[]).map((r: any) => new Player(r.uuid, r.name));
56
11
  }
57
- playSoundSync(sound: string, volume?: number, pitch?: number): void {
58
- call('player.playSound',{uuid:this.uuid,sound,volume,pitch});
59
- }
60
- kick(reason?: string): Promise<void> { return call('player.kick',{uuid:this.uuid,reason}) as Promise<void>; }
61
- kickSync(reason?: string): void { call('player.kick',{uuid:this.uuid,reason}); }
62
12
 
63
- private _inv: Inventory | null = null;
64
- get inventory(): Inventory { return this._inv ?? (this._inv = new Inventory(this.uuid)); }
13
+ constructor(public readonly uuid: string, private _name?: string) {}
14
+
15
+ get name(): string { return this._name ?? ''; }
16
+ get ping(): number { return call('player.getPing', { uuid: this.uuid }) as number; }
17
+ get gamemode(): string { return call('player.getGamemode', { uuid: this.uuid }) as string; }
18
+ set gamemode(v: string) { call('player.setGamemode', { uuid: this.uuid, value: v }); }
19
+
20
+ sendMessage(msg: string): void { call('player.sendMessage', { uuid: this.uuid, message: msg }); }
21
+ kick(reason?: string): void { call('player.kick', { uuid: this.uuid, reason }); }
65
22
  }
package/src/server.ts CHANGED
@@ -1,29 +1,5 @@
1
1
  import { call } from './task.js';
2
2
 
3
- export function broadcast(msg: string): Promise<void> {
4
- return call('server.broadcast', { message: msg }) as Promise<void>;
5
- }
6
-
7
- export function broadcastSync(msg: string): void {
8
- call('server.broadcast', { message: msg });
9
- }
10
-
11
- export function getMotd(): string {
12
- return call('server.getMotd', {}) as string;
13
- }
14
-
15
- export function getVersion(): string {
16
- return call('server.getVersion', {}) as string;
17
- }
18
-
19
- export function getMaxPlayers(): number {
20
- return call('server.getMaxPlayers', {}) as number;
21
- }
22
-
23
- export function dispatchCommand(cmd: string): Promise<void> {
24
- return call('server.dispatchCommand', { command: cmd }) as Promise<void>;
25
- }
26
-
27
- export function dispatchCommandSync(cmd: string): void {
28
- call('server.dispatchCommand', { command: cmd });
29
- }
3
+ export function broadcast(msg: string): void { call('server.broadcast', { message: msg }); }
4
+ export function getMotd(): string { return call('server.getMotd', {}) as string; }
5
+ export function getVersion(): string { return call('server.getVersion', {}) as string; }
package/src/task.ts CHANGED
@@ -1,19 +1,17 @@
1
- export function call(type: string, params?: Record<string, unknown>): unknown {
2
- const json = typeof params === 'object' && params !== null ? JSON.stringify(params) : '{}';
3
- const raw = (globalThis as any).$submitSync(type, json);
4
- if (typeof raw !== 'string') return undefined;
5
- const result = JSON.parse(raw);
6
- if (result && result.err) throw new Error(result.err);
7
- return result;
8
- }
9
-
10
- export function post(type: string, params?: Record<string, unknown>): Promise<unknown> {
1
+ export function post(type: string, params: Record<string, unknown> = {}): Promise<unknown> {
11
2
  return new Promise((resolve, reject) => {
12
- const json = typeof params === 'object' && params !== null ? JSON.stringify(params) : '{}';
13
- const cbId = (globalThis as any)._registerCallback((result: any) => {
3
+ const cbId = (globalThis as any)._regCb((result: any) => {
14
4
  if (result?.err) reject(new Error(result.err));
15
5
  else resolve(result);
16
6
  });
17
- (globalThis as any).$submitAsync(type, json, cbId);
7
+ (globalThis as any).$send('game', JSON.stringify({ t: type, p: params, cb: cbId }));
18
8
  });
19
9
  }
10
+
11
+ export function call(type: string, params: Record<string, unknown> = {}): unknown {
12
+ const raw = (globalThis as any).$send('game', JSON.stringify({ t: type, p: params }));
13
+ if (!raw) return undefined;
14
+ const r = JSON.parse(raw as string);
15
+ if (r.err) throw new Error(r.err);
16
+ return r;
17
+ }