Last active
November 30, 2024 14:12
-
-
Save boly38/35106f6aa4935c22314c4911d8a870c0 to your computer and use it in GitHub Desktop.
Helper for howToReproduce https://github.com/PrismarineJS/mineflayer/issues/2757
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WARN WARN - this sample introduce some issue as #2757 is talking about : Event blockUpdate:(x, y, z) did not fire within timeout of 5000ms | |
// use FIXED version below instead of this | |
export const isAirBlock = b => b && b.name === 'air'; | |
/** | |
* Find a clear position dégagée next to the bot to place a bloc. | |
* @param {Bot} bot - Mineflyer Bot Instance | |
* @param {number} range - Around bot max range to explore. | |
* @returns {pos} - A valid position or null if no one is found. | |
*/ | |
export const findClearPosition = (bot, range = 3) => { | |
const botPos = bot.entity.position; | |
for (let dx = -range; dx <= range; dx++) { | |
for (let dz = -range; dz <= range; dz++) { | |
for (let dy = -2; dy <= 2; dy++) { | |
const targetPos = botPos.offset(dx, dy, dz); | |
const blockBelow = bot.blockAt(targetPos.offset(0, -1, 0)); // Bloc below | |
const blockAtTarget = bot.blockAt(targetPos); // Bloc at target | |
if (blockBelow && blockBelow.boundingBox === 'block' && isAirBlock(blockAtTarget)) { | |
console.log(`==>> check position : ${targetPos} | block under is ${blockBelow?.name || 'null'}, at target bock is ${blockAtTarget?.name || 'null'}`); | |
return targetPos; | |
} | |
} | |
} | |
} | |
return null; // no clear position found | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isAirOrSnowBlock = b => new Set(['air', 'snow']).has(b?.name); | |
export const isCraftingTableBlock = b => b?.name === 'crafting_table'; | |
export const isChestBlock = b => b?.name === 'chest'; | |
export const isBedBlock = b => b?.name?.startsWith('bed_'); | |
export const isBelowPlaceCorrect = b => b && !isChestBlock(b) && !isCraftingTableBlock(b) && !isBedBlock() | |
/** | |
* Find a clear position dégagée next to the bot to place a bloc. | |
* @param {Bot} bot - Mineflyer Bot Instance | |
* @param {number} range - Around bot max range to explore. | |
* @returns {pos} - A valid position or null if no one is found. | |
*/ | |
export const findClearPosition = (bot, range = 3) => { | |
const botPos = bot.entity.position; | |
for (let dx = -range; dx <= range; dx++) { | |
for (let dz = -range; dz <= range; dz++) { | |
for (let dy = -2; dy <= 2; dy++) { | |
const targetPos = botPos.offset(dx, dy, dz); | |
const blockBelow = bot.blockAt(targetPos.offset(0, -1, 0)); // Bloc below | |
const blockAtTarget = bot.blockAt(targetPos); // Bloc at target | |
if (blockBelow && blockBelow.boundingBox === 'block' | |
&& isAirOrSnowBlock(blockAtTarget) | |
&& isBelowPlaceCorrect(blockBelow) | |
&& targetPos !== botPos | |
) { | |
console.log(`=> clear position : ${targetPos} | block under ${blockBelow?.name} target bock ${blockAtTarget?.name}`); | |
return targetPos; | |
} | |
} | |
} | |
} | |
return null; // no clear position found | |
} | |
/** | |
* Place current block at a clear place | |
* - pre-condition: bot hand is equipped with a block | |
* XX Issue "Event blockUpdate:(x, y, z) did not fire within timeout of 5000ms", cf. https://github.com/PrismarineJS/mineflayer/issues/2757 | |
* @param bot mineflyer bot instance | |
* @param range max distance around bot | |
* @returns {Promise<pos|null>} | |
*/ | |
export const placeCurrentBlockAtClearPlace = async (bot, range = 3) => { | |
const clearPosition = findClearPosition(bot, range); | |
if (!clearPosition) { | |
bot.chat("Aucune position dégagée trouvée pour placer un block !"); | |
return null | |
} | |
const blockBelow = bot.blockAt(clearPosition.offset(0, -1, 0)); | |
if (!blockBelow) { | |
bot.chat(`oO, aucun bloc sous la position dégagée ${clearPosition}!`); | |
return null | |
} | |
await bot.placeBlock(blockBelow, new Vec3(0, 1, 0)); // Place au-dessus du bloc trouvé | |
return clearPosition; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment