mineflayer-pathfinder

repository·master·Indexed 43 days ago

https://github.com/prismarinejs/mineflayer-pathfinder

A pathfinding plugin for the Mineflayer Minecraft bot API that enables autonomous navigation of complex terrain using A* pathfinding. It features customizable movement rules via the Movements class, various goal types (such as GoalNear and GoalFollow), and a createHuman controller for smooth, human-like walking. Version 2.4.5 supports block breaking, building, and complex path computation with support for dynamic goals and custom cost functions.

Tokens
8K
Snippets
37
Records
46
Agent score
55%

What's inside mineflayer-pathfinder

  1. Customize pathfinding behavior with the Movements class

    master

    The Movements class configures how the pathfinder plans its paths, including block breaking, building, and movement costs. To change default behavior, create a new instance of Movements and pass it to bot.pathfinder.setMovements().

    Example:

    const { Movements } = require('mineflayer-pathfinder')
    
    bot.once('spawn', () => {
      const defaultMove = new Movements(bot)
    
      defaultMove.allow1by1towers = false // Do not build 1x1 towers
      defaultMove.canDig = false // Disable breaking blocks
      defaultMove.scafoldingBlocks.push(bot.registry.itemsByName['netherrack'].id)
      
      bot.pathfinder.setMovements(defaultMove)
    })
    const { Movements } = require('mineflayer-pathfinder')
    
    bot.once('spawn', () => {
      const defaultMove = new Movements(bot)
    
      defaultMove.allow1by1towers = false // Do not build 1x1 towers when going up
      defaultMove.canDig = false // Disable breaking of blocks when pathing 
      defaultMove.scafoldingBlocks.push(bot.registry.itemsByName['netherrack'].id) // Add nether rack to allowed scaffolding items
      bot.pathfinder.setMovements(defaultMove) // Update the movement instance pathfinder uses
    
      // Do pathfinder things
      // ...
    })
  2. Combine goals with GoalCompositeAll

    master

    A GoalCompositeAll is a composite goal that is satisfied only when the bot fulfills all of the goals contained within its provided array.

    Note: The bot must satisfy all conditions simultaneously. It is not enough to visit each location sequentially; the bot must reach a state where it is within the parameters of every goal in the array at the same time.

    const goalsArray = [Goal1, Goal2, Goal3]
    const goalAll = new GoalCompositeAll(goalsArray)
    
    await bot.pathfinder.goto(goalAll)
  3. Use different Goal types for navigation

    master

    Goals define the destination or objective for the pathfinder. You should not instantiate the abstract Goal class directly; instead, use the provided implementations or extend Goal to create custom ones.

    Position-Based Goals

    • GoalBlock(x, y, z): Stand inside this specific block at foot level.
    • GoalNear(x, y, z, range): Get within a certain radius of this block position.
    • GoalXZ(x, z): Reach a specific X and Z coordinate (ignores Y level).
    • GoalNearXZ(x, z, range): Reach an approximate X and Z coordinate within a range.
    • GoalY(y): Reach a specific Y level.
    • GoalGetToBlock(x, y, z): Get directly adjacent to the block (useful for interacting with chests).

    Interaction & Entity Goals

    • GoalFollow(entity, range): Follow a specific entity within a given range.
    • GoalLookAtBlock(pos, world, options): Path to a position where a blockface at pos is visible.
      • options.reach: Max distance from the clicked face (Default: 4.5).
      • options.entityHeight: Default 1.6.
    • GoalPlaceBlock(pos, world, options): Position the bot to place a block at pos.
      • options.range: Max distance from the clicked face.
      • options.faces: Allowed click directions.
      • options.facing: Required direction the player must face.
      • options.facing3D: Boolean for 3D vs 2D facing.
      • options.half: 'top' or 'bottom' half to click.

    Composite Goals

    • GoalCompositeAny(Array<Goal>): Satisfied if any one of the provided goals is met.
    • GoalCompositeAll(Array<Goal>): Satisfied only if all provided goals are met.
    • GoalInvert(goal): Inverts the logic of the provided goal.
  4. Set up mineflayer-pathfinder with a bot

    master

    To use pathfinder, you must load the plugin into your Mineflayer bot and configure its Movements. The Movements class defines the bot's capabilities, such as which blocks it can break/place, its ability to pillar, and its maximum fall distance.

    1. Import mineflayer, pathfinder, and Movements.
    2. Load the plugin using bot.loadPlugin(pathfinder).
    3. Create a Movements instance with the bot.
    4. Register the movements using bot.pathfinder.setMovements(movements).
    const mineflayer = require('mineflayer')
    const { pathfinder, Movements, goals: { GoalBlock } } = require('mineflayer-pathfinder')
    
    const bot = mineflayer.createBot({
      host: 'localhost',
      port: 25565,
      username: 'Pathfinder',
      auth: 'offline'
    })
    
    bot.once('spawn', () => {
      bot.loadPlugin(pathfinder)
      const defaultMovements = new Movements(bot)
      bot.pathfinder.setMovements(defaultMovements)
    })
  5. Customize movement exclusion areas

    master

    You can influence the pathfinder by defining custom exclusion areas. These areas apply weights to blocks, making the pathfinder avoid them for specific actions.

    Use the following methods to add exclusion functions to the Movements instance:

    • exclusionAreasStep: Affects where the bot can step.
    • exclusionAreasBreak: Affects where the bot can break blocks.
    • exclusionAreasPlace: Affects where the bot can place blocks.

    Each exclusion area is a function that takes a block and returns a weight (number).

  6. Use Goal classes to define navigation targets

    master

    In mineflayer-pathfinder, navigation targets are defined using Goal classes. When calling bot.pathfinder.goto(goal), the goal parameter must be an instance of one of these classes. These classes determine when the bot has reached its destination (isEnd), how to estimate the distance to the target (heuristic), and whether the target has moved or changed (hasChanged).

    // Example: Pathfinding to a specific block
    const { GoalBlock } = require('mineflayer-pathfinder').goals;
    const goal = new GoalBlock(10, 64, 10);
    bot.pathfinder.goto(goal);
  7. Configure movement behavior with the Movements class

    master

    The Movements class is used to configure how the bot traverses terrain. When you instantiate Movements(bot), it sets up default behaviors for digging, placing, and traversing different types of blocks and entities. You can customize these behaviors by modifying the properties on the Movements instance.

    Key Configuration Properties

    Costs

    Adjust these to influence the pathfinder's preference for certain actions:

    • digCost: Cost of digging a block.
    • placeCost: Cost of placing a block.
    • liquidCost: Cost of moving through liquid.
    • entityCost: Cost of moving through/near entities.

    Capabilities

    Enable or disable specific movement types:

    • canDig: Whether the bot is allowed to dig blocks.
    • allow1by1towers: Whether the bot can build 1x1 towers to climb.
    • allowFreeMotion: Whether the bot can move freely (not standard).
    • allowParkour: Whether the bot can perform parkour jumps.
    • allowSprinting: Whether the bot can sprint.
    • allowEntityDetection: Whether the bot detects entities to avoid them.
    • canOpenDoors: Whether the bot attempts to open gates/doors (Note: may cause issues on some servers).

    Constraints

    • dontCreateFlow: If true, the bot avoids digging blocks adjacent to liquids to prevent flowing water/lava.
    • dontMineUnderFallingBlock: If true, the bot avoids digging blocks that are supporting falling blocks or entities.
    • maxDropDown: The maximum distance the bot is willing to drop down (default: 4).
    • infiniteLiquidDropdownDistance: If true, ignores maxDropDown when dropping into liquids.
  8. Install and initialize mineflayer-pathfinder

    master

    To use the pathfinder, you must inject it into your Mineflayer bot instance. The plugin attaches a pathfinder object to your bot.

    const mineflayer = require('mineflayer')
    const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
    
    const bot = mineflayer.createBot({ /* ... */ })
    pathfinder(bot)
    
    // Now you can use bot.pathfinder
    const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
    pathfinder(bot)
  9. Basic usage of mineflayer-pathfinder

    master

    To use the pathfinder, load the plugin into your bot, define a Movements instance, and set a Goal. In this example, the bot will move to a player when they type 'come' in chat.

    const mineflayer = require('mineflayer')
    const pathfinder = require('mineflayer-pathfinder').pathfinder
    const Movements = require('mineflayer-pathfinder').Movements
    const { GoalNear } = require('mineflayer-pathfinder').goals
    const bot = mineflayer.createBot({ username: 'Player' })
    
    bot.loadPlugin(pathfinder)
    
    bot.once('spawn', () => {
      const defaultMove = new Movements(bot)
      
      bot.on('chat', function(username, message) {
      
        if (username === bot.username) return
    
        const target = bot.players[username] ? bot.players[username].entity : null
        if (message === 'come') {
          if (!target) {
            bot.chat('I don\'t see you !')
            return
          }
          const p = target.position
    
          bot.pathfinder.setMovements(defaultMove)
          bot.pathfinder.setGoal(new GoalNear(p.x, p.y, p.z, 1))
        } 
      })
    })
  10. Use GoalBlock to navigate to a specific coordinate

    master

    A GoalBlock is a goal that is satisfied when the bot's feet are inside a specific block.

    To use it:

    1. Instantiate GoalBlock with x, y, and z coordinates (integers).
    2. Use await bot.pathfinder.goto(goal) to command the bot to move to that location.
    // Create a goal for coordinates 15, 3, 75
    const myGoal = new GoalBlock(15, 3, 75)
    
    // Command the bot to go there
    await bot.pathfinder.goto(myGoal)