Skip to content

Commit ab78bf8

Browse files
authored
Fix hardcoded diggingface for cancel digging (#3322)
* remove strange huge space * implement digface in stopDigging, fix comments * more strange spaces * remove double space * revert changes, set to forced 0 instead * actually change the value.. * lint * cancellation face the same as vanilla
1 parent 7c01eeb commit ab78bf8

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

lib/plugins/digging.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ function inject (bot) {
1212
let diggingTask = createDoneTask()
1313

1414
bot.targetDigBlock = null
15+
bot.targetDigFace = null
1516
bot.lastDigTime = null
1617

1718
async function dig (block, forceLook, digFace) {
1819
if (block === null || block === undefined) {
1920
throw new Error('dig was called with an undefined or null block')
2021
}
22+
2123
if (!digFace || typeof digFace === 'function') {
2224
digFace = 'auto'
2325
}
2426

25-
if (bot.targetDigBlock) bot.stopDigging()
26-
27-
let diggingFace = 1 // Default (top)
27+
bot.targetDigFace = 1 // Default (top)
2828

2929
if (forceLook !== 'ignore') {
3030
if (digFace?.x || digFace?.y || digFace?.z) {
3131
// Determine the block face the bot should mine
3232
if (digFace.x) {
33-
diggingFace = digFace.x > 0 ? BlockFaces.EAST : BlockFaces.WEST
33+
bot.targetDigFace = digFace.x > 0 ? BlockFaces.EAST : BlockFaces.WEST
3434
} else if (digFace.y) {
35-
diggingFace = digFace.y > 0 ? BlockFaces.TOP : BlockFaces.BOTTOM
35+
bot.targetDigFace = digFace.y > 0 ? BlockFaces.TOP : BlockFaces.BOTTOM
3636
} else if (digFace.z) {
37-
diggingFace = digFace.z > 0 ? BlockFaces.SOUTH : BlockFaces.NORTH
37+
bot.targetDigFace = digFace.z > 0 ? BlockFaces.SOUTH : BlockFaces.NORTH
3838
}
3939
await bot.lookAt(
4040
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) {
7676
const rayPos = rayBlock.position
7777
if (
7878
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
8181
) {
8282
validFaces.push({
8383
face: rayBlock.face,
@@ -86,6 +86,7 @@ function inject (bot) {
8686
}
8787
}
8888
}
89+
8990
if (validFaces.length > 0) {
9091
// Chose closest valid face
9192
let closest
@@ -101,11 +102,11 @@ function inject (bot) {
101102
}
102103
}
103104
await bot.lookAt(closest.targetPos, forceLook)
104-
diggingFace = closest.face
105+
bot.targetDigFace = closest.face
105106
} else if (closerBlocks.length === 0 && block.shapes.length === 0) {
106107
// no other blocks were detected and the block has no shapes.
107108
// 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.
109110
await bot.lookAt(block.position.offset(0.5, 0.5, 0.5), forceLook)
110111
} else {
111112
// Block is obstructed return error?
@@ -116,11 +117,15 @@ function inject (bot) {
116117
}
117118
}
118119

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+
119124
diggingTask = createTask()
120125
bot._client.write('block_dig', {
121126
status: 0, // start digging
122127
location: block.position,
123-
face: diggingFace // default face is 1 (top)
128+
face: bot.targetDigFace // default face is 1 (top)
124129
})
125130
const waitTime = bot.digTime(block)
126131
waitTimeout = setTimeout(finishDigging, waitTime)
@@ -140,19 +145,27 @@ function inject (bot) {
140145
bot._client.write('block_dig', {
141146
status: 2, // finish digging
142147
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
144149
})
145150
}
146151
bot.targetDigBlock = null
152+
bot.targetDigFace = null
147153
bot.lastDigTime = performance.now()
148154
bot._updateBlockState(block.position, 0)
149155
}
150156

151157
const eventName = `blockUpdate:${block.position}`
152158
bot.on(eventName, onBlockUpdate)
153159

160+
const currentBlock = block
154161
bot.stopDigging = () => {
155162
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+
156169
bot.removeListener(eventName, onBlockUpdate)
157170
clearInterval(swingInterval)
158171
clearTimeout(waitTimeout)
@@ -161,10 +174,11 @@ function inject (bot) {
161174
bot._client.write('block_dig', {
162175
status: 1, // cancel digging
163176
location: bot.targetDigBlock.position,
164-
face: 1 // hard coded to always dig from the top
177+
face: cancellationDiggingFace
165178
})
166179
const block = bot.targetDigBlock
167180
bot.targetDigBlock = null
181+
bot.targetDigFace = null
168182
bot.lastDigTime = performance.now()
169183
bot.emit('diggingAborted', block)
170184
bot.stopDigging = noop
@@ -182,6 +196,7 @@ function inject (bot) {
182196
swingInterval = null
183197
waitTimeout = null
184198
bot.targetDigBlock = null
199+
bot.targetDigFace = null
185200
bot.lastDigTime = performance.now()
186201
bot.emit('diggingCompleted', newBlock)
187202
diggingTask.finish()
@@ -199,8 +214,8 @@ function inject (bot) {
199214
function canDigBlock (block) {
200215
return (
201216
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
204219
)
205220
}
206221

0 commit comments

Comments
 (0)