Skip to content

Instantly share code, notes, and snippets.

@boly38
Last active November 30, 2024 14:12
Show Gist options
  • Select an option

  • Save boly38/35106f6aa4935c22314c4911d8a870c0 to your computer and use it in GitHub Desktop.

Select an option

Save boly38/35106f6aa4935c22314c4911d8a870c0 to your computer and use it in GitHub Desktop.

Revisions

  1. boly38 revised this gist Nov 30, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions botUtil.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // 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.
  2. boly38 revised this gist Nov 30, 2024. 1 changed file with 57 additions and 0 deletions.
    57 changes: 57 additions & 0 deletions botUtil___FIXED.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    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;
    }
  3. boly38 created this gist Nov 29, 2024.
    25 changes: 25 additions & 0 deletions botUtil.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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
    }