-
Basically im trying to constantly follow a player and update thier position. My code looks something like this: const mineflayer = require("mineflayer");
const pathfinder = require('mineflayer-pathfinder').pathfinder;
const Movements = require('mineflayer-pathfinder').Movements;
const { GoalNear } = require('mineflayer-pathfinder').goals
if (process.argv.length != 5) {
throw new Error("Usage: node main.js <username> <address> <port>")
}
const options = {
username: process.argv[2],
host: process.argv[3],
port: parseInt(process.argv[4])
};
const bot = mineflayer.createBot(options);
let following = false;
bot.loadPlugin(pathfinder)
bindEvents(bot);
function bindEvents(bot){
bot.once("login", () => {
console.log(`${process.argv[2]} joined the server!`);
});
bot.on("chat", async (author, message) => {
if (author == bot.username) { return };
if (message.startsWith("follow")) {
following = true
while (following) {
goToPlayer(bot, message.split(" ")[1])
await sleep(500)
}
}
if (message == "stop") {
following = false
}
});
bot.once("end", (reason) => {
console.log(`${process.argv[2]} ended! End reason: ${reason.reason}`);
});
}
const sleep = ms => new Promise(r => setTimeout(r, ms));
getCoords = (bot, name) => {
const player = bot.players[name] ? bot.players[name].entity : null;
if (player) {
return player.position;
} else {
return null;
}
};
goToPlayer = (bot, name) => {
coords = getCoords(bot, name)
if (coords) {
const mcData = require('minecraft-data')(bot.version);
const defaultMove = new Movements(bot, mcData);
bot.pathfinder.setMovements(defaultMove);
bot.pathfinder.setGoal(new GoalNear(coords.x, coords.y, coords.z, 1));
}
} It's updating the position every 500ms but trying to move to it makes the bot stop any actions - like sprinting, mining a block. I would like to follow the player but not stop anything. Im probably going to need to rewrite my logic but I can handle it. |
Beta Was this translation helpful? Give feedback.
Answered by
Sunderw3k
Apr 20, 2022
Replies: 1 comment 3 replies
-
Oh so i realised I can just use GoalFollow instead of GoalNear. Let me know if thats correct? |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Sunderw3k
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh so i realised I can just use GoalFollow instead of GoalNear. Let me know if thats correct?