2020-12-22 06:30:16 +00:00
|
|
|
// const mineflayer = require('mineflayer')
|
|
|
|
// const { pathfinder } = require('mineflayer-pathfinder')
|
|
|
|
let pathfinder
|
2020-12-21 16:08:38 +00:00
|
|
|
const {
|
|
|
|
gameplay,
|
|
|
|
MoveTo,
|
|
|
|
BreakBlock,
|
|
|
|
ObtainItems,
|
|
|
|
ObtainItem,
|
|
|
|
GiveTo,
|
|
|
|
Craft
|
|
|
|
} = require('prismarine-gameplay')
|
|
|
|
// const { Gameplay } = require('prismarine-gameplay/lib/gameplay')
|
2020-12-22 06:30:16 +00:00
|
|
|
const { Vec3 } = require('vec3')
|
2020-12-21 16:08:38 +00:00
|
|
|
|
|
|
|
let cfg = {}
|
|
|
|
let bot = {}
|
|
|
|
|
|
|
|
// if (process.argv.length < 4 || process.argv.length > 6) {
|
|
|
|
// console.log('Usage : node miner.js <host> <port> [<name>] [<password>]')
|
|
|
|
// process.exit(1)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const bot = mineflayer.createBot({
|
|
|
|
// host: process.argv[2],
|
|
|
|
// port: parseInt(process.argv[3]),
|
|
|
|
// username: process.argv[4] ? process.argv[4] : 'collect_items',
|
|
|
|
// // password: process.argv[5]
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
|
|
// bot.on('spawn', () => bot.gameplay.debugText = true)
|
|
|
|
|
|
|
|
// bot.on('chat', (username, message) => mine(username, message))
|
|
|
|
|
|
|
|
function checkBlockExists(name){
|
2020-12-22 06:30:16 +00:00
|
|
|
const item = require('minecraft-data')(bot.version).findItemOrBlockByName(name)
|
|
|
|
if (item === undefined) {
|
|
|
|
bot.chat(`${name} is not a block or item name`)
|
2020-12-21 16:08:38 +00:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function miner(username, message) {
|
|
|
|
const player = bot.players[username] ? bot.players[username].entity : null
|
|
|
|
|
|
|
|
const command = message.split(' ')
|
|
|
|
switch (true) {
|
|
|
|
// case /^echo .*/.test(message):
|
|
|
|
// bot.chat(command.slice(1).join(" "))
|
|
|
|
// break
|
|
|
|
|
|
|
|
// case /^zz+/.test(message):
|
|
|
|
// bot.chat("/afk")
|
|
|
|
// break
|
|
|
|
case /^debug$/.test(message):
|
|
|
|
bot.gameplay.debugText = !!!bot.gameplay.debugText
|
|
|
|
break
|
|
|
|
|
|
|
|
case /^moveto -?[0-9]+ -?[0-9]+$/.test(message):
|
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new MoveTo({
|
|
|
|
x: parseInt(command[1]),
|
|
|
|
z: parseInt(command[2])
|
|
|
|
}), logError)
|
|
|
|
break
|
|
|
|
|
|
|
|
case /^moveto -?[0-9]+ -?[0-9]+ -?[0-9]+$/.test(message):
|
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new MoveTo({
|
|
|
|
x: parseInt(command[1]),
|
|
|
|
y: parseInt(command[2]),
|
|
|
|
z: parseInt(command[3])
|
|
|
|
}), logError)
|
|
|
|
break
|
|
|
|
|
|
|
|
case /^moveto \w+$/.test(message):
|
|
|
|
const player2 = bot.players[command[1]] ? bot.players[command[1]].entity : null
|
|
|
|
if (!player2) {
|
|
|
|
bot.chat(`can't see ${command[1]}..`)
|
|
|
|
} else {
|
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new MoveTo({
|
|
|
|
x: player2.position.x,
|
|
|
|
y: player2.position.y,
|
|
|
|
z: player2.position.z
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case /^comehere$/.test(message):
|
|
|
|
if (!player) {
|
|
|
|
bot.chat("can't see you..")
|
|
|
|
} else {
|
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new MoveTo({
|
|
|
|
x: player.position.x,
|
|
|
|
y: player.position.y,
|
|
|
|
z: player.position.z
|
|
|
|
}), logError)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case /^break -?[0-9]+ -?[0-9]+ -?[0-9]+$/.test(message):
|
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new BreakBlock({
|
|
|
|
position: new Vec3(
|
|
|
|
parseInt(command[1]),
|
|
|
|
parseInt(command[2]),
|
|
|
|
parseInt(command[3])
|
|
|
|
)
|
|
|
|
}), logError)
|
|
|
|
break
|
|
|
|
|
|
|
|
case /^collect [0-9]+ [a-zA-Z_]+$/.test(message):
|
|
|
|
if(!checkBlockExists(command[2])) {return false}
|
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new ObtainItems({
|
|
|
|
itemType: command[2],
|
|
|
|
count: parseInt(command[1])
|
|
|
|
}), logError)
|
2020-12-22 06:30:16 +00:00
|
|
|
|
2020-12-21 16:08:38 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case /^collect [a-zA-Z_]+$/.test(message):
|
2020-12-22 06:30:16 +00:00
|
|
|
if (!checkBlockExists(command[1])) { return false }
|
2020-12-21 16:08:38 +00:00
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new ObtainItem({
|
|
|
|
itemType: command[1]
|
|
|
|
}), logError)
|
|
|
|
break
|
|
|
|
|
|
|
|
case /^bringme [0-9]+ [a-zA-Z_]+$/.test(message):
|
|
|
|
if(!checkBlockExists(command[2])) {return false}
|
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new GiveTo({
|
|
|
|
itemType: command[2],
|
|
|
|
count: parseInt(command[1]),
|
|
|
|
entity: player
|
|
|
|
}), logError)
|
|
|
|
break
|
|
|
|
|
2020-12-22 06:30:16 +00:00
|
|
|
case /^craftmine [0-9]+ [a-zA-Z_]+$/.test(message):
|
2020-12-21 16:08:38 +00:00
|
|
|
if(!checkBlockExists(command[2])) {return false}
|
|
|
|
bot.gameplay.solveFor(
|
|
|
|
new Craft({
|
|
|
|
itemType: command[2],
|
|
|
|
count: parseInt(command[1]),
|
|
|
|
entity: player
|
|
|
|
}), logError)
|
|
|
|
break
|
|
|
|
|
|
|
|
case /^stop$/.test(message):
|
2020-12-22 06:30:16 +00:00
|
|
|
cfg.quiet || bot.chat("♪♪ can't stop me now!! ♪♪")
|
|
|
|
bot.pathfinder.setGoal(null)
|
|
|
|
bot.stopDigging()
|
2020-12-21 16:08:38 +00:00
|
|
|
// player = bot.player.entity
|
2020-12-22 06:30:16 +00:00
|
|
|
// if (player) {
|
|
|
|
// bot.gameplay.solveFor(
|
|
|
|
// new MoveTo({
|
|
|
|
// x: player.position.x,
|
|
|
|
// y: player.position.y,
|
|
|
|
// z: player.position.z
|
|
|
|
// }), logError)
|
|
|
|
// }
|
2020-12-21 16:08:38 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function logError(err) {
|
2020-12-22 06:30:16 +00:00
|
|
|
if (err) {
|
|
|
|
switch (err.message) {
|
|
|
|
case "No more solutions available!":
|
|
|
|
console.log("miner: failed")
|
|
|
|
cfg.quiet || bot.chat("miner: out of solutions")
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
console.log(err)
|
|
|
|
cfg.quiet || bot.chat("miner: unknown error, check logs")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const load = (config) => {
|
|
|
|
cfg = config
|
|
|
|
bot = cfg.bot
|
|
|
|
|
2020-12-22 06:30:16 +00:00
|
|
|
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
|
|
|
|
// bot.loadPlugin(pathfinder)
|
|
|
|
cfg.plugins.mover.initMoves(bot)
|
|
|
|
|
2020-12-21 16:08:38 +00:00
|
|
|
bot.loadPlugin(gameplay)
|
2020-12-22 06:30:16 +00:00
|
|
|
cfg.plugins.mover.initMoves(bot)
|
2020-12-21 16:08:38 +00:00
|
|
|
bot.on("chat", miner)
|
2020-12-22 06:30:16 +00:00
|
|
|
|
|
|
|
// bot.pathfinder.canDig
|
2020-12-21 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const unload = () => {
|
|
|
|
// bot.gameplay
|
|
|
|
bot.off("chat", miner)
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:30:16 +00:00
|
|
|
module.exports = { load, unload, miner }
|