feat: add mover plugin

General purpose mover / goto plugin.
Replaces the old solver based `comehere` in miner.js.
Instead, directly based on mineflayer bot apis and pathfinder.
This makes it simpler and easier to debug.
While less general, pathfinder is sophisticated enough for most cases.

For anything that needs moving from point A to point B.
Such as:
- following
- go to a location

Not in scope: locations and places. Would be a separate plugin.
This commit is contained in:
jay 2020-12-22 15:38:45 +05:00
parent 787d08dd31
commit b970231519

95
lib/plugins/mover.js Normal file
View File

@ -0,0 +1,95 @@
// import { EntityFilters } from "mineflayer-statemachine"
// import v from "vec3"
// import { Movements } from "mineflayer-pathfinder"
// const mineflayer = require('mineflayer')
const { Movements } = require('mineflayer-pathfinder')
// const { GoalBLah } = require('mineflayer-pathfinder').goals
const v = require('vec3')
let cfg = {}
let bot = {}
// let moving
let pathfinder
let movements = []
function initMoves(bot = bot, mcData = require('minecraft-data')(bot.version)) {
let defaultMove = new Movements(bot, mcData)
defaultMove.canDig = false
defaultMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id)
// defaultMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
// defaultMove.blocksToAvoid.add(mcData.blocksByName.magma.id)
movements.defaultMove = defaultMove
bot.pathfinder.setMovements(defaultMove)
}
function moveNear(pos, distance = 3) {
const { GoalNear } = require('mineflayer-pathfinder').goals
cfg.quiet || bot.chat(`moving to ${pos}`)
pos = v(pos)
bot.pathfinder.setMovements(movements.defaultMove)
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, distance))
}
function follow(entity, dynamic = true) {
console.assert(entity)
const { GoalFollow } = require('mineflayer-pathfinder').goals
cfg.quiet && console.log(entity)
|| bot.chat(
`following ${entity.type
}: ${entity.username || entity.displayName
}${dynamic ? "" : " once"}`
)
entity = entity.entity ? entity.entity : entity
// console.log(entity)
bot.pathfinder.setMovements(movements.defaultMove)
bot.pathfinder.setGoal(new GoalFollow(entity, 3), dynamic)
}
function hit(blockOrEntity) {
bot.chat(`hitting ${entity.name || entity.type}`)
}
function stop() {
bot.pathfinder.setGoal(null)
bot.stopDigging()
}
const load = (config) => {
cfg = config
bot = cfg.bot
cfg.move = {
// auto: true,
canDig: false,
// list: ["hello", "wassup"],
quiet: !!cfg.quiet,
movements: []
}
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
// initMoves(bot, mcData)
setTimeout(initMoves, 500, bot)
// bot.loadPlugin(pathfinder)
// bot.on('time', hello)
}
const unload = () => {
// TODO stop pathfinding maybe?
}
module.exports = { load, unload, stop, initMoves, moveNear, follow }