PrismarineJS / mineflayer

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

I Cant Run My Bot #2088

Closed ghost closed 3 years ago

ghost commented 3 years ago

it says events.js:292 throw er; // Unhandled 'error' event ^

Error: connect ECONNREFUSED 127.0.0.1:25565 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) Emitted 'error' event at: at Client. (C:\Users\hi\Desktop\Bots\PVP_BOT\node_modules\mineflayer\lib\loader.js:98:9) at Client.emit (events.js:315:20) at C:\Users\hi\Desktop\Bots\PVP_BOT\node_modules\minecraft-protocol\src\client\autoVersion.js:13:30 at Client. (C:\Users\hi\Desktop\Bots\PVP_BOT\node_modules\minecraft-protocol\src\ping.js:24:5) at Client.emit (events.js:315:20) at Socket.onFatalError (C:\Users\hi\Desktop\Bots\PVP_BOT\node_modules\minecraft-protocol\src\client.js:149:12) at Socket.emit (events.js:327:22) at emitErrorNT (internal/streams/destroy.js:106:8) at emitErrorCloseNT (internal/streams/destroy.js:74:3) at processTicksAndRejections (internal/process/task_queues.js:80:21) { errno: -4078, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 25565 }

imharvol commented 3 years ago

Show your code. Though ECONNREFUSED errors are usually due to wrong port, host or credentials

ghost commented 3 years ago

Ok

ghost commented 3 years ago
const mineflayer = require('mineflayer')
const pvp = require('mineflayer-pvp').plugin
const ( pathfinder, Movements, goals ) = require('mineflayer-pathfinder')
const armorManager = require('mineflayer-armor-manager')

const bot = mineflayer.createBot({
    host: '127.0.0.1',
    port: 25565,
    username: 'Source_',
    version: '1.12.2',
    logErrors: false
})

bot.loadPlugin(pvp)
bot.loadPlugin(armorManager)
bot.loadPlugin(pathfinder)

bot.on('playerCollect', (collector, itemDrop) => {
  if (collector !== bot.entity) return

  setTimeout(() => {
    const sword = bot.inventory.items().find(item => item.name.includes('sword'))
    if (sword) bot.equip(sword, 'hand')
  }, 150)
})

bot.on('playerCollect', (collector, itemDrop) => {
  if (collector !== bot.entity) return

  setTimeout(() => {
    const axe = bot.inventory.items().find(item => item.name.includes('sword'))
    if (axe) bot.equip(axe, 'hand')
  }, 150)
})

bot.on('playerCollect', (collector, itemDrop) => {
  if (collector !== bot.entity) return

  setTimeout(() => {
    const shield = bot.inventory.items().find(item => item.name.includes('shield'))
    if (shield) bot.equip(shield, 'off-hand')
  }, 250)
})

let guardPos = null

function guardArea (pos) {
  guardPos = pos.clone()

  if (!bot.pvp.target) {
    moveToGuardPos()
  }
}

function stopGuarding () {
  guardPos = null
  bot.pvp.stop()
  bot.pathfinder.setGoal(null)
}

function moveToGuardPos () {
  const mcData = require('minecraft-data')(bot.version)
  bot.pathfinder.setMovements(new Movements(bot, mcData))
  bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z))
}

bot.on('stoppedAttacking', () => {
  if (guardPos) {
    moveToGuardPos()
  }
})

bot.on('physicTick', () => {
  if (bot.pvp.target) return
  if (bot.pathfinder.isMoving()) return

  const entity = bot.nearestEntity()
  if (entity) bot.lookAt(entity.position.offset(0, entity.height, 0))
})

bot.on('physicTick', () => {
  if (!guardPos) return

  const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 16 &&
                      e.mobType !== 'Armor Stand' // Mojang classifies armor stands as mobs for some reason?

  const entity = bot.nearestEntity(filter)
  if (entity) {
    bot.pvp.attack(entity)
  }
})

bot.on('chat', (username, message) => {
  if (message === '!guard') {
    const player = bot.players[username]

    if (!player) {
      bot.chat("I can't see you.")
      return
    }

    bot.chat('I will guard that location.')
    guardArea(player.entity.position)
  }

  if (message === '!fight-me') {
    const player = bot.players[username]

    if (!player) {
      bot.chat("I can't see you.")
      return
    }

    bot.chat('Prepare to fight!')
    bot.pvp.attack(player.entity)
  }

  if (message === '!stop') {
    bot.chat('I will no longer guard this area.')
    stopGuarding()
  }
})
u9g commented 3 years ago

it means you don't have a minecraft server on 127.0.0.1:25565 , this isn't an issue though, open a discussion if you have a question