2020-12-24 04:33:08 +00:00
|
|
|
|
|
|
|
const { Movements } = require('mineflayer-pathfinder')
|
|
|
|
const v = require('vec3')
|
|
|
|
|
|
|
|
let cfg = {}
|
|
|
|
let bot = {}
|
|
|
|
// let moving
|
|
|
|
let pathfinder
|
2020-12-24 14:57:45 +00:00
|
|
|
let mcData
|
2020-12-24 04:33:08 +00:00
|
|
|
let movements = []
|
|
|
|
|
|
|
|
|
2020-12-24 14:57:45 +00:00
|
|
|
function initMoves(bot = bot, mcData = bot.mcData) {
|
2020-12-24 06:26:19 +00:00
|
|
|
console.info(movements)
|
|
|
|
if (movements.length > 0) {
|
2020-12-24 14:57:45 +00:00
|
|
|
bot.pathfinder.setMovements(movements.defaultMove)
|
2020-12-24 06:26:19 +00:00
|
|
|
return console.warn("movements already initialized!")
|
|
|
|
}
|
2020-12-24 04:33:08 +00:00
|
|
|
let defaultMove = new Movements(bot, mcData)
|
|
|
|
defaultMove.canDig = false
|
|
|
|
defaultMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id)
|
2020-12-24 14:57:45 +00:00
|
|
|
defaultMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
|
|
|
|
defaultMove.blocksToAvoid.add(mcData.blocksByName.magma_block.id)
|
2020-12-24 06:26:19 +00:00
|
|
|
movements.push(defaultMove)
|
|
|
|
movements.defaultMove = movements[0]
|
2020-12-24 04:33:08 +00:00
|
|
|
|
|
|
|
bot.pathfinder.setMovements(defaultMove)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function moveNear(pos, distance = 3) {
|
|
|
|
const { GoalNear } = require('mineflayer-pathfinder').goals
|
|
|
|
|
|
|
|
pos = v(pos)
|
2021-01-16 08:57:21 +00:00
|
|
|
cfg.quiet || bot.chat(`moving to ${pos.floored()}`)
|
2020-12-24 04:33:08 +00:00
|
|
|
bot.pathfinder.setMovements(movements.defaultMove)
|
|
|
|
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, distance))
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:36:17 +00:00
|
|
|
function moveXZ(pos) {
|
|
|
|
const { GoalXZ } = require('mineflayer-pathfinder').goals
|
|
|
|
|
|
|
|
if (Array.isArray(pos) && pos.length == 2) {
|
|
|
|
pos = v(pos[0], 0, pos[1])
|
|
|
|
}
|
|
|
|
pos = v(pos)
|
|
|
|
console.log(pos)
|
2021-01-16 08:57:21 +00:00
|
|
|
cfg.quiet || bot.chat(`moving to ${pos.floored()}`)
|
2021-01-14 19:36:17 +00:00
|
|
|
bot.pathfinder.setMovements(movements.defaultMove)
|
|
|
|
bot.pathfinder.setGoal(new GoalXZ(pos.x, pos.z))
|
|
|
|
}
|
|
|
|
|
2021-01-17 08:56:18 +00:00
|
|
|
function moveY(pos) {
|
|
|
|
const { GoalY } = require('mineflayer-pathfinder').goals
|
|
|
|
|
|
|
|
if (Array.isArray(pos) && pos.length == 1) {
|
|
|
|
pos = v(null, pos[0], null)
|
|
|
|
}
|
|
|
|
pos = v(pos)
|
|
|
|
console.log(pos)
|
|
|
|
cfg.quiet || bot.chat(`moving to ${pos.floored()}`)
|
|
|
|
bot.pathfinder.setMovements(movements.defaultMove)
|
|
|
|
bot.pathfinder.setGoal(new GoalY(pos.y))
|
|
|
|
}
|
|
|
|
|
2021-01-05 03:11:11 +00:00
|
|
|
function follow(entity, dynamic = true, distance = 3) {
|
2020-12-24 04:33:08 +00:00
|
|
|
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)
|
2021-01-05 03:11:11 +00:00
|
|
|
bot.pathfinder.setGoal(new GoalFollow(entity, distance), dynamic)
|
2020-12-24 04:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hit(blockOrEntity) {
|
|
|
|
bot.chat(`hitting ${entity.name || entity.type}`)
|
|
|
|
}
|
|
|
|
|
2021-01-16 08:25:26 +00:00
|
|
|
function goalReached(goal) {
|
|
|
|
console.log(goal)
|
|
|
|
const entity = goal?.entity
|
|
|
|
let entityInfo = ""
|
|
|
|
if (entity) {
|
|
|
|
entityInfo += entity.type + ": "
|
|
|
|
switch (entity.type) {
|
|
|
|
case "player":
|
|
|
|
entityInfo += entity.username
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.quiet || bot.chat(`goal reached: ${entityInfo}; pos: [x:${goal?.x}, y:${goal?.y}, z:${goal?.z}]`)
|
|
|
|
}
|
|
|
|
|
2020-12-24 04:33:08 +00:00
|
|
|
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: []
|
|
|
|
}
|
|
|
|
|
2020-12-24 14:57:45 +00:00
|
|
|
mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
|
2020-12-24 04:33:08 +00:00
|
|
|
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
|
|
|
|
|
|
|
|
// initMoves(bot, mcData)
|
2020-12-24 14:57:45 +00:00
|
|
|
setTimeout(initMoves, 500, bot, mcData)
|
2021-01-16 08:25:26 +00:00
|
|
|
bot.on('goal_reached', goalReached)
|
2020-12-24 04:33:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const unload = () => {
|
2020-12-24 14:57:45 +00:00
|
|
|
stop()
|
2021-01-16 08:25:26 +00:00
|
|
|
bot.off('goal_reached', goalReached)
|
2020-12-24 04:33:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 08:56:18 +00:00
|
|
|
module.exports = { load, unload, stop, initMoves, moveNear, moveXZ, moveY, follow }
|