From b9702315196d7baec5d4f4cad6bc06f1f2fa9b93 Mon Sep 17 00:00:00 2001 From: jay Date: Tue, 22 Dec 2020 15:38:45 +0500 Subject: [PATCH] feat: :sparkles: 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. --- lib/plugins/mover.js | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 lib/plugins/mover.js diff --git a/lib/plugins/mover.js b/lib/plugins/mover.js new file mode 100644 index 0000000..90057ed --- /dev/null +++ b/lib/plugins/mover.js @@ -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 } \ No newline at end of file