PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.96k stars 904 forks source link

error with node minecraft protocols #3025

Closed Maleavour closed 1 year ago

Maleavour commented 1 year ago

Versions

Detailed description of a problem

A clear and concise description of what the problem is, with as much context as possible. What are you building? What problem are you trying to solve?

i tried modifying the version to 1.19,4 unsuccesful

Did you try any method from the API? Did you try any example? Any error from those? i could not find anything relating to ping.js or autoversion.js

Your current code

autoversion.js | v

'use strict'

const ping = require('../ping')
const debug = require('debug')('minecraft-protocol')
const states = require('../states')
const minecraftData = require('minecraft-data')

module.exports = function (client, options) {
  client.wait_connect = true // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
  debug('pinging', options.host)
  // TODO: use 0xfe ping instead for better compatibility/performance? https://github.com/deathcap/node-minecraft-ping
  ping(options, function (err, response) {
    if (err) { return client.emit('error', err) }
    debug('ping response', response)
    // TODO: could also use ping pre-connect to save description, type, max players, etc.
    const motd = response.description
    debug('Server description:', motd) // TODO: save

    // Pass server-reported version to protocol handler
    // The version string is interpreted by https://github.com/PrismarineJS/node-minecraft-data
    const brandedMinecraftVersion = response.version.name // 1.8.9, 1.7.10
    const protocolVersion = response.version.protocol//    47,      5
    const guessFromName = [brandedMinecraftVersion]
      .concat(brandedMinecraftVersion.match(/((\d+\.)+\d+)/g) || [])
      .map(function (version) {
        return minecraftData.versionsByMinecraftVersion.pc[version]
      })
      .filter(function (info) { return info })
      .sort(function (a, b) { return b.version - a.version })
    const versions = (minecraftData.postNettyVersionsByProtocolVersion.pc[protocolVersion] || []).concat(guessFromName)
    if (versions.length === 0) {
      client.emit('error', new Error(`unsupported/unknown protocol version: ${protocolVersion}, update minecraft-data`))
    }
    const minecraftVersion = versions[0].minecraftVersion

    debug(`Server version: ${minecraftVersion}, protocol: ${protocolVersion}`)

    options.version = minecraftVersion
    options.protocolVersion = protocolVersion

    // Reinitialize client object with new version TODO: move out of its constructor?
    client.version = minecraftVersion
    client.state = states.HANDSHAKING

    // Let other plugins such as Forge/FML (modinfo) respond to the ping response
    if (client.autoVersionHooks) {
      client.autoVersionHooks.forEach((hook) => {
        hook(response, client, options)
      })
    }

    // Finished configuring client object, let connection proceed
    client.emit('connect_allowed')
    client.wait_connect = false
  })
  return client
}
----------------------------------------------------------------------------------------------------------------------------------------------------
index.js |
            v
const mineflayer = require('mineflayer')

const bot = mineflayer.createBot({
  host: 'localhost', // minecraft server ip
  username: 'Bot223567', // minecraft username
  auth: 'offline', // for offline mode servers, you can set this to 'offline'
  // port: 25565,                // only set if you need a port that isn't 25565
  // version: false,             // only set if you need a specific version or snapshot (ie: "1.8.9" or "1.16.5"), otherwise it's set automatically
  // password: '12345678'        // set if you want to use password-based auth (may be unreliable)
})

bot.on('chat', (username, message) => {
  if (username === bot.username) return
  bot.chat(message)
})

// Log errors and kick reasons:
bot.on('kicked', console.log)
bot.on('error', console.log)
----------------------------------------------------------------------------------------------------------------------------------------------------
ping.js |
           v
'use strict'

const Client = require('./client')
const states = require('./states')
const tcpDns = require('./client/tcp_dns')

module.exports = cbPing

function cbPing (options, cb) {
  const pingPromise = ping(options)
  if (cb) {
    pingPromise.then((d) => {
      cb(null, d)
    }).catch((err) => {
      cb(err, null)
    })
  }
  return pingPromise
};

function ping (options) {
  options.host = options.host || 'localhost'
  options.port = options.port || 25565
  const optVersion = options.version || require('./version').defaultVersion
  const mcData = require('minecraft-data')(optVersion)
  const version = mcData.version
  options.majorVersion = version.majorVersion
  options.protocolVersion = version.version
  let closeTimer = null
  options.closeTimeout = options.closeTimeout || 120 * 1000
  options.noPongTimeout = options.noPongTimeout || 5 * 1000

  const client = new Client(false, version.minecraftVersion)
  return new Promise((resolve, reject) => {
    client.on('error', function (err) {
      clearTimeout(closeTimer)
      client.end()
      reject(err)
    })
    client.once('server_info', function (packet) {
      const data = JSON.parse(packet.response)
      const start = Date.now()
      const maxTime = setTimeout(() => {
        clearTimeout(closeTimer)
        client.end()
        resolve(data)
      }, options.noPongTimeout)
      client.once('ping', function (packet) {
        data.latency = Date.now() - start
        clearTimeout(maxTime)
        clearTimeout(closeTimer)
        client.end()
        resolve(data)
      })
      client.write('ping', { time: [0, 0] })
    })
    client.on('state', function (newState) {
      if (newState === states.STATUS) { client.write('ping_start', {}) }
    })
    // TODO: refactor with src/client/setProtocol.js
    client.on('connect', function () {
      client.write('set_protocol', {
        protocolVersion: options.protocolVersion,
        serverHost: options.host,
        serverPort: options.port,
        nextState: 1
      })
      client.state = states.STATUS
    })
    // timeout against servers that never reply while keeping
    // the connection open and alive.
    closeTimer = setTimeout(function () {
      client.end()
      reject(new Error('ETIMEDOUT'))
    }, options.closeTimeout)
    tcpDns(client, options)
    options.connect(client)
  })
};
----------------------------------------------------------------------------------------------------------------------------------------------------
/*
Package.json
{
  "name": "mineflayer",
  "version": "4.8.1",
  "description": "create minecraft bots with a stable, high level API",
  "main": "index.js",
  "types": "index.d.ts",
  "scripts": {
    "mocha_test": "mocha --reporter spec --exit",
    "test": "npm run mocha_test",
    "pretest": "npm run lint",
    "lint": "standard && standard-markdown",
    "fix": "standard --fix && standard-markdown --fix",
    "prepublishOnly": "cp docs/README.md README.md"
  },
  "repository": {
    "type": "git",
    "url": "git://github.com/PrismarineJS/mineflayer.git"
  },
  "engines": {
    "node": ">=14"
  },
  "license": "MIT",
  "dependencies": {
    "minecraft-data": "^3.26.0",
    "minecraft-protocol": "^1.40.3",
    "prismarine-biome": "^1.1.1",
    "prismarine-block": "^1.13.1",
    "prismarine-chat": "^1.7.1",
    "prismarine-chunk": "^1.32.0",
    "prismarine-entity": "^2.2.0",
    "prismarine-item": "^1.12.1",
    "prismarine-nbt": "^2.0.0",
    "prismarine-physics": "^1.3.1",
    "prismarine-recipe": "^1.3.0",
    "prismarine-registry": "^1.5.0",
    "prismarine-windows": "^2.5.0",
    "prismarine-world": "^3.6.0",
    "protodef": "^1.14.0",
    "typed-emitter": "^1.0.0",
    "vec3": "^0.1.7"
  },
  "devDependencies": {
    "@types/node": "^18.0.6",
    "doctoc": "^2.0.1",
    "minecraft-wrap": "^1.3.0",
    "mineflayer": "file:.",
    "mocha": "^10.0.0",
    "standard": "^17.0.0",
    "standard-markdown": "^7.1.0",
    "typescript": "^5.0.3"
  }
}

*/

Expected behavior

i expected it to connect to my singleplayer world that has the port 25565 open currently

Additional context

the error code is:

TypeError: Cannot read properties of undefined (reading 'minecraftVersion') at c:\Users\fungi\node_modules\minecraft-protocol\src\client\autoVersion.js:34:42 at c:\Users\fungi\node_modules\minecraft-protocol\src\ping.js:13:7 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {stack: 'TypeError: Cannot read properties of undefine…ions (node:internal/process/task_queues:95:5)', message: 'Cannot read properties of undefined (reading 'minecraftVersion')'}

loader.js:78 No debugger available, can not send 'variables' TypeError: Cannot read properties of undefined (reading 'minecraftVersion') at c:\Users\fungi\node_modules\minecraft-protocol\src\client\autoVersion.js:34:42 at c:\Users\fungi\node_modules\minecraft-protocol\src\ping.js:13:7 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {stack: 'TypeError: Cannot read properties of undefine…ions (node:internal/process/task_queues:95:5)', message: 'Cannot read properties of undefined (reading 'minecraftVersion')'}

rom1504 commented 1 year ago

1.19.4 is not supported yet

On Tue, Apr 18, 2023, 09:24 Deadshot222224 @.***> wrote:

Versions

  • mineflayer: 4.8.1
  • server: vanilla/spigot/paper single player world local host
  • node: 18.16.0

Detailed description of a problem

A clear and concise description of what the problem is, with as much context as possible. What are you building? What problem are you trying to solve? i tried modifying the version to 1.19,4 unsuccesful

Did you try any method from the API? Did you try any example? Any error from those? i could not find anything relating to ping.js or autoversion.js Your current code

autoversion.js | v

'use strict' const ping = require('../ping')const debug = require('debug')('minecraft-protocol')const states = require('../states')const minecraftData = require('minecraft-data') module.exports = function (client, options) { client.wait_connect = true // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed' debug('pinging', options.host) // TODO: use 0xfe ping instead for better compatibility/performance? https://github.com/deathcap/node-minecraft-ping ping(options, function (err, response) { if (err) { return client.emit('error', err) } debug('ping response', response) // TODO: could also use ping pre-connect to save description, type, max players, etc. const motd = response.description debug('Server description:', motd) // TODO: save

// Pass server-reported version to protocol handler
// The version string is interpreted by https://github.com/PrismarineJS/node-minecraft-data
const brandedMinecraftVersion = response.version.name // 1.8.9, 1.7.10
const protocolVersion = response.version.protocol//    47,      5
const guessFromName = [brandedMinecraftVersion]
  .concat(brandedMinecraftVersion.match(/((\d+\.)+\d+)/g) || [])
  .map(function (version) {
    return minecraftData.versionsByMinecraftVersion.pc[version]
  })
  .filter(function (info) { return info })
  .sort(function (a, b) { return b.version - a.version })
const versions = (minecraftData.postNettyVersionsByProtocolVersion.pc[protocolVersion] || []).concat(guessFromName)
if (versions.length === 0) {
  client.emit('error', new Error(`unsupported/unknown protocol version: ${protocolVersion}, update minecraft-data`))
}
const minecraftVersion = versions[0].minecraftVersion

debug(`Server version: ${minecraftVersion}, protocol: ${protocolVersion}`)

options.version = minecraftVersion
options.protocolVersion = protocolVersion

// Reinitialize client object with new version TODO: move out of its constructor?
client.version = minecraftVersion
client.state = states.HANDSHAKING

// Let other plugins such as Forge/FML (modinfo) respond to the ping response
if (client.autoVersionHooks) {
  client.autoVersionHooks.forEach((hook) => {
    hook(response, client, options)
  })
}

// Finished configuring client object, let connection proceed
client.emit('connect_allowed')
client.wait_connect = false

}) return client}----------------------------------------------------------------------------------------------------------------------------------------------------index.js | vconst mineflayer = require('mineflayer') const bot = mineflayer.createBot({ host: 'localhost', // minecraft server ip username: 'Bot223567', // minecraft username auth: 'offline', // for offline mode servers, you can set this to 'offline' // port: 25565, // only set if you need a port that isn't 25565 // version: false, // only set if you need a specific version or snapshot (ie: "1.8.9" or "1.16.5"), otherwise it's set automatically // password: '12345678' // set if you want to use password-based auth (may be unreliable)}) bot.on('chat', (username, message) => { if (username === bot.username) return bot.chat(message)}) // Log errors and kick reasons:bot.on('kicked', console.log)bot.on('error', console.log)----------------------------------------------------------------------------------------------------------------------------------------------------ping.js | v'use strict' const Client = require('./client')const states = require('./states')const tcpDns = require('./client/tcp_dns') module.exports = cbPing function cbPing (options, cb) { const pingPromise = ping(options) if (cb) { pingPromise.then((d) => { cb(null, d) }).catch((err) => { cb(err, null) }) } return pingPromise}; function ping (options) { options.host = options.host || 'localhost' options.port = options.port || 25565 const optVersion = options.version || require('./version').defaultVersion const mcData = require('minecraft-data')(optVersion) const version = mcData.version options.majorVersion = version.majorVersion options.protocolVersion = version.version let closeTimer = null options.closeTimeout = options.closeTimeout || 120 1000 options.noPongTimeout = options.noPongTimeout || 5 1000

const client = new Client(false, version.minecraftVersion) return new Promise((resolve, reject) => { client.on('error', function (err) { clearTimeout(closeTimer) client.end() reject(err) }) client.once('server_info', function (packet) { const data = JSON.parse(packet.response) const start = Date.now() const maxTime = setTimeout(() => { clearTimeout(closeTimer) client.end() resolve(data) }, options.noPongTimeout) client.once('ping', function (packet) { data.latency = Date.now() - start clearTimeout(maxTime) clearTimeout(closeTimer) client.end() resolve(data) }) client.write('ping', { time: [0, 0] }) }) client.on('state', function (newState) { if (newState === states.STATUS) { client.write('ping_start', {}) } }) // TODO: refactor with src/client/setProtocol.js client.on('connect', function () { client.write('set_protocol', { protocolVersion: options.protocolVersion, serverHost: options.host, serverPort: options.port, nextState: 1 }) client.state = states.STATUS }) // timeout against servers that never reply while keeping // the connection open and alive. closeTimer = setTimeout(function () { client.end() reject(new Error('ETIMEDOUT')) }, options.closeTimeout) tcpDns(client, options) options.connect(client) })};----------------------------------------------------------------------------------------------------------------------------------------------------/*Package.json{ "name": "mineflayer", "version": "4.8.1", "description": "create minecraft bots with a stable, high level API", "main": "index.js", "types": "index.d.ts", "scripts": { "mocha_test": "mocha --reporter spec --exit", "test": "npm run mocha_test", "pretest": "npm run lint", "lint": "standard && standard-markdown", "fix": "standard --fix && standard-markdown --fix", "prepublishOnly": "cp docs/README.md README.md" }, "repository": { "type": "git", "url": "git://github.com/PrismarineJS/mineflayer.git" }, "engines": { "node": ">=14" }, "license": "MIT", "dependencies": { "minecraft-data": "^3.26.0", "minecraft-protocol": "^1.40.3", "prismarine-biome": "^1.1.1", "prismarine-block": "^1.13.1", "prismarine-chat": "^1.7.1", "prismarine-chunk": "^1.32.0", "prismarine-entity": "^2.2.0", "prismarine-item": "^1.12.1", "prismarine-nbt": "^2.0.0", "prismarine-physics": "^1.3.1", "prismarine-recipe": "^1.3.0", "prismarine-registry": "^1.5.0", "prismarine-windows": "^2.5.0", "prismarine-world": "^3.6.0", "protodef": "^1.14.0", "typed-emitter": "^1.0.0", "vec3": "^0.1.7" }, "devDependencies": { @.**/node": "^18.0.6", "doctoc": "^2.0.1", "minecraft-wrap": "^1.3.0", "mineflayer": "file:.", "mocha": "^10.0.0", "standard": "^17.0.0", "standard-markdown": "^7.1.0", "typescript": "^5.0.3" }}/

Expected behavior

i expected it to connect to my singleplayer world that has the port 25565 open currently Additional context

the error code is:

TypeError: Cannot read properties of undefined (reading 'minecraftVersion') at c:\Users\fungi\node_modules\minecraft-protocol\src\client\autoVersion.js:34:42 at c:\Users\fungi\node_modules\minecraft-protocol\src\ping.js:13:7 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {stack: 'TypeError: Cannot read properties of undefine…ions (node:internal/process/task_queues:95:5)', message: 'Cannot read properties of undefined (reading 'minecraftVersion')'}

loader.js:78 No debugger available, can not send 'variables' TypeError: Cannot read properties of undefined (reading 'minecraftVersion') at c:\Users\fungi\node_modules\minecraft-protocol\src\client\autoVersion.js:34:42 at c:\Users\fungi\node_modules\minecraft-protocol\src\ping.js:13:7 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {stack: 'TypeError: Cannot read properties of undefine…ions (node:internal/process/task_queues:95:5)', message: 'Cannot read properties of undefined (reading 'minecraftVersion')'}

— Reply to this email directly, view it on GitHub https://github.com/PrismarineJS/mineflayer/issues/3025, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAR437XZAMUAYYUNOTNWW5TXBY6SLANCNFSM6AAAAAAXCGSU3Y . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Maleavour commented 1 year ago

ok thank you