PrismarineJS / mineflayer

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

Plugin "mineflayer-navigate" doesn't want to load #2978

Closed SonicandTailsCD closed 1 year ago

SonicandTailsCD 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. The mineflayer-navigate plugin doesn't want to load! What are you building? A script for the Mineflayer bot to follow me What problem are you trying to solve? Plugin doesn't want to load

What did you try yet?

Did you try any method from the API? No, I don't know how. Did you try any example? yes Any error from those? yes

Your current code


// Here I tell node.js mineflayer and a few other plugins are required to load
const mineflayer = require('mineflayer')
const mineflayerViewer = require('prismarine-viewer').mineflayer
const pathfinder = require('mineflayer-pathfinder').pathfinder
const pvp = require('mineflayer-pvp').plugin
const Movements = require('mineflayer-pathfinder').Movements
const goals = require('mineflayer-pathfinder').goals
const navigatePlugin = require('mineflayer-navigate').plugin

// Here, we set up the server connection (in this case, my skin plugin server)
const bot = mineflayer.createBot({
    host: 'SonicJavaBots.aternos.me',
    port: '37867',
    username: "SonicandTailsCDb",
    version: '1.18.2'
})

// Here the variables need to be set. It's important to load them first, otherwise there will be an error.
let attackCurrent = false
let guardPos = null

// Here, we reset the guard position.
bot.on('spawn', () => {
  stopGuarding()
  bot.chat('The guard position was reset. Sorry!')
})
// Now here comes the fun part: the coding. This is the AI, basically.
bot.loadPlugin(pathfinder)
bot.loadPlugin(pvp)
bot.loadPlugin(navigatePlugin)

// Assign the given location to be guarded
function guardArea (pos) {
  guardPos = pos
  let attackCurrent = false
  // We we are not currently in combat, move to the guard pos
  if (!bot.pvp.target) {
    moveToGuardPos()
  }
}

// Cancel all pathfinder and combat
function stopGuarding () {
  guardPos = null
  bot.pvp.stop()
  bot.pathfinder.setGoal(null)
}

// Pathfinder to the guard position
function moveToGuardPos () {
  if (!guardPos) return
  bot.pathfinder.setMovements(new Movements(bot))
  bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z))
}

// Called when the bot has killed it's target.
bot.on('stoppedAttacking', () => {
  if (guardPos) {
    moveToGuardPos()
  }
})

function lookAtNearestPlayer () {
  const playerFilter = (entity) => entity.type === 'player'
  const playerEntity = bot.nearestEntity(playerFilter)

  if (!playerEntity) return

  const pos = playerEntity.position.offset(0, playerEntity.height, 0)
  bot.lookAt(pos)
}

function onPhysicsTick () {
  if (!guardPos) return // Do nothing if bot is not guarding anything

  // Only look for mobs within 16 blocks
  const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 16 &&
                    e.mobType !== 'Armor Stand' && e.mobType !== 'Creeper'

  const entity = bot.nearestEntity(filter)
  if (entity) {
    // Start attacking
    bot.pvp.attack(entity)
  } else if (!bot.pvp.target) {
    // Move towards the nearest player if the bot is not attacking anything
    const playerFilter = (entity) => entity.type === 'player'
    const playerEntity = bot.nearestEntity(playerFilter)

    if (!playerEntity) return

    const pos = playerEntity.position
    bot.navigate.to(pos)
  }
}

bot.on('physicsTick', onPhysicsTick)

// Listen for player commands
bot.on('chat', (username, message) => {
  // Guard the location the player is standing
  if (message === 'guard') {
    const player = bot.players[username]
    let attackCurrent = false
    if (!player) {
      bot.chat("I can't see you.")
      return
    }

    bot.chat('I will guard that location.')
    guardArea(player.entity.position.offset(0, 0, -1))
  }

  // Stop guarding
  if (message === 'stop') {
    bot.chat('I will no longer guard this area.')
    stopGuarding()
    let attackCurrent = null
  }
})

Expected behavior

A clear and concise description of what you expected to happen. The bot would join, as soon as I say "guard", the bot follows me.

Additional context

The error was: "node:assert:399 throw err; ^

AssertionError [ERR_ASSERTION]: plugin needs to be a function at EventEmitter.loadPlugin (/Users/adm/Desktop/Bot/node_modules/mineflayer/lib/plugin_loader.js:16:12) at Object. (/Users/adm/Desktop/Bot/bot.js:30:5) at Module._compile (node:internal/modules/cjs/loader:1099:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:975:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==' }

Node.js v17.9.1" Also, the function onPhysicsTick was written by ChatGPT. That's all.

SonicandTailsCD commented 1 year ago

Also, please don't close this. I've never closed one, so I want to try out closing it.

amoraschi commented 1 year ago

mineflayer-navigate is deprecated iirc. Use mineflayer-pathfinder instead.

SonicandTailsCD commented 1 year ago

Ok! How do I do that?

amoraschi commented 1 year ago

Instead of taking code from sources like ChatGPT, try learning the basics of JS and understand each part of your code.

Sorry, but I don't think there's a better way than this.

SonicandTailsCD commented 1 year ago

I know the basics of JS.

SonicandTailsCD commented 1 year ago

But sometimes I need quick answers. Besides, the rest of the code, that was made by me! I only asked ChatGPT to write a function.

SonicandTailsCD commented 1 year ago

No offense, but can you read at all? I typed at the bottom of the issue "The function onPhysicsTick was made by ChatGPT."…

amoraschi commented 1 year ago

Sorry, the code is from examples/guard.js with some additional code, and I assumed you didn't know the basics because I read:

Did you try any method from the API? No, I don't know how.
Did you try any example? yes
Any error from those? yes

If you want to get started with mineflayer-pathfinder, this might be a good video (I haven't checked if the code still works tho): https://www.youtube.com/watch?v=UWGSf08wQSc&list=PLh_alXmxHmzGy3FKbo95AkPp5D8849PEV&index=2

SonicandTailsCD commented 1 year ago

Okay! I'll try. But one thing: maybe you can help correct my code?

SonicandTailsCD commented 1 year ago

Sorry, the code is from examples/guard.js with some additional code, and I assumed you didn't know the basics because I read:

Did you try any method from the API? No, I don't know how.
Did you try any example? yes
Any error from those? yes

If you want to get started with mineflayer-pathfinder, this might be a good video (I haven't checked if the code still works tho): https://www.youtube.com/watch?v=UWGSf08wQSc&list=PLh_alXmxHmzGy3FKbo95AkPp5D8849PEV&index=2

No problem, lah. I forgive you :)