Giter Club home page Giter Club logo

Comments (6)

TheDudeFromCI avatar TheDudeFromCI commented on September 17, 2024 1

This is because you forgot to check the target in the event.

bot.on('entityGone', (entity) => {
  if (entity !== target) return // <======== This Line ==========
  attackNearestPlayer()
})

Without that line, the bot will select a new target any time any entity despawns, dies, teleports, gets unloaded, moves out of render distance, etc. Which is quite often.

Also, I recommend you move that target variable to a global scope so it can be read by both functions.

Lastly, don't call bot.on inside a command, since this creates a new listener each time, but does not remove the existing ones. You'll create a memory leak and also have the same code running multiple times in parallel which can cause unexpected bugs later on. It's best to create that method right after creating the bot. With the if (entity !== target) return line, this means that if not target is assigned, it will never be called. If a target is assigned, it will only be called when that target specifically disappears.

from mineflayer-pathfinder.

TheDudeFromCI avatar TheDudeFromCI commented on September 17, 2024

You can check for an entityGone event on the bot, then call to select a new nearby player.

bot.on('entityGone', (entity) => {
  if (entity !== target) return
  // Entity has either died, disconnected, or teleported. Select a new target.
  attackNearestPlayer()
})

from mineflayer-pathfinder.

shketov avatar shketov commented on September 17, 2024

You can check for an entityGone event on the bot, then call to select a new nearby player.

bot.on('entityGone', (entity) => {
  if (entity !== target) return
  // Entity has either died, disconnected, or teleported. Select a new target.
  attackNearestPlayer()
})

Either I am using this incorrectly, or it is not what I want.

It should be:

  1. Bot is looking for the nearest player
  2. The bot is chasing him
  3. The bot attacks him
  4. Player exits
  5. The bot is looking for a new nearest player
  6. Repeat step # 1

What happens without your decision:

  1. Bot is looking for the nearest player
  2. The bot is chasing him
  3. The bot attacks him
  4. Player exits
  5. The bot does nothing

What happens to your decision:

  1. The bot is looking for the nearest player
  2. The bot is chasing him
  3. The bot attacks him
  4. Player exits
  5. The bot finds a new player, but does not pursue
    And also with this solution, the bot does not remember a new goal, but is constantly looking for a new one.
var target
target = minecraft.nearestEntity(({ type }) => type === 'player')
console.log(target.username)
minecraft.on('entityGone', (entity) => {
  if (entity !== target) return
        target = minecraft.nearestEntity(({ type }) => type === 'player')
        console.log(target.username)
})
        minecraft.pathfinder.setMovements(defaultMove)
        minecraft.pathfinder.setGoal(new GoalFollow(target, 1), true)

from mineflayer-pathfinder.

TheDudeFromCI avatar TheDudeFromCI commented on September 17, 2024

In my example, the attackNearestPlayer() function also creates a new GoalFollow for the new target. Essentially all of the code for finding a player and beginning the attack loop was wrapped in that function.

from mineflayer-pathfinder.

shketov avatar shketov commented on September 17, 2024

In my example, the attackNearestPlayer() function also creates a new GoalFollow for the new target. Essentially all of the code for finding a player and beginning the attack loop was wrapped in that function.

if(message == ".farm"){
    attackNearestPlayer()
    minecraft.on('entityGone', (entity) => {
        attackNearestPlayer()
    }) 
}

function attackNearestPlayer(){
    if(Object.keys(minecraft.players).length == 1){
        console.log("There are no players")
        setTimeout(attackNearestPlayer,200)
        return;
    }
    let target = minecraft.nearestEntity(({ type }) => type === 'player')
    console.log(target.username + " is a new target")
    minecraft.pathfinder.setMovements(defaultMove)
    minecraft.pathfinder.setGoal(new GoalFollow(target, 1), true)
} 

The first time you enter .farm, the bot chases the player, so it remembers only him. If the player exits, the bot chooses a new victim, but does not remember only it. That is, if the bot has chosen a new player X, and player Y is next to player X, it will be closer than player X, then it will forget player X and choose player Y as a victim, but the problem is that player X does not leave server, does not die or teleport

UPD:
No, the bot will switch to a new player anyway if it is closer

from mineflayer-pathfinder.

shketov avatar shketov commented on September 17, 2024

This is because you forgot to check the target in the event.

bot.on('entityGone', (entity) => {
  if (entity !== target) return // <======== This Line ==========
  attackNearestPlayer()
})

Without that line, the bot will select a new target any time any entity despawns, dies, teleports, gets unloaded, moves out of render distance, etc. Which is quite often.

Also, I recommend you move that target variable to a global scope so it can be read by both functions.

Lastly, don't call bot.on inside a command, since this creates a new listener each time, but does not remove the existing ones. You'll create a memory leak and also have the same code running multiple times in parallel which can cause unexpected bugs later on. It's best to create that method right after creating the bot. With the if (entity !== target) return line, this means that if not target is assigned, it will never be called. If a target is assigned, it will only be called when that target specifically disappears.

Oh, my bad. Thank you so much, everything is working as it should!

from mineflayer-pathfinder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.