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/package.json
CHANGED
package/src/assets.ts
CHANGED
|
@@ -1,41 +1,48 @@
|
|
|
1
|
-
function _sendAssets(payload) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
function _sendAssets(payload: Record<string, unknown>): unknown {
|
|
2
|
+
const r = $send('assets', 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 read(path) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
export function read(path: string): Promise<string> {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
try {
|
|
11
|
+
const r = _sendAssets({ t: 'read', p: { path } }) as { data: string };
|
|
12
|
+
resolve(r.data);
|
|
13
|
+
} catch (e) { reject(e); }
|
|
14
|
+
});
|
|
12
15
|
}
|
|
13
|
-
export function readSync(path) {
|
|
14
|
-
|
|
16
|
+
export function readSync(path: string): string {
|
|
17
|
+
return (_sendAssets({ t: 'read', p: { path } }) as { data: string }).data;
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
export function readBase64(path) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
export function readBase64(path: string): Promise<string> {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
try {
|
|
23
|
+
const r = _sendAssets({ t: 'readBase64', p: { path } }) as { data: string };
|
|
24
|
+
resolve(r.data);
|
|
25
|
+
} catch (e) { reject(e); }
|
|
26
|
+
});
|
|
21
27
|
}
|
|
22
|
-
export function readBase64Sync(path) {
|
|
23
|
-
|
|
28
|
+
export function readBase64Sync(path: string): string {
|
|
29
|
+
return (_sendAssets({ t: 'readBase64', p: { path } }) as { data: string }).data;
|
|
24
30
|
}
|
|
25
31
|
|
|
26
|
-
export function extract(path, dest) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
});
|
|
32
|
+
export function extract(path: string, dest?: string): Promise<string> {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
try {
|
|
35
|
+
const p: Record<string, unknown> = { path };
|
|
36
|
+
if (dest) p.dest = dest;
|
|
37
|
+
const r = _sendAssets({ t: 'extract', p }) as { path: string };
|
|
38
|
+
resolve(r.path);
|
|
39
|
+
} catch (e) { reject(e); }
|
|
40
|
+
});
|
|
34
41
|
}
|
|
35
|
-
export function extractSync(path, dest) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
export function extractSync(path: string, dest?: string): string {
|
|
43
|
+
const p: Record<string, unknown> = { path };
|
|
44
|
+
if (dest) p.dest = dest;
|
|
45
|
+
return (_sendAssets({ t: 'extract', p }) as { path: string }).path;
|
|
39
46
|
}
|
|
40
47
|
|
|
41
48
|
export const assets = { read, readSync, readBase64, readBase64Sync, extract, extractSync };
|
package/src/block.ts
CHANGED
|
@@ -2,16 +2,18 @@ import { call } from './task.js';
|
|
|
2
2
|
import { Location } from './location.js';
|
|
3
3
|
|
|
4
4
|
export class Block {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
constructor(
|
|
6
|
+
public readonly world: string,
|
|
7
|
+
public readonly x: number,
|
|
8
|
+
public readonly y: number,
|
|
9
|
+
public readonly z: number,
|
|
10
|
+
public readonly type: string,
|
|
11
|
+
) {}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
get location(): Location {
|
|
14
|
+
return new Location(this.x, this.y, this.z, undefined, undefined, this.world);
|
|
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 }); }
|
|
17
19
|
}
|
package/src/command.ts
CHANGED
|
@@ -1,58 +1,104 @@
|
|
|
1
1
|
import { call } from './task.js';
|
|
2
2
|
|
|
3
3
|
export interface CommandSender {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly uuid: string;
|
|
6
|
+
readonly isPlayer: boolean;
|
|
7
|
+
sendMessage(msg: string): void;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export interface CommandPayload {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
readonly sender: CommandSender;
|
|
12
|
+
readonly args: string[];
|
|
13
|
+
readonly label: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type CompleterFn = (sender: CommandSender, args: string[]) => string[] | Promise<string[]>;
|
|
17
|
+
export interface ManualCompleter {
|
|
18
|
+
manualRelease: true;
|
|
19
|
+
handler: (sender: CommandSender, args: string[], complete: (result: string[]) => void) => void;
|
|
14
20
|
}
|
|
15
21
|
|
|
16
22
|
export interface CommandOptions {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
completer?: (sender: CommandSender, args: string[]) => string[];
|
|
23
|
+
description?: string;
|
|
24
|
+
usage?: string;
|
|
25
|
+
permission?: string;
|
|
26
|
+
aliases?: string[];
|
|
27
|
+
executor: (payload: CommandPayload) => void;
|
|
28
|
+
completer?: CompleterFn | ManualCompleter;
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
export function registerCommand(name: string, options: CommandOptions): boolean {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const cbId = (globalThis as any)._registerCallback((payload: CommandPayload) => {
|
|
32
|
-
const sender = payload.sender;
|
|
33
|
-
if (sender?.uuid && sender.isPlayer) {
|
|
34
|
-
(sender as any).sendMessage = (msg: string) => {
|
|
35
|
-
call('player.sendMessage', { uuid: sender.uuid, message: msg });
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
executor(payload);
|
|
39
|
-
}, { persistent: true });
|
|
32
|
+
const executor = options.executor;
|
|
33
|
+
const completer = options.completer;
|
|
34
|
+
const pluginName = __plugin?.name || 'unknown';
|
|
40
35
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
36
|
+
const cbId = _registerCallback((payload: CommandPayload) => {
|
|
37
|
+
const sender = payload.sender;
|
|
38
|
+
if (sender?.uuid && sender.isPlayer) {
|
|
39
|
+
(sender as any).sendMessage = (msg: string) => {
|
|
40
|
+
call('player.sendMessage', { uuid: sender.uuid, message: msg });
|
|
41
|
+
};
|
|
46
42
|
}
|
|
43
|
+
executor(payload);
|
|
44
|
+
}, { persistent: true });
|
|
45
|
+
|
|
46
|
+
let compCbId = '';
|
|
47
|
+
if (completer) {
|
|
48
|
+
let manualRelease = false;
|
|
49
|
+
let completerFn: Function;
|
|
50
|
+
if (typeof completer === 'object' && (completer as any).manualRelease) {
|
|
51
|
+
manualRelease = true;
|
|
52
|
+
completerFn = (completer as any).handler;
|
|
53
|
+
} else {
|
|
54
|
+
completerFn = completer as CompleterFn;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
compCbId = _registerCallback((data: any) => {
|
|
58
|
+
try {
|
|
59
|
+
if (manualRelease) {
|
|
60
|
+
const complete = (result: string[]) => {
|
|
61
|
+
$send('task', {
|
|
62
|
+
type: 'command.tabComplete',
|
|
63
|
+
params: { callbackId: compCbId, completions: result },
|
|
64
|
+
cb: '',
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
completerFn(data.sender, data.args, complete);
|
|
68
|
+
} else {
|
|
69
|
+
const result = completerFn(data.sender, data.args);
|
|
70
|
+
if (result && typeof result.then === 'function') {
|
|
71
|
+
$send('task', {
|
|
72
|
+
type: 'command.tabComplete',
|
|
73
|
+
params: { callbackId: compCbId, completions: [] },
|
|
74
|
+
cb: '',
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
$send('task', {
|
|
78
|
+
type: 'command.tabComplete',
|
|
79
|
+
params: { callbackId: compCbId, completions: result || [] },
|
|
80
|
+
cb: '',
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} catch (_e) {
|
|
85
|
+
$send('task', {
|
|
86
|
+
type: 'command.tabComplete',
|
|
87
|
+
params: { callbackId: compCbId, completions: [] },
|
|
88
|
+
cb: '',
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}, { persistent: true });
|
|
92
|
+
}
|
|
47
93
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
94
|
+
return call<boolean>('command.register', {
|
|
95
|
+
pluginName,
|
|
96
|
+
commandName: name,
|
|
97
|
+
callbackId: String(cbId),
|
|
98
|
+
completerCbId: compCbId,
|
|
99
|
+
description: options.description,
|
|
100
|
+
usage: options.usage,
|
|
101
|
+
permission: options.permission,
|
|
102
|
+
aliases: options.aliases,
|
|
103
|
+
});
|
|
58
104
|
}
|
package/src/entity.ts
CHANGED
|
@@ -1,36 +1,39 @@
|
|
|
1
1
|
import { call, post } from './task.js';
|
|
2
|
-
import { Location } from './location.js';
|
|
2
|
+
import { Location, LocationData } from './location.js';
|
|
3
3
|
|
|
4
4
|
export class Entity {
|
|
5
|
-
|
|
5
|
+
constructor(public readonly uuid: string) {}
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
7
|
+
get type(): string { return call<string>('entity.getType', { uuid: this.uuid }); }
|
|
8
|
+
get name(): string { return call<string>('entity.getName', { uuid: this.uuid }); }
|
|
9
|
+
get customName(): string | null { return call<string | null>('entity.getCustomName', { uuid: this.uuid }); }
|
|
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 }); }
|
|
12
|
+
get world(): string | null { return call<string | null>('entity.getWorld', { uuid: this.uuid }); }
|
|
13
|
+
get location(): Location | null {
|
|
14
|
+
const r = call<LocationData>('entity.getLocation', { uuid: this.uuid });
|
|
15
|
+
return r ? Location.from(r) : null;
|
|
16
|
+
}
|
|
17
|
+
get isGlowing(): boolean { return call<boolean>('entity.isGlowing', { uuid: this.uuid }); }
|
|
18
|
+
set isGlowing(v: boolean) { call('entity.setGlowing', { uuid: this.uuid, value: v }); }
|
|
19
|
+
get isInvulnerable(): boolean { return call<boolean>('entity.isInvulnerable', { uuid: this.uuid }); }
|
|
20
|
+
set isInvulnerable(v: boolean) { call('entity.setInvulnerable', { uuid: this.uuid, value: v }); }
|
|
21
|
+
get isSilent(): boolean { return call<boolean>('entity.isSilent', { uuid: this.uuid }); }
|
|
22
|
+
set isSilent(v: boolean) { call('entity.setSilent', { uuid: this.uuid, value: v }); }
|
|
23
|
+
get hasGravity(): boolean { return call<boolean>('entity.hasGravity', { uuid: this.uuid }); }
|
|
24
|
+
set hasGravity(v: boolean) { call('entity.setGravity', { uuid: this.uuid, value: v }); }
|
|
25
|
+
get passengers(): string[] { return call<string[]>('entity.getPassengers', { uuid: this.uuid }); }
|
|
26
|
+
get vehicle(): string | null { return call<string | null>('entity.getVehicle', { uuid: this.uuid }); }
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
remove(): Promise<void> { return post('entity.remove', { uuid: this.uuid }); }
|
|
29
|
+
removeSync(): void { call('entity.remove', { uuid: this.uuid }); }
|
|
30
|
+
teleport(loc: Location): Promise<void> { return post('entity.teleport', { uuid: this.uuid, ...loc.toObject() }); }
|
|
31
|
+
teleportSync(loc: Location): void { call('entity.teleport', { uuid: this.uuid, ...loc.toObject() }); }
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
export class LivingEntity extends Entity {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
get health(): number { return call<number>('entity.getHealth', { uuid: this.uuid }); }
|
|
36
|
+
set health(v: number) { call('entity.setHealth', { uuid: this.uuid, value: v }); }
|
|
37
|
+
get maxHealth(): number { return call<number>('entity.getMaxHealth', { uuid: this.uuid }); }
|
|
38
|
+
get isDead(): boolean { return call<boolean>('entity.isDead', { uuid: this.uuid }); }
|
|
36
39
|
}
|