yeow-api 0.2.30 → 0.2.33
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/assets.ts +36 -29
- package/src/block.ts +13 -11
- package/src/command.ts +88 -42
- package/src/entity.ts +30 -27
- package/src/event.ts +259 -283
- package/src/fs.ts +64 -47
- package/src/global.d.ts +32 -0
- package/src/http.ts +58 -47
- package/src/index.ts +4 -2
- package/src/inventory.ts +33 -28
- package/src/lifecycle.ts +3 -0
- package/src/location.ts +24 -11
- package/src/player.ts +48 -41
- package/src/server.ts +7 -7
- package/src/task.ts +30 -20
- package/src/world.ts +70 -33
package/src/fs.ts
CHANGED
|
@@ -1,69 +1,86 @@
|
|
|
1
|
-
function _sendFs(payload) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
function _sendFs(payload: Record<string, unknown>): unknown {
|
|
2
|
+
const r = $send('fs', payload);
|
|
3
|
+
if (r == null) return undefined;
|
|
4
|
+
if ((r as any)?.err) throw new Error((r as any).err);
|
|
5
|
+
return r;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export function readFile(path) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
export function readFile(path: string): Promise<string> {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
try {
|
|
11
|
+
const r = _sendFs({ t: 'readFile', p: { path } }) as { data: string };
|
|
12
|
+
resolve(r.data);
|
|
13
|
+
} catch (e) { reject(e); }
|
|
14
|
+
});
|
|
12
15
|
}
|
|
13
|
-
export function readFileSync(path) {
|
|
14
|
-
|
|
16
|
+
export function readFileSync(path: string): string {
|
|
17
|
+
return (_sendFs({ t: 'readFile', p: { path } }) as { data: string }).data;
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
export function readFileBase64(path) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
export function readFileBase64(path: string): Promise<string> {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
try {
|
|
23
|
+
const r = _sendFs({ t: 'readBase64', p: { path } }) as { data: string };
|
|
24
|
+
resolve(r.data);
|
|
25
|
+
} catch (e) { reject(e); }
|
|
26
|
+
});
|
|
21
27
|
}
|
|
22
|
-
export function readFileBase64Sync(path) {
|
|
23
|
-
|
|
28
|
+
export function readFileBase64Sync(path: string): string {
|
|
29
|
+
return (_sendFs({ t: 'readBase64', p: { path } }) as { data: string }).data;
|
|
24
30
|
}
|
|
25
31
|
|
|
26
|
-
export function writeFile(path, data) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
export function writeFile(path: string, data: string): Promise<void> {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
try { _sendFs({ t: 'writeFile', p: { path, data } }); resolve(); }
|
|
35
|
+
catch (e) { reject(e); }
|
|
36
|
+
});
|
|
30
37
|
}
|
|
31
|
-
export function writeFileSync(path, data) {
|
|
32
|
-
|
|
38
|
+
export function writeFileSync(path: string, data: string): void {
|
|
39
|
+
_sendFs({ t: 'writeFile', p: { path, data } });
|
|
33
40
|
}
|
|
34
41
|
|
|
35
|
-
export function writeFileBase64(path, data) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
export function writeFileBase64(path: string, data: string): Promise<void> {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
try { _sendFs({ t: 'writeBase64', p: { path, data } }); resolve(); }
|
|
45
|
+
catch (e) { reject(e); }
|
|
46
|
+
});
|
|
39
47
|
}
|
|
40
|
-
export function writeFileBase64Sync(path, data) {
|
|
41
|
-
|
|
48
|
+
export function writeFileBase64Sync(path: string, data: string): void {
|
|
49
|
+
_sendFs({ t: 'writeBase64', p: { path, data } });
|
|
42
50
|
}
|
|
43
51
|
|
|
44
|
-
export function existsSync(path) {
|
|
45
|
-
|
|
46
|
-
|
|
52
|
+
export function existsSync(path: string): boolean {
|
|
53
|
+
const r = _sendFs({ t: 'exists', p: { path } });
|
|
54
|
+
return r === true || String(r) === 'true';
|
|
47
55
|
}
|
|
48
56
|
|
|
49
|
-
export function deleteFile(path) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
export function deleteFile(path: string): Promise<boolean> {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
try {
|
|
60
|
+
const r = _sendFs({ t: 'delete', p: { path } });
|
|
61
|
+
resolve(r === true || String(r) === 'true');
|
|
62
|
+
} catch (e) { reject(e); }
|
|
63
|
+
});
|
|
53
64
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
65
|
+
|
|
66
|
+
export function mkdir(path: string): Promise<void> {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
try { _sendFs({ t: 'mkdir', p: { path } }); resolve(); }
|
|
69
|
+
catch (e) { reject(e); }
|
|
70
|
+
});
|
|
58
71
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
72
|
+
|
|
73
|
+
export function list(path: string): Promise<string[]> {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
try {
|
|
76
|
+
const r = _sendFs({ t: 'list', p: { path } });
|
|
77
|
+
resolve(r as string[]);
|
|
78
|
+
} catch (e) { reject(e); }
|
|
79
|
+
});
|
|
63
80
|
}
|
|
64
81
|
|
|
65
82
|
export const fs = {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
83
|
+
readFile, readFileSync, readFileBase64, readFileBase64Sync,
|
|
84
|
+
writeFile, writeFileSync, writeFileBase64, writeFileBase64Sync,
|
|
85
|
+
existsSync, deleteFile, mkdir, list,
|
|
69
86
|
};
|
package/src/global.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Runtime globals injected by init.js / yeow-api
|
|
2
|
+
declare global {
|
|
3
|
+
var $dev: boolean | undefined;
|
|
4
|
+
var $send: (channel: string, payload: Record<string, unknown>) => unknown | null;
|
|
5
|
+
var $hm: (json: string) => string | null;
|
|
6
|
+
|
|
7
|
+
function _registerCallback(
|
|
8
|
+
fn: (...args: any[]) => any,
|
|
9
|
+
options?: { persistent?: boolean }
|
|
10
|
+
): string;
|
|
11
|
+
function _unregisterCallback(id: string): void;
|
|
12
|
+
function _getCurrentCbStack(): string | null;
|
|
13
|
+
|
|
14
|
+
var __plugin:
|
|
15
|
+
| {
|
|
16
|
+
name: string;
|
|
17
|
+
version: string;
|
|
18
|
+
author: string;
|
|
19
|
+
}
|
|
20
|
+
| undefined;
|
|
21
|
+
|
|
22
|
+
var __yeowInitCbs: (() => void)[] | undefined;
|
|
23
|
+
var __yeowLoadCbs: (() => void)[] | undefined;
|
|
24
|
+
var __yeowUnloadCbs: (() => void)[] | undefined;
|
|
25
|
+
var __yeowEventHandlers:
|
|
26
|
+
| Record<
|
|
27
|
+
string,
|
|
28
|
+
{ cbId: string; handler: Function; manualRelease: boolean }[]
|
|
29
|
+
>
|
|
30
|
+
| undefined;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
package/src/http.ts
CHANGED
|
@@ -1,62 +1,73 @@
|
|
|
1
|
-
|
|
1
|
+
interface HttpResult {
|
|
2
|
+
serverId?: string;
|
|
3
|
+
port?: number;
|
|
4
|
+
status?: number;
|
|
5
|
+
body?: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
err?: string;
|
|
8
|
+
}
|
|
2
9
|
|
|
3
|
-
function _sendHttp(payload) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
function _sendHttp(payload: Record<string, unknown>): HttpResult {
|
|
11
|
+
const r = $send('http', payload);
|
|
12
|
+
if (!r) return {};
|
|
13
|
+
if ((r as any).err) throw new Error((r as any).err);
|
|
14
|
+
return r as HttpResult;
|
|
8
15
|
}
|
|
9
16
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
const _servers = new Set<string>();
|
|
18
|
+
|
|
19
|
+
export function listen(callback: (req: Record<string, unknown>) => void, port?: number): Promise<HttpResult> {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const cbId = _registerCallback((payload: unknown) => {
|
|
22
|
+
callback(payload as Record<string, unknown>);
|
|
23
|
+
}, { persistent: true });
|
|
24
|
+
try {
|
|
25
|
+
const result = _sendHttp({
|
|
26
|
+
t: 'listen', p: {
|
|
27
|
+
pluginName: __plugin?.name || 'unknown',
|
|
28
|
+
callbackId: String(cbId),
|
|
29
|
+
port: port || 0,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
if (result.serverId) _servers.add(result.serverId);
|
|
33
|
+
resolve(result);
|
|
34
|
+
} catch (e) { reject(e); }
|
|
35
|
+
});
|
|
26
36
|
}
|
|
27
37
|
|
|
28
|
-
export function respond(serverId, connId, opts = {}) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
export function respond(serverId: string, connId: string, opts: Record<string, unknown> = {}): Promise<void> {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
try {
|
|
41
|
+
_sendHttp({ t: 'respond', p: { serverId, connId, ...opts } });
|
|
42
|
+
resolve();
|
|
43
|
+
} catch (e) { reject(e); }
|
|
44
|
+
});
|
|
35
45
|
}
|
|
36
46
|
|
|
37
|
-
export function close(serverId) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
export function close(serverId: string): Promise<void> {
|
|
48
|
+
_servers.delete(serverId);
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
try { _sendHttp({ t: 'close', p: { serverId } }); resolve(); } catch (e) { reject(e); }
|
|
51
|
+
});
|
|
42
52
|
}
|
|
43
53
|
|
|
54
|
+
// Auto-close servers on shutdown
|
|
44
55
|
_registerCallback(() => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
56
|
+
for (const id of _servers) {
|
|
57
|
+
try { _sendHttp({ t: 'close', p: { serverId: id } }); } catch { /* ignore */ }
|
|
58
|
+
}
|
|
59
|
+
_servers.clear();
|
|
49
60
|
}, { persistent: true });
|
|
50
61
|
|
|
51
|
-
export function request(url, opts = {}) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
export function request(url: string, opts: Record<string, unknown> = {}): Promise<HttpResult> {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
try {
|
|
65
|
+
const result = _sendHttp({ t: 'request', p: { url, ...opts } });
|
|
66
|
+
resolve(result);
|
|
67
|
+
} catch (e) { reject(e); }
|
|
68
|
+
});
|
|
58
69
|
}
|
|
59
70
|
|
|
60
|
-
export function requestSync(url, opts = {}) {
|
|
61
|
-
|
|
71
|
+
export function requestSync(url: string, opts: Record<string, unknown> = {}): HttpResult {
|
|
72
|
+
return _sendHttp({ t: 'request', p: { url, ...opts } });
|
|
62
73
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/// <reference path="global.d.ts" />
|
|
2
|
+
|
|
1
3
|
export { call } from './task.js';
|
|
2
4
|
export { Location } from './location.js';
|
|
3
5
|
export { Player } from './player.js';
|
|
@@ -6,7 +8,7 @@ export { Entity, LivingEntity } from './entity.js';
|
|
|
6
8
|
export { Block } from './block.js';
|
|
7
9
|
export { Inventory } from './inventory.js';
|
|
8
10
|
export { registerCommand } from './command.js';
|
|
9
|
-
export type { CommandOptions, CommandPayload, CommandSender } from './command.js';
|
|
11
|
+
export type { CommandOptions, CommandPayload, CommandSender, ManualCompleter } from './command.js';
|
|
10
12
|
export { eventOn, eventOff } from './event.js';
|
|
11
13
|
export type {
|
|
12
14
|
PlayerJoinEvent, PlayerQuitEvent, PlayerChatEvent, PlayerMoveEvent,
|
|
@@ -21,7 +23,7 @@ export type {
|
|
|
21
23
|
InventoryOpenEvent, InventoryCloseEvent,
|
|
22
24
|
ServerPingEvent, ServerCommandEvent,
|
|
23
25
|
} from './event.js';
|
|
24
|
-
export { onInit, onLoad } from './lifecycle.js';
|
|
26
|
+
export { onInit, onLoad, onUnload } from './lifecycle.js';
|
|
25
27
|
export { broadcast, broadcastSync, dispatchCommand, dispatchCommandSync, setMotd, setMotdSync, setIcon, setIconSync, getMotd, getVersion } from './server.js';
|
|
26
28
|
export { fs, readFile, readFileSync, readFileBase64, readFileBase64Sync, writeFile, writeFileSync, writeFileBase64, writeFileBase64Sync, existsSync, deleteFile, mkdir, list } from './fs.js';
|
|
27
29
|
export { assets, read as assetsRead, readSync as assetsReadSync, readBase64 as assetsReadBase64, readBase64Sync as assetsReadBase64Sync, extract as assetsExtract, extractSync as assetsExtractSync } from './assets.js';
|
package/src/inventory.ts
CHANGED
|
@@ -1,33 +1,38 @@
|
|
|
1
1
|
import { call, post } from './task.js';
|
|
2
2
|
|
|
3
|
+
interface ItemData {
|
|
4
|
+
type: string;
|
|
5
|
+
amount: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
3
8
|
export class Inventory {
|
|
4
|
-
|
|
9
|
+
constructor(private readonly uuid: string) {}
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
11
|
+
getItem(slot: number): ItemData | null {
|
|
12
|
+
return call<ItemData | null>('inventory.getItem', { uuid: this.uuid, slot });
|
|
13
|
+
}
|
|
14
|
+
setItem(slot: number, itemType: string, amount?: number): Promise<void> {
|
|
15
|
+
return post('inventory.setItem', { uuid: this.uuid, slot, itemType, amount });
|
|
16
|
+
}
|
|
17
|
+
setItemSync(slot: number, itemType: string, amount?: number): void {
|
|
18
|
+
call('inventory.setItem', { uuid: this.uuid, slot, itemType, amount });
|
|
19
|
+
}
|
|
20
|
+
addItem(itemType: string, amount?: number): Promise<void> {
|
|
21
|
+
return post('inventory.addItem', { uuid: this.uuid, itemType, amount });
|
|
22
|
+
}
|
|
23
|
+
addItemSync(itemType: string, amount?: number): void {
|
|
24
|
+
call('inventory.addItem', { uuid: this.uuid, itemType, amount });
|
|
25
|
+
}
|
|
26
|
+
removeItem(itemType: string, amount?: number): Promise<void> {
|
|
27
|
+
return post('inventory.removeItem', { uuid: this.uuid, itemType, amount });
|
|
28
|
+
}
|
|
29
|
+
removeItemSync(itemType: string, amount?: number): void {
|
|
30
|
+
call('inventory.removeItem', { uuid: this.uuid, itemType, amount });
|
|
31
|
+
}
|
|
32
|
+
clear(slot?: number): Promise<void> {
|
|
33
|
+
return post('inventory.clear', { uuid: this.uuid, slot });
|
|
34
|
+
}
|
|
35
|
+
clearSync(slot?: number): void {
|
|
36
|
+
call('inventory.clear', { uuid: this.uuid, slot });
|
|
37
|
+
}
|
|
33
38
|
}
|
package/src/lifecycle.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
const _initCbs: (() => void)[] = [];
|
|
2
2
|
const _loadCbs: (() => void)[] = [];
|
|
3
|
+
const _unloadCbs: (() => void)[] = [];
|
|
3
4
|
|
|
4
5
|
export function onInit(cb: () => void): void { _initCbs.push(cb); }
|
|
5
6
|
export function onLoad(cb: () => void): void { _loadCbs.push(cb); }
|
|
7
|
+
export function onUnload(cb: () => void): void { _unloadCbs.push(cb); }
|
|
6
8
|
|
|
7
9
|
(globalThis as any).__yeowInitCbs = _initCbs;
|
|
8
10
|
(globalThis as any).__yeowLoadCbs = _loadCbs;
|
|
11
|
+
(globalThis as any).__yeowUnloadCbs = _unloadCbs;
|
package/src/location.ts
CHANGED
|
@@ -1,14 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
export interface LocationData {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
z: number;
|
|
5
|
+
yaw?: number;
|
|
6
|
+
pitch?: number;
|
|
7
|
+
world?: string;
|
|
8
|
+
}
|
|
2
9
|
|
|
3
10
|
export class Location {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
constructor(
|
|
12
|
+
public readonly x: number,
|
|
13
|
+
public readonly y: number,
|
|
14
|
+
public readonly z: number,
|
|
15
|
+
public readonly yaw: number = 0,
|
|
16
|
+
public readonly pitch: number = 0,
|
|
17
|
+
public readonly world?: string,
|
|
18
|
+
) {}
|
|
19
|
+
|
|
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
|
+
}
|
|
23
|
+
|
|
24
|
+
toObject(): LocationData {
|
|
25
|
+
return { x: this.x, y: this.y, z: this.z, yaw: this.yaw, pitch: this.pitch, world: this.world };
|
|
26
|
+
}
|
|
14
27
|
}
|
package/src/player.ts
CHANGED
|
@@ -1,48 +1,55 @@
|
|
|
1
1
|
import { call, post } from './task.js';
|
|
2
|
-
import { Location } from './location.js';
|
|
2
|
+
import { Location, LocationData } from './location.js';
|
|
3
|
+
|
|
4
|
+
interface PlayerData {
|
|
5
|
+
uuid: string;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
3
8
|
|
|
4
9
|
export class Player {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
static get(identifier: string): Player | null {
|
|
11
|
+
const d = call<PlayerData>('player.get', { identifier });
|
|
12
|
+
return d ? new Player(d.uuid, d.name) : null;
|
|
13
|
+
}
|
|
14
|
+
static getAll(): Player[] {
|
|
15
|
+
return call<PlayerData[]>('player.getAll', {}).map((r) => new Player(r.uuid, r.name));
|
|
16
|
+
}
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
constructor(public readonly uuid: string, private _name?: string) {}
|
|
14
19
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
get name(): string { return this._name ?? ''; }
|
|
21
|
+
get ping(): number { return call<number>('player.getPing', { uuid: this.uuid }); }
|
|
22
|
+
get gamemode(): string { return call<string>('player.getGamemode', { uuid: this.uuid }); }
|
|
23
|
+
set gamemode(v: string) { call('player.setGamemode', { uuid: this.uuid, value: v }); }
|
|
24
|
+
get world(): string { return call<string>('player.getWorld', { uuid: this.uuid }); }
|
|
25
|
+
get location(): Location | null {
|
|
26
|
+
const r = call<LocationData>('player.getLocation', { uuid: this.uuid });
|
|
27
|
+
return r ? Location.from(r) : null;
|
|
28
|
+
}
|
|
29
|
+
get displayName(): string { return call<string>('player.getDisplayName', { uuid: this.uuid }); }
|
|
30
|
+
set displayName(v: string | null) { call('player.setDisplayName', { uuid: this.uuid, value: v }); }
|
|
31
|
+
get saturation(): number { return call<number>('player.getSaturation', { uuid: this.uuid }); }
|
|
32
|
+
get totalExperience(): number { return call<number>('player.getTotalExperience', { uuid: this.uuid }); }
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
sendTitle
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
playSound
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
teleportSync(loc: Location): void { call('player.teleport', { uuid: this.uuid, ...loc.toObject() }); }
|
|
34
|
+
sendMessage(msg: string): Promise<void> { return post('player.sendMessage', { uuid: this.uuid, message: msg }); }
|
|
35
|
+
sendMessageSync(msg: string): void { call('player.sendMessage', { uuid: this.uuid, message: msg }); }
|
|
36
|
+
kick(reason?: string): Promise<void> { return post('player.kick', { uuid: this.uuid, reason }); }
|
|
37
|
+
kickSync(reason?: string): void { call('player.kick', { uuid: this.uuid, reason }); }
|
|
38
|
+
sendTitle(title?: string, subtitle?: string, fadeIn?: number, stay?: number, fadeOut?: number): Promise<void> {
|
|
39
|
+
return post('player.sendTitle', { uuid: this.uuid, title, subtitle, fadeIn, stay, fadeOut });
|
|
40
|
+
}
|
|
41
|
+
sendTitleSync(title?: string, subtitle?: string, fadeIn?: number, stay?: number, fadeOut?: number): void {
|
|
42
|
+
call('player.sendTitle', { uuid: this.uuid, title, subtitle, fadeIn, stay, fadeOut });
|
|
43
|
+
}
|
|
44
|
+
playSound(sound: string, volume?: number, pitch?: number): Promise<void> {
|
|
45
|
+
return post('player.playSound', { uuid: this.uuid, sound, volume, pitch });
|
|
46
|
+
}
|
|
47
|
+
playSoundSync(sound: string, volume?: number, pitch?: number): void {
|
|
48
|
+
call('player.playSound', { uuid: this.uuid, sound, volume, pitch });
|
|
49
|
+
}
|
|
50
|
+
giveExp(amount: number): Promise<void> { return post('player.giveExp', { uuid: this.uuid, amount }); }
|
|
51
|
+
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 }); }
|
|
53
|
+
teleport(loc: Location): Promise<void> { return post('player.teleport', { uuid: this.uuid, ...loc.toObject() }); }
|
|
54
|
+
teleportSync(loc: Location): void { call('player.teleport', { uuid: this.uuid, ...loc.toObject() }); }
|
|
48
55
|
}
|
package/src/server.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { call, post } from './task.js';
|
|
2
2
|
|
|
3
|
-
export function broadcast(msg: string): Promise<void> { return post('server.broadcast', { message: msg })
|
|
3
|
+
export function broadcast(msg: string): Promise<void> { return post('server.broadcast', { message: msg }); }
|
|
4
4
|
export function broadcastSync(msg: string): void { call('server.broadcast', { message: msg }); }
|
|
5
|
-
export function dispatchCommand(cmd: string): Promise<boolean> { return post('command.dispatch', { command: cmd })
|
|
6
|
-
export function dispatchCommandSync(cmd: string): boolean { return call('command.dispatch', { command: cmd })
|
|
7
|
-
export function setMotd(motd: string): Promise<void> { return post('server.setMotd', { motd })
|
|
5
|
+
export function dispatchCommand(cmd: string): Promise<boolean> { return post<boolean>('command.dispatch', { command: cmd }); }
|
|
6
|
+
export function dispatchCommandSync(cmd: string): boolean { return call<boolean>('command.dispatch', { command: cmd }); }
|
|
7
|
+
export function setMotd(motd: string): Promise<void> { return post('server.setMotd', { motd }); }
|
|
8
8
|
export function setMotdSync(motd: string): void { call('server.setMotd', { motd }); }
|
|
9
|
-
export function setIcon(base64: string): Promise<void> { return post('server.setIcon', { icon: base64 })
|
|
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('server.getMotd', {})
|
|
12
|
-
export function getVersion(): string { return call('server.getVersion', {})
|
|
11
|
+
export function getMotd(): string { return call<string>('server.getMotd', {}); }
|
|
12
|
+
export function getVersion(): string { return call<string>('server.getVersion', {}); }
|
package/src/task.ts
CHANGED
|
@@ -1,24 +1,34 @@
|
|
|
1
|
-
export function post
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
export function post<T = unknown>(
|
|
2
|
+
type: string,
|
|
3
|
+
params: Record<string, unknown> = {},
|
|
4
|
+
priority?: string
|
|
5
|
+
): Promise<T> {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
const cbId = _registerCallback((result: any) => {
|
|
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;
|
|
13
|
+
}
|
|
14
|
+
reject(e);
|
|
15
|
+
} else resolve(result as T);
|
|
14
16
|
});
|
|
17
|
+
const pld: Record<string, unknown> = { type, params, cb: cbId };
|
|
18
|
+
if (priority) pld.priority = priority;
|
|
19
|
+
$send('task', pld);
|
|
20
|
+
});
|
|
15
21
|
}
|
|
16
22
|
|
|
17
|
-
export function call
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
export function call<T = unknown>(
|
|
24
|
+
type: string,
|
|
25
|
+
params: Record<string, unknown> = {},
|
|
26
|
+
priority?: string
|
|
27
|
+
): T {
|
|
28
|
+
const pld: Record<string, unknown> = { type, params };
|
|
29
|
+
if (priority) pld.priority = priority;
|
|
30
|
+
const r = $send('task', pld);
|
|
31
|
+
if (r == null) return undefined as T;
|
|
32
|
+
if ((r as any)?.err) throw new Error((r as any).err);
|
|
33
|
+
return r as T;
|
|
24
34
|
}
|