From 22e065a58079be6dabae956d854b725c34b2226b Mon Sep 17 00:00:00 2001 From: Vakore <94314052+Vakore@users.noreply.github.com> Date: Mon, 31 Jul 2023 19:56:31 -0400 Subject: [PATCH 1/6] Update digging.js for state changes Made digging.js compatible with state changes, i.e. when the bot switches from being on ground to not on ground and in water, the digging progress is saved and accurately updated. --- lib/plugins/digging.js | 104 +++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/lib/plugins/digging.js b/lib/plugins/digging.js index d9d26a0fd..e0586fe42 100644 --- a/lib/plugins/digging.js +++ b/lib/plugins/digging.js @@ -7,12 +7,25 @@ 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', () => { + //if (bot.heldItem) {console.log("this why " + bot.heldItem.name);} + //if (!bot.heldItem) {console.log("this why " + bot.heldItem);} + var 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) { @@ -36,17 +49,15 @@ function inject (bot) { } else if (digFace.z) { diggingFace = digFace.z > 0 ? BlockFaces.SOUTH : BlockFaces.NORTH } - await bot.lookAt( - block.position.offset(0.5, 0.5, 0.5).offset(digFace.x * 0.5, digFace.y * 0.5, digFace.z * 0.5), - forceLook - ) + await bot.lookAt(block.position.offset(0.5, 0.5, 0.5).offset(digFace.x * 0.5, digFace.y * 0.5, digFace.z * 0.5), forceLook) } else if (digFace === 'raycast') { // Check faces that could be seen from the current position. If the delta is smaller then 0.5 that means the // bot cam most likely not see the face as the block is 1 block thick // this could be false for blocks that have a smaller bounding box then 1x1x1 - const dx = bot.entity.position.x - (block.position.x + 0.5) - const dy = bot.entity.position.y + bot.entity.height - (block.position.y + 0.5) - const dz = bot.entity.position.z - (block.position.z + 0.5) + const dx = bot.entity.position.x - block.position.x + 0.5 + const dy = bot.entity.position.y - block.position.y - 0.5 + bot.entity.height // -0.5 because the bot position + // is calculated from the block position that is inside its feet so 0.5 - 1 = -0.5 + const dz = bot.entity.position.z - block.position.z + 0.5 // Check y first then x and z const visibleFaces = { y: Math.sign(Math.abs(dy) > 0.5 ? dy : 0), @@ -57,20 +68,12 @@ function inject (bot) { for (const i in visibleFaces) { if (!visibleFaces[i]) continue // skip as this face is not visible // target position on the target block face. -> 0.5 + (current face) * 0.5 - const targetPos = block.position.offset( - 0.5 + (i === 'x' ? visibleFaces[i] * 0.5 : 0), - 0.5 + (i === 'y' ? visibleFaces[i] * 0.5 : 0), - 0.5 + (i === 'z' ? visibleFaces[i] * 0.5 : 0) - ) + const targetPos = block.position.offset(0.5 + (i === 'x' ? visibleFaces[i] * 0.5 : 0), 0.5 + (i === 'y' ? visibleFaces[i] * 0.5 : 0), 0.5 + (i === 'z' ? visibleFaces[i] * 0.5 : 0)) const startPos = bot.entity.position.offset(0, bot.entity.height, 0) const rayBlock = bot.world.raycast(startPos, targetPos.clone().subtract(startPos).normalize(), 5) if (rayBlock) { const rayPos = rayBlock.position - if ( - rayPos.x === block.position.x && - rayPos.y === block.position.y && - rayPos.z === block.position.z - ) { + if (rayPos.x === block.position.x && rayPos.y === block.position.y && rayPos.z === block.position.z) { // console.info(rayBlock) validFaces.push({ face: rayBlock.face, @@ -85,9 +88,7 @@ function inject (bot) { let distSqrt = 999 for (const i in validFaces) { const tPos = validFaces[i].targetPos - const cDist = new Vec3(tPos.x, tPos.y, tPos.z).distanceSquared( - bot.entity.position.offset(0, bot.entity.height, 0) - ) + const cDist = new Vec3(tPos.x, tPos.y, tPos.z).distanceSquared(bot.entity.position.offset(0, bot.entity.height, 0)) if (distSqrt > cDist) { closest = validFaces[i] distSqrt = cDist @@ -111,19 +112,38 @@ function inject (bot) { face: diggingFace // default face is 1 (top) }) const waitTime = bot.digTime(block) - waitTimeout = setTimeout(finishDigging, waitTime) + //waitInterval = setTimeout(finishDigging, waitTime) + if (waitInterval) {clearInterval(waitInterval);} + //bot.digPercentage = (50 / bot.digTime(block)); + //bot.referenceTime = window.performance.now(); + waitInterval = setInterval(() => {//!!!! + bot.digPercentage += (50 / bot.digTime(block)); + //bot.digPercentage += (Math.abs(bot.referenceTime - window.performance.now()) / bot.digTime(block)); + //console.log(Math.abs(bot.referenceTime - window.performance.now()) + ", " + bot.digPercentage); + //bot.referenceTime = window.performance.now(); + if (bot.digPercentage >= 1.0) { + //console.log("broke it"); + finishDigging(); + } + }, 50); bot.targetDigBlock = block bot.swingArm() + swingInterval = setInterval(() => { - bot.swingArm() - }, 350) + if (bot.targetDigBlock) { + bot.swingArm() + } else { + clearInterval(swingInterval); + } + }, 250) 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 @@ -137,15 +157,17 @@ function inject (bot) { } const eventName = `blockUpdate:${block.position}` + //console.log(bot.digPercentage + "% " + eventName); bot.on(eventName, onBlockUpdate) bot.stopDigging = () => { 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,14 @@ 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. - if (newBlock?.type !== 0) return + if (!newBlock) return; + 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) @@ -185,11 +208,7 @@ function inject (bot) { }) function canDigBlock (block) { - return ( - block && - block.diggable && - block.position.offset(0.5, 0.5, 0.5).distanceTo(bot.entity.position.offset(0, 1.65, 0)) <= 5.1 - ) + return block && block.diggable && block.position.offset(0.5, 0.5, 0.5).distanceTo(bot.entity.position.offset(0, 1.65, 0)) <= 5.1 } function digTime (block) { @@ -212,14 +231,7 @@ function inject (bot) { } const creative = bot.game.gameMode === 'creative' - return block.digTime( - type, - creative, - bot.entity.isInWater, - !bot.entity.onGround, - enchantments, - bot.entity.effects - ) + return block.digTime(type, creative, bot.entity.isInWater, !bot.entity.onGround, enchantments, bot.entity.effects) } bot.dig = dig @@ -230,4 +242,4 @@ function inject (bot) { function noop (err) { if (err) throw err -} +} From f5da310dcc00f1de9a3836199fb33305d7bdc604 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 5 Aug 2023 13:43:37 +0000 Subject: [PATCH 2/6] Fix linting errors --- lib/plugins/digging.js | 63 +++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/lib/plugins/digging.js b/lib/plugins/digging.js index e0586fe42..5ee3f10ed 100644 --- a/lib/plugins/digging.js +++ b/lib/plugins/digging.js @@ -13,19 +13,19 @@ function inject (bot) { bot.targetDigBlock = null bot.lastDigTime = null - bot.digPercentage = 0; - bot.lastHeldItemName = ""; - if (bot.heldItem) {bot.lastHeldItemName = bot.heldItem.name;} + bot.digPercentage = 0 + bot.lastHeldItemName = '' + if (bot.heldItem) { bot.lastHeldItemName = bot.heldItem.name } bot.on('heldItemChanged', () => { - //if (bot.heldItem) {console.log("this why " + bot.heldItem.name);} - //if (!bot.heldItem) {console.log("this why " + bot.heldItem);} - var nameToCompare = ""; - if (bot.heldItem) {nameToCompare = bot.heldItem.name;} + // if (bot.heldItem) {console.log("this why " + bot.heldItem.name);} + // if (!bot.heldItem) {console.log("this why " + bot.heldItem);} + let nameToCompare = '' + if (bot.heldItem) { nameToCompare = bot.heldItem.name } if (bot.lastHeldItemName != nameToCompare) { - bot.lastHeldItemName = nameToCompare; - bot.digPercentage = 0; + bot.lastHeldItemName = nameToCompare + bot.digPercentage = 0 } - }); + }) async function dig (block, forceLook, digFace) { if (block === null || block === undefined) { @@ -112,34 +112,33 @@ function inject (bot) { face: diggingFace // default face is 1 (top) }) const waitTime = bot.digTime(block) - //waitInterval = setTimeout(finishDigging, waitTime) - if (waitInterval) {clearInterval(waitInterval);} - //bot.digPercentage = (50 / bot.digTime(block)); - //bot.referenceTime = window.performance.now(); - waitInterval = setInterval(() => {//!!!! - bot.digPercentage += (50 / bot.digTime(block)); - //bot.digPercentage += (Math.abs(bot.referenceTime - window.performance.now()) / bot.digTime(block)); - //console.log(Math.abs(bot.referenceTime - window.performance.now()) + ", " + bot.digPercentage); - //bot.referenceTime = window.performance.now(); - if (bot.digPercentage >= 1.0) { - //console.log("broke it"); - finishDigging(); - } - }, 50); + // waitInterval = setTimeout(finishDigging, waitTime) + if (waitInterval) { clearInterval(waitInterval) } + // bot.digPercentage = (50 / bot.digTime(block)); + // bot.referenceTime = window.performance.now(); + waitInterval = setInterval(() => { //! !!! + bot.digPercentage += (50 / bot.digTime(block)) + // bot.digPercentage += (Math.abs(bot.referenceTime - window.performance.now()) / bot.digTime(block)); + // console.log(Math.abs(bot.referenceTime - window.performance.now()) + ", " + bot.digPercentage); + // bot.referenceTime = window.performance.now(); + if (bot.digPercentage >= 1.0) { + // console.log("broke it"); + finishDigging() + } + }, 50) bot.targetDigBlock = block bot.swingArm() - swingInterval = setInterval(() => { if (bot.targetDigBlock) { bot.swingArm() } else { - clearInterval(swingInterval); + clearInterval(swingInterval) } }, 250) function finishDigging () { - bot.digPercentage = 0; + bot.digPercentage = 0 clearInterval(swingInterval) clearInterval(waitInterval) swingInterval = null @@ -157,14 +156,14 @@ function inject (bot) { } const eventName = `blockUpdate:${block.position}` - //console.log(bot.digPercentage + "% " + eventName); + // console.log(bot.digPercentage + "% " + eventName); bot.on(eventName, onBlockUpdate) bot.stopDigging = () => { if (!bot.targetDigBlock) return bot.removeListener(eventName, onBlockUpdate) clearInterval(swingInterval) - bot.digPercentage = 0; + bot.digPercentage = 0 clearInterval(waitInterval) swingInterval = null waitInterval = null @@ -184,14 +183,14 @@ 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 - if (!newBlock) return; + if (!newBlock) return if (newBlock.type !== 0) return bot.removeListener(eventName, onBlockUpdate) clearInterval(swingInterval) clearInterval(waitInterval) swingInterval = null waitInterval = null - bot.digPercentage = 0.0; + bot.digPercentage = 0.0 bot.targetDigBlock = null bot.lastDigTime = performance.now() bot.emit('diggingCompleted', newBlock) @@ -242,4 +241,4 @@ function inject (bot) { function noop (err) { if (err) throw err -} +} From 55097ce339789a7e83d2758437617b62ce3456f8 Mon Sep 17 00:00:00 2001 From: Vakore <94314052+Vakore@users.noreply.github.com> Date: Sun, 13 Aug 2023 17:27:15 -0400 Subject: [PATCH 3/6] Update digging.js --- lib/plugins/digging.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/plugins/digging.js b/lib/plugins/digging.js index 5ee3f10ed..9fc3ae291 100644 --- a/lib/plugins/digging.js +++ b/lib/plugins/digging.js @@ -17,8 +17,6 @@ function inject (bot) { bot.lastHeldItemName = '' if (bot.heldItem) { bot.lastHeldItemName = bot.heldItem.name } bot.on('heldItemChanged', () => { - // if (bot.heldItem) {console.log("this why " + bot.heldItem.name);} - // if (!bot.heldItem) {console.log("this why " + bot.heldItem);} let nameToCompare = '' if (bot.heldItem) { nameToCompare = bot.heldItem.name } if (bot.lastHeldItemName != nameToCompare) { From b8bf65c0029dd683e7f087443c8459e99577e9ea Mon Sep 17 00:00:00 2001 From: Vakore <94314052+Vakore@users.noreply.github.com> Date: Sun, 13 Aug 2023 17:28:34 -0400 Subject: [PATCH 4/6] Update digging.js --- lib/plugins/digging.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/plugins/digging.js b/lib/plugins/digging.js index 9fc3ae291..8537c28be 100644 --- a/lib/plugins/digging.js +++ b/lib/plugins/digging.js @@ -19,7 +19,7 @@ function inject (bot) { bot.on('heldItemChanged', () => { let nameToCompare = '' if (bot.heldItem) { nameToCompare = bot.heldItem.name } - if (bot.lastHeldItemName != nameToCompare) { + if (bot.lastHeldItemName !== nameToCompare) { bot.lastHeldItemName = nameToCompare bot.digPercentage = 0 } @@ -109,18 +109,10 @@ function inject (bot) { location: block.position, face: diggingFace // default face is 1 (top) }) - const waitTime = bot.digTime(block) - // waitInterval = setTimeout(finishDigging, waitTime) if (waitInterval) { clearInterval(waitInterval) } - // bot.digPercentage = (50 / bot.digTime(block)); - // bot.referenceTime = window.performance.now(); - waitInterval = setInterval(() => { //! !!! + waitInterval = setInterval(() => { bot.digPercentage += (50 / bot.digTime(block)) - // bot.digPercentage += (Math.abs(bot.referenceTime - window.performance.now()) / bot.digTime(block)); - // console.log(Math.abs(bot.referenceTime - window.performance.now()) + ", " + bot.digPercentage); - // bot.referenceTime = window.performance.now(); if (bot.digPercentage >= 1.0) { - // console.log("broke it"); finishDigging() } }, 50) @@ -154,7 +146,6 @@ function inject (bot) { } const eventName = `blockUpdate:${block.position}` - // console.log(bot.digPercentage + "% " + eventName); bot.on(eventName, onBlockUpdate) bot.stopDigging = () => { From 1124f5b3f56e66b994d4285fa8cf05c016a33783 Mon Sep 17 00:00:00 2001 From: Vakore <94314052+Vakore@users.noreply.github.com> Date: Sun, 13 Aug 2023 17:35:58 -0400 Subject: [PATCH 5/6] Update digging.js --- lib/plugins/digging.js | 43 ++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/plugins/digging.js b/lib/plugins/digging.js index 8537c28be..4c515bdfb 100644 --- a/lib/plugins/digging.js +++ b/lib/plugins/digging.js @@ -47,15 +47,17 @@ function inject (bot) { } else if (digFace.z) { diggingFace = digFace.z > 0 ? BlockFaces.SOUTH : BlockFaces.NORTH } - await bot.lookAt(block.position.offset(0.5, 0.5, 0.5).offset(digFace.x * 0.5, digFace.y * 0.5, digFace.z * 0.5), forceLook) + await bot.lookAt( + block.position.offset(0.5, 0.5, 0.5).offset(digFace.x * 0.5, digFace.y * 0.5, digFace.z * 0.5), + forceLook + ) } else if (digFace === 'raycast') { // Check faces that could be seen from the current position. If the delta is smaller then 0.5 that means the // bot cam most likely not see the face as the block is 1 block thick // this could be false for blocks that have a smaller bounding box then 1x1x1 - const dx = bot.entity.position.x - block.position.x + 0.5 - const dy = bot.entity.position.y - block.position.y - 0.5 + bot.entity.height // -0.5 because the bot position - // is calculated from the block position that is inside its feet so 0.5 - 1 = -0.5 - const dz = bot.entity.position.z - block.position.z + 0.5 + const dx = bot.entity.position.x - (block.position.x + 0.5) + const dy = bot.entity.position.y + bot.entity.height - (block.position.y + 0.5) + const dz = bot.entity.position.z - (block.position.z + 0.5) // Check y first then x and z const visibleFaces = { y: Math.sign(Math.abs(dy) > 0.5 ? dy : 0), @@ -66,12 +68,20 @@ function inject (bot) { for (const i in visibleFaces) { if (!visibleFaces[i]) continue // skip as this face is not visible // target position on the target block face. -> 0.5 + (current face) * 0.5 - const targetPos = block.position.offset(0.5 + (i === 'x' ? visibleFaces[i] * 0.5 : 0), 0.5 + (i === 'y' ? visibleFaces[i] * 0.5 : 0), 0.5 + (i === 'z' ? visibleFaces[i] * 0.5 : 0)) + const targetPos = block.position.offset( + 0.5 + (i === 'x' ? visibleFaces[i] * 0.5 : 0), + 0.5 + (i === 'y' ? visibleFaces[i] * 0.5 : 0), + 0.5 + (i === 'z' ? visibleFaces[i] * 0.5 : 0) + ) const startPos = bot.entity.position.offset(0, bot.entity.height, 0) const rayBlock = bot.world.raycast(startPos, targetPos.clone().subtract(startPos).normalize(), 5) if (rayBlock) { const rayPos = rayBlock.position - if (rayPos.x === block.position.x && rayPos.y === block.position.y && rayPos.z === block.position.z) { + if ( + rayPos.x === block.position.x && + rayPos.y === block.position.y && + rayPos.z === block.position.z + ) { // console.info(rayBlock) validFaces.push({ face: rayBlock.face, @@ -86,7 +96,9 @@ function inject (bot) { let distSqrt = 999 for (const i in validFaces) { const tPos = validFaces[i].targetPos - const cDist = new Vec3(tPos.x, tPos.y, tPos.z).distanceSquared(bot.entity.position.offset(0, bot.entity.height, 0)) + const cDist = new Vec3(tPos.x, tPos.y, tPos.z).distanceSquared( + bot.entity.position.offset(0, bot.entity.height, 0) + ) if (distSqrt > cDist) { closest = validFaces[i] distSqrt = cDist @@ -196,7 +208,11 @@ function inject (bot) { }) function canDigBlock (block) { - return block && block.diggable && block.position.offset(0.5, 0.5, 0.5).distanceTo(bot.entity.position.offset(0, 1.65, 0)) <= 5.1 + return ( + block && + block.diggable && + block.position.offset(0.5, 0.5, 0.5).distanceTo(bot.entity.position.offset(0, 1.65, 0)) <= 5.1 + ) } function digTime (block) { @@ -219,7 +235,14 @@ function inject (bot) { } const creative = bot.game.gameMode === 'creative' - return block.digTime(type, creative, bot.entity.isInWater, !bot.entity.onGround, enchantments, bot.entity.effects) + return block.digTime( + type, + creative, + bot.entity.isInWater, + !bot.entity.onGround, + enchantments, + bot.entity.effects + ) } bot.dig = dig From 48871026a47fd6eac30f75ef46061c1daf095e6f Mon Sep 17 00:00:00 2001 From: Vakore <94314052+Vakore@users.noreply.github.com> Date: Sun, 13 Aug 2023 18:02:21 -0400 Subject: [PATCH 6/6] Update digging.js --- lib/plugins/digging.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/plugins/digging.js b/lib/plugins/digging.js index 4c515bdfb..c355adfc9 100644 --- a/lib/plugins/digging.js +++ b/lib/plugins/digging.js @@ -184,8 +184,7 @@ 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 - if (!newBlock) return - if (newBlock.type !== 0) return + if (newBlock?.type !== 0) return bot.removeListener(eventName, onBlockUpdate) clearInterval(swingInterval) clearInterval(waitInterval)