PrismarineJS / mineflayer

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

I think this is a bug, or is there a function to prevent this? #1306

Closed amoraschi closed 4 years ago

amoraschi commented 4 years ago

Versions

Detailed description of a problem

When the bot joins, it thinks that the NPC of the Citizens2 for 1.16.1 are actual players, is there a way to prevent this? So it doesn't spam the console?

Your current code


const mineflayer = require('mineflayer')
const notifier = require('node-notifier');
const { link } = require('fs');

if (process.argv.length < 4 || process.argv.length > 6) {
  bot.dashboard.log('Usage : AFK.js <host> <port> [<name, gmail in case the account is premium>] [<password, ignore in case the account is cracked>]')
  process.exit(1)
}

const bot = mineflayer.createBot({
  host: process.argv[2],
  port: parseInt(process.argv[3]),
  username: process.argv[4] ? process.argv[4] : 'AFKBot',
  password: process.argv[5]
})

//LOOKING
bot.once('login', function () {
  setInterval(() => {
      const entity = bot.nearestEntity()
      if (entity !== null) {
        if (entity.type === 'player') {
        bot.lookAt(entity.position.offset(0, 1.6, 0))
        } else if (entity.type === 'mob') {
          bot.lookAt(entity.position)
        }
      }
  }, 2000)
})

//CHAT
function startChat() {
  bot.once('chat', function (username, message) {
    if (username === bot.username) return
    setTimeout(function() {
    bot.chat('I am an AFK bot')
    bot.dashboard.log('\x1b[32m<STATUS> Correctly replied that I am a bot','\x1b[0m')
  }, 1000)})

bot.on('chat', (username, message) => {
  if (username === bot.username) return
    if (username === 'you') return
      bot.dashboard.log(`<CHAT>`,username,'\x1b[31m>\x1b[0m',message)
        notifier.notify({
          title: 'Chat Message',
          message: ('<' + username + '>' + ' '+ message),
          icon: 'logo.png',
        });
})}

bot.on('whisper', (username, message, rawMessage) => {
  bot.dashboard.log(`<CHAT> <WHISPERS TO YOU>`,username,'\x1b[34m>\x1b[0m', message)
  notifier.notify({
          title: 'Whisper Message',
          message: ('<' + username + '>' + ' '+ message),
          icon: 'logo.png',
  })
})

bot.on('spawn', function () {
  setTimeout(() => {
  startChat()
  }, 2000);
})

//KICK & ERRORS
bot.on('kicked', (reason, loggedIn) => bot.dashboard.log('\x1b[32m<STATUS>\x1b[0m \x1b[31mI got kicked for','\x1b[0m',reason, loggedIn))

//EVENTS
bot.on('death', () => {
  bot.dashboard.log('\x1b[32m<STATUS>\x1b[0m \x1b[31mI died','\x1b[0m')
})

bot.once('spawn', () => {
  bot.dashboard.log('\x1b[32m<STATUS> Correctly spawned','\x1b[0m')
})

bot.on('respawn', () => {
  bot.dashboard.log('\x1b[32m<STATUS> Correctly respawned','\x1b[0m')
})

bot.on('login', () => {
  bot.dashboard.log('\x1b[32m<STATUS> Correctly logged in','\x1b[0m')
})

bot.once('health', () => {
  bot.dashboard.log(`\x1b[32m<STATUS> I have ${bot.health} health.`,'\x1b[0m')
})

bot.on('health', () => {
  if (bot.health <= 5)
    bot.dashboard.log(`\x1b[32m<STATUS> \x1b[33mMy remaining health is ${bot.health}`,'\x1b[0m')
})

//WORLD
bot.once('time', () => {
  setTimeout(function() {
  bot.dashboard.log(`\x1b[36m<WORLD> \x1b[36mCurrent time: `+bot.time.timeOfDay,'\x1b[0m')
}, 1000)})

bot.on('entityHurt', (entity) => {
  if (entity.type === 'player') {
    if (entity.username !== bot.username) 
      bot.dashboard.log(`\x1b[36m<WORLD>\x1b[0m \x1b[33m${entity.username} got hurt, beware`,`\x1b[0m`)
    else
      bot.dashboard.log(`\x1b[32m<STATUS>\x1b[0m \x1b[33mI've got hurt, beware`,`\x1b[0m`)
  }
})

bot.on('playerJoined', (player) => {
  setTimeout(function() {
  if (player.username !== bot.username) {
    bot.dashboard.log(`\x1b[36m<WORLD>\x1b[0m \x1b[32m${player.username} joined`,`\x1b[0m`)
  }
}, 3000)})

bot.on('playerLeft', (player) => {
  if (player.username === bot.username) return
  bot.dashboard.log(`\x1b[36m<WORLD>\x1b[0m \x1b[32m${player.username} left`,`\x1b[0m`)
})

bot.loadPlugin(require('mineflayer-dashboard'))

Expected behavior

Bot ignores NPC's of other plugins.

wvffle commented 4 years ago

Citizens 2 creates fake players and names them as NPCs. Thus mineflayer thinks they are actual players. I don't think there is a way to differentiate them. You can check all of the fields of one NPC, and of one player character, compare them and find some field(s) that will let you mark player as NPC in your code and then ignore it.

So it doesn't spam the console? Is it due to playerJoined and playerLeft? Or some other event that you're logging?

amoraschi commented 4 years ago

Oh, yeah it spams the console. It spams:

[npcName] has joined [npcName] has left

It's like as the npc's where leaving and joining

marquepage commented 4 years ago

What is possible to do is to get their UUID, and check them against mojang API, if they don't exist, ignore them

amoraschi commented 4 years ago

Could you make/write an example? I think I'm not pro enough to do that

marquepage commented 4 years ago

Use this

There is an example in the third link

amoraschi commented 4 years ago

Thanks!