yeow-api 0.3.0 → 0.3.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yeow-api",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Yeow API",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/command.ts CHANGED
@@ -79,23 +79,13 @@ export function registerCommand(name: string, options: CommandOptions, taskOptio
79
79
  } else {
80
80
  const result = completerFn(sender, data.args);
81
81
  if (result && typeof result.then === 'function') {
82
- // 异步 completer:await 结果后再回传(原实现立即发空补全、Promise 结果被丢弃)
83
- (result as Promise<string[]>).then(
84
- (list) => {
85
- $send('task', {
86
- type: 'command.tabComplete',
87
- params: { callbackId: compCbId, completions: list || [] },
88
- cb: '',
89
- });
90
- },
91
- () => {
92
- $send('task', {
93
- type: 'command.tabComplete',
94
- params: { callbackId: compCbId, completions: [] },
95
- cb: '',
96
- });
97
- },
98
- );
82
+ // 自动模式不等待 Promise(与事件处理一致):收到 Promise 视为无补全,
83
+ // 立即释放(空补全);异步补全请使用手动模式 complete(res)
84
+ $send('task', {
85
+ type: 'command.tabComplete',
86
+ params: { callbackId: compCbId, completions: [] },
87
+ cb: '',
88
+ });
99
89
  } else {
100
90
  $send('task', {
101
91
  type: 'command.tabComplete',
package/src/event.ts CHANGED
@@ -358,11 +358,12 @@ function loc(raw: Record<string, unknown> | null): Location | null {
358
358
  );
359
359
  }
360
360
 
361
- function adaptEvent<K extends keyof EventMap>(type: K, data: RawEvent): EventMap[K] {
362
- const wrap: Record<string, unknown> = {};
361
+ function adaptEvent<K extends keyof EventMap>(type: K, data: RawEvent): { event: EventMap[K]; mods: Record<string, unknown> } {
362
+ // 初始值:原始数据(跳过 _ 前缀内部字段),player/from/to/respawnLocation 适配为对象
363
+ const initial: Record<string, unknown> = {};
363
364
  for (const key of Object.keys(data)) {
364
365
  if (key.startsWith('_')) continue;
365
- wrap[key] = data[key];
366
+ initial[key] = data[key];
366
367
  }
367
368
  const hasPlayer = [
368
369
  'playerJoin', 'playerQuit', 'playerChat', 'playerMove',
@@ -375,13 +376,33 @@ function adaptEvent<K extends keyof EventMap>(type: K, data: RawEvent): EventMap
375
376
  'playerAdvancementDone', 'playerToggleSneak', 'playerToggleFlight',
376
377
  'inventoryClick', 'playerResourcePackStatus',
377
378
  ] as K[];
378
- if (hasPlayer.includes(type) && data.player) {
379
- wrap.player = Player.getSync(data.player as string);
379
+ if (hasPlayer.includes(type) && data.player) initial.player = Player.getSync(data.player as string);
380
+ if (data.from) initial.from = loc(data.from as Record<string, unknown>);
381
+ if (data.to) initial.to = loc(data.to as Record<string, unknown>);
382
+ if (data.respawnLocation) initial.respawnLocation = loc(data.respawnLocation as Record<string, unknown>);
383
+
384
+ // 修改收集:所有字段经 getter/setter——handler 直接赋值(e.xxx = ...)即记录为回写 mods。
385
+ // cancelled 单独处理(仅可取消事件暴露;读取语义保持原状:未设置时返回 false)。
386
+ const mods: Record<string, unknown> = {};
387
+ const wrap: Record<string, unknown> = {};
388
+ for (const key of Object.keys(initial)) {
389
+ if (key === 'cancelled') continue;
390
+ Object.defineProperty(wrap, key, {
391
+ get: () => (key in mods ? mods[key] : initial[key]),
392
+ set: (v: unknown) => { mods[key] = v; },
393
+ enumerable: true,
394
+ configurable: true,
395
+ });
380
396
  }
381
- if (data.from) wrap.from = loc(data.from as Record<string, unknown>);
382
- if (data.to) wrap.to = loc(data.to as Record<string, unknown>);
383
- if (data.respawnLocation) wrap.respawnLocation = loc(data.respawnLocation as Record<string, unknown>);
384
- return wrap as unknown as EventMap[K];
397
+ if (data._cancellable) {
398
+ Object.defineProperty(wrap, 'cancelled', {
399
+ get: () => (mods.cancelled as boolean) || false,
400
+ set: (v: boolean) => { mods.cancelled = v; },
401
+ enumerable: true,
402
+ configurable: true,
403
+ });
404
+ }
405
+ return { event: wrap as unknown as EventMap[K], mods };
385
406
  }
386
407
 
387
408
  // ── Event Subscription ─────────────────────────────────────────────
@@ -411,18 +432,8 @@ export function eventOn<K extends keyof EventMap>(
411
432
  const pluginName = __plugin?.name || 'unknown';
412
433
 
413
434
  const cbId = _registerCallback((data: RawEvent) => {
414
- const wrapped = adaptEvent(eventType, data);
435
+ const { event: wrapped, mods: mutatedMods } = adaptEvent(eventType, data);
415
436
  const eventId = data?._eventId;
416
- const cancellable = data?._cancellable;
417
- const localMods: { cancelled?: boolean } = {};
418
-
419
- if (cancellable) {
420
- Object.defineProperty(wrapped, 'cancelled', {
421
- get: () => localMods.cancelled || false,
422
- set: (v: boolean) => { localMods.cancelled = v; },
423
- enumerable: true,
424
- });
425
- }
426
437
 
427
438
  if (manualRelease) {
428
439
  const complete = (result?: Record<string, unknown>) => {
@@ -437,10 +448,12 @@ export function eventOn<K extends keyof EventMap>(
437
448
  }
438
449
 
439
450
  const result = (handler as EventHandler<typeof eventType>)(wrapped);
451
+ // 回写合并:返回值(mods)优先合并,事件参数直接赋值(e.xxx = ...)覆盖之——
452
+ // 返回 Promise 时视为无修改(Promise 不展开),事件立即释放
440
453
  const mods: Record<string, unknown> = {
441
454
  ...(result && typeof result === 'object' ? result : {}),
455
+ ...mutatedMods,
442
456
  };
443
- if (localMods.cancelled) mods.cancelled = true;
444
457
  $send('task', { type: 'event.complete', params: { eventId, mods }, cb: '' });
445
458
  }, { persistent: true });
446
459