PrismarineJS / mineflayer

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

TypeError: Cannot read properties of undefined (reading 'setMovements') #2997

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. Weird error that doesn't allow me to create a bot. That's all I know. What are you building? A pathfinding bot that follows me when I say "follow me" What problem are you trying to solve?

What did you try yet?

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

Your current code


// Here I tell node.js mineflayer and a few other plugins are required to load
const { versions } = require('minecraft-data')
const MinecraftData = require('minecraft-data')
const mineflayer = require('mineflayer')
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
const GoalFollow = goals.GoalFollow
const pvp = require('mineflayer-pvp').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'
})

// And now, we work on the AI here!
// Here's all of the functions:
// One note tho: All functions are placeholders.
function onPhysicsTick () {}

function onSpawn () {
    bot.chat("Hey! I'm working properly :D")
    bot.chat("/skin set SonicandTailsCDb robot1_stevetest")
}

function onSpawnMessage () {
    bot.on('chat', (daname, msgg)=>{
        const player = bot.players[daname]
        if (msgg == "follow me") {
            bot.chat("Okay " + daname)
        }
        if (!player || !player.entity){
            bot.chat("I can't see you, " + daname) 
            return
        }
        const data = require('minecraft-data')(bot.version)
        const movements = new Movements(bot, data)
        bot.pathfinder.setMovements(movements)

        const goal = new GoalFollow(player.entity, 2)
        bot.pathfinder.setGoal(goal, true)
    })
}

// I need to set physicsTick because I want the AI to target me.
bot.on('physicsTick', onPhysicsTick)

// Next, I'm gonna set spawn actions.
bot.once('spawn', onSpawn)
bot.on('spawn', onSpawnMessage)

Expected behavior

A clear and concise description of what you expected to happen. I expected the bot to join and follow me when I say "follow me"

Additional context

Add any other context about the problem here. NO. Thanks :)

SonicandTailsCD commented 1 year ago

The error was /Users/adm/Desktop/Bot/node_modules/minecraft-protocol/src/transforms/framing.js:69 throw e ^

TypeError: Cannot read properties of undefined (reading 'setMovements') at EventEmitter. (/Users/adm/Desktop/Bot/bot2.js:39:24) at EventEmitter.emit (node:events:539:35) at EventEmitter. (/Users/adm/Desktop/Bot/node_modules/mineflayer/lib/plugins/chat.js:85:13) at EventEmitter.emit (node:events:527:28) at Client. (/Users/adm/Desktop/Bot/node_modules/mineflayer/lib/plugins/chat.js:130:9) at Client.emit (node:events:527:28) at Client. (/Users/adm/Desktop/Bot/node_modules/minecraft-protocol/src/client/play.js:43:16) at Client.emit (node:events:527:28) at FullPacketParser. (/Users/adm/Desktop/Bot/node_modules/minecraft-protocol/src/client.js:91:12) at FullPacketParser.emit (node:events:527:28)

Node.js v17.9.1

ghost commented 1 year ago

You didn't load the pathfinder plugin. Add the code below under mineflayer.createBot():

bot.loadPlugin(pathfinder)
ghost commented 1 year ago

btw, you should just move bot.on('chat') inside onSpawn and remove bot.on('spawn', onSpawnMessage) and onSpawnMessage

SonicandTailsCD commented 1 year ago

btw, you should just move bot.on('chat') inside onSpawn and remove bot.on('spawn', onSpawnMessage) and onSpawnMessage

The thing is that a server plugin sends the spawn message to the client when its skin is loaded. That causes the bot to loop sending a command.

You didn't load the pathfinder plugin.

I'm gonna try to load the server now with the correction...

SonicandTailsCD commented 1 year ago

Also, wanna join the server? We try to fix it in real time? It isn't an issue if you don't agree.

SonicandTailsCD commented 1 year ago

LOL it said "I can't see you, SkinsRestorer". How do I fix this?

ghost commented 1 year ago

Also, wanna join the server? We try to fix it in real time?

yeah sure

SonicandTailsCD commented 1 year ago

Alright. You know the IP and port?

ghost commented 1 year ago

yup

SonicandTailsCD commented 1 year ago

// Here I tell node.js mineflayer and a few other plugins are required to load const { versions } = require('minecraft-data') const MinecraftData = require('minecraft-data') const mineflayer = require('mineflayer') const { pathfinder, Movements, goals } = require('mineflayer-pathfinder') const GoalFollow = goals.GoalFollow const pvp = require('mineflayer-pvp').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' })

// Loading! bot.loadPlugin(pathfinder)

// And now, we work on the AI here! // Here's all of the functions: // One note tho: All functions are placeholders. function onPhysicsTick () {}

function onSpawn () { bot.chat("Hey! I'm working properly :D") bot.chat("/skin set SonicandTailsCDb robot1_stevetest") }

function onSpawnMessage () { bot.on('chat', (daname, msgg)=>{ const player = bot.players[daname] if (msgg == "follow me") { bot.chat("Okay " + daname) } if (!player || !player.entity){ bot.chat("I can't see you, " + daname) return } const data = require('minecraft-data')(bot.version) const movements = new Movements(bot, data) bot.pathfinder.setMovements(movements)

    const goal = new GoalFollow(player.entity, 2)
    bot.pathfinder.setGoal(goal, true)
})

}

// I need to set physicsTick because I want the AI to target me. bot.on('physicsTick', onPhysicsTick)

// Next, I'm gonna set spawn actions. bot.once('spawn', onSpawn) bot.on('space', onSpawnMessage)

ghost commented 1 year ago
// Here I tell node.js mineflayer and a few other plugins are required to load
const { versions } = require('minecraft-data')
const mineflayer = require('mineflayer')
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
const GoalFollow = goals.GoalFollow
const pvp = require('mineflayer-pvp').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: "aaaa",
    version: '1.18.2'
})

const mcData = require('minecraft-data')(bot.version)

// Setup pathfinder
bot.loadPlugin(pathfinder)
bot.pathfinder.setMovements( new Movements(bot, mcData))

// And now, we work on the AI here!
// Here's all of the functions:
// One note tho: All functions are placeholders.
function onPhysicsTick () {}

function onSpawn () {
    bot.chat("Hey! I'm working properly :D")
    bot.chat("/skin set SonicandTailsCDb robot1_stevetest")

    bot.on('chat', (daname, msg)=>{
        const player = bot.players[daname]
        if (msg == "follow me") {
            if (!player?.entity){
                bot.chat("I can't see you, " + daname)
                return
            }

            bot.chat("Okay " + daname)

            const goal = new GoalFollow(player.entity, 2)
            bot.pathfinder.setGoal(goal, true)
            return
        }
    })
}

// I need to set physicsTick because I want the AI to target me.
bot.on('physicsTick', onPhysicsTick)

// Next, I'm gonna set spawn actions.
bot.once('spawn', onSpawn)
SonicandTailsCD commented 1 year ago

I'm ready! :D

SonicandTailsCD commented 1 year ago

This issue is complete, I don't have that specific issue anymore. Thanks to @678435021 for his help :)