Comments (6)
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.
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.
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:
- Bot is looking for the nearest player
- The bot is chasing him
- The bot attacks him
- Player exits
- The bot is looking for a new nearest player
- Repeat step # 1
What happens without your decision:
- Bot is looking for the nearest player
- The bot is chasing him
- The bot attacks him
- Player exits
- The bot does nothing
What happens to your decision:
- The bot is looking for the nearest player
- The bot is chasing him
- The bot attacks him
- Player exits
- 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.
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.
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.
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 theif (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)
- The bot does not enter the server (fabric)
- Bot can't go through doors HOT 2
- Bot never goes under layer 0 HOT 2
- Not working with Mineflayer 4.5.1? HOT 8
- Saying the the mcdata script in the Movements script is undefinded in the code this.blocksCantBreak.add(mcData.blocksByName.chest.id) HOT 3
- Typo HOT 8
- Bot tries to open fence gate that is already open HOT 4
- Bot thinks it can travel diagonally when blocked by other blocks HOT 4
- Bot trying to open the chest?¿? when he's walking on the chest HOT 3
- Bot goes to unexpected ways HOT 1
- (python) TypeError: Cannot read properties of null (reading 'blocksByName') HOT 2
- Stuck in dripstone HOT 1
- The code that is completely identical to the example that used to work normally does not work. HOT 8
- Bot Constantly Stuck or Halts with GoalFollow - HOT 2
- Failed to find path including bottom slabs
- GoalPlaceBlock ask HOT 1
- [Feature request] Creative/Flight navigation HOT 1
- goal_reached doesnt activate at all HOT 1
- VulcanAC vs mineflayer pathfinding HOT 1
- Bot fails to follow simple path requiring a forward walk + jump HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mineflayer-pathfinder.