yeow-api 0.2.36 → 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 +1 -1
- package/src/entity.ts +3 -3
- package/src/http.ts +16 -31
- package/src/index.ts +2 -2
- package/src/inventory.ts +6 -3
- package/src/location.ts +9 -2
- package/src/player.ts +18 -13
- package/src/task.ts +18 -6
- package/src/world.ts +19 -28
package/package.json
CHANGED
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<
|
|
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/http.ts
CHANGED
|
@@ -16,39 +16,28 @@ function _sendHttp(payload: Record<string, unknown>): HttpResult {
|
|
|
16
16
|
|
|
17
17
|
const _servers = new Set<string>();
|
|
18
18
|
|
|
19
|
-
export function listen(callback: (req: Record<string, unknown>) => void, port?: number):
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
port: port || 0,
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
if (result.serverId) _servers.add(result.serverId);
|
|
33
|
-
resolve(result);
|
|
34
|
-
} catch (e) { reject(e); }
|
|
19
|
+
export function listen(callback: (req: Record<string, unknown>) => void, port?: number): HttpResult {
|
|
20
|
+
const cbId = _registerCallback((payload: unknown) => {
|
|
21
|
+
callback(payload as Record<string, unknown>);
|
|
22
|
+
}, { persistent: true });
|
|
23
|
+
const result = _sendHttp({
|
|
24
|
+
t: 'listen', p: {
|
|
25
|
+
pluginName: __plugin?.name || 'unknown',
|
|
26
|
+
callbackId: String(cbId),
|
|
27
|
+
port: port || 0,
|
|
28
|
+
},
|
|
35
29
|
});
|
|
30
|
+
if (result.serverId) _servers.add(result.serverId);
|
|
31
|
+
return result;
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
export function respond(serverId: string, connId: string, opts: Record<string, unknown> = {}):
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
_sendHttp({ t: 'respond', p: { serverId, connId, ...opts } });
|
|
42
|
-
resolve();
|
|
43
|
-
} catch (e) { reject(e); }
|
|
44
|
-
});
|
|
34
|
+
export function respond(serverId: string, connId: string, opts: Record<string, unknown> = {}): void {
|
|
35
|
+
_sendHttp({ t: 'respond', p: { serverId, connId, ...opts } });
|
|
45
36
|
}
|
|
46
37
|
|
|
47
|
-
export function close(serverId: string):
|
|
38
|
+
export function close(serverId: string): void {
|
|
48
39
|
_servers.delete(serverId);
|
|
49
|
-
|
|
50
|
-
try { _sendHttp({ t: 'close', p: { serverId } }); resolve(); } catch (e) { reject(e); }
|
|
51
|
-
});
|
|
40
|
+
_sendHttp({ t: 'close', p: { serverId } });
|
|
52
41
|
}
|
|
53
42
|
|
|
54
43
|
// Auto-close servers on shutdown
|
|
@@ -67,7 +56,3 @@ export function request(url: string, opts: Record<string, unknown> = {}): Promis
|
|
|
67
56
|
} catch (e) { reject(e); }
|
|
68
57
|
});
|
|
69
58
|
}
|
|
70
|
-
|
|
71
|
-
export function requestSync(url: string, opts: Record<string, unknown> = {}): HttpResult {
|
|
72
|
-
return _sendHttp({ t: 'request', p: { url, ...opts } });
|
|
73
|
-
}
|
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';
|
|
@@ -39,5 +39,5 @@ export { assets, read as assetsRead, readSync as assetsReadSync,
|
|
|
39
39
|
readBase64 as assetsReadBase64, readBase64Sync as assetsReadBase64Sync,
|
|
40
40
|
extract as assetsExtract, extractSync as assetsExtractSync } from './assets.js';
|
|
41
41
|
export { path } from './path.js';
|
|
42
|
-
export { listen, respond, close, request
|
|
42
|
+
export { listen, respond, close, request } from './http.js';
|
|
43
43
|
export { logError } from './log-error.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<
|
|
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
|
-
|
|
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(
|
|
21
|
-
|
|
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<
|
|
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<
|
|
15
|
-
|
|
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<
|
|
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<
|
|
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<
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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)
|
|
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<
|
|
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<
|
|
15
|
-
return d ? new World(d
|
|
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<
|
|
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<
|
|
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<
|
|
36
|
-
return r ? new Location(r
|
|
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<
|
|
66
|
-
|
|
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<
|
|
70
|
-
return r ? new Block(this.name, r
|
|
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
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
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
|
}
|