190 lines
4.7 KiB
JavaScript
190 lines
4.7 KiB
JavaScript
|
const mineflayer = require('mineflayer')
|
||
|
const pathfinder = require('mineflayer-pathfinder').pathfinder
|
||
|
const {
|
||
|
gameplay,
|
||
|
MoveTo,
|
||
|
BreakBlock,
|
||
|
ObtainItems,
|
||
|
ObtainItem,
|
||
|
GiveTo,
|
||
|
Craft
|
||
|
} = require('prismarine-gameplay')
|
||
|
// const { Gameplay } = require('prismarine-gameplay/lib/gameplay')
|
||
|
// const { Vec3 } = require('vec3')
|
||
|
let mcData = require('minecraft-data')
|
||
|
|
||
|
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){
|
||
|
if (mcData.blocksByName[name] === undefined) {
|
||
|
bot.chat(`${name} is not a block 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[2])) {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 /^craft [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):
|
||
|
bot.chat("♪♪ can't stop me now!! ♪♪")
|
||
|
// 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)
|
||
|
bot.chat(`Failed task: ${err.message}`)
|
||
|
}
|
||
|
|
||
|
|
||
|
const load = (config) => {
|
||
|
cfg = config
|
||
|
bot = cfg.bot
|
||
|
|
||
|
mcData = mcData(bot.version)
|
||
|
bot.loadPlugin(pathfinder)
|
||
|
bot.loadPlugin(gameplay)
|
||
|
bot.on("chat", miner)
|
||
|
}
|
||
|
|
||
|
const unload = () => {
|
||
|
// bot.gameplay
|
||
|
bot.off("chat", miner)
|
||
|
}
|
||
|
|
||
|
module.exports = { load, unload }
|