@@ -12,29 +12,29 @@ function inject (bot) {
12
12
let diggingTask = createDoneTask ( )
13
13
14
14
bot . targetDigBlock = null
15
+ bot . targetDigFace = null
15
16
bot . lastDigTime = null
16
17
17
18
async function dig ( block , forceLook , digFace ) {
18
19
if ( block === null || block === undefined ) {
19
20
throw new Error ( 'dig was called with an undefined or null block' )
20
21
}
22
+
21
23
if ( ! digFace || typeof digFace === 'function' ) {
22
24
digFace = 'auto'
23
25
}
24
26
25
- if ( bot . targetDigBlock ) bot . stopDigging ( )
26
-
27
- let diggingFace = 1 // Default (top)
27
+ bot . targetDigFace = 1 // Default (top)
28
28
29
29
if ( forceLook !== 'ignore' ) {
30
30
if ( digFace ?. x || digFace ?. y || digFace ?. z ) {
31
31
// Determine the block face the bot should mine
32
32
if ( digFace . x ) {
33
- diggingFace = digFace . x > 0 ? BlockFaces . EAST : BlockFaces . WEST
33
+ bot . targetDigFace = digFace . x > 0 ? BlockFaces . EAST : BlockFaces . WEST
34
34
} else if ( digFace . y ) {
35
- diggingFace = digFace . y > 0 ? BlockFaces . TOP : BlockFaces . BOTTOM
35
+ bot . targetDigFace = digFace . y > 0 ? BlockFaces . TOP : BlockFaces . BOTTOM
36
36
} else if ( digFace . z ) {
37
- diggingFace = digFace . z > 0 ? BlockFaces . SOUTH : BlockFaces . NORTH
37
+ bot . targetDigFace = digFace . z > 0 ? BlockFaces . SOUTH : BlockFaces . NORTH
38
38
}
39
39
await bot . lookAt (
40
40
block . position . offset ( 0.5 , 0.5 , 0.5 ) . offset ( digFace . x * 0.5 , digFace . y * 0.5 , digFace . z * 0.5 ) ,
@@ -76,8 +76,8 @@ function inject (bot) {
76
76
const rayPos = rayBlock . position
77
77
if (
78
78
rayPos . x === block . position . x &&
79
- rayPos . y === block . position . y &&
80
- rayPos . z === block . position . z
79
+ rayPos . y === block . position . y &&
80
+ rayPos . z === block . position . z
81
81
) {
82
82
validFaces . push ( {
83
83
face : rayBlock . face ,
@@ -86,6 +86,7 @@ function inject (bot) {
86
86
}
87
87
}
88
88
}
89
+
89
90
if ( validFaces . length > 0 ) {
90
91
// Chose closest valid face
91
92
let closest
@@ -101,11 +102,11 @@ function inject (bot) {
101
102
}
102
103
}
103
104
await bot . lookAt ( closest . targetPos , forceLook )
104
- diggingFace = closest . face
105
+ bot . targetDigFace = closest . face
105
106
} else if ( closerBlocks . length === 0 && block . shapes . length === 0 ) {
106
107
// no other blocks were detected and the block has no shapes.
107
108
// The block in question is replaceable (like tall grass) so we can just dig it
108
- // TODO: do AABB + ray intercept check to this position for diggingFace .
109
+ // TODO: do AABB + ray intercept check to this position for digFace .
109
110
await bot . lookAt ( block . position . offset ( 0.5 , 0.5 , 0.5 ) , forceLook )
110
111
} else {
111
112
// Block is obstructed return error?
@@ -116,11 +117,15 @@ function inject (bot) {
116
117
}
117
118
}
118
119
120
+ // In vanilla the client will cancel digging the current block once the other block is at the crosshair.
121
+ // Todo: don't wait until lookAt is at middle of the block, but at the edge of it.
122
+ if ( bot . targetDigBlock ) bot . stopDigging ( )
123
+
119
124
diggingTask = createTask ( )
120
125
bot . _client . write ( 'block_dig' , {
121
126
status : 0 , // start digging
122
127
location : block . position ,
123
- face : diggingFace // default face is 1 (top)
128
+ face : bot . targetDigFace // default face is 1 (top)
124
129
} )
125
130
const waitTime = bot . digTime ( block )
126
131
waitTimeout = setTimeout ( finishDigging , waitTime )
@@ -140,19 +145,27 @@ function inject (bot) {
140
145
bot . _client . write ( 'block_dig' , {
141
146
status : 2 , // finish digging
142
147
location : bot . targetDigBlock . position ,
143
- face : diggingFace // hard coded to always dig from the top
148
+ face : bot . targetDigFace // always the same as the start face
144
149
} )
145
150
}
146
151
bot . targetDigBlock = null
152
+ bot . targetDigFace = null
147
153
bot . lastDigTime = performance . now ( )
148
154
bot . _updateBlockState ( block . position , 0 )
149
155
}
150
156
151
157
const eventName = `blockUpdate:${ block . position } `
152
158
bot . on ( eventName , onBlockUpdate )
153
159
160
+ const currentBlock = block
154
161
bot . stopDigging = ( ) => {
155
162
if ( ! bot . targetDigBlock ) return
163
+
164
+ // Replicate the odd vanilla cancellation face value.
165
+ // 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.
166
+ const stoppedBecauseOfNewDigRequest = ! currentBlock . position . equals ( bot . targetDigBlock . position )
167
+ const cancellationDiggingFace = ! stoppedBecauseOfNewDigRequest ? bot . targetDigFace : 0
168
+
156
169
bot . removeListener ( eventName , onBlockUpdate )
157
170
clearInterval ( swingInterval )
158
171
clearTimeout ( waitTimeout )
@@ -161,10 +174,11 @@ function inject (bot) {
161
174
bot . _client . write ( 'block_dig' , {
162
175
status : 1 , // cancel digging
163
176
location : bot . targetDigBlock . position ,
164
- face : 1 // hard coded to always dig from the top
177
+ face : cancellationDiggingFace
165
178
} )
166
179
const block = bot . targetDigBlock
167
180
bot . targetDigBlock = null
181
+ bot . targetDigFace = null
168
182
bot . lastDigTime = performance . now ( )
169
183
bot . emit ( 'diggingAborted' , block )
170
184
bot . stopDigging = noop
@@ -182,6 +196,7 @@ function inject (bot) {
182
196
swingInterval = null
183
197
waitTimeout = null
184
198
bot . targetDigBlock = null
199
+ bot . targetDigFace = null
185
200
bot . lastDigTime = performance . now ( )
186
201
bot . emit ( 'diggingCompleted' , newBlock )
187
202
diggingTask . finish ( )
@@ -199,8 +214,8 @@ function inject (bot) {
199
214
function canDigBlock ( block ) {
200
215
return (
201
216
block &&
202
- block . diggable &&
203
- block . position . offset ( 0.5 , 0.5 , 0.5 ) . distanceTo ( bot . entity . position . offset ( 0 , 1.65 , 0 ) ) <= 5.1
217
+ block . diggable &&
218
+ block . position . offset ( 0.5 , 0.5 , 0.5 ) . distanceTo ( bot . entity . position . offset ( 0 , 1.65 , 0 ) ) <= 5.1
204
219
)
205
220
}
206
221
0 commit comments