angram/lib/plugins/miner.js
2020-12-22 11:30:16 +05:00

210 lines
5.2 KiB
JavaScript

// const mineflayer = require('mineflayer')
// const { pathfinder } = require('mineflayer-pathfinder')
let pathfinder
const {
gameplay,
MoveTo,
BreakBlock,
ObtainItems,
ObtainItem,
GiveTo,
Craft
} = require('prismarine-gameplay')
// const { Gameplay } = require('prismarine-gameplay/lib/gameplay')
const { Vec3 } = require('vec3')
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){
const item = require('minecraft-data')(bot.version).findItemOrBlockByName(name)
if (item === undefined) {
bot.chat(`${name} is not a block or item name`)
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)
break
case /^collect [a-zA-Z_]+$/.test(message):
if (!checkBlockExists(command[1])) { return false }
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
case /^craftmine [0-9]+ [a-zA-Z_]+$/.test(message):
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):
cfg.quiet || bot.chat("♪♪ can't stop me now!! ♪♪")
bot.pathfinder.setGoal(null)
bot.stopDigging()
// player = bot.player.entity
// if (player) {
// bot.gameplay.solveFor(
// new MoveTo({
// x: player.position.x,
// y: player.position.y,
// z: player.position.z
// }), logError)
// }
break
}
}
function logError(err) {
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;
}
}
}
const load = (config) => {
cfg = config
bot = cfg.bot
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
// bot.loadPlugin(pathfinder)
cfg.plugins.mover.initMoves(bot)
bot.loadPlugin(gameplay)
cfg.plugins.mover.initMoves(bot)
bot.on("chat", miner)
// bot.pathfinder.canDig
}
const unload = () => {
// bot.gameplay
bot.off("chat", miner)
}
module.exports = { load, unload, miner }