Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot doesn't reconnect to the server #3372

Closed
1 task done
Skwrr opened this issue May 6, 2024 · 1 comment
Closed
1 task done

Bot doesn't reconnect to the server #3372

Skwrr opened this issue May 6, 2024 · 1 comment
Labels
possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f

Comments

@Skwrr
Copy link

Skwrr commented May 6, 2024

  • The FAQ doesn't contain a resolution to my issue

Versions

  • mineflayer: 4.20.1
  • server: folia 1.19.4
  • node: 18.17.0

Detailed description of a problem

Hello! I made a minecraft bot linked with a discord bot to send blocks to players. Everything works fine, but when the bot leaves the server for whatever reason (server restart, kicked for AFK, etc) and the bot tries to reconnect, the CPU reaches 100% and the bot stays in the lobby without walking to the portal that teleports you to the server.

What did you try yet?

Adding logging when bot tries to walk (I get no output).

Your current code

I have a very long code, full code can be found here
Minimal code to reproduce the bug:

const mineflayer = require("mineflayer")
const pathfinder = require("mineflayer-pathfinder").pathfinder;
const fs = require("fs");

function runBot(){
    let bot = createBot();
    bot.queue = []

    registerModules();

    bot.isEnding = false;

    function createBot(){
        return mineflayer.createBot({
            host: "6b6t.org",
            port: 25565,
            version: "1.18.4",
            username: "MartuDelivery"
        })
    }

    function registerModules(){
        const MODULES_DIRECTORY = __dirname+"/modules";
        const modules = fs
        .readdirSync(MODULES_DIRECTORY)
        .filter((x) => x.endsWith(".js"))
        .map((pluginName) => {
            bot.loadPlugin(require(MODULES_DIRECTORY+"/"+pluginName))
            console.log(`Loaded \x1b[32m${pluginName}\x1b[0m`);
        });

        bot.loadPlugin(pathfinder)
    }

    bot.on("end", () => {
        console.log("\x1b[31mThe bot has ended. Reconnecting... \x1b[0m");
        bot.isEnding = true;
        bot.removeAllListeners();
        bot = null
        setTimeout(runBot, 60000);
    });
    
    bot.on("error", (err) => {
        console.log(err);
    });
        
    bot.on("kicked", (err) => {
        console.log(err);
        bot.end();
    });

    return bot;
}

runBot();

Expected behavior

Bot should timeout 60s and then reconnect to the server, login with password and walk straight forward towards a nether portal to teleport to the actual server.

Additional context

I think it's similar to #1519

@Skwrr Skwrr added possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f labels May 6, 2024
@extremeheat
Copy link
Member

Your link is dead. But Via Gemini over your current code, this seems like this could be due to some pathfinder loop running in background:

There's nothing glaringly wrong with the code at first glance, but a few things could be contributing to the issue:

  1. Pathfinding Loop: The bot is supposed to walk to a Nether portal. If the pathfinding algorithm encounters an issue (e.g., the portal is obstructed or the path is too complex), it could get stuck in an infinite loop, causing high CPU usage.

  2. Resource Leak: The code doesn't explicitly clear the bot's pathfinder goals when reconnecting. If the bot keeps adding new goals without clearing old ones, it could lead to memory leaks and increased CPU load over time.

Try removing pathfinder to see if you can repro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f
Projects
None yet
Development

No branches or pull requests

2 participants