mineflayer

repository·master·Indexed 27 days ago

https://github.com/prismarinejs/mineflayer

A high-level JavaScript API for creating Minecraft bots, supporting versions 1.8 to 1.21.11. It provides features for entity tracking, block querying, physics, inventory management, and interaction with game elements like furnaces and villagers. The library is pluggable and supports various third-party plugins for pathfinding, combat, and LLM integration.

Tokens
25.8K
Snippets
38
Records
191
Agent score
92%

What's inside mineflayer

  1. Debug and test Mineflayer 1.21.5 support

    master

    If you are contributing to or testing the 1.21.5 support implementation, you can use specific Mocha test patterns to isolate issues. Use the DEBUG environment variable to inspect protocol-level details during chunk loading tests.

    Running Tests

    Test current 1.21.5 status:

    npm run mocha_test -- -g "mineflayer_external 1.21.5v"

    Debug chunk loading:

    DEBUG="minecraft-protocol" npm run mocha_test -- -g "mineflayer_external 1.21.5v.*blocks"

    Debug creative mode:

    npm run mocha_test -- -g "mineflayer_external 1.21.5v.*creative"

    Validate specific functionality:

    • Inventory: npm run mocha_test -- -g "mineflayer_external 1.21.5v.*inventory"
    • Entities: npm run mocha_test -- -g "mineflayer_external 1.21.5v.*entities"
    # Test current 1.21.5 status
    npm run mocha_test -- -g "mineflayer_external 1.21.5v"
    
    # Debug chunk loading
    DEBUG="minecraft-protocol" npm run mocha_test -- -g "mineflayer_external 1.21.5v.*blocks"
    
    # Debug creative mode
    npm run mocha_test -- -g "mineflayer_external 1.21.5v.*creative"
  2. Create a basic Mineflayer bot

    master

    To create a bot, require the mineflayer module and call mineflayer.createBot(). By default, it attempts to connect to localhost on port 25565.

    To specify a different server, pass an options object containing host and port.

    const mineflayer = require('mineflayer')
    
    const options = {
      host: 'localhost',
      port: 25565
    }
    
    const bot = mineflayer.createBot(options)
  3. Use command line arguments for bot configuration

    master

    To avoid hardcoding sensitive information like passwords, you can use process.argv to pass configuration via the command line.

    Example usage: node filename.js <host> <port> <username> <password>

    const bot = mineflayer.createBot({
      host: process.argv[2],
      port: parseInt(process.argv[3]),
      username: process.argv[4],
      password: process.argv[5]
    })
  4. Get item lore or text

    master

    Item lore is stored in the item.nbt property. It is recommended to use the prismarine-nbt library to simplify the NBT data and prismarine-chat to convert the lore lines into readable strings.

    function getLore (item) {
      let message = ''
      if (item.nbt == null) return message
    
      const nbt = require('prismarine-nbt')
      const ChatMessage = require('prismarine-chat')(bot.version)
    
      const data = nbt.simplify(item.nbt)
      const display = data.display
      if (display == null) return message
    
      const lore = display.Lore
      if (lore == null) return message
      for (const line of lore) {
        message += new ChatMessage(line).toString()
        message += '\n'
      }
    
      return message
    }
  5. Connect via SOCKS5 proxy

    master

    To use a SOCKS5 proxy, provide a connect function in the createBot options. This function should use a library like socks to create a connection and then pass the resulting socket to the client using client.setSocket(info.socket).

    // Example configuration for SOCKS5
    // Requires environment variables: PROXY_IP, PROXY_PORT, PROXY_USERNAME, PROXY_PASSWORD, MC_SERVER_IP, MC_SERVER_PORT
    
    createBot({
      // Do not include 'host' here if using custom connect
      connect: (client) => {
        socks.createConnection({
          proxy: {
            host: PROXY_IP,
            port: PROXY_PORT,
            type: 5,
            userId: PROXY_USERNAME,
            password: PROXY_PASSWORD
          },
          command: 'connect',
          destination: {
            host: MC_SERVER_IP,
            port: MC_SERVER_PORT
          }
        }, (err, info) => {
          if (err) {
            console.log(err)
            return
          }
          client.setSocket(info.socket)
          client.emit('connect')
        })
      }
    })
  6. Create an external test for Mineflayer

    master

    External tests are located in test/externalTests/ and are run against a vanilla Minecraft server.

    To create one, add a new file to test/externalTests/. The file must export a function that returns either a single function or an array of functions. Each function receives the bot object and a done callback as parameters. You should include assertions within these functions to verify the tested functionality.

  7. Create a third-party Mineflayer plugin

    master

    Mineflayer supports plugins that add high-level APIs to the bot. To create a plugin, follow these steps:

    1. Create a new repository.
    2. In your index.js, export a function that initializes the plugin, accepting Mineflayer as an argument.
    3. This initialization function must return a function that introduces the plugin to the bot object.
    4. Inside that returned function, you can add new functionalities to the bot.

    Important: Because the Mineflayer object is passed as an argument, your plugin should not depend on the mineflayer package directly (do not include mineflayer in your package.json dependencies).

  8. Run Mineflayer tests

    master

    You can run the test suite using npm commands. You can test the entire project, a specific Minecraft version, or a specific test case using the mocha_test script.

    Test everything

    Run all tests in the repository:

    npm test

    Test a specific Minecraft version

    Use the -g flag followed by the version string (e.g., 1.12, 1.15.2):

    npm run mocha_test -- -g <version>

    Test a specific test name

    Use the -g flag followed by the name of the test (e.g., bed, useChests, rayTrace):

    npm run mocha_test -- -g <test_name>

    Example: Test a specific feature for a specific version

    To run the block finder test specifically for version 1.18.1:

    npm run mocha_test -- -g "1.18.1.*BlockFinder"