@@ -25,11 +25,13 @@ function inject (bot) {
2525 // Direct debug logging
2626
2727 async function dig ( block , forceLook , digFace ) {
28+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] called' , { block : block ? { x : block . position . x , y : block . position . y , z : block . position . z , name : block . name } : null , forceLook, digFace } )
2829 if ( block === null || block === undefined ) {
2930 throw new Error ( 'dig was called with an undefined or null block' )
3031 }
3132
3233 const distance = block . position . distanceTo ( bot . entity . position . offset ( 0 , bot . entity . eyeHeight , 0 ) )
34+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] distance' , distance )
3335 if ( distance > 4.5 ) {
3436 return false
3537 }
@@ -136,6 +138,7 @@ function inject (bot) {
136138 if ( bot . targetDigBlock ) bot . stopDigging ( )
137139
138140 diggingTask = createTask ( )
141+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] send START' , { pos : block . position , face : bot . targetDigFace } )
139142 // Begin digging: send START and setup tick-based progress
140143 bot . _client . write ( 'block_dig' , {
141144 status : 0 , // start digging
@@ -145,7 +148,9 @@ function inject (bot) {
145148 bot . targetDigBlock = block
146149 bot . swingArm ( )
147150
151+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] swing init' )
148152 swingInterval = setInterval ( ( ) => {
153+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] swing tick' )
149154 bot . swingArm ( )
150155 } , 350 )
151156
@@ -176,11 +181,13 @@ function inject (bot) {
176181 }
177182
178183 physicsTickListener = ( ) => {
184+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] physics tick' , { target : bot . targetDigBlock ? { x : bot . targetDigBlock . position . x , y : bot . targetDigBlock . position . y , z : bot . targetDigBlock . position . z } : null } )
179185 if ( ! bot . targetDigBlock ) return
180186 // Abort if target block conditions are no longer valid mid-dig
181187 const eyePos = bot . entity . position . offset ( 0 , bot . entity . eyeHeight , 0 )
182188 const targetCenter = block . position . offset ( 0.5 , 0.5 , 0.5 )
183189 const reachDistance = targetCenter . distanceTo ( eyePos )
190+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] reachDistance' , reachDistance )
184191
185192 // If the block is suddenly out of reach, terminate digging
186193 if ( reachDistance > 5.1 ) {
@@ -190,6 +197,7 @@ function inject (bot) {
190197
191198 // If the chunk unloaded or block reference disappeared, terminate digging
192199 const worldBlock = bot . world ?. getBlock ( block . position )
200+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] worldBlock check' , { worldBlock : worldBlock ? { type : worldBlock . type , name : worldBlock . name } : null } )
193201 if ( ! worldBlock ) {
194202 bot . stopDigging ( )
195203 return
@@ -203,14 +211,12 @@ function inject (bot) {
203211
204212 // Re-evaluate current dig time with dynamic conditions
205213 const digMs = bot . digTime ( block )
206- if ( digMs === Infinity ) {
207- // Cannot dig in current conditions; attempt abort/restart once
208- abortAndRestart ( )
209- if ( restarts >= MAX_DIG_RESTARTS ) {
210- bot . stopDigging ( )
211- }
214+ if ( ! Number . isFinite ( digMs ) || digMs <= 0 ) {
215+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] abort because invalid digMs' , digMs )
216+ bot . stopDigging ( )
212217 return
213218 }
219+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] digMs' , digMs , 'progress' , progress )
214220 progress += TICK_MS / digMs
215221
216222 // After 2x current dig time, if still not finished and no air update, restart
@@ -225,6 +231,7 @@ function inject (bot) {
225231
226232 if ( progress >= 1 && bot . targetDigBlock ) {
227233 finishing = true
234+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] send FINISH' )
228235 // Keep sending finish each tick until block update confirms air
229236 bot . _client . write ( 'block_dig' , {
230237 status : 2 ,
@@ -237,6 +244,7 @@ function inject (bot) {
237244
238245 const eventName = `blockUpdate:${ block . position } `
239246 bot . on ( eventName , onBlockUpdate )
247+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] listen' , eventName )
240248
241249 const currentBlock = block
242250
@@ -251,6 +259,7 @@ function inject (bot) {
251259 }
252260 bot . stopDigging = ( ) => {
253261 if ( ! bot . targetDigBlock ) return
262+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] stopDigging' , { current : bot . targetDigBlock . position } )
254263
255264 // Replicate the odd vanilla cancellation face value.
256265 // When the cancellation is because of a new dig request on another block it's the same as the new dig start face. In all other cases it's 0.
@@ -280,6 +289,7 @@ function inject (bot) {
280289 }
281290
282291 function onBlockUpdate ( oldBlock , newBlock ) {
292+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] blockUpdate' , { newType : newBlock ?. type , pos : block . position } )
283293 // vanilla server never actually interrupt digging, but some server send block update when you start digging
284294 // so ignore block update if not air
285295 // All block update listeners receive (null, null) when the world is unloaded. So newBlock can be null.
@@ -299,7 +309,9 @@ function inject (bot) {
299309 diggingTask . finish ( )
300310 }
301311
312+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] awaiting dig promise' )
302313 await diggingTask . promise
314+ if ( DEBUG_DIGGING ) console . log ( '[mineflayer.dig] dig promise resolved' )
303315 }
304316
305317 bot . on ( 'death' , ( ) => {
0 commit comments