Skip to content

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

Open
@liammcmillen

Description

@liammcmillen
  • 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;
      }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Stage1just created by someone new to the project, we don't know yet if it deserves an implementation / a fpossible bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions