yeow-api 0.2.37 → 0.2.38

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.37",
3
+ "version": "0.2.38",
4
4
  "description": "Yeow API �?TypeScript OOP wrappers over the task protocol",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/entity.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { call, post } from './task.js';
1
+ import { call, post, val } from './task.js';
2
2
  import { Location, LocationData } from './location.js';
3
3
 
4
4
  export class Entity {
@@ -16,8 +16,8 @@ export class Entity {
16
16
  }
17
17
  get world(): string | null { return call<string | null>('entity.getWorld', { uuid: this.uuid }); }
18
18
  get location(): Location | null {
19
- const r = call<LocationData>('entity.getLocation', { uuid: this.uuid });
20
- return r ? Location.from(r) : null;
19
+ const r = call<any>('entity.getLocation', { uuid: this.uuid });
20
+ return r ? Location.from(val(r, 'x'), val(r, 'y'), val(r, 'z'), val(r, 'yaw'), val(r, 'pitch'), val(r, 'world')) : null;
21
21
  }
22
22
  get isGlowing(): boolean { return call<boolean>('entity.isGlowing', { uuid: this.uuid }); }
23
23
  set isGlowing(v: boolean) { call('entity.setGlowing', { uuid: this.uuid, value: v }); }
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference path="global.d.ts" />
2
2
 
3
- export { call } from './task.js';
3
+ export { call, val } from './task.js';
4
4
  export { Location } from './location.js';
5
5
  export { Player } from './player.js';
6
6
  export { World } from './world.js';
package/src/inventory.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { call, post } from './task.js';
1
+ import { call, post, val } from './task.js';
2
2
 
3
3
  interface ItemData {
4
4
  type: string;
@@ -9,10 +9,13 @@ export class Inventory {
9
9
  constructor(private readonly uuid: string) {}
10
10
 
11
11
  getItem(slot: number): Promise<ItemData | null> {
12
- return post<ItemData | null>('inventory.getItem', { uuid: this.uuid, slot });
12
+ return post<any>('inventory.getItem', { uuid: this.uuid, slot }).then((r) =>
13
+ r ? { type: val(r, 'type'), amount: val(r, 'amount') } : null
14
+ );
13
15
  }
14
16
  getItemSync(slot: number): ItemData | null {
15
- return call<ItemData | null>('inventory.getItem', { uuid: this.uuid, slot });
17
+ const r = call<any>('inventory.getItem', { uuid: this.uuid, slot });
18
+ return r ? { type: val(r, 'type'), amount: val(r, 'amount') } : null;
16
19
  }
17
20
  setItem(slot: number, itemType: string, amount?: number): Promise<void> {
18
21
  return post('inventory.setItem', { uuid: this.uuid, slot, itemType, amount });
package/src/location.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { val } from './task.js';
2
+
1
3
  export interface LocationData {
2
4
  x: number;
3
5
  y: number;
@@ -17,8 +19,13 @@ export class Location {
17
19
  public readonly world?: string,
18
20
  ) {}
19
21
 
20
- static from(raw: LocationData): Location {
21
- return new Location(raw.x, raw.y, raw.z, raw.yaw ?? 0, raw.pitch ?? 0, raw.world);
22
+ static from(x: number, y: number, z: number, yaw?: number, pitch?: number, world?: string): Location;
23
+ static from(raw: LocationData): Location;
24
+ static from(a: any, b?: any, c?: any, d?: any, e?: any, f?: any): Location {
25
+ if (typeof a === 'object' && a !== null) {
26
+ return new Location(val(a, 'x'), val(a, 'y'), val(a, 'z'), val(a, 'yaw') ?? 0, val(a, 'pitch') ?? 0, val(a, 'world'));
27
+ }
28
+ return new Location(a, b, c, d ?? 0, e ?? 0, f);
22
29
  }
23
30
 
24
31
  toObject(): LocationData {
package/src/player.ts CHANGED
@@ -1,24 +1,29 @@
1
- import { call, post } from './task.js';
1
+ import { call, post, val } from './task.js';
2
2
  import { Location, LocationData } from './location.js';
3
3
 
4
- interface PlayerData {
5
- uuid: string;
6
- name: string;
7
- }
8
-
9
4
  export class Player {
10
5
  static get(identifier: string): Promise<Player | null> {
11
- return post<PlayerData>('player.get', { identifier }).then((d) => (d ? new Player(d.uuid, d.name) : null));
6
+ return post<any>('player.get', { identifier }).then((d: any) => {
7
+ if (!d) return null;
8
+ const uuid = val(d, 'uuid');
9
+ const name = val(d, 'name');
10
+ return uuid ? new Player(uuid, name) : null;
11
+ });
12
12
  }
13
13
  static getSync(identifier: string): Player | null {
14
- const d = call<PlayerData>('player.get', { identifier });
15
- return d ? new Player(d.uuid, d.name) : null;
14
+ const d = call<any>('player.get', { identifier });
15
+ if (!d) return null;
16
+ return new Player(val(d, 'uuid'), val(d, 'name'));
16
17
  }
17
18
  static getAll(): Promise<Player[]> {
18
- return post<PlayerData[]>('player.getAll', {}).then((list) => list.map((r) => new Player(r.uuid, r.name)));
19
+ return post<any[]>('player.getAll', {}).then((list) =>
20
+ (list || []).map((r: any) => new Player(val(r, 'uuid'), val(r, 'name')))
21
+ );
19
22
  }
20
23
  static getAllSync(): Player[] {
21
- return call<PlayerData[]>('player.getAll', {}).map((r) => new Player(r.uuid, r.name));
24
+ return (call<any[]>('player.getAll', {}) || []).map((r: any) =>
25
+ new Player(val(r, 'uuid'), val(r, 'name'))
26
+ );
22
27
  }
23
28
 
24
29
  constructor(public readonly uuid: string, private _name?: string) {}
@@ -29,8 +34,8 @@ export class Player {
29
34
  set gamemode(v: string) { call('player.setGamemode', { uuid: this.uuid, value: v }); }
30
35
  get world(): string { return call<string>('player.getWorld', { uuid: this.uuid }); }
31
36
  get location(): Location | null {
32
- const r = call<LocationData>('player.getLocation', { uuid: this.uuid });
33
- return r ? Location.from(r) : null;
37
+ const r = call<any>('player.getLocation', { uuid: this.uuid });
38
+ return r ? Location.from(val(r, 'x'), val(r, 'y'), val(r, 'z'), val(r, 'yaw'), val(r, 'pitch'), val(r, 'world')) : null;
34
39
  }
35
40
  get displayName(): string { return call<string>('player.getDisplayName', { uuid: this.uuid }); }
36
41
  set displayName(v: string | null) { call('player.setDisplayName', { uuid: this.uuid, value: v }); }
package/src/task.ts CHANGED
@@ -6,12 +6,16 @@ export function post<T = unknown>(
6
6
  return new Promise((resolve, reject) => {
7
7
  const cbId = _registerCallback((result: any) => {
8
8
  if (result?.err) {
9
- const e = new Error(result.err);
10
- if ($dev && typeof _getCurrentCbStack === 'function') {
11
- const s = _getCurrentCbStack();
12
- if (s) e.stack += '\n --- cb registered at ---\n' + s;
9
+ if ($dev) {
10
+ const e = new Error(result.err);
11
+ if (typeof _getCurrentCbStack === 'function') {
12
+ const s = _getCurrentCbStack();
13
+ if (s) e.stack += '\n --- cb registered at ---\n' + s;
14
+ }
15
+ reject(e);
16
+ } else {
17
+ reject(new Error(result.err));
13
18
  }
14
- reject(e);
15
19
  } else resolve(result as T);
16
20
  });
17
21
  const pld: Record<string, unknown> = { type, params, cb: cbId };
@@ -29,6 +33,14 @@ export function call<T = unknown>(
29
33
  if (priority) pld.priority = priority;
30
34
  const r = $send('task', pld);
31
35
  if (r == null) return undefined as T;
32
- if ((r as any)?.err) throw new Error((r as any).err);
36
+ if ((r as any)?.err) {
37
+ if ($dev) throw new Error((r as any).err);
38
+ throw new Error(String((r as any).err));
39
+ }
33
40
  return r as T;
34
41
  }
42
+
43
+ /** Extract value from a buffer result (Map-like with get()) or a plain JSON object. */
44
+ export function val(obj: any, key: string): any {
45
+ return obj && typeof obj.get === 'function' ? obj.get(key) : obj?.[key];
46
+ }
package/src/world.ts CHANGED
@@ -1,24 +1,22 @@
1
- import { call, post } from './task.js';
1
+ import { call, post, val } from './task.js';
2
2
  import { Location, LocationData } from './location.js';
3
3
  import { Block } from './block.js';
4
4
 
5
- interface WorldData {
6
- name: string;
7
- }
8
-
9
5
  export class World {
10
6
  static get(name: string): Promise<World | null> {
11
- return post<WorldData>('world.get', { name }).then((d) => (d ? new World(d.name) : null));
7
+ return post<any>('world.get', { name }).then((d) => (d ? new World(val(d, 'name')) : null));
12
8
  }
13
9
  static getSync(name: string): World | null {
14
- const d = call<WorldData>('world.get', { name });
15
- return d ? new World(d.name) : null;
10
+ const d = call<any>('world.get', { name });
11
+ return d ? new World(val(d, 'name')) : null;
16
12
  }
17
13
  static getAll(): Promise<World[]> {
18
- return post<WorldData[]>('world.getAll', {}).then((list) => list.map((r) => new World(r.name)));
14
+ return post<any[]>('world.getAll', {}).then((list) =>
15
+ (list || []).map((r: any) => new World(val(r, 'name')))
16
+ );
19
17
  }
20
18
  static getAllSync(): World[] {
21
- return call<WorldData[]>('world.getAll', {}).map((r) => new World(r.name));
19
+ return (call<any[]>('world.getAll', {}) || []).map((r: any) => new World(val(r, 'name')));
22
20
  }
23
21
 
24
22
  constructor(public readonly name: string) {}
@@ -32,8 +30,8 @@ export class World {
32
30
  get difficulty(): string { return call<string>('world.getDifficulty', { world: this.name }); }
33
31
  set difficulty(v: string) { call('world.setDifficulty', { world: this.name, value: v }); }
34
32
  get spawnLocation(): Location | null {
35
- const r = call<LocationData>('world.getSpawnLocation', { world: this.name });
36
- return r ? new Location(r.x, r.y, r.z, r.yaw, r.pitch) : null;
33
+ const r = call<any>('world.getSpawnLocation', { world: this.name });
34
+ return r ? new Location(val(r, 'x'), val(r, 'y'), val(r, 'z'), val(r, 'yaw'), val(r, 'pitch')) : null;
37
35
  }
38
36
  set spawnLocation(v: Location) { call('world.setSpawnLocation', { world: this.name, ...v.toObject() }); }
39
37
 
@@ -62,12 +60,13 @@ export class World {
62
60
  return call<string>('world.getBiome', { world: this.name, x, y, z });
63
61
  }
64
62
  getBlock(x: number, y: number, z: number): Promise<Block | null> {
65
- return post<{ x: number; y: number; z: number; type: string }>('world.getBlock', { world: this.name, x, y, z })
66
- .then((r) => (r ? new Block(this.name, r.x, r.y, r.z, r.type) : null));
63
+ return post<any>('world.getBlock', { world: this.name, x, y, z }).then((r) =>
64
+ r ? new Block(this.name, val(r, 'x'), val(r, 'y'), val(r, 'z'), val(r, 'type')) : null
65
+ );
67
66
  }
68
67
  getBlockSync(x: number, y: number, z: number): Block | null {
69
- const r = call<{ x: number; y: number; z: number; type: string }>('world.getBlock', { world: this.name, x, y, z });
70
- return r ? new Block(this.name, r.x, r.y, r.z, r.type) : null;
68
+ const r = call<any>('world.getBlock', { world: this.name, x, y, z });
69
+ return r ? new Block(this.name, val(r, 'x'), val(r, 'y'), val(r, 'z'), val(r, 'type')) : null;
71
70
  }
72
71
  setBlock(x: number, y: number, z: number, blockType: string): Promise<void> {
73
72
  return post('world.setBlock', { world: this.name, x, y, z, blockType });
@@ -75,18 +74,10 @@ export class World {
75
74
  setBlockSync(x: number, y: number, z: number, blockType: string): void {
76
75
  call('world.setBlock', { world: this.name, x, y, z, blockType });
77
76
  }
78
- getEntities(): Promise<string[]> {
79
- return post<string[]>('world.getEntities', { world: this.name });
80
- }
81
- getEntitiesSync(): string[] {
82
- return call<string[]>('world.getEntities', { world: this.name });
83
- }
84
- getPlayers(): Promise<string[]> {
85
- return post<string[]>('world.getPlayers', { world: this.name });
86
- }
87
- getPlayersSync(): string[] {
88
- return call<string[]>('world.getPlayers', { world: this.name });
89
- }
77
+ getEntities(): Promise<string[]> { return post<string[]>('world.getEntities', { world: this.name }); }
78
+ getEntitiesSync(): string[] { return call<string[]>('world.getEntities', { world: this.name }); }
79
+ getPlayers(): Promise<string[]> { return post<string[]>('world.getPlayers', { world: this.name }); }
80
+ getPlayersSync(): string[] { return call<string[]>('world.getPlayers', { world: this.name }); }
90
81
  getNearbyEntities(x: number, y: number, z: number, radius: number): Promise<string[]> {
91
82
  return post<string[]>('world.getNearbyEntities', { world: this.name, x, y, z, radius });
92
83
  }