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
|
|
|
if (movements.length > 0) {
|
2020-12-24 14:57:45 +00:00
|
|
|
bot.pathfinder.setMovements(movements.defaultMove)
|
2021-04-07 01:37:26 +00:00
|
|
|
return console.warn("go init: movements already initialized!", movements)
|
2020-12-24 06:26:19 +00:00
|
|
|
}
|
2021-04-07 02:02:40 +00:00
|
|
|
const normalMove = new Movements(bot, mcData)
|
|
|
|
normalMove.canDig = false
|
|
|
|
normalMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id)
|
|
|
|
normalMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
|
|
|
|
normalMove.blocksToAvoid.add(mcData.blocksByName.magma_block.id)
|
|
|
|
movements.push(normalMove)
|
2020-12-24 06:26:19 +00:00
|
|
|
movements.defaultMove = movements[0]
|
2020-12-24 04:33:08 +00:00
|
|
|
|
2021-04-11 18:30:34 +00:00
|
|
|
bot.pathfinder.setMovements(normalMove)
|
2020-12-24 04:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-01-27 21:10:52 +00:00
|
|
|
// console.log(entity)
|
|
|
|
cfg.quiet || bot.chat(
|
|
|
|
`following ${entity.type
|
|
|
|
}: ${entity.username || entity.displayName
|
|
|
|
}${dynamic ? "" : " once"}`
|
|
|
|
)
|
2020-12-24 04:33:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-07 01:26:16 +00:00
|
|
|
function away(entity = bot.nearestEntity(), invertInvert = true, dynamic = true, distance = 10) {
|
|
|
|
const currentGoal = bot.pathfinder.goal
|
|
|
|
console.assert(currentGoal || entity)
|
|
|
|
const { GoalInvert } = require('mineflayer-pathfinder').goals
|
|
|
|
|
|
|
|
bot.pathfinder.setMovements(movements.defaultMove)
|
|
|
|
|
|
|
|
if (!currentGoal) {
|
|
|
|
const { GoalFollow } = require('mineflayer-pathfinder').goals
|
|
|
|
|
|
|
|
if (entity.entity) {
|
|
|
|
console.log("go away entity:", entity, entity.entity)
|
|
|
|
entity = entity.entity
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.quiet || bot.chat(
|
|
|
|
`going away from ${entity?.type
|
|
|
|
}: ${entity?.username || entity?.displayName
|
|
|
|
}${dynamic ? "" : " once"}`
|
|
|
|
)
|
|
|
|
// alternative implementation
|
|
|
|
// follow(entity, dynamic, distance)
|
|
|
|
// bot.pathfinder.setGoal(new GoalInvert(bot.pathfinder.goal), dynamic)
|
|
|
|
return bot.pathfinder.setGoal(new GoalInvert(
|
|
|
|
new GoalFollow(entity, distance)
|
|
|
|
), dynamic)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentGoal instanceof GoalInvert) {
|
|
|
|
const currEntity = currentGoal.goal.entity
|
|
|
|
console.log("go away inverse goal:", currentGoal.goal)
|
|
|
|
if (invertInvert) {
|
|
|
|
cfg.quiet || bot.chat(
|
|
|
|
`switching towards ${currentGoal.goal?.constructor.name
|
|
|
|
}: ${currEntity?.type
|
|
|
|
}: ${currEntity?.username || currEntity?.displayName
|
|
|
|
}${dynamic ? "" : " once"}`
|
|
|
|
)
|
|
|
|
bot.pathfinder.setGoal(currentGoal.goal, dynamic)
|
|
|
|
} else {
|
|
|
|
cfg.quiet || bot.chat(
|
|
|
|
`already going away from ${currentGoal.goal?.constructor.name
|
|
|
|
}; not switching`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const currEntity = currentGoal.entity
|
|
|
|
console.log("go away goal:", currentGoal)
|
|
|
|
cfg.quiet || bot.chat(
|
|
|
|
`going away from ${currentGoal?.constructor.name
|
|
|
|
}: ${currEntity?.type
|
|
|
|
}: ${currEntity?.username || currEntity?.displayName
|
|
|
|
}${dynamic ? "" : " once"}`
|
|
|
|
)
|
|
|
|
bot.pathfinder.setGoal(new GoalInvert(currentGoal), dynamic)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:48:45 +00:00
|
|
|
function ride(entity) {
|
|
|
|
entity = entity?.entity || entity
|
2021-04-19 08:25:04 +00:00
|
|
|
const ridableMobs = ["Horse", "Donkey", "Pig", "Strider", "Mule"]
|
2021-01-27 21:50:08 +00:00
|
|
|
const vehicle = entity && typeof entity !== "string" ? entity : bot.nearestEntity(e => {
|
|
|
|
if (typeof entity === "string") return e.name === entity
|
2021-01-27 19:48:45 +00:00
|
|
|
const maybeRidableMob = e.mobType?.split(" ")
|
|
|
|
return e.kind == "Vehicles"
|
|
|
|
|| ridableMobs.includes(e.mobType)
|
2021-01-27 20:28:37 +00:00
|
|
|
|| maybeRidableMob && ridableMobs.includes(maybeRidableMob[maybeRidableMob.length - 1])
|
2021-01-27 19:48:45 +00:00
|
|
|
})
|
|
|
|
if (!vehicle) {
|
2021-01-27 21:50:08 +00:00
|
|
|
return cfg.quiet || bot.chat(`nothing to ride!`)
|
2021-01-27 19:48:45 +00:00
|
|
|
} else if ((dist = bot.entity.position.distanceSquared(vehicle.position)) > 36) {
|
|
|
|
bot.lookAt(vehicle.position)
|
|
|
|
follow(vehicle, false)
|
|
|
|
bot.once('goal_reached', ride)
|
|
|
|
return cfg.quiet || bot.chat(`${vehicle.name} bit far`)
|
|
|
|
}
|
|
|
|
console.log("vehicle:", vehicle)
|
|
|
|
bot.mount(vehicle)
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:18:42 +00:00
|
|
|
function moveOrRide(turn = false, reverse = -1, directionLabel, message_parts2) {
|
|
|
|
// bot.once("attach", state = "vehiccel")
|
|
|
|
if (bot.vehicle) {
|
2021-04-07 01:42:11 +00:00
|
|
|
// FIXME moveVehicle should be +-1 or 0?
|
2021-04-14 04:57:36 +00:00
|
|
|
const amount = parseInt(message_parts2[0]) * -reverse
|
|
|
|
bot.moveVehicle(turn && Math.sign(amount) || 0, !turn && amount || 0)
|
2021-01-27 21:18:42 +00:00
|
|
|
} else {
|
|
|
|
command([directionLabel].concat(message_parts2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:59:39 +00:00
|
|
|
function command(message_parts, player) {
|
|
|
|
const message_parts2 = message_parts.slice(1)
|
|
|
|
switch (message_parts[0]) {
|
|
|
|
case "init":
|
|
|
|
initMoves()
|
|
|
|
break
|
|
|
|
case "near":
|
|
|
|
switch (message_parts2.length) {
|
|
|
|
case 0:
|
|
|
|
moveNear(bot.nearestEntity().position)
|
|
|
|
break
|
|
|
|
case 1:
|
|
|
|
switch (message_parts2[0]) {
|
|
|
|
case "me":
|
|
|
|
if (player) {
|
|
|
|
moveNear(player.position)
|
|
|
|
} else {
|
|
|
|
cfg.quiet || bot.chat("can't see you")
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
const aPlayer = bot.players[message_parts2[0]] ? bot.players[message_parts2[0]].entity : null
|
|
|
|
if (aPlayer) {
|
|
|
|
moveNear(aPlayer.position)
|
|
|
|
} else {
|
|
|
|
cfg.quiet || bot.chat(`can't see ${message_parts2[0]}`)
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 2:
|
|
|
|
//TODO this isn't near
|
|
|
|
moveXZ(message_parts2)
|
|
|
|
break
|
|
|
|
case 3:
|
|
|
|
//TODO more checks
|
|
|
|
moveNear(message_parts2)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case "follow":
|
|
|
|
// message_parts2 = message_parts.slice(2)
|
|
|
|
switch (message_parts2.length) {
|
|
|
|
case 0:
|
|
|
|
follow(bot.nearestEntity())
|
|
|
|
break
|
|
|
|
case 1:
|
|
|
|
let dist = 3
|
|
|
|
switch (message_parts2[0]) {
|
|
|
|
case "close":
|
|
|
|
dist = 1
|
|
|
|
case "me":
|
|
|
|
case "once":
|
|
|
|
if (player) {
|
|
|
|
follow(player, message_parts2[0] === "me", dist)
|
|
|
|
} else {
|
|
|
|
cfg.quiet || bot.chat("can't see you")
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
const aPlayer = bot.players[message_parts2[0]] ? bot.players[message_parts2[0]].entity : null
|
|
|
|
if (aPlayer) {
|
|
|
|
follow(aPlayer)
|
|
|
|
} else {
|
|
|
|
cfg.quiet || bot.chat(`can't see ${message_parts2[0]}`)
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break
|
|
|
|
// case 2:
|
|
|
|
// bot.lookAt({}) goalxz?
|
|
|
|
// break
|
|
|
|
// case 3:
|
|
|
|
//TODO more checks
|
|
|
|
// moveNear(message_parts2)
|
|
|
|
// break
|
|
|
|
default:
|
|
|
|
cfg.quiet || bot.chat("unknown or bad command")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
2021-01-26 17:49:23 +00:00
|
|
|
case "ride":
|
|
|
|
case "mount":
|
2021-01-27 21:50:08 +00:00
|
|
|
ride(message_parts2[0])
|
2021-01-26 17:49:23 +00:00
|
|
|
break
|
2021-04-07 01:26:16 +00:00
|
|
|
case "away":
|
|
|
|
case "run":
|
|
|
|
case "runaway":
|
|
|
|
away()
|
|
|
|
break
|
2021-01-18 10:13:53 +00:00
|
|
|
case "w":
|
|
|
|
case "f":
|
2021-01-27 21:18:42 +00:00
|
|
|
moveOrRide(0, -1, "forward", message_parts2)
|
2021-01-18 10:13:53 +00:00
|
|
|
break
|
|
|
|
case "s":
|
|
|
|
case "b":
|
2021-01-27 21:18:42 +00:00
|
|
|
moveOrRide(0, 1, "back", message_parts2)
|
2021-01-18 10:13:53 +00:00
|
|
|
break
|
|
|
|
case "a":
|
|
|
|
case "l":
|
2021-01-27 21:18:42 +00:00
|
|
|
moveOrRide(1, -1, "right", message_parts2)
|
2021-01-18 10:13:53 +00:00
|
|
|
break
|
|
|
|
case "d":
|
|
|
|
case "r":
|
2021-01-27 21:18:42 +00:00
|
|
|
moveOrRide(1, 1, "left", message_parts2)
|
2021-01-18 10:13:53 +00:00
|
|
|
break
|
2021-04-05 13:16:13 +00:00
|
|
|
case "up":
|
|
|
|
case "u":
|
|
|
|
case "j":
|
2021-04-19 08:31:36 +00:00
|
|
|
moveOrRide(1, 1, "jump", message_parts2)
|
2021-04-05 13:16:13 +00:00
|
|
|
break
|
2021-01-18 10:13:53 +00:00
|
|
|
case "back":
|
|
|
|
case "forward":
|
|
|
|
case "jump":
|
|
|
|
case "left":
|
|
|
|
case "right":
|
|
|
|
case "sneak":
|
|
|
|
case "sprint":
|
|
|
|
console.info(bot.controlState[message_parts[0]], bot.entity.position.floored())
|
|
|
|
bot.setControlState(message_parts[0], true)
|
|
|
|
console.info(bot.controlState[message_parts[0]])
|
2021-01-27 22:52:12 +00:00
|
|
|
setTimeout(bot.setControlState, 100 * (message_parts[1] || 2), message_parts[0], false)
|
2021-01-18 10:13:53 +00:00
|
|
|
setTimeout(console.info, 5000, bot.controlState[message_parts[0]], bot.entity.position.floored())
|
|
|
|
break
|
2021-01-17 18:59:39 +00:00
|
|
|
case "stop":
|
|
|
|
stop()
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return cfg.quiet || bot.chat(`unknown command ${message_parts[0]}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 04:33:08 +00:00
|
|
|
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-27 19:48:45 +00:00
|
|
|
module.exports = {
|
|
|
|
load, unload, command,
|
|
|
|
stop, initMoves,
|
|
|
|
moveNear, moveXZ, moveY, follow,
|
|
|
|
ride
|
|
|
|
}
|