minecraft-inventory 0.1.25 → 0.1.27

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": "minecraft-inventory",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "release": {
@@ -133,15 +133,26 @@ export function InventoryOverlay({
133
133
  }, [jeiOnGetRecipes, jeiOnGetUsages, pushRecipeFrame])
134
134
 
135
135
  // Fires for any click that isn't stopped by an interactive panel (inventory, hotbar, JEI, etc.)
136
- const handleBackdropClick = useCallback(() => {
137
- // Clicking the backdrop clears focused slot (regardless of heldItem)
138
- if (focusedSlot !== null) {
139
- setFocusedSlot(null)
140
- return
141
- }
136
+ const handleBackdropClick = useCallback((e: React.MouseEvent) => {
137
+ // Only handle left (drop all) and right (drop one) mouse buttons
138
+ if (e.button !== 0 && e.button !== 2) return
142
139
  if (heldItem) {
143
- sendAction({ type: 'drop', slotIndex: -1, all: true })
144
- setHeldItem(null)
140
+ const dropAll = e.button === 0 // LMB = drop all, RMB = drop one
141
+ sendAction({ type: 'drop', slotIndex: -1, all: dropAll })
142
+ if (dropAll) {
143
+ setHeldItem(null)
144
+ } else {
145
+ // Right click: drop one, keep rest on cursor
146
+ if (heldItem.count > 1) {
147
+ setHeldItem({ ...heldItem, count: heldItem.count - 1 })
148
+ } else {
149
+ setHeldItem(null)
150
+ }
151
+ }
152
+ // Also clear focused slot if any
153
+ if (focusedSlot !== null) setFocusedSlot(null)
154
+ } else if (focusedSlot !== null) {
155
+ setFocusedSlot(null)
145
156
  } else {
146
157
  onClose?.()
147
158
  }
@@ -183,7 +194,8 @@ export function InventoryOverlay({
183
194
  <>
184
195
  <div
185
196
  className={['mc-inv-overlay', className].filter(Boolean).join(' ')}
186
- onClick={handleBackdropClick}
197
+ onMouseDown={handleBackdropClick}
198
+ onContextMenu={(e) => e.preventDefault()}
187
199
  style={{
188
200
  position: 'absolute',
189
201
  inset: 0,
@@ -217,6 +229,7 @@ export function InventoryOverlay({
217
229
  {showJEI && jeiPosition === 'left' && (
218
230
  <div
219
231
  className="mc-inv-overlay-jei mc-inv-overlay-jei-left"
232
+ onMouseDown={(e) => e.stopPropagation()}
220
233
  onClick={(e) => e.stopPropagation()}
221
234
  style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column', width: '100%' }}
222
235
  >
@@ -237,12 +250,14 @@ export function InventoryOverlay({
237
250
  display: 'flex',
238
251
  justifyContent: 'center',
239
252
  alignItems: 'center',
253
+ pointerEvents: 'none',
240
254
  }}
241
255
  >
242
- <div className="mc-inv-overlay-content" style={{ position: 'relative' }}>
256
+ <div className="mc-inv-overlay-content" style={{ position: 'relative', pointerEvents: 'auto' }}>
243
257
  {/* Inventory / Recipe view — clicks clear focused slot; slots stop propagation themselves */}
244
258
  <div
245
259
  className="mc-inv-overlay-window"
260
+ onMouseDown={(e) => e.stopPropagation()}
246
261
  onClick={(e) => {
247
262
  e.stopPropagation()
248
263
  // Clicking the inventory background (not a slot) clears focused slot
@@ -274,6 +289,7 @@ export function InventoryOverlay({
274
289
  {showJEI && jeiPosition === 'right' && (
275
290
  <div
276
291
  className="mc-inv-overlay-side mc-inv-overlay-side-right"
292
+ onMouseDown={(e) => e.stopPropagation()}
277
293
  onClick={(e) => e.stopPropagation()}
278
294
  style={{ ...sidePanelBase, right: sideGapPx, pointerEvents: 'auto' }}
279
295
  >
@@ -310,7 +326,7 @@ export function InventoryOverlay({
310
326
  lineHeight: 1,
311
327
  }}
312
328
  >
313
- INV 0.1.25
329
+ INV 0.1.27
314
330
  </a>
315
331
  )}
316
332
 
@@ -1,6 +1,15 @@
1
1
  import type { InventoryAction, InventoryWindowState, PlayerState, SlotState, ItemStack } from '../types'
2
2
  import type { InventoryConnector, ConnectorListener, ConnectorEvent, MineflayerBot } from './types'
3
3
  import { getInventoryType } from '../registry'
4
+ import {
5
+ createInventoryDebugSession,
6
+ getActionSlotIndexes,
7
+ sanitizeDebugValue,
8
+ summarizeAction,
9
+ summarizeItem,
10
+ summarizeSlots,
11
+ summarizeWindowState,
12
+ } from '../debug/inventoryDebug'
4
13
 
5
14
  type RawSlot = { type: number; count: number; metadata?: number; nbt?: unknown }
6
15
 
@@ -170,6 +179,24 @@ export function createMineflayerConnector(bot: MineflayerBot, options?: Mineflay
170
179
  // stale server responses overwriting our predicted stateId mid-sequence.
171
180
  let isDraggingRaw = false
172
181
 
182
+ const debugSession = createInventoryDebugSession(
183
+ hotbarOnly ? 'mineflayer-connector:hotbar' : 'mineflayer-connector',
184
+ () => {
185
+ const state = buildWindowState()
186
+ return {
187
+ windowState: state,
188
+ playerState: buildPlayerState(),
189
+ heldItem: state?.heldItem ?? null,
190
+ }
191
+ },
192
+ )
193
+ debugSession.log({
194
+ event: 'connector.mount',
195
+ data: {
196
+ hotbarOnly,
197
+ },
198
+ })
199
+
173
200
  // Resolve the Item class (prismarine-item) for converting notch-format items.
174
201
  // We extract it lazily from the first non-null slot item's constructor.
175
202
  let ItemClass: { fromNotch(notch: unknown): unknown } | null = null
@@ -318,6 +345,58 @@ export function createMineflayerConnector(bot: MineflayerBot, options?: Mineflay
318
345
  }
319
346
  }
320
347
 
348
+ function logActionEvent(
349
+ event: string,
350
+ action: InventoryAction,
351
+ data?: Record<string, unknown>,
352
+ ) {
353
+ const state = buildWindowState()
354
+ const slotIndexes = getActionSlotIndexes(action)
355
+ debugSession.log({
356
+ event,
357
+ windowId: state?.windowId,
358
+ windowType: state?.type,
359
+ action: summarizeAction(action),
360
+ stateId: dragStateId,
361
+ slots: state ? summarizeSlots(state.slots, slotIndexes) : undefined,
362
+ heldItem: summarizeItem(state?.heldItem),
363
+ data: data ? sanitizeDebugValue(data) : summarizeWindowState(state, slotIndexes),
364
+ })
365
+ }
366
+
367
+ function logPacketWrite(packet: string, params: unknown) {
368
+ const state = buildWindowState()
369
+ const paramsObject = params && typeof params === 'object' ? params as Record<string, unknown> : {}
370
+ debugSession.log({
371
+ event: 'connector.packetWrite',
372
+ windowId: typeof paramsObject.windowId === 'number' ? paramsObject.windowId : state?.windowId,
373
+ windowType: state?.type,
374
+ stateId: typeof paramsObject.stateId === 'number' ? paramsObject.stateId : dragStateId,
375
+ data: {
376
+ packet,
377
+ params: sanitizeDebugValue(params),
378
+ },
379
+ })
380
+ }
381
+
382
+ function logHelperIntent(action: InventoryAction, helper: string, params: Record<string, unknown>) {
383
+ const state = buildWindowState()
384
+ const slotIndexes = getActionSlotIndexes(action)
385
+ debugSession.log({
386
+ event: 'connector.helperIntent',
387
+ windowId: state?.windowId,
388
+ windowType: state?.type,
389
+ action: summarizeAction(action),
390
+ stateId: dragStateId,
391
+ slots: state ? summarizeSlots(state.slots, slotIndexes) : undefined,
392
+ heldItem: summarizeItem(state?.heldItem),
393
+ data: {
394
+ helper,
395
+ params: sanitizeDebugValue(params),
396
+ },
397
+ })
398
+ }
399
+
321
400
  /** Compute anvil cost client-side if callback is provided and window is anvil. */
322
401
  function tryComputeAnvilCost() {
323
402
  if (!computeAnvilCost || !currentWindowType) return
@@ -516,26 +595,40 @@ export function createMineflayerConnector(bot: MineflayerBot, options?: Mineflay
516
595
  openPlayerInventory,
517
596
 
518
597
  sendAction: async (action: InventoryAction) => {
598
+ logActionEvent('connector.action.start', action)
519
599
  try {
520
600
  // Hotbar "open inventory" button — delegates to openPlayerInventory()
521
601
  if (action.type === 'open-inventory') {
522
602
  await openPlayerInventory()
603
+ logActionEvent('connector.action.success', action)
523
604
  return
524
605
  }
525
606
 
526
607
  const win = bot.currentWindow
527
608
 
528
609
  if (action.type === 'trade' && win) {
610
+ let handled = false
529
611
  if (ext.trade && isVillagerWindow(win)) {
612
+ logHelperIntent(action, 'bot.trade', { tradeIndex: action.tradeIndex, count: 1 })
530
613
  await ext.trade(win, action.tradeIndex, 1)
614
+ handled = true
531
615
  } else if (isVillagerWindow(win)) {
616
+ logHelperIntent(action, 'window.trade', { tradeIndex: action.tradeIndex, count: 1 })
532
617
  await win.trade(action.tradeIndex, 1)
618
+ handled = true
619
+ }
620
+ if (handled) {
621
+ logActionEvent('connector.action.success', action)
622
+ } else {
623
+ logActionEvent('connector.action.skipped', action, { reason: 'missing_trade_handler' })
533
624
  }
534
625
  return
535
626
  }
536
627
 
537
628
  if (action.type === 'enchant' && win && isEnchantmentTableWindow(win)) {
629
+ logHelperIntent(action, 'window.enchant', { enchantIndex: action.enchantIndex })
538
630
  await win.enchant(action.enchantIndex)
631
+ logActionEvent('connector.action.success', action)
539
632
  return
540
633
  }
541
634
 
@@ -544,23 +637,44 @@ export function createMineflayerConnector(bot: MineflayerBot, options?: Mineflay
544
637
  // because it tries to transfer items from player inventory into anvil,
545
638
  // which fails when the user already placed items via the UI.
546
639
  if (ext.supportFeature?.('useMCItemName')) {
547
- if (!ext._client.registerChannel) return
640
+ if (!ext._client.registerChannel) {
641
+ logActionEvent('connector.action.skipped', action, { reason: 'missing_registerChannel' })
642
+ return
643
+ }
548
644
  ext._client.registerChannel('MC|ItemName', 'string')
645
+ logPacketWrite('MC|ItemName', action.text)
549
646
  ext._client.writeChannel?.('MC|ItemName', action.text)
550
647
  } else {
551
- ext._client.write('name_item', { name: action.text })
648
+ const packet = { name: action.text }
649
+ logPacketWrite('name_item', packet)
650
+ ext._client.write('name_item', packet)
552
651
  }
652
+ logActionEvent('connector.action.success', action)
553
653
  return
554
654
  }
555
655
 
556
656
  if (action.type === 'beacon' && win && isBeaconWindow(win)) {
657
+ let handled = false
557
658
  if (typeof win.setBeaconEffects === 'function') {
659
+ logHelperIntent(action, 'window.setBeaconEffects', {
660
+ primaryEffect: action.primaryEffect,
661
+ secondaryEffect: action.secondaryEffect,
662
+ })
558
663
  await win.setBeaconEffects(action.primaryEffect, action.secondaryEffect)
664
+ handled = true
559
665
  } else if (ext._client) {
560
- ext._client.write('beacon_effect', {
666
+ const packet = {
561
667
  primaryEffect: action.primaryEffect,
562
668
  secondaryEffect: action.secondaryEffect,
563
- })
669
+ }
670
+ logPacketWrite('beacon_effect', packet)
671
+ ext._client.write('beacon_effect', packet)
672
+ handled = true
673
+ }
674
+ if (handled) {
675
+ logActionEvent('connector.action.success', action)
676
+ } else {
677
+ logActionEvent('connector.action.skipped', action, { reason: 'missing_beacon_handler' })
564
678
  }
565
679
  return
566
680
  }
@@ -568,10 +682,13 @@ export function createMineflayerConnector(bot: MineflayerBot, options?: Mineflay
568
682
  if (action.type === 'click' && action.mode === 'double') {
569
683
  // bot.clickWindow() throws for mode=6 (prismarine-windows doubleClick is unimplemented).
570
684
  // Send raw window_click packet directly, same approach as drag (mode=5).
571
- if (!ext._client) return
685
+ if (!ext._client) {
686
+ logActionEvent('connector.action.skipped', action, { reason: 'missing_client' })
687
+ return
688
+ }
572
689
  const windowId = bot.currentWindow ? bot.currentWindow.id : 0
573
690
 
574
- ext._client.write('window_click', {
691
+ const packet = {
575
692
  windowId,
576
693
  stateId: dragStateId,
577
694
  slot: action.slotIndex,
@@ -579,19 +696,31 @@ export function createMineflayerConnector(bot: MineflayerBot, options?: Mineflay
579
696
  mode: 6,
580
697
  changedSlots: [],
581
698
  cursorItem: { present: false } as any,
582
- })
699
+ }
700
+ logPacketWrite('window_click', packet)
701
+ ext._client.write('window_click', packet)
583
702
  dragStateId++
703
+ logActionEvent('connector.action.success', action)
584
704
  return
585
705
  }
586
706
 
587
707
  if (action.type === 'click') {
588
708
  const [mouseButton, mode] = modeFromAction(action)
709
+ logHelperIntent(action, 'bot.clickWindow', {
710
+ slot: action.slotIndex,
711
+ mouseButton,
712
+ mode,
713
+ })
589
714
  await bot.clickWindow(action.slotIndex, mouseButton, mode)
590
715
  onSetSlot()
716
+ logActionEvent('connector.action.success', action)
591
717
  } else if (action.type === 'drag') {
592
718
  // bot.clickWindow() throws for mode=5 (prismarine-windows dragClick is unimplemented).
593
719
  // Send raw window_click packets directly via _client.write.
594
- if (!ext._client) return
720
+ if (!ext._client) {
721
+ logActionEvent('connector.action.skipped', action, { reason: 'missing_client' })
722
+ return
723
+ }
595
724
  const isRight = action.button === 'right'
596
725
  const startButton = isRight ? 4 : 0
597
726
  const slotButton = isRight ? 5 : 1
@@ -600,43 +729,94 @@ export function createMineflayerConnector(bot: MineflayerBot, options?: Mineflay
600
729
  const cursorItem = { present: false } as any
601
730
 
602
731
  isDraggingRaw = true
603
- const writeClick = (slot: number, mouseButton: number) => {
604
- ext._client!.write('window_click', {
732
+ try {
733
+ const writeClick = (slot: number, mouseButton: number) => {
734
+ const packet = {
735
+ windowId,
736
+ stateId: dragStateId,
737
+ slot,
738
+ mouseButton,
739
+ mode: 5,
740
+ changedSlots: [],
741
+ cursorItem,
742
+ }
743
+ logPacketWrite('window_click', packet)
744
+ ext._client!.write('window_click', packet)
745
+ dragStateId++
746
+ }
747
+
748
+ writeClick(-999, startButton)
749
+ for (const slot of action.slots) {
750
+ writeClick(slot, slotButton)
751
+ }
752
+ writeClick(-999, endButton)
753
+ } finally {
754
+ isDraggingRaw = false
755
+ }
756
+ logActionEvent('connector.action.success', action)
757
+ } else if (action.type === 'drop') {
758
+ if (action.slotIndex === -1) {
759
+ // Drop cursor item by clicking outside the window: slot=-999, mode=0
760
+ // Left click (all=true) drops entire stack, right click (all=false) drops one
761
+ if (!ext._client) {
762
+ logActionEvent('connector.action.skipped', action, { reason: 'missing_client' })
763
+ return
764
+ }
765
+ const windowId = bot.currentWindow ? bot.currentWindow.id : 0
766
+ const mouseButton = action.all ? 0 : 1
767
+ const packet = {
605
768
  windowId,
606
769
  stateId: dragStateId,
607
- slot,
770
+ slot: -999,
608
771
  mouseButton,
609
- mode: 5,
772
+ mode: 0,
610
773
  changedSlots: [],
611
- cursorItem,
612
- })
774
+ cursorItem: { present: false } as any,
775
+ }
776
+ logPacketWrite('window_click', packet)
777
+ ext._client.write('window_click', packet)
613
778
  dragStateId++
779
+ // Skip onSetSlot() — mineflayer's selectedItem is not updated yet.
780
+ // The server will send set_slot/window_items to sync the cursor state.
781
+ } else {
782
+ // Drop from specific slot via Q key: mode=4
783
+ logHelperIntent(action, 'bot.clickWindow', {
784
+ slot: action.slotIndex,
785
+ mouseButton: action.all ? 1 : 0,
786
+ mode: 4,
787
+ })
788
+ await bot.clickWindow(action.slotIndex, action.all ? 1 : 0, 4)
789
+ onSetSlot()
614
790
  }
615
-
616
- writeClick(-999, startButton)
617
- for (const slot of action.slots) {
618
- writeClick(slot, slotButton)
619
- }
620
- writeClick(-999, endButton)
621
- isDraggingRaw = false
622
- } else if (action.type === 'drop') {
623
- await bot.clickWindow(action.slotIndex, action.all ? 1 : 0, 4)
624
- onSetSlot()
791
+ logActionEvent('connector.action.success', action)
625
792
  } else if (action.type === 'close') {
626
793
  if (win) {
794
+ logHelperIntent(action, 'bot.closeWindow', { windowId: win.id })
627
795
  bot.closeWindow(win)
628
796
  } else {
629
797
  // Player inventory (synthetic) — send close_window so server drops cursor items
630
798
  if (ext._client) {
631
- ext._client.write('close_window', { windowId: 0 })
799
+ const packet = { windowId: 0 }
800
+ logPacketWrite('close_window', packet)
801
+ ext._client.write('close_window', packet)
632
802
  }
633
803
  ;(bot.inventory as any).selectedItem = null
634
804
  }
805
+ logActionEvent('connector.action.success', action)
635
806
  } else if (action.type === 'hotbar-swap') {
807
+ logHelperIntent(action, 'bot.clickWindow', {
808
+ slot: action.slotIndex,
809
+ mouseButton: action.hotbarSlot,
810
+ mode: 2,
811
+ })
636
812
  await bot.clickWindow(action.slotIndex, action.hotbarSlot, 2)
637
813
  onSetSlot()
814
+ logActionEvent('connector.action.success', action)
815
+ } else {
816
+ logActionEvent('connector.action.ignored', action, { reason: 'no_matching_handler' })
638
817
  }
639
818
  } catch (err) {
819
+ logActionEvent('connector.action.failure', action, { error: err instanceof Error ? err.message : String(err) })
640
820
  const detail = 'slotIndex' in action ? ` slot=${(action as any).slotIndex}` : ''
641
821
  console.error(`[minecraft-inventory] sendAction "${action.type}"${detail} failed:`, err)
642
822
  }
@@ -674,7 +854,9 @@ export function createMineflayerConnector(bot: MineflayerBot, options?: Mineflay
674
854
  if (bot.currentWindow) {
675
855
  ;(bot.currentWindow as any).off('updateSlot', scheduleSlotUpdate)
676
856
  }
857
+ debugSession.log({ event: 'connector.unmount' })
858
+ debugSession.dispose()
677
859
  }
678
860
  },
679
861
  }
680
- }
862
+ }
@@ -2,6 +2,15 @@ import React, { createContext, useContext, useEffect, useRef, useState, useCallb
2
2
  import type { InventoryWindowState, PlayerState, ItemStack, SlotState } from '../types'
3
3
  import type { InventoryConnector } from '../connector/types'
4
4
  import { isItemEqual, getMaxStackSize } from '../utils/isItemEqual'
5
+ import {
6
+ createInventoryDebugSession,
7
+ getActionSlotIndexes,
8
+ summarizeAction,
9
+ summarizeItem,
10
+ summarizeSlots,
11
+ summarizeWindowState,
12
+ type InventoryDebugSession,
13
+ } from '../debug/inventoryDebug'
5
14
 
6
15
  export interface DragPreviewEntry {
7
16
  count: number
@@ -109,8 +118,30 @@ export function InventoryProvider({ connector, children, noDragSpread = false, n
109
118
  // Set to true when endDrag fires; cleared on the next mouseDown in Slot.
110
119
  // Prevents spurious mouseUp events from sending unwanted clicks after a drag.
111
120
  const dragEndedRef = useRef(false)
112
- /** Latest full context value; updated every render for global debug (`globalThis.__mcInv.state`). */
121
+ /** Latest full context value; used by the DevTools debug API (`globalThis.__mcInv.state`). */
113
122
  const valueRef = useRef<InventoryContextValue | null>(null)
123
+ const debugSessionRef = useRef<InventoryDebugSession | null>(null)
124
+
125
+ useEffect(() => {
126
+ const session = createInventoryDebugSession('inventory-provider', () => {
127
+ const value = valueRef.current
128
+ if (!value) return null
129
+ return {
130
+ windowState: value.windowState,
131
+ playerState: value.playerState,
132
+ heldItem: value.heldItem,
133
+ isDragging: value.isDragging,
134
+ dragSlots: [...value.dragSlots],
135
+ }
136
+ })
137
+ debugSessionRef.current = session
138
+ session.log({ event: 'provider.mount' })
139
+ return () => {
140
+ session.log({ event: 'provider.unmount' })
141
+ session.dispose()
142
+ if (debugSessionRef.current === session) debugSessionRef.current = null
143
+ }
144
+ }, [])
114
145
 
115
146
  useEffect(() => {
116
147
  if (!connector) return
@@ -119,16 +150,35 @@ export function InventoryProvider({ connector, children, noDragSpread = false, n
119
150
 
120
151
  return connector.subscribe((event) => {
121
152
  if (event.type === 'windowOpen' || event.type === 'windowUpdate') {
153
+ debugSessionRef.current?.log({
154
+ event: `connector.${event.type}`,
155
+ windowId: event.state.windowId,
156
+ windowType: event.state.type,
157
+ slots: summarizeSlots(event.state.slots),
158
+ heldItem: summarizeItem(event.state.heldItem),
159
+ })
122
160
  setWindowState({ ...event.state })
123
161
  setHeldItemState(event.state.heldItem)
124
162
  } else if (event.type === 'windowClose') {
163
+ debugSessionRef.current?.log({ event: 'connector.windowClose' })
125
164
  setWindowState(null)
126
165
  setHeldItemState(null)
127
166
  setIsDragging(false)
128
167
  setDragSlots([])
129
168
  } else if (event.type === 'playerUpdate') {
169
+ debugSessionRef.current?.log({
170
+ event: 'connector.playerUpdate',
171
+ slots: summarizeSlots(event.state.inventory),
172
+ data: {
173
+ activeHotbarSlot: event.state.activeHotbarSlot,
174
+ },
175
+ })
130
176
  setPlayerState(event.state)
131
177
  } else if (event.type === 'heldItemChange') {
178
+ debugSessionRef.current?.log({
179
+ event: 'connector.heldItemChange',
180
+ heldItem: summarizeItem(event.item),
181
+ })
132
182
  setHeldItemState(event.item)
133
183
  }
134
184
  })
@@ -140,6 +190,19 @@ export function InventoryProvider({ connector, children, noDragSpread = false, n
140
190
 
141
191
  const sendAction = useCallback<InventoryConnector['sendAction']>(
142
192
  (action) => {
193
+ const slotIndexes = getActionSlotIndexes(action)
194
+ const state = valueRef.current?.windowState ?? connectorRef.current?.getWindowState() ?? null
195
+ debugSessionRef.current?.log({
196
+ event: 'ui.action',
197
+ windowId: state?.windowId,
198
+ windowType: state?.type,
199
+ action: summarizeAction(action),
200
+ slots: state ? summarizeSlots(state.slots, slotIndexes) : undefined,
201
+ heldItem: summarizeItem(valueRef.current?.heldItem ?? state?.heldItem),
202
+ data: {
203
+ before: summarizeWindowState(state, slotIndexes),
204
+ },
205
+ })
143
206
  return connectorRef.current?.sendAction(action)
144
207
  },
145
208
  [],
@@ -214,6 +277,17 @@ export function InventoryProvider({ connector, children, noDragSpread = false, n
214
277
 
215
278
  // Only send drag action if multiple slots were involved (single slot = normal click)
216
279
  if (slots.length > 1 && button && held) {
280
+ debugSessionRef.current?.log({
281
+ event: 'ui.drag.optimisticUpdate',
282
+ windowId: ws?.windowId,
283
+ windowType: ws?.type,
284
+ action: summarizeAction({ type: 'drag', slots, button }),
285
+ slots: ws ? summarizeSlots(ws.slots, slots) : undefined,
286
+ heldItem: summarizeItem(held),
287
+ data: {
288
+ before: summarizeWindowState(ws, slots),
289
+ },
290
+ })
217
291
  connectorRef.current?.sendAction({ type: 'drag', slots, button })
218
292
 
219
293
  // Optimistic client-side update: apply item distribution immediately
@@ -332,18 +406,5 @@ export function InventoryProvider({ connector, children, noDragSpread = false, n
332
406
 
333
407
  valueRef.current = value
334
408
 
335
- // Full inventory UI state on global for DevTools: globalThis.__mcInv.state (e.g. windowState, playerState, heldItem, drag state).
336
- useEffect(() => {
337
- const g = globalThis as any
338
- g.__mcInv = {
339
- get state() {
340
- return valueRef.current
341
- },
342
- }
343
- return () => {
344
- delete g.__mcInv
345
- }
346
- }, [])
347
-
348
409
  return <InventoryContext.Provider value={value}>{children}</InventoryContext.Provider>
349
410
  }
@@ -0,0 +1,69 @@
1
+ # Inventory Debug API
2
+
3
+ Глобальный объект `__mcInv` доступен в DevTools сразу после монтирования `InventoryProvider`.
4
+
5
+ ## Быстрый старт
6
+
7
+ ```js
8
+ // Текущее состояние инвентаря
9
+ __mcInv.state
10
+
11
+ // Все сессии (провайдер + коннектор)
12
+ __mcInv.getStates()
13
+
14
+ // Лог событий
15
+ __mcInv.getLogs()
16
+
17
+ // Последние N событий
18
+ __mcInv.getLogs().slice(-20)
19
+
20
+ // Экспорт для сравнения / передачи
21
+ copy(JSON.stringify(__mcInv.exportLogs(), null, 2))
22
+
23
+ // Очистить буфер
24
+ __mcInv.clearLogs()
25
+ ```
26
+
27
+ ## События
28
+
29
+ | Событие | Источник | Когда |
30
+ |---|---|---|
31
+ | `connector.mount` | mineflayer-connector | Коннектор создан |
32
+ | `connector.unmount` | mineflayer-connector | Коннектор уничтожен |
33
+ | `provider.mount` | inventory-provider | Провайдер смонтирован |
34
+ | `provider.unmount` | inventory-provider | Провайдер размонтирован |
35
+ | `connector.windowOpen` | inventory-provider | Открыто новое окно |
36
+ | `connector.windowUpdate` | inventory-provider | Слоты обновились от сервера |
37
+ | `connector.windowClose` | inventory-provider | Окно закрыто |
38
+ | `connector.playerUpdate` | inventory-provider | Обновился инвентарь игрока |
39
+ | `connector.heldItemChange` | inventory-provider | Изменился предмет в руке |
40
+ | `ui.action` | inventory-provider | Пользователь инициировал действие |
41
+ | `ui.drag.optimisticUpdate` | inventory-provider | Drag завершён, применён оптимистичный апдейт |
42
+ | `connector.action.start` | mineflayer-connector | Начало отправки пакета |
43
+ | `connector.action.success` | mineflayer-connector | Пакет успешно отправлен |
44
+ | `connector.action.failure` | mineflayer-connector | Ошибка при отправке |
45
+ | `connector.action.skipped` | mineflayer-connector | Действие пропущено (нет нужного хендлера) |
46
+ | `connector.action.ignored` | mineflayer-connector | Неизвестный тип действия |
47
+ | `connector.helperIntent` | mineflayer-connector | Вызов mineflayer-хелпера (bot.clickWindow и др.) |
48
+ | `connector.packetWrite` | mineflayer-connector | Прямая запись пакета через `_client.write` |
49
+
50
+ ## Фильтрация логов
51
+
52
+ ```js
53
+ // Только ошибки
54
+ __mcInv.getLogs().filter(e => e.event === 'connector.action.failure')
55
+
56
+ // Только действия пользователя
57
+ __mcInv.getLogs().filter(e => e.event.startsWith('ui.'))
58
+
59
+ // Конкретное окно
60
+ __mcInv.getLogs().filter(e => e.windowType === 'enchanting_table')
61
+
62
+ // Пакеты конкретного типа
63
+ __mcInv.getLogs().filter(e => e.event === 'connector.packetWrite' && e.data?.packet === 'window_click')
64
+ ```
65
+
66
+ ## Ограничения
67
+
68
+ - Буфер хранит последние **1000** записей. При активном геймплее старые события вытесняются — используйте `exportLogs()` своевременно.
69
+ - `__mcInv.state` всегда показывает состояние последней созданной сессии (обычно `inventory-provider`). Для состояния коннектора используйте `getStates()`.
@@ -0,0 +1,240 @@
1
+ import type { InventoryAction, InventoryWindowState, ItemStack, PlayerState, SlotState } from '../types'
2
+
3
+ type JsonPrimitive = string | number | boolean | null
4
+ type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue }
5
+
6
+ export interface InventoryDebugLogEntry {
7
+ id: number
8
+ time: number
9
+ source: string
10
+ event: string
11
+ sessionId?: string
12
+ windowId?: number
13
+ windowType?: string
14
+ action?: JsonValue
15
+ stateId?: number
16
+ slots?: JsonValue
17
+ heldItem?: JsonValue
18
+ data?: JsonValue
19
+ error?: JsonValue
20
+ }
21
+
22
+ export interface InventoryDebugState {
23
+ windowState: InventoryWindowState | null
24
+ playerState: PlayerState | null
25
+ heldItem: ItemStack | null
26
+ isDragging?: boolean
27
+ dragSlots?: number[]
28
+ }
29
+
30
+ export interface InventoryDebugApi {
31
+ readonly state: InventoryDebugState | null
32
+ getStates(): Array<{ sessionId: string; source: string; state: InventoryDebugState | null }>
33
+ getLogs(): InventoryDebugLogEntry[]
34
+ clearLogs(): void
35
+ exportLogs(): {
36
+ exportedAt: string
37
+ logs: InventoryDebugLogEntry[]
38
+ states: JsonValue
39
+ }
40
+ }
41
+
42
+ interface ProviderRegistration {
43
+ sessionId: string
44
+ source: string
45
+ getState: () => InventoryDebugState | null
46
+ }
47
+
48
+ export interface InventoryDebugSession {
49
+ sessionId: string
50
+ source: string
51
+ log(entry: Omit<InventoryDebugLogEntry, 'id' | 'time' | 'source' | 'sessionId'>): void
52
+ dispose(): void
53
+ }
54
+
55
+ const MAX_LOGS = 1000
56
+ const MAX_OBJECT_DEPTH = 4
57
+ const MAX_ARRAY_ITEMS = 80
58
+
59
+ let nextLogId = 1
60
+ let nextSessionId = 1
61
+ const logs: InventoryDebugLogEntry[] = []
62
+ const providers = new Map<string, ProviderRegistration>()
63
+ let activeSessionId: string | null = null
64
+
65
+ function isObject(value: unknown): value is Record<string, unknown> {
66
+ return typeof value === 'object' && value !== null
67
+ }
68
+
69
+ function sanitize(value: unknown, depth = 0, seen = new WeakSet<object>()): JsonValue {
70
+ if (value == null) return null
71
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') return value
72
+ if (typeof value === 'bigint') return String(value)
73
+ if (typeof value === 'function' || typeof value === 'symbol' || typeof value === 'undefined') return null
74
+ if (depth >= MAX_OBJECT_DEPTH) return '[truncated]'
75
+
76
+ if (Array.isArray(value)) {
77
+ return value.slice(0, MAX_ARRAY_ITEMS).map((item) => sanitize(item, depth + 1, seen))
78
+ }
79
+
80
+ if (value instanceof Uint8Array) {
81
+ return `[Uint8Array:${value.byteLength}]`
82
+ }
83
+
84
+ if (!isObject(value)) return String(value)
85
+ if (seen.has(value)) return '[circular]'
86
+ seen.add(value)
87
+
88
+ const output: Record<string, JsonValue> = {}
89
+ for (const [key, entryValue] of Object.entries(value)) {
90
+ if (key === 'nbt') {
91
+ output.hasNbt = entryValue != null
92
+ continue
93
+ }
94
+ output[key] = sanitize(entryValue, depth + 1, seen)
95
+ }
96
+ return output
97
+ }
98
+
99
+ export function summarizeItem(item: ItemStack | null | undefined): JsonValue {
100
+ if (!item) return null
101
+ const summary: Record<string, JsonValue> = {
102
+ type: item.type,
103
+ count: item.count,
104
+ }
105
+ if (item.metadata !== undefined) summary.metadata = item.metadata
106
+ if (item.name) summary.name = item.name
107
+ if (item.displayName) summary.displayName = item.displayName
108
+ if (item.debugKey) summary.debugKey = item.debugKey
109
+ if (item.nbt) summary.hasNbt = true
110
+ if (item.enchantments?.length) summary.enchantments = sanitize(item.enchantments)
111
+ return summary
112
+ }
113
+
114
+ export function summarizeSlots(slots: SlotState[] | undefined, slotIndexes?: number[]): JsonValue {
115
+ if (!slots) return []
116
+ const filter = slotIndexes ? new Set(slotIndexes) : null
117
+ const selected = filter ? slots.filter((slot) => filter.has(slot.index)) : slots.filter((slot) => slot.item)
118
+ return selected.map((slot) => ({
119
+ index: slot.index,
120
+ item: summarizeItem(slot.item),
121
+ }))
122
+ }
123
+
124
+ export function summarizeAction(action: InventoryAction): JsonValue {
125
+ return sanitize(action)
126
+ }
127
+
128
+ export function sanitizeDebugValue(value: unknown): JsonValue {
129
+ return sanitize(value)
130
+ }
131
+
132
+ export function summarizeWindowState(state: InventoryWindowState | null | undefined, slotIndexes?: number[]): JsonValue {
133
+ if (!state) return null
134
+ return {
135
+ windowId: state.windowId,
136
+ type: state.type,
137
+ title: state.title ?? null,
138
+ heldItem: summarizeItem(state.heldItem),
139
+ properties: sanitize(state.properties ?? null),
140
+ slots: summarizeSlots(state.slots, slotIndexes),
141
+ }
142
+ }
143
+
144
+ export function getActionSlotIndexes(action: InventoryAction): number[] | undefined {
145
+ if (action.type === 'click' || action.type === 'drop' || action.type === 'hotbar-swap') return [action.slotIndex]
146
+ if (action.type === 'drag') return [...action.slots]
147
+ return undefined
148
+ }
149
+
150
+ function getActiveState(): InventoryDebugState | null {
151
+ if (activeSessionId && providers.has(activeSessionId)) {
152
+ return providers.get(activeSessionId)!.getState()
153
+ }
154
+ const last = [...providers.values()].at(-1)
155
+ return last?.getState() ?? null
156
+ }
157
+
158
+ function getStates(): Array<{ sessionId: string; source: string; state: InventoryDebugState | null }> {
159
+ return [...providers.values()].map((provider) => ({
160
+ sessionId: provider.sessionId,
161
+ source: provider.source,
162
+ state: provider.getState(),
163
+ }))
164
+ }
165
+
166
+ function installGlobalApi() {
167
+ const g = globalThis as typeof globalThis & { __mcInv?: InventoryDebugApi }
168
+ if (g.__mcInv?.getLogs === getLogs) return
169
+
170
+ const api: InventoryDebugApi = {
171
+ get state() {
172
+ return getActiveState()
173
+ },
174
+ getStates,
175
+ getLogs,
176
+ clearLogs,
177
+ exportLogs,
178
+ }
179
+ g.__mcInv = api
180
+ }
181
+
182
+ function getLogs(): InventoryDebugLogEntry[] {
183
+ return logs.map((entry) => ({ ...entry }))
184
+ }
185
+
186
+ function clearLogs() {
187
+ logs.length = 0
188
+ }
189
+
190
+ function exportLogs() {
191
+ return {
192
+ exportedAt: new Date().toISOString(),
193
+ logs: getLogs(),
194
+ states: sanitize(getStates()),
195
+ }
196
+ }
197
+
198
+ export function logInventoryDebug(entry: Omit<InventoryDebugLogEntry, 'id' | 'time'>): void {
199
+ const fullEntry: InventoryDebugLogEntry = {
200
+ ...entry,
201
+ id: nextLogId++,
202
+ time: Date.now(),
203
+ }
204
+ logs.push(fullEntry)
205
+ if (logs.length > MAX_LOGS) logs.splice(0, logs.length - MAX_LOGS)
206
+ installGlobalApi()
207
+ }
208
+
209
+ export function createInventoryDebugSession(
210
+ source: string,
211
+ getState: () => InventoryDebugState | null,
212
+ ): InventoryDebugSession {
213
+ installGlobalApi()
214
+ const sessionId = `${source}-${nextSessionId++}`
215
+ providers.set(sessionId, { sessionId, source, getState })
216
+ activeSessionId = sessionId
217
+
218
+ return {
219
+ sessionId,
220
+ source,
221
+ log(entry) {
222
+ logInventoryDebug({
223
+ ...entry,
224
+ source,
225
+ sessionId,
226
+ })
227
+ },
228
+ dispose() {
229
+ providers.delete(sessionId)
230
+ if (activeSessionId === sessionId) {
231
+ activeSessionId = [...providers.keys()].at(-1) ?? null
232
+ }
233
+ },
234
+ }
235
+ }
236
+
237
+ export function getInventoryDebugApi(): InventoryDebugApi {
238
+ installGlobalApi()
239
+ return (globalThis as typeof globalThis & { __mcInv: InventoryDebugApi }).__mcInv
240
+ }
package/src/index.tsx CHANGED
@@ -36,6 +36,23 @@ export { useKeyboardShortcuts } from './hooks/useKeyboardShortcuts'
36
36
 
37
37
  // Utilities
38
38
  export { isItemEqual, getMaxStackSize } from './utils/isItemEqual'
39
+ export {
40
+ getInventoryDebugApi,
41
+ logInventoryDebug,
42
+ createInventoryDebugSession,
43
+ summarizeAction,
44
+ summarizeItem,
45
+ summarizeSlots,
46
+ summarizeWindowState,
47
+ getActionSlotIndexes,
48
+ sanitizeDebugValue,
49
+ } from './debug/inventoryDebug'
50
+ export type {
51
+ InventoryDebugApi,
52
+ InventoryDebugLogEntry,
53
+ InventoryDebugSession,
54
+ InventoryDebugState,
55
+ } from './debug/inventoryDebug'
39
56
 
40
57
  // Connector
41
58
  export { createMineflayerConnector } from './connector/mineflayer'