A mineflayer task queue manager. It's promise based, but you can also use non async functions in the queue.
npm install mineflayer-task-managerjs
const createBot = require("mineflayer").createBot;
const taskManager = require("mineflayer-task-manager").taskManager;
const bot = createBot({
username: "Steve",
host: "localhost",
port: 25565
})
bot.loadPlugin(taskManager)
bot.once("spawn", () => {
// Creates tasks
for (let i = 0; i < 10; i++) {
bot.taskManager.Add("Spin", spin, 500);
}
// Writes the tasks to the console
console.log(bot.taskManager.GetWholeQueue().map(e => e.name).join(", "));
})
// Rotate by 45 degrees or π/4 radians.
function spin(b) {
b.entity.yaw += Math.PI / 4;
}
`
Here's the same example but using the InsertQueue function: (javascript)
`js
const createBot = require("mineflayer").createBot;
const taskManager = require("mineflayer-task-manager").taskManager;
const bot = createBot({
username: "Steve",
host: "localhost",
port: 25565
})
bot.loadPlugin(taskManager)
bot.once("spawn", () => {
var actions = [];
var delays = [];
for (let i = 0; i < 10; i++) {
actions.push(spin);
delays.push(500);
}
bot.taskManager.InsertQueue("Spin", actions, false, delays);
console.log(bot.taskManager.GetWholeQueue().map(e => e.name + " " + e.delay).join(", "));
})
function spin(b) {
b.entity.yaw += Math.PI / 4;
}
`
Hello example: (typescript)
`ts
import { createBot } from "mineflayer";
import { taskManager } from "mineflayer-task-manager";
const bot = createBot({
username: "Steve",
host: "localhost",
port: 25565
})
bot.loadPlugin(taskManager)
bot.once("spawn", () => {
// Bot will say "Hello 2" one second after it spawned because of Hello 1 executing after 1000 ms,
// since it was inserted to the start after this was added.
bot.taskManager.Add("Hello 2", (b) => b.chat("Hello 2"), 0)
// Bot will execute this second
bot.taskManager.Insert("Hello 1", (b) => b.chat("Hello 1"), 1000)
// Bot will execute this first
bot.taskManager.Insert("Hello 0", (b) => b.chat("Hello 0"))
bot.taskManager.Add("look", async (b) => await b.lookAt(b.entity.position.offset(0, 0, 1)), 0)
})
`
Looks at the nearest entity, then says hello: (typescript)
`ts
import { createBot } from "mineflayer";
import { taskManager } from "mineflayer-task-manager";
const bot = createBot({
username: "Steve",
host: "localhost",
port: 25565
})
bot.loadPlugin(taskManager)
bot.once("spawn", () => {
bot.taskManager.Add("look", async (b) => {
var entity = b.nearestEntity();
if (entity == null)
b.chat("No entity");
else
await b.lookAt(entity.position.offset(0, entity.height, 0))
}, 0)
bot.taskManager.Add("hello", (b) => b.chat("Hello"))
})
``