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

TypeError: Cannot read properties of undefined (reading 'setBlockEntity') #3096

Open CordasMC opened 1 year ago

CordasMC commented 1 year ago

Versions

Detailed description of a problem

Im building a bot that connects to a server, sneaks and reconnects every 31 seconds until it menaged to do so when the kick or end event is called. On running this, before the spawn event is even called( I think this is an important piece to solving the puzzle) I get the error: "TypeError: Cannot read properties of undefined (reading 'setBlockEntity') at Client. (/root/node_modules/mineflayer/lib/plugins/blocks.js:417:14) at Client.emit (node:events:512:28) at emitPacket (/root/node_modules/minecraft-protocol/src/client.js:83:12) at FullPacketParser. (/root/node_modules/minecraft-protocol/src/client.js:106:9) at FullPacketParser.emit (node:events:512:28) at addChunk (/root/node_modules/protodef/node_modules/readable-stream/lib/_stream_readable.js:279:12) at readableAddChunk (/root/node_modules/protodef/node_modules/readable-stream/lib/_stream_readable.js:262:11) at Readable.push (/root/node_modules/protodef/node_modules/readable-stream/lib/_stream_readable.js:228:10) at Transform.push (/root/node_modules/protodef/node_modules/readable-stream/lib/_stream_transform.js:132:32) at FullPacketParser._transform (/root/node_modules/protodef/src/serializer.js:89:10) "

What did you try yet?

I have:

  1. Added a timeout of 5 seconds after the spawn event occurs to ensure that the object is not undefined because something is just taking a while to load.
  2. I have done research on this error but have not found any solved issues for it.
  3. Added code to handle the error which works, but is just not ideal.

Your current code


/*
const mineflayer = require("mineflayer");

const options = {
    host: "serverip",
    version: "1.19.4",
    auth: "microsoft",
    username: "mymail",
    password: "mypassword"
};

let bot = initializeBot(options);

process.on('uncaughtException', (err) => {
    console.error('There was an uncaught error', err);
    //for now, Im handling the error like this.
});

function initializeBot(settings) {
    let returnBot = mineflayer.createBot(settings);

    returnBot.on("spawn", () => {
    returnBot.once("chunkColumnLoad", () => {
        setTimeout(() => {
            console.log("Minecraft Bot logged in");
            console.log("Minecraft Bot joined servername");
            bot.setControlState('sneak', true);
        }, 5000);  
    });
});

    returnBot.on("end", (reason) => {
        console.log("ended for: " + reason);
        bot.quit('Reconnecting'); 
        setTimeout(reconnect, 31000);
    });

    returnBot.on("kick", (reason) => {
        console.log("kicked for: " + reason);
        bot.quit('Reconnecting'); 
        setTimeout(reconnect, 31000);
    });

    return returnBot;
}

function reconnect() {
    bot = initializeBot(options);
    bot.on('error', err => {
        console.log(`Error: ${err.message}`);
        console.log('Reconnect failed, retrying...');
        setTimeout(reconnect, 31000);
    });

*/

Expected behavior

Once the spawn event is called, wait 5 seconds and start sneaking. Once the end or kick event is called, try to reconnect every 31 seconds until the reconnect was succesful.

Additional context

none

LestAndrea commented 1 year ago

how did you solve it?

CordasMC commented 1 year ago

how did you solve it?

I didn't

LestAndrea commented 1 year ago

I had fixed it a while ago, I modified a file in the npm manually. if you are interested I am looking for my old bot and I will send you the file.

extremeheat commented 1 year ago

Can anyone confirm if this is still a problem after running npm update --depth 9999?