yeow-api 0.1.8 → 0.2.0
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 +3 -3
- package/src/command.ts +1 -11
- package/src/event.ts +42 -50
- package/src/index.ts +4 -12
- package/src/location.ts +5 -15
- package/src/player.ts +15 -58
- package/src/server.ts +3 -27
- package/src/task.ts +11 -13
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yeow-api",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Yeow API �?TypeScript OOP wrappers over the task protocol",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Yeow API �?TypeScript OOP wrappers over the task protocol",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
7
7
|
"types": "./src/index.ts",
|
|
@@ -9,4 +9,4 @@
|
|
|
9
9
|
"src/"
|
|
10
10
|
],
|
|
11
11
|
"license": "MIT"
|
|
12
|
-
}
|
|
12
|
+
}
|
package/src/command.ts
CHANGED
|
@@ -1,25 +1,15 @@
|
|
|
1
1
|
import { call } from './task.js';
|
|
2
2
|
|
|
3
3
|
export interface CommandSender {
|
|
4
|
-
/** Sender display name. */
|
|
5
4
|
readonly name: string;
|
|
6
|
-
/** Player UUID, or "CONSOLE" for console senders. */
|
|
7
5
|
readonly uuid: string;
|
|
8
|
-
/** Whether this sender is a player. */
|
|
9
6
|
readonly isPlayer: boolean;
|
|
10
|
-
/**
|
|
11
|
-
* Send a message back to the sender. Injected by the API at callback time.
|
|
12
|
-
* MiniMessage format is supported.
|
|
13
|
-
*/
|
|
14
7
|
sendMessage(msg: string): void;
|
|
15
8
|
}
|
|
16
9
|
|
|
17
10
|
export interface CommandPayload {
|
|
18
|
-
/** The command sender. Use `Player.get(sender.uuid)` to access full Player properties. */
|
|
19
11
|
readonly sender: CommandSender;
|
|
20
|
-
/** Space-delimited command arguments. */
|
|
21
12
|
readonly args: string[];
|
|
22
|
-
/** The alias used to invoke the command. */
|
|
23
13
|
readonly label: string;
|
|
24
14
|
}
|
|
25
15
|
|
|
@@ -35,7 +25,7 @@ export function registerCommand(name: string, options: CommandOptions): boolean
|
|
|
35
25
|
const executor = options.executor;
|
|
36
26
|
const pluginName = (globalThis as any).__plugin?.name || 'unknown';
|
|
37
27
|
|
|
38
|
-
const cbId = (globalThis as any).
|
|
28
|
+
const cbId = (globalThis as any)._regCb((payload: CommandPayload) => {
|
|
39
29
|
const sender = payload.sender;
|
|
40
30
|
if (sender?.uuid && sender.isPlayer) {
|
|
41
31
|
(sender as any).sendMessage = (msg: string) => {
|
package/src/event.ts
CHANGED
|
@@ -1,18 +1,49 @@
|
|
|
1
1
|
import { call } from './task.js';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export function eventOn(eventType: string, handler: (e: any) => void): () => void {
|
|
4
|
+
// eventOn registers a handler on the JS side.
|
|
5
|
+
// The _onEvent function in init.js dispatches SYNC_CALLBACK messages to handlers.
|
|
6
|
+
// For now, event handlers are set up via a global registry.
|
|
7
|
+
// This is a simplified version — full sync callback event system requires
|
|
8
|
+
// the PluginThread to call _onEvent which processes handlers.
|
|
9
|
+
if (!(globalThis as any).__yeowEventHandlers) (globalThis as any).__yeowEventHandlers = {};
|
|
10
|
+
if (!(globalThis as any).__yeowEventHandlers[eventType]) {
|
|
11
|
+
(globalThis as any).__yeowEventHandlers[eventType] = [];
|
|
12
|
+
call('event.subscribe', {
|
|
13
|
+
pluginName: (globalThis as any).__plugin?.name || 'unknown',
|
|
14
|
+
eventType,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
(globalThis as any).__yeowEventHandlers[eventType].push(handler);
|
|
18
|
+
return () => eventOff(eventType, handler);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function eventOff(eventType: string, handler: (e: any) => void): void {
|
|
22
|
+
const handlers = (globalThis as any).__yeowEventHandlers?.[eventType];
|
|
23
|
+
if (!handlers) return;
|
|
24
|
+
const idx = handlers.indexOf(handler);
|
|
25
|
+
if (idx !== -1) handlers.splice(idx, 1);
|
|
26
|
+
if (handlers.length === 0) {
|
|
27
|
+
delete (globalThis as any).__yeowEventHandlers[eventType];
|
|
28
|
+
call('event.unsubscribe', {
|
|
29
|
+
pluginName: (globalThis as any).__plugin?.name || 'unknown',
|
|
30
|
+
eventType,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
4
34
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
35
|
+
// Install _onEvent handler called by init.js for SYNC_CALLBACK events
|
|
36
|
+
(globalThis as any)._onEvent = (eventType: string, snapshot: any): any => {
|
|
37
|
+
const handlers = (globalThis as any).__yeowEventHandlers?.[eventType];
|
|
38
|
+
if (!handlers?.length) return {};
|
|
8
39
|
|
|
9
|
-
const cancellable = snapshot
|
|
10
|
-
const mods: Record<string,
|
|
40
|
+
const cancellable = snapshot?._cancellable;
|
|
41
|
+
const mods: Record<string, any> = {};
|
|
11
42
|
|
|
12
|
-
for (const
|
|
43
|
+
for (const handler of handlers) {
|
|
13
44
|
try {
|
|
14
45
|
const proxy: any = {};
|
|
15
|
-
for (const key of Object.keys(snapshot)) {
|
|
46
|
+
for (const key of Object.keys(snapshot || {})) {
|
|
16
47
|
if (!key.startsWith('_')) proxy[key] = snapshot[key];
|
|
17
48
|
}
|
|
18
49
|
const localMods: Record<string, any> = {};
|
|
@@ -23,48 +54,9 @@ function _process(internalType: string, snapshot: Record<string, unknown>): Reco
|
|
|
23
54
|
enumerable: true,
|
|
24
55
|
});
|
|
25
56
|
}
|
|
26
|
-
|
|
27
|
-
if (cancellable && localMods.cancelled)
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
} catch (e: any) { console.error(`event: ${e?.message || e}`); }
|
|
57
|
+
handler(proxy);
|
|
58
|
+
if (cancellable && localMods.cancelled) mods.cancelled = true;
|
|
59
|
+
} catch (e: any) { console.error('event:', e?.message || e); }
|
|
31
60
|
}
|
|
32
61
|
return mods;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// SYNC_CALLBACK handler — receives snapshot, returns modifications
|
|
36
|
-
(globalThis as any)._onEvent = (eventType: string, snapshot: any): any => {
|
|
37
|
-
return _process(eventType, snapshot);
|
|
38
62
|
};
|
|
39
|
-
|
|
40
|
-
export function eventOn(eventType: string, handler: (e: any) => void): () => void {
|
|
41
|
-
if (!_handlers[eventType]) {
|
|
42
|
-
_handlers[eventType] = [];
|
|
43
|
-
call('event.subscribe', {
|
|
44
|
-
pluginName: (globalThis as any).__plugin?.name || 'unknown',
|
|
45
|
-
eventType,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
_handlers[eventType].push({ h: handler });
|
|
49
|
-
return () => eventOff(eventType, handler);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function eventOnce(eventType: string, handler: (e: any) => void): () => void {
|
|
53
|
-
const wrapper = (e: any) => { eventOff(eventType, wrapper); handler(e); };
|
|
54
|
-
return eventOn(eventType, wrapper);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function eventOff(eventType: string, handler: (e: any) => void) {
|
|
58
|
-
const entries = _handlers[eventType];
|
|
59
|
-
if (!entries) return;
|
|
60
|
-
for (let i = 0; i < entries.length; i++) {
|
|
61
|
-
if (entries[i].h === handler) { entries.splice(i, 1); break; }
|
|
62
|
-
}
|
|
63
|
-
if (entries.length === 0) {
|
|
64
|
-
delete _handlers[eventType];
|
|
65
|
-
call('event.unsubscribe', {
|
|
66
|
-
pluginName: (globalThis as any).__plugin?.name || 'unknown',
|
|
67
|
-
eventType,
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
export { call
|
|
1
|
+
export { call } from './task.js';
|
|
2
2
|
export { Location } from './location.js';
|
|
3
|
-
export { Entity, LivingEntity } from './entity.js';
|
|
4
3
|
export { Player } from './player.js';
|
|
5
|
-
export { World } from './world.js';
|
|
6
|
-
export { Block } from './block.js';
|
|
7
|
-
export { Inventory } from './inventory.js';
|
|
8
|
-
export { fs, readFileSync, writeFileSync, existsSync } from './fs.js';
|
|
9
|
-
export { path, join, basename, dirname, extname } from './path.js';
|
|
10
4
|
export { registerCommand } from './command.js';
|
|
11
|
-
export type { CommandOptions, CommandPayload } from './command.js';
|
|
12
|
-
export { eventOn,
|
|
13
|
-
export {
|
|
14
|
-
export type { HttpRequest } from './http.js';
|
|
15
|
-
export { broadcast, broadcastSync, dispatchCommand, dispatchCommandSync, getMotd, getMaxPlayers, getVersion } from './server.js';
|
|
5
|
+
export type { CommandOptions, CommandPayload, CommandSender } from './command.js';
|
|
6
|
+
export { eventOn, eventOff } from './event.js';
|
|
7
|
+
export { broadcast, getMotd, getVersion } from './server.js';
|
package/src/location.ts
CHANGED
|
@@ -2,23 +2,13 @@ import { call } from './task.js';
|
|
|
2
2
|
|
|
3
3
|
export class Location {
|
|
4
4
|
constructor(
|
|
5
|
-
public readonly x: number,
|
|
6
|
-
public readonly
|
|
7
|
-
public readonly z: number,
|
|
8
|
-
public readonly yaw: number = 0,
|
|
9
|
-
public readonly pitch: number = 0,
|
|
5
|
+
public readonly x: number, public readonly y: number, public readonly z: number,
|
|
6
|
+
public readonly yaw: number = 0, public readonly pitch: number = 0,
|
|
10
7
|
public readonly world?: string
|
|
11
8
|
) {}
|
|
12
|
-
|
|
13
9
|
static from(raw: Record<string, unknown>): Location {
|
|
14
|
-
return new Location(
|
|
15
|
-
raw.
|
|
16
|
-
(raw.yaw as number) ?? 0, (raw.pitch as number) ?? 0,
|
|
17
|
-
raw.world as string | undefined
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
toObject(): Record<string, unknown> {
|
|
22
|
-
return { x: this.x, y: this.y, z: this.z, yaw: this.yaw, pitch: this.pitch, world: this.world };
|
|
10
|
+
return new Location(raw.x as number, raw.y as number, raw.z as number,
|
|
11
|
+
(raw.yaw as number) ?? 0, (raw.pitch as number) ?? 0, raw.world as string | undefined);
|
|
23
12
|
}
|
|
13
|
+
toObject(): Record<string, unknown> { return { x: this.x, y: this.y, z: this.z, yaw: this.yaw, pitch: this.pitch, world: this.world }; }
|
|
24
14
|
}
|
package/src/player.ts
CHANGED
|
@@ -1,65 +1,22 @@
|
|
|
1
1
|
import { call } from './task.js';
|
|
2
2
|
import { Location } from './location.js';
|
|
3
|
-
import { LivingEntity } from './entity.js';
|
|
4
|
-
import { Inventory } from './inventory.js';
|
|
5
3
|
|
|
6
|
-
export class Player
|
|
7
|
-
static get(identifier: string): Player | null {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
constructor(uuid: string, private _name?: string) { super(uuid); }
|
|
11
|
-
get name(): string { return this._name ?? ''; }
|
|
12
|
-
get ping(): number { return call('player.getPing',{uuid:this.uuid}) as number; }
|
|
13
|
-
get gamemode(): string { return call('player.getGamemode',{uuid:this.uuid}) as string; }
|
|
14
|
-
set gamemode(v: string) { call('player.setGamemode',{uuid:this.uuid,value:v}); }
|
|
15
|
-
get displayName(): string { return call('player.getDisplayName',{uuid:this.uuid}) as string; }
|
|
16
|
-
set displayName(v: string) { call('player.setDisplayName',{uuid:this.uuid,value:v}); }
|
|
17
|
-
get playerListName(): string { return call('player.getPlayerListName',{uuid:this.uuid}) as string; }
|
|
18
|
-
set playerListName(v: string) { call('player.setPlayerListName',{uuid:this.uuid,value:v}); }
|
|
19
|
-
get foodLevel(): number { return call('player.getFoodLevel',{uuid:this.uuid}) as number; }
|
|
20
|
-
set foodLevel(v: number) { call('player.setFoodLevel',{uuid:this.uuid,value:v}); }
|
|
21
|
-
get saturation(): number { return call('player.getSaturation',{uuid:this.uuid}) as number; }
|
|
22
|
-
set saturation(v: number) { call('player.setSaturation',{uuid:this.uuid,value:v}); }
|
|
23
|
-
get exp(): number { return call('player.getExp',{uuid:this.uuid}) as number; }
|
|
24
|
-
set exp(v: number) { call('player.setExp',{uuid:this.uuid,value:v}); }
|
|
25
|
-
get level(): number { return call('player.getLevel',{uuid:this.uuid}) as number; }
|
|
26
|
-
set level(v: number) { call('player.setLevel',{uuid:this.uuid,value:v}); }
|
|
27
|
-
get totalExperience(): number { return call('player.getTotalExperience',{uuid:this.uuid}) as number; }
|
|
28
|
-
giveExp(v: number): Promise<void> { return call('player.giveExp',{uuid:this.uuid,value:v}) as Promise<void>; }
|
|
29
|
-
giveExpSync(v: number): void { call('player.giveExp',{uuid:this.uuid,value:v}); }
|
|
30
|
-
get isOp(): boolean { return call('player.isOp',{uuid:this.uuid}) as boolean; }
|
|
31
|
-
get allowFlight(): boolean { return call('player.getAllowFlight',{uuid:this.uuid}) as boolean; }
|
|
32
|
-
set allowFlight(v: boolean) { call('player.setAllowFlight',{uuid:this.uuid,value:v}); }
|
|
33
|
-
get isFlying(): boolean { return call('player.isFlying',{uuid:this.uuid}) as boolean; }
|
|
34
|
-
set isFlying(v: boolean) { call('player.setFlying',{uuid:this.uuid,value:v}); }
|
|
35
|
-
get walkSpeed(): number { return call('player.getWalkSpeed',{uuid:this.uuid}) as number; }
|
|
36
|
-
set walkSpeed(v: number) { call('player.setWalkSpeed',{uuid:this.uuid,value:v}); }
|
|
37
|
-
get flySpeed(): number { return call('player.getFlySpeed',{uuid:this.uuid}) as number; }
|
|
38
|
-
set flySpeed(v: number) { call('player.setFlySpeed',{uuid:this.uuid,value:v}); }
|
|
39
|
-
hasPermission(node: string): boolean { return call('player.hasPermission',{uuid:this.uuid,node}) as boolean; }
|
|
40
|
-
get compassTarget(): Location | null { const r=call('player.getCompassTarget',{uuid:this.uuid}) as any; return r?Location.from(r):null; }
|
|
41
|
-
set compassTarget(loc: Location) { call('player.setCompassTarget',{uuid:this.uuid,...loc.toObject()}); }
|
|
42
|
-
get bedSpawnLocation(): Location | null { const r=call('player.getBedSpawnLocation',{uuid:this.uuid}) as any; return r?Location.from(r):null; }
|
|
43
|
-
setBedSpawnLocation(loc: Location): Promise<void> { return call('player.setBedSpawnLocation',{uuid:this.uuid,...loc.toObject()}) as Promise<void>; }
|
|
44
|
-
setBedSpawnLocationSync(loc: Location): void { call('player.setBedSpawnLocation',{uuid:this.uuid,...loc.toObject()}); }
|
|
45
|
-
|
|
46
|
-
sendMessage(msg: string): Promise<void> { return call('player.sendMessage',{uuid:this.uuid,message:msg}) as Promise<void>; }
|
|
47
|
-
sendMessageSync(msg: string): void { call('player.sendMessage',{uuid:this.uuid,message:msg}); }
|
|
48
|
-
sendTitle(title: string, subtitle?: string, fadeIn?: number, stay?: number, fadeOut?: number): Promise<void> {
|
|
49
|
-
return call('player.sendTitle',{uuid:this.uuid,title,subtitle,fadeIn,stay,fadeOut}) as Promise<void>;
|
|
50
|
-
}
|
|
51
|
-
sendTitleSync(title: string, subtitle?: string, fadeIn?: number, stay?: number, fadeOut?: number): void {
|
|
52
|
-
call('player.sendTitle',{uuid:this.uuid,title,subtitle,fadeIn,stay,fadeOut});
|
|
4
|
+
export class Player {
|
|
5
|
+
static get(identifier: string): Player | null {
|
|
6
|
+
const d = call('player.get', { identifier }) as any;
|
|
7
|
+
return d ? new Player(d.uuid, d.name) : null;
|
|
53
8
|
}
|
|
54
|
-
|
|
55
|
-
return call('player.
|
|
9
|
+
static getAll(): Player[] {
|
|
10
|
+
return (call('player.getAll', {}) as any[]).map((r: any) => new Player(r.uuid, r.name));
|
|
56
11
|
}
|
|
57
|
-
playSoundSync(sound: string, volume?: number, pitch?: number): void {
|
|
58
|
-
call('player.playSound',{uuid:this.uuid,sound,volume,pitch});
|
|
59
|
-
}
|
|
60
|
-
kick(reason?: string): Promise<void> { return call('player.kick',{uuid:this.uuid,reason}) as Promise<void>; }
|
|
61
|
-
kickSync(reason?: string): void { call('player.kick',{uuid:this.uuid,reason}); }
|
|
62
12
|
|
|
63
|
-
|
|
64
|
-
|
|
13
|
+
constructor(public readonly uuid: string, private _name?: string) {}
|
|
14
|
+
|
|
15
|
+
get name(): string { return this._name ?? ''; }
|
|
16
|
+
get ping(): number { return call('player.getPing', { uuid: this.uuid }) as number; }
|
|
17
|
+
get gamemode(): string { return call('player.getGamemode', { uuid: this.uuid }) as string; }
|
|
18
|
+
set gamemode(v: string) { call('player.setGamemode', { uuid: this.uuid, value: v }); }
|
|
19
|
+
|
|
20
|
+
sendMessage(msg: string): void { call('player.sendMessage', { uuid: this.uuid, message: msg }); }
|
|
21
|
+
kick(reason?: string): void { call('player.kick', { uuid: this.uuid, reason }); }
|
|
65
22
|
}
|
package/src/server.ts
CHANGED
|
@@ -1,29 +1,5 @@
|
|
|
1
1
|
import { call } from './task.js';
|
|
2
2
|
|
|
3
|
-
export function broadcast(msg: string):
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function broadcastSync(msg: string): void {
|
|
8
|
-
call('server.broadcast', { message: msg });
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function getMotd(): string {
|
|
12
|
-
return call('server.getMotd', {}) as string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function getVersion(): string {
|
|
16
|
-
return call('server.getVersion', {}) as string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function getMaxPlayers(): number {
|
|
20
|
-
return call('server.getMaxPlayers', {}) as number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function dispatchCommand(cmd: string): Promise<void> {
|
|
24
|
-
return call('server.dispatchCommand', { command: cmd }) as Promise<void>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function dispatchCommandSync(cmd: string): void {
|
|
28
|
-
call('server.dispatchCommand', { command: cmd });
|
|
29
|
-
}
|
|
3
|
+
export function broadcast(msg: string): void { call('server.broadcast', { message: msg }); }
|
|
4
|
+
export function getMotd(): string { return call('server.getMotd', {}) as string; }
|
|
5
|
+
export function getVersion(): string { return call('server.getVersion', {}) as string; }
|
package/src/task.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
export function
|
|
2
|
-
const json = typeof params === 'object' && params !== null ? JSON.stringify(params) : '{}';
|
|
3
|
-
const raw = (globalThis as any).$submitSync(type, json);
|
|
4
|
-
if (typeof raw !== 'string') return undefined;
|
|
5
|
-
const result = JSON.parse(raw);
|
|
6
|
-
if (result && result.err) throw new Error(result.err);
|
|
7
|
-
return result;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function post(type: string, params?: Record<string, unknown>): Promise<unknown> {
|
|
1
|
+
export function post(type: string, params: Record<string, unknown> = {}): Promise<unknown> {
|
|
11
2
|
return new Promise((resolve, reject) => {
|
|
12
|
-
const
|
|
13
|
-
const cbId = (globalThis as any)._registerCallback((result: any) => {
|
|
3
|
+
const cbId = (globalThis as any)._regCb((result: any) => {
|
|
14
4
|
if (result?.err) reject(new Error(result.err));
|
|
15
5
|
else resolve(result);
|
|
16
6
|
});
|
|
17
|
-
(globalThis as any).$
|
|
7
|
+
(globalThis as any).$send('game', JSON.stringify({ t: type, p: params, cb: cbId }));
|
|
18
8
|
});
|
|
19
9
|
}
|
|
10
|
+
|
|
11
|
+
export function call(type: string, params: Record<string, unknown> = {}): unknown {
|
|
12
|
+
const raw = (globalThis as any).$send('game', JSON.stringify({ t: type, p: params }));
|
|
13
|
+
if (!raw) return undefined;
|
|
14
|
+
const r = JSON.parse(raw as string);
|
|
15
|
+
if (r.err) throw new Error(r.err);
|
|
16
|
+
return r;
|
|
17
|
+
}
|