yeow-api 0.2.34 → 0.2.36

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.34",
3
+ "version": "0.2.36",
4
4
  "description": "Yeow API �?TypeScript OOP wrappers over the task protocol",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/block.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { call } from './task.js';
1
+ import { call, post } from './task.js';
2
2
  import { Location } from './location.js';
3
3
 
4
4
  export class Block {
@@ -13,7 +13,10 @@ export class Block {
13
13
  get location(): Location {
14
14
  return new Location(this.x, this.y, this.z, undefined, undefined, this.world);
15
15
  }
16
- isSolid(): boolean { return call<boolean>('block.isSolid', { world: this.world, x: this.x, y: this.y, z: this.z }); }
17
- isLiquid(): boolean { return call<boolean>('block.isLiquid', { world: this.world, x: this.x, y: this.y, z: this.z }); }
18
- isEmpty(): boolean { return call<boolean>('block.isEmpty', { world: this.world, x: this.x, y: this.y, z: this.z }); }
16
+ isSolid(): Promise<boolean> { return post<boolean>('block.isSolid', { world: this.world, x: this.x, y: this.y, z: this.z }); }
17
+ isSolidSync(): boolean { return call<boolean>('block.isSolid', { world: this.world, x: this.x, y: this.y, z: this.z }); }
18
+ isLiquid(): Promise<boolean> { return post<boolean>('block.isLiquid', { world: this.world, x: this.x, y: this.y, z: this.z }); }
19
+ isLiquidSync(): boolean { return call<boolean>('block.isLiquid', { world: this.world, x: this.x, y: this.y, z: this.z }); }
20
+ isEmpty(): Promise<boolean> { return post<boolean>('block.isEmpty', { world: this.world, x: this.x, y: this.y, z: this.z }); }
21
+ isEmptySync(): boolean { return call<boolean>('block.isEmpty', { world: this.world, x: this.x, y: this.y, z: this.z }); }
19
22
  }
package/src/entity.ts CHANGED
@@ -8,7 +8,12 @@ export class Entity {
8
8
  get name(): string { return call<string>('entity.getName', { uuid: this.uuid }); }
9
9
  get customName(): string | null { return call<string | null>('entity.getCustomName', { uuid: this.uuid }); }
10
10
  set customName(v: string | null) { call('entity.setCustomName', { uuid: this.uuid, value: v }); }
11
- setCustomNameVisible(v: boolean): Promise<void> { return post('entity.setCustomNameVisible', { uuid: this.uuid, value: v }); }
11
+ setCustomNameVisible(v: boolean): Promise<void> {
12
+ return post('entity.setCustomNameVisible', { uuid: this.uuid, value: v });
13
+ }
14
+ setCustomNameVisibleSync(v: boolean): void {
15
+ call('entity.setCustomNameVisible', { uuid: this.uuid, value: v });
16
+ }
12
17
  get world(): string | null { return call<string | null>('entity.getWorld', { uuid: this.uuid }); }
13
18
  get location(): Location | null {
14
19
  const r = call<LocationData>('entity.getLocation', { uuid: this.uuid });
package/src/event.ts CHANGED
@@ -258,7 +258,7 @@ function adaptEvent<K extends keyof EventMap>(type: K, data: RawEvent): EventMap
258
258
  'blockPlace', 'inventoryOpen', 'inventoryClose',
259
259
  ] as K[];
260
260
  if (hasPlayer.includes(type) && data.player) {
261
- wrap.player = Player.get(data.player as string);
261
+ wrap.player = Player.getSync(data.player as string);
262
262
  }
263
263
  if (data.from) wrap.from = loc(data.from as Record<string, unknown>);
264
264
  if (data.to) wrap.to = loc(data.to as Record<string, unknown>);
@@ -322,26 +322,26 @@ export function eventOn<K extends keyof EventMap>(
322
322
  $send('task', { type: 'event.complete', params: { callbackId: cbId, mods }, cb: '' });
323
323
  }, { persistent: true });
324
324
 
325
- if (!__yeowEventHandlers) (globalThis as any).__yeowEventHandlers = {};
326
- const handlers = __yeowEventHandlers!;
327
- if (!handlers[eventType]) handlers[eventType] = [];
328
- handlers[eventType].push({ cbId, handler, manualRelease });
325
+ const gh = globalThis as any;
326
+ if (!gh.__yeowEventHandlers) gh.__yeowEventHandlers = {};
327
+ if (!gh.__yeowEventHandlers[eventType]) gh.__yeowEventHandlers[eventType] = [];
328
+ gh.__yeowEventHandlers[eventType].push({ cbId, handler, manualRelease });
329
329
 
330
330
  call('event.subscribe', { pluginName, eventType, callbackId: String(cbId) });
331
331
  return () => eventOff(eventType, handler);
332
332
  }
333
333
 
334
334
  export function eventOff(eventType: string, handler: Function): void {
335
- if (!__yeowEventHandlers) return;
336
- const entries = __yeowEventHandlers[eventType];
335
+ const gh = globalThis as any;
336
+ const entries = gh.__yeowEventHandlers?.[eventType];
337
337
  if (!entries) return;
338
- const idx = entries.findIndex((e) => e.handler === handler);
338
+ const idx = entries.findIndex((e: any) => e.handler === handler);
339
339
  if (idx !== -1) {
340
340
  const removed = entries.splice(idx, 1)[0];
341
341
  if (removed.cbId) _unregisterCallback(removed.cbId);
342
342
  }
343
343
  if (entries.length === 0) {
344
- delete __yeowEventHandlers![eventType];
344
+ delete gh.__yeowEventHandlers[eventType];
345
345
  call('event.unsubscribe', {
346
346
  pluginName: __plugin?.name || 'unknown',
347
347
  eventType,
package/src/fs.ts CHANGED
@@ -49,6 +49,14 @@ export function writeFileBase64Sync(path: string, data: string): void {
49
49
  _sendFs({ t: 'writeBase64', p: { path, data } });
50
50
  }
51
51
 
52
+ export function exists(path: string): Promise<boolean> {
53
+ return new Promise((resolve, reject) => {
54
+ try {
55
+ const r = _sendFs({ t: 'exists', p: { path } });
56
+ resolve(r === true || String(r) === 'true');
57
+ } catch (e) { reject(e); }
58
+ });
59
+ }
52
60
  export function existsSync(path: string): boolean {
53
61
  const r = _sendFs({ t: 'exists', p: { path } });
54
62
  return r === true || String(r) === 'true';
@@ -62,6 +70,10 @@ export function deleteFile(path: string): Promise<boolean> {
62
70
  } catch (e) { reject(e); }
63
71
  });
64
72
  }
73
+ export function deleteFileSync(path: string): boolean {
74
+ const r = _sendFs({ t: 'delete', p: { path } });
75
+ return r === true || String(r) === 'true';
76
+ }
65
77
 
66
78
  export function mkdir(path: string): Promise<void> {
67
79
  return new Promise((resolve, reject) => {
@@ -69,6 +81,9 @@ export function mkdir(path: string): Promise<void> {
69
81
  catch (e) { reject(e); }
70
82
  });
71
83
  }
84
+ export function mkdirSync(path: string): void {
85
+ _sendFs({ t: 'mkdir', p: { path } });
86
+ }
72
87
 
73
88
  export function list(path: string): Promise<string[]> {
74
89
  return new Promise((resolve, reject) => {
@@ -78,9 +93,12 @@ export function list(path: string): Promise<string[]> {
78
93
  } catch (e) { reject(e); }
79
94
  });
80
95
  }
96
+ export function listSync(path: string): string[] {
97
+ return _sendFs({ t: 'list', p: { path } }) as string[];
98
+ }
81
99
 
82
100
  export const fs = {
83
101
  readFile, readFileSync, readFileBase64, readFileBase64Sync,
84
102
  writeFile, writeFileSync, writeFileBase64, writeFileBase64Sync,
85
- existsSync, deleteFile, mkdir, list,
103
+ exists, existsSync, deleteFile, deleteFileSync, mkdir, mkdirSync, list, listSync,
86
104
  };
package/src/index.ts CHANGED
@@ -24,9 +24,20 @@ export type {
24
24
  ServerPingEvent, ServerCommandEvent,
25
25
  } from './event.js';
26
26
  export { onInit, onLoad, onUnload } from './lifecycle.js';
27
- export { broadcast, broadcastSync, dispatchCommand, dispatchCommandSync, setMotd, setMotdSync, setIcon, setIconSync, getMotd, getVersion } from './server.js';
28
- export { fs, readFile, readFileSync, readFileBase64, readFileBase64Sync, writeFile, writeFileSync, writeFileBase64, writeFileBase64Sync, existsSync, deleteFile, mkdir, list } from './fs.js';
29
- export { assets, read as assetsRead, readSync as assetsReadSync, readBase64 as assetsReadBase64, readBase64Sync as assetsReadBase64Sync, extract as assetsExtract, extractSync as assetsExtractSync } from './assets.js';
27
+ export {
28
+ broadcast, broadcastSync, dispatchCommand, dispatchCommandSync,
29
+ setMotd, setMotdSync, setIcon, setIconSync,
30
+ getMotd, getMotdSync, getVersion, getVersionSync,
31
+ } from './server.js';
32
+ export {
33
+ fs,
34
+ readFile, readFileSync, readFileBase64, readFileBase64Sync,
35
+ writeFile, writeFileSync, writeFileBase64, writeFileBase64Sync,
36
+ exists, existsSync, deleteFile, deleteFileSync, mkdir, mkdirSync, list, listSync,
37
+ } from './fs.js';
38
+ export { assets, read as assetsRead, readSync as assetsReadSync,
39
+ readBase64 as assetsReadBase64, readBase64Sync as assetsReadBase64Sync,
40
+ extract as assetsExtract, extractSync as assetsExtractSync } from './assets.js';
30
41
  export { path } from './path.js';
31
42
  export { listen, respond, close, request, requestSync } from './http.js';
32
43
  export { logError } from './log-error.js';
package/src/inventory.ts CHANGED
@@ -8,7 +8,10 @@ interface ItemData {
8
8
  export class Inventory {
9
9
  constructor(private readonly uuid: string) {}
10
10
 
11
- getItem(slot: number): ItemData | null {
11
+ getItem(slot: number): Promise<ItemData | null> {
12
+ return post<ItemData | null>('inventory.getItem', { uuid: this.uuid, slot });
13
+ }
14
+ getItemSync(slot: number): ItemData | null {
12
15
  return call<ItemData | null>('inventory.getItem', { uuid: this.uuid, slot });
13
16
  }
14
17
  setItem(slot: number, itemType: string, amount?: number): Promise<void> {
package/src/player.ts CHANGED
@@ -7,11 +7,17 @@ interface PlayerData {
7
7
  }
8
8
 
9
9
  export class Player {
10
- static get(identifier: string): Player | null {
10
+ static get(identifier: string): Promise<Player | null> {
11
+ return post<PlayerData>('player.get', { identifier }).then((d) => (d ? new Player(d.uuid, d.name) : null));
12
+ }
13
+ static getSync(identifier: string): Player | null {
11
14
  const d = call<PlayerData>('player.get', { identifier });
12
15
  return d ? new Player(d.uuid, d.name) : null;
13
16
  }
14
- static getAll(): Player[] {
17
+ static getAll(): Promise<Player[]> {
18
+ return post<PlayerData[]>('player.getAll', {}).then((list) => list.map((r) => new Player(r.uuid, r.name)));
19
+ }
20
+ static getAllSync(): Player[] {
15
21
  return call<PlayerData[]>('player.getAll', {}).map((r) => new Player(r.uuid, r.name));
16
22
  }
17
23
 
@@ -49,7 +55,12 @@ export class Player {
49
55
  }
50
56
  giveExp(amount: number): Promise<void> { return post('player.giveExp', { uuid: this.uuid, amount }); }
51
57
  giveExpSync(amount: number): void { call('player.giveExp', { uuid: this.uuid, amount }); }
52
- hasPermission(node: string): boolean { return call<boolean>('player.hasPermission', { uuid: this.uuid, permission: node }); }
58
+ hasPermission(node: string): Promise<boolean> {
59
+ return post<boolean>('player.hasPermission', { uuid: this.uuid, permission: node });
60
+ }
61
+ hasPermissionSync(node: string): boolean {
62
+ return call<boolean>('player.hasPermission', { uuid: this.uuid, permission: node });
63
+ }
53
64
  teleport(loc: Location): Promise<void> { return post('player.teleport', { uuid: this.uuid, ...loc.toObject() }); }
54
65
  teleportSync(loc: Location): void { call('player.teleport', { uuid: this.uuid, ...loc.toObject() }); }
55
66
  }
package/src/server.ts CHANGED
@@ -8,5 +8,7 @@ export function setMotd(motd: string): Promise<void> { return post('server.setMo
8
8
  export function setMotdSync(motd: string): void { call('server.setMotd', { motd }); }
9
9
  export function setIcon(base64: string): Promise<void> { return post('server.setIcon', { icon: base64 }); }
10
10
  export function setIconSync(base64: string): void { call('server.setIcon', { icon: base64 }); }
11
- export function getMotd(): string { return call<string>('server.getMotd', {}); }
12
- export function getVersion(): string { return call<string>('server.getVersion', {}); }
11
+ export function getMotd(): Promise<string> { return post<string>('server.getMotd', {}); }
12
+ export function getMotdSync(): string { return call<string>('server.getMotd', {}); }
13
+ export function getVersion(): Promise<string> { return post<string>('server.getVersion', {}); }
14
+ export function getVersionSync(): string { return call<string>('server.getVersion', {}); }
package/src/world.ts CHANGED
@@ -7,11 +7,17 @@ interface WorldData {
7
7
  }
8
8
 
9
9
  export class World {
10
- static get(name: string): World | null {
10
+ static get(name: string): Promise<World | null> {
11
+ return post<WorldData>('world.get', { name }).then((d) => (d ? new World(d.name) : null));
12
+ }
13
+ static getSync(name: string): World | null {
11
14
  const d = call<WorldData>('world.get', { name });
12
15
  return d ? new World(d.name) : null;
13
16
  }
14
- static getAll(): World[] {
17
+ static getAll(): Promise<World[]> {
18
+ return post<WorldData[]>('world.getAll', {}).then((list) => list.map((r) => new World(r.name)));
19
+ }
20
+ static getAllSync(): World[] {
15
21
  return call<WorldData[]>('world.getAll', {}).map((r) => new World(r.name));
16
22
  }
17
23
 
@@ -31,12 +37,35 @@ export class World {
31
37
  }
32
38
  set spawnLocation(v: Location) { call('world.setSpawnLocation', { world: this.name, ...v.toObject() }); }
33
39
 
34
- getHighestBlockY(x: number, z: number): number { return call<number>('world.getHighestBlockY', { world: this.name, x, z }); }
35
- getGameRule(rule: string): string | null { return call<string | null>('world.getGameRule', { world: this.name, rule }); }
36
- setGameRule(rule: string, value: string): Promise<boolean> { return post('world.setGameRule', { world: this.name, rule, value }); }
37
- setGameRuleSync(rule: string, value: string): boolean { return call<boolean>('world.setGameRule', { world: this.name, rule, value }); }
38
- getBiome(x: number, y: number, z: number): string { return call<string>('world.getBiome', { world: this.name, x, y, z }); }
39
- getBlock(x: number, y: number, z: number): Block | null {
40
+ getHighestBlockY(x: number, z: number): Promise<number> {
41
+ return post<number>('world.getHighestBlockY', { world: this.name, x, z });
42
+ }
43
+ getHighestBlockYSync(x: number, z: number): number {
44
+ return call<number>('world.getHighestBlockY', { world: this.name, x, z });
45
+ }
46
+ getGameRule(rule: string): Promise<string | null> {
47
+ return post<string | null>('world.getGameRule', { world: this.name, rule });
48
+ }
49
+ getGameRuleSync(rule: string): string | null {
50
+ return call<string | null>('world.getGameRule', { world: this.name, rule });
51
+ }
52
+ setGameRule(rule: string, value: string): Promise<boolean> {
53
+ return post('world.setGameRule', { world: this.name, rule, value });
54
+ }
55
+ setGameRuleSync(rule: string, value: string): boolean {
56
+ return call<boolean>('world.setGameRule', { world: this.name, rule, value });
57
+ }
58
+ getBiome(x: number, y: number, z: number): Promise<string> {
59
+ return post<string>('world.getBiome', { world: this.name, x, y, z });
60
+ }
61
+ getBiomeSync(x: number, y: number, z: number): string {
62
+ return call<string>('world.getBiome', { world: this.name, x, y, z });
63
+ }
64
+ 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));
67
+ }
68
+ getBlockSync(x: number, y: number, z: number): Block | null {
40
69
  const r = call<{ x: number; y: number; z: number; type: string }>('world.getBlock', { world: this.name, x, y, z });
41
70
  return r ? new Block(this.name, r.x, r.y, r.z, r.type) : null;
42
71
  }
@@ -46,9 +75,22 @@ export class World {
46
75
  setBlockSync(x: number, y: number, z: number, blockType: string): void {
47
76
  call('world.setBlock', { world: this.name, x, y, z, blockType });
48
77
  }
49
- getEntities(): string[] { return call<string[]>('world.getEntities', { world: this.name }); }
50
- getPlayers(): string[] { return call<string[]>('world.getPlayers', { world: this.name }); }
51
- getNearbyEntities(x: number, y: number, z: number, radius: number): string[] {
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
+ }
90
+ getNearbyEntities(x: number, y: number, z: number, radius: number): Promise<string[]> {
91
+ return post<string[]>('world.getNearbyEntities', { world: this.name, x, y, z, radius });
92
+ }
93
+ getNearbyEntitiesSync(x: number, y: number, z: number, radius: number): string[] {
52
94
  return call<string[]>('world.getNearbyEntities', { world: this.name, x, y, z, radius });
53
95
  }
54
96
  dropItem(x: number, y: number, z: number, itemType: string, amount?: number): Promise<void> {