Skip to content

bot.players not returning all connected players on paperMC servers #3608

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

Open
1 task done
liammcmillen opened this issue Mar 16, 2025 · 0 comments
Open
1 task done
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

@liammcmillen
Copy link

liammcmillen commented Mar 16, 2025

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

Versions

  • mineflayer: 4.26.0
  • server: paper 1.21.4
  • node: 22.14.0

Detailed description of a problem

My bot connects to a server with 5 people online. When I log the result of bot.players the only player that it finds is the bot itself. It will only find another player if they connected AFTER the bot did. So to show up to the bot, you must reconnect first.

What did you try yet?

I tried instead using bot.entities and filtering out everything except players like this:

const playerlist = Object.values(bot.entities).filter(entity => entity.type === 'player');
    playerlist.forEach(player => {
      console.log(`Player: ${player.username}, Position: ${JSON.stringify(player.position)}`);
    });

But it gives me the same result, it only lists one player: the bot. I made sure there was at least one other person in the vicinity of the bot but still they never show up unless they reconnect. I am not sure what else to try other than to verify that it works on a server running spigot, which indeed it does work.

Your current code

function createBot() {
    const bot = mineflayer.createBot({
    host: '<serverip>',
    port: 25565,
    username: 'cropbot',
    auth: 'microsoft'
    });

    bot.on('spawn', async () => {
        console.log("[SPAWN] Bot spawned with version:", bot.version);
        bot.loadPlugin(pathfinder);
        bot.loadPlugin(pvp)
        let playerlist = Object.keys(bot.players);
        console.log(playerlist);
    });
}

createBot();

Expected behavior

I expect to see a list of the connected players like so:
Player1,Player2,Player3,etc.
When I connect to a server running spigot that's what I get, however on the server I play on, which is running paper, the only player I see in the output is the bot unless another player connects/reconnects

Workaround

I managed to find a workaround. bot.entities was still coming up with players' uuids, so I just resolved the player names through the mojang api and then updated the playerlist.

async function getUsernameFromUuid(uuid) {
  if (!uuid || typeof uuid !== 'string' || uuid.trim().length === 0) {
    console.error("Invalid uuid provided:", uuid);
    return null;
  }
  const url = `https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`;
  try {
    const response = await fetch(url);
    if (response.status === 404) {
      console.error(`UUID ${uuid} not found in Mojang API.`);
      return null;
    }
    if (!response.ok) {
      throw new Error(`Error fetching data: ${response.statusText}`);
    }
    const nameHistory = await response.json();
    return nameHistory.name;
  } catch (error) {
    console.error("Failed to fetch username from UUID:", error);
    return null;
  }
}

And then:

    const playerlist = Object.values(bot.entities).filter(entity => entity.type === 'player');
    for (const player of playerlist) {
      if (!player.uuid) {
        continue;
      }
      const updatedUsername = await getUsernameFromUuid(player.uuid);
      if (updatedUsername) {
        player.username = updatedUsername;
      }
@liammcmillen liammcmillen 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 Mar 16, 2025
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

1 participant