PrismarineJS / mineflayer

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

TypeError: Cannot read properties of null (reading 'version') #3325

Closed bulieme closed 6 months ago

bulieme commented 6 months ago

idk what just happened, im just adding a configuration.json feature. anyways here's my code without the config.json


const mineflayer = require('mineflayer');
const { pathfinder, Movements, goals: { GoalNear } } = require('mineflayer-pathfinder');
var Vec3 = require('vec3').Vec3;
const jsonfile = require('jsonfile')

const bot = mineflayer.createBot({
    "host": "ruby.magmanode.com",
    "username": "boty",
    "auth": "offline",
    "port": 31963,
    "version": "1.20.4"
})
bot.loadPlugin(pathfinder)

function walk2goal(x,y,z, range) {
    const mcdata = require("minecraft-data")(bot.version);
    const mvs = new Movements(bot, mcdata);
    mvs.allow1by1towers = false // Do not build 1x1 towers when going up
    //mvs.canDig = false // Disable breaking of blocks when pathing 
    bot.pathfinder.setMovements(mvs);
    bot.lookAt(new Vec3(x, y, z))
    bot.pathfinder.setGoal(new GoalNear(x, y, z, range), false);
}

function between(min, max) {  
  return Math.floor(
    Math.random() * (max - min) + min
  )
}

bot.on("login", () => {
    console.log("Logged in!");

    bot.on("spawn", () => {
        console.log(`Spawned in ${bot.game.gameMode}.`);
    })
})

var whileWalk = false;
var isReached = true; // Because initially our destination is reached
var rwalkPos = new Vec3(0,0,0); // 0,0,0 pos. its because the bot digs everything to enter destination

bot.on("physicsTick", () => {
    if (whileWalk && isReached) {
        isReached = false; // because our destination will be different
        var offsX = between(-3,3);
        var offsZ = between(-3,3);
        console.log(new Vec3(rwalkPos.x+offsX, rwalkPos.y, rwalkPos.z+offsZ))
        walk2goal(rwalkPos.x+offsX, rwalkPos.y, rwalkPos.z+offsZ, 0);
    }
})

bot.on("goal_reached", (goal) => {
    console.log(`pathfinding to [${goal.x}, ${goal.y}, ${goal.z}] is done!!`);
    isReached = true;
    if (whileWalk){
        bot.waitForTicks(100)
    }
})

bot.on("path_stop", () => {
    isReached = true;
    console.log("Stopped!")
})

bot.on('whisper', (username, message) => {
    if (username === bot.username) return;
    if (username === botOwner) {
        if (message === "walk2me") {
            const ownerEntity = bot.players[botOwner]?.entity;
            const { x: posX, y: posY, z: posZ } = ownerEntity.position;
            walk2goal(posX, posY, posZ, 2);
        } else if (message === "walkRandomly") {
            if (whileWalk && !isReached) {whileWalk=false; isReached = true};
            var myself = bot.entity;
            rwalkPos = myself.position; // starting pos
            whileWalk = true;
        } else if (message === "stop") {
            bot.pathfinder.stop();
            if (whileWalk && !isReached) {
                whileWalk = false;
                isReached = true;
                walk2goal(rwalkPos.x, rwalkPos.y, rwalkPos.z, 0);
            };
        }
    }
})

bot.on("message", (jsonMsg, position, sender, verified) => {
    let msg = jsonMsg.toString()
    console.log(`[${position}] ${msg}`)
})

// Log errors and kick reasons:
bot.on('kicked', console.log)
bot.on('error', console.log)

the error:

\botmc\node_modules\mineflayer\lib\loader.js:108
    const version = require('minecraft-data')(bot._client.version).version
                                                                  ^

TypeError: Cannot read properties of null (reading 'version')
    at next (C:\Users\MY\Desktop\Rafie\botmc\node_modules\mineflayer\lib\loader.js:108:67)
    at Object.createBot (C:\Users\MY\Desktop\Rafie\botmc\node_modules\mineflayer\lib\loader.js:105:34)
    at Object.<anonymous> (C:\Users\MY\Desktop\Rafie\botmc\index.js:6:24)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47
bulieme commented 6 months ago

Just setting the version to false is emoughe lmao >.<

bulieme commented 6 months ago

nvm what is this image

wgaylord commented 6 months ago

Remove the " from around the names of the arguments on the createBot.

See the example in the readme of the repo.

bastianmarin commented 6 months ago

same problem, it seems that it happened when updating to the latest version of mineflayer UPDATE: The problem is older version (For some reason it installs everything and I download the version that was the problem, update to the most recent one and this problem will be fixed (problem with 4.0.0))

extremeheat commented 6 months ago

I believe this was fixed recently in a PR, so you can update with npm update

extremeheat commented 6 months ago

https://github.com/PrismarineJS/node-minecraft-protocol/pull/1291

rom1504 commented 6 months ago

fixed now