yeow-api 0.2.38 → 0.2.39
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/index.ts +1 -1
- package/src/inventory.ts +3 -6
- package/src/location.ts +2 -9
- package/src/player.ts +13 -18
- package/src/task.ts +6 -18
- package/src/world.ts +28 -19
package/package.json
CHANGED
package/src/entity.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { call, post
|
|
1
|
+
import { call, post } 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(
|
|
19
|
+
const r = call<LocationData>('entity.getLocation', { uuid: this.uuid });
|
|
20
|
+
return r ? Location.from(r) : 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
package/src/inventory.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { call, post
|
|
1
|
+
import { call, post } from './task.js';
|
|
2
2
|
|
|
3
3
|
interface ItemData {
|
|
4
4
|
type: string;
|
|
@@ -9,13 +9,10 @@ export class Inventory {
|
|
|
9
9
|
constructor(private readonly uuid: string) {}
|
|
10
10
|
|
|
11
11
|
getItem(slot: number): Promise<ItemData | null> {
|
|
12
|
-
return post<
|
|
13
|
-
r ? { type: val(r, 'type'), amount: val(r, 'amount') } : null
|
|
14
|
-
);
|
|
12
|
+
return post<ItemData | null>('inventory.getItem', { uuid: this.uuid, slot });
|
|
15
13
|
}
|
|
16
14
|
getItemSync(slot: number): ItemData | null {
|
|
17
|
-
|
|
18
|
-
return r ? { type: val(r, 'type'), amount: val(r, 'amount') } : null;
|
|
15
|
+
return call<ItemData | null>('inventory.getItem', { uuid: this.uuid, slot });
|
|
19
16
|
}
|
|
20
17
|
setItem(slot: number, itemType: string, amount?: number): Promise<void> {
|
|
21
18
|
return post('inventory.setItem', { uuid: this.uuid, slot, itemType, amount });
|
package/src/location.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { val } from './task.js';
|
|
2
|
-
|
|
3
1
|
export interface LocationData {
|
|
4
2
|
x: number;
|
|
5
3
|
y: number;
|
|
@@ -19,13 +17,8 @@ export class Location {
|
|
|
19
17
|
public readonly world?: string,
|
|
20
18
|
) {}
|
|
21
19
|
|
|
22
|
-
static from(
|
|
23
|
-
|
|
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);
|
|
20
|
+
static from(raw: LocationData): Location {
|
|
21
|
+
return new Location(raw.x, raw.y, raw.z, raw.yaw ?? 0, raw.pitch ?? 0, raw.world);
|
|
29
22
|
}
|
|
30
23
|
|
|
31
24
|
toObject(): LocationData {
|
package/src/player.ts
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
|
-
import { call, post
|
|
1
|
+
import { call, post } from './task.js';
|
|
2
2
|
import { Location, LocationData } from './location.js';
|
|
3
3
|
|
|
4
|
+
interface PlayerData {
|
|
5
|
+
uuid: string;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
4
9
|
export class Player {
|
|
5
10
|
static get(identifier: string): Promise<Player | null> {
|
|
6
|
-
return post<
|
|
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
|
-
});
|
|
11
|
+
return post<PlayerData>('player.get', { identifier }).then((d) => (d ? new Player(d.uuid, d.name) : null));
|
|
12
12
|
}
|
|
13
13
|
static getSync(identifier: string): Player | null {
|
|
14
|
-
const d = call<
|
|
15
|
-
|
|
16
|
-
return new Player(val(d, 'uuid'), val(d, 'name'));
|
|
14
|
+
const d = call<PlayerData>('player.get', { identifier });
|
|
15
|
+
return d ? new Player(d.uuid, d.name) : null;
|
|
17
16
|
}
|
|
18
17
|
static getAll(): Promise<Player[]> {
|
|
19
|
-
return post<
|
|
20
|
-
(list || []).map((r: any) => new Player(val(r, 'uuid'), val(r, 'name')))
|
|
21
|
-
);
|
|
18
|
+
return post<PlayerData[]>('player.getAll', {}).then((list) => list.map((r) => new Player(r.uuid, r.name)));
|
|
22
19
|
}
|
|
23
20
|
static getAllSync(): Player[] {
|
|
24
|
-
return
|
|
25
|
-
new Player(val(r, 'uuid'), val(r, 'name'))
|
|
26
|
-
);
|
|
21
|
+
return call<PlayerData[]>('player.getAll', {}).map((r) => new Player(r.uuid, r.name));
|
|
27
22
|
}
|
|
28
23
|
|
|
29
24
|
constructor(public readonly uuid: string, private _name?: string) {}
|
|
@@ -34,8 +29,8 @@ export class Player {
|
|
|
34
29
|
set gamemode(v: string) { call('player.setGamemode', { uuid: this.uuid, value: v }); }
|
|
35
30
|
get world(): string { return call<string>('player.getWorld', { uuid: this.uuid }); }
|
|
36
31
|
get location(): Location | null {
|
|
37
|
-
const r = call<
|
|
38
|
-
return r ? Location.from(
|
|
32
|
+
const r = call<LocationData>('player.getLocation', { uuid: this.uuid });
|
|
33
|
+
return r ? Location.from(r) : null;
|
|
39
34
|
}
|
|
40
35
|
get displayName(): string { return call<string>('player.getDisplayName', { uuid: this.uuid }); }
|
|
41
36
|
set displayName(v: string | null) { call('player.setDisplayName', { uuid: this.uuid, value: v }); }
|
package/src/task.ts
CHANGED
|
@@ -6,16 +6,12 @@ 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
|
-
|
|
13
|
-
if (s) e.stack += '\n --- cb registered at ---\n' + s;
|
|
14
|
-
}
|
|
15
|
-
reject(e);
|
|
16
|
-
} else {
|
|
17
|
-
reject(new Error(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;
|
|
18
13
|
}
|
|
14
|
+
reject(e);
|
|
19
15
|
} else resolve(result as T);
|
|
20
16
|
});
|
|
21
17
|
const pld: Record<string, unknown> = { type, params, cb: cbId };
|
|
@@ -33,14 +29,6 @@ export function call<T = unknown>(
|
|
|
33
29
|
if (priority) pld.priority = priority;
|
|
34
30
|
const r = $send('task', pld);
|
|
35
31
|
if (r == null) return undefined as T;
|
|
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
|
-
}
|
|
32
|
+
if ((r as any)?.err) throw new Error((r as any).err);
|
|
40
33
|
return r as T;
|
|
41
34
|
}
|
|
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,22 +1,24 @@
|
|
|
1
|
-
import { call, post
|
|
1
|
+
import { call, post } 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
|
+
|
|
5
9
|
export class World {
|
|
6
10
|
static get(name: string): Promise<World | null> {
|
|
7
|
-
return post<
|
|
11
|
+
return post<WorldData>('world.get', { name }).then((d) => (d ? new World(d.name) : null));
|
|
8
12
|
}
|
|
9
13
|
static getSync(name: string): World | null {
|
|
10
|
-
const d = call<
|
|
11
|
-
return d ? new World(
|
|
14
|
+
const d = call<WorldData>('world.get', { name });
|
|
15
|
+
return d ? new World(d.name) : null;
|
|
12
16
|
}
|
|
13
17
|
static getAll(): Promise<World[]> {
|
|
14
|
-
return post<
|
|
15
|
-
(list || []).map((r: any) => new World(val(r, 'name')))
|
|
16
|
-
);
|
|
18
|
+
return post<WorldData[]>('world.getAll', {}).then((list) => list.map((r) => new World(r.name)));
|
|
17
19
|
}
|
|
18
20
|
static getAllSync(): World[] {
|
|
19
|
-
return
|
|
21
|
+
return call<WorldData[]>('world.getAll', {}).map((r) => new World(r.name));
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
constructor(public readonly name: string) {}
|
|
@@ -30,8 +32,8 @@ export class World {
|
|
|
30
32
|
get difficulty(): string { return call<string>('world.getDifficulty', { world: this.name }); }
|
|
31
33
|
set difficulty(v: string) { call('world.setDifficulty', { world: this.name, value: v }); }
|
|
32
34
|
get spawnLocation(): Location | null {
|
|
33
|
-
const r = call<
|
|
34
|
-
return r ? new Location(
|
|
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;
|
|
35
37
|
}
|
|
36
38
|
set spawnLocation(v: Location) { call('world.setSpawnLocation', { world: this.name, ...v.toObject() }); }
|
|
37
39
|
|
|
@@ -60,13 +62,12 @@ export class World {
|
|
|
60
62
|
return call<string>('world.getBiome', { world: this.name, x, y, z });
|
|
61
63
|
}
|
|
62
64
|
getBlock(x: number, y: number, z: number): Promise<Block | null> {
|
|
63
|
-
return post<
|
|
64
|
-
r ? new Block(this.name,
|
|
65
|
-
);
|
|
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));
|
|
66
67
|
}
|
|
67
68
|
getBlockSync(x: number, y: number, z: number): Block | null {
|
|
68
|
-
const r = call<
|
|
69
|
-
return r ? new Block(this.name,
|
|
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;
|
|
70
71
|
}
|
|
71
72
|
setBlock(x: number, y: number, z: number, blockType: string): Promise<void> {
|
|
72
73
|
return post('world.setBlock', { world: this.name, x, y, z, blockType });
|
|
@@ -74,10 +75,18 @@ export class World {
|
|
|
74
75
|
setBlockSync(x: number, y: number, z: number, blockType: string): void {
|
|
75
76
|
call('world.setBlock', { world: this.name, x, y, z, blockType });
|
|
76
77
|
}
|
|
77
|
-
getEntities(): Promise<string[]> {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
}
|
|
81
90
|
getNearbyEntities(x: number, y: number, z: number, radius: number): Promise<string[]> {
|
|
82
91
|
return post<string[]>('world.getNearbyEntities', { world: this.name, x, y, z, radius });
|
|
83
92
|
}
|