-
-
Notifications
You must be signed in to change notification settings - Fork 926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update digging.js for state changes #3136
base: master
Are you sure you want to change the base?
Changes from all commits
22e065a
f5da310
55097ce
b8bf65c
1124f5b
4887102
2107785
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,23 @@ module.exports = inject | |
|
||
function inject (bot) { | ||
let swingInterval = null | ||
let waitTimeout = null | ||
let waitInterval = null | ||
|
||
let diggingTask = createDoneTask() | ||
|
||
bot.targetDigBlock = null | ||
bot.lastDigTime = null | ||
bot.digPercentage = 0 | ||
bot.lastHeldItemName = '' | ||
if (bot.heldItem) { bot.lastHeldItemName = bot.heldItem.name } | ||
bot.on('heldItemChanged', () => { | ||
let nameToCompare = '' | ||
if (bot.heldItem) { nameToCompare = bot.heldItem.name } | ||
if (bot.lastHeldItemName !== nameToCompare) { | ||
bot.lastHeldItemName = nameToCompare | ||
bot.digPercentage = 0 | ||
} | ||
}) | ||
|
||
async function dig (block, forceLook, digFace) { | ||
if (block === null || block === undefined) { | ||
|
@@ -110,20 +121,30 @@ function inject (bot) { | |
location: block.position, | ||
face: diggingFace // default face is 1 (top) | ||
}) | ||
const waitTime = bot.digTime(block) | ||
waitTimeout = setTimeout(finishDigging, waitTime) | ||
if (waitInterval) { clearInterval(waitInterval) } | ||
waitInterval = setInterval(() => { | ||
bot.digPercentage += (50 / bot.digTime(block)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does the vanilla client follow this algorithm when state change while digging ? do you have a reference ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not this exact algorithm but it mimics what I've seen in game, and I've yet to see something abnormal from it. |
||
if (bot.digPercentage >= 1.0) { | ||
finishDigging() | ||
} | ||
}, 50) | ||
bot.targetDigBlock = block | ||
bot.swingArm() | ||
|
||
swingInterval = setInterval(() => { | ||
bot.swingArm() | ||
}, 350) | ||
if (bot.targetDigBlock) { | ||
bot.swingArm() | ||
} else { | ||
clearInterval(swingInterval) | ||
} | ||
}, 250) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the change from 350 to 250 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Vakore can you change this back or give w reason to why you changed the interval? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the interval to fix the arm swing bug |
||
|
||
function finishDigging () { | ||
bot.digPercentage = 0 | ||
clearInterval(swingInterval) | ||
clearTimeout(waitTimeout) | ||
clearInterval(waitInterval) | ||
swingInterval = null | ||
waitTimeout = null | ||
waitInterval = null | ||
if (bot.targetDigBlock) { | ||
bot._client.write('block_dig', { | ||
status: 2, // finish digging | ||
|
@@ -143,9 +164,10 @@ function inject (bot) { | |
if (!bot.targetDigBlock) return | ||
bot.removeListener(eventName, onBlockUpdate) | ||
clearInterval(swingInterval) | ||
clearTimeout(waitTimeout) | ||
bot.digPercentage = 0 | ||
clearInterval(waitInterval) | ||
swingInterval = null | ||
waitTimeout = null | ||
waitInterval = null | ||
bot._client.write('block_dig', { | ||
status: 1, // cancel digging | ||
location: bot.targetDigBlock.position, | ||
|
@@ -162,13 +184,13 @@ function inject (bot) { | |
function onBlockUpdate (oldBlock, newBlock) { | ||
// vanilla server never actually interrupt digging, but some server send block update when you start digging | ||
// so ignore block update if not air | ||
// All block update listeners receive (null, null) when the world is unloaded. So newBlock can be null. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove this comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably on accident. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the comment back? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a more updated version of digging.js on the discord. Search for attachments from 'Vakore', unable to do stuff with npm rn. |
||
if (newBlock?.type !== 0) return | ||
bot.removeListener(eventName, onBlockUpdate) | ||
clearInterval(swingInterval) | ||
clearTimeout(waitTimeout) | ||
clearInterval(waitInterval) | ||
swingInterval = null | ||
waitTimeout = null | ||
waitInterval = null | ||
bot.digPercentage = 0.0 | ||
bot.targetDigBlock = null | ||
bot.lastDigTime = performance.now() | ||
bot.emit('diggingCompleted', newBlock) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think any held item change cancels the dig process? Even increasing the item amount in a slot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surprisingly no. If you have two pickaxes with equal durability and/or nbt, and switch between them, or switch between different fist slots while mining, it won't cancel the mining timer. Mining a tree with fists and picking up a log while mining will reset it, though constantly mining with logs while the stack size increases doesn't last time I checked. I'll double check later today but I'm pretty sure this is correct.
Though now that I'm looking at this the system here won't take into account durability/enchant differences, though that's just nbt and can likely be changed easily. This system will still work in most cases though and is better than nothing, but it should be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better way to check if items are equal is the stringify the item and compare that. You could make a string of the item you started mining with and reset the progress if the string of the held item changes.